1238104Sdes/*	$NetBSD: t_tls_static.c,v 1.2 2012/01/17 20:34:57 joerg Exp $	*/
2238104Sdes/*-
3238104Sdes * Copyright (c) 2011 The NetBSD Foundation, Inc.
4238104Sdes * All rights reserved.
5238104Sdes *
6238104Sdes * This code is derived from software contributed to The NetBSD Foundation
7238104Sdes * by Joerg Sonnenberger.
8238104Sdes *
9238104Sdes * Redistribution and use in source and binary forms, with or without
10238104Sdes * modification, are permitted provided that the following conditions
11238104Sdes * are met:
12238104Sdes *
13238104Sdes * 1. Redistributions of source code must retain the above copyright
14238104Sdes *    notice, this list of conditions and the following disclaimer.
15238104Sdes * 2. Redistributions in binary form must reproduce the above copyright
16238104Sdes *    notice, this list of conditions and the following disclaimer in
17238104Sdes *    the documentation and/or other materials provided with the
18238104Sdes *    distribution.
19238104Sdes *
20238104Sdes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21238104Sdes * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22238104Sdes * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23238104Sdes * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
24238104Sdes * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25249307Sdes * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26249307Sdes * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27238104Sdes * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28238104Sdes * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29238104Sdes * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30238104Sdes * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31238104Sdes * SUCH DAMAGE.
32238104Sdes */
33238104Sdes
34249307Sdes#include <sys/cdefs.h>
35249307Sdes__RCSID("$NetBSD: t_tls_static.c,v 1.2 2012/01/17 20:34:57 joerg Exp $");
36249307Sdes
37249307Sdes#include <atf-c.h>
38238104Sdes#include <pthread.h>
39238104Sdes
40238104Sdes#ifdef __NetBSD__
41238104Sdes#include <sys/tls.h>
42238104Sdes#endif
43238104Sdes
44238104Sdes#ifdef __HAVE_NO___THREAD
45238104Sdes#define	__thread
46238104Sdes#endif
47238104Sdes
48238104SdesATF_TC(t_tls_static);
49238104Sdes
50238104SdesATF_TC_HEAD(t_tls_static, tc)
51238104Sdes{
52238104Sdes	atf_tc_set_md_var(tc, "descr",
53238104Sdes	    "Test (un)initialized TLS variables in static binaries");
54238104Sdes}
55238104Sdes
56238104Sdesvoid testf_helper(void);
57238104Sdes
58238104Sdes__thread int var1 = 1;
59238104Sdes__thread int var2;
60238104Sdes
61238104Sdesstatic void *
62238104Sdestestf(void *dummy)
63238104Sdes{
64238104Sdes	ATF_CHECK_EQ(var1, 1);
65238104Sdes	ATF_CHECK_EQ(var2, 0);
66238104Sdes	testf_helper();
67249307Sdes	ATF_CHECK_EQ(var1, -1);
68249307Sdes	ATF_CHECK_EQ(var2, -1);
69249307Sdes
70249307Sdes	return NULL;
71238104Sdes}
72238104Sdes
73238104SdesATF_TC_BODY(t_tls_static, tc)
74238104Sdes{
75238104Sdes	pthread_t t;
76238104Sdes
77238104Sdes#ifdef __HAVE_NO___THREAD
78238104Sdes	atf_tc_skip("no TLS support on this platform");
79238104Sdes#endif
80238104Sdes
81238104Sdes	testf(NULL);
82238104Sdes
83238104Sdes	pthread_create(&t, 0, testf, 0);
84238104Sdes	pthread_join(t, NULL);
85238104Sdes
86238104Sdes	pthread_create(&t, 0, testf, 0);
87238104Sdes	pthread_join(t, NULL);
88238104Sdes}
89238104Sdes
90238104SdesATF_TP_ADD_TCS(tp)
91238104Sdes{
92238104Sdes	ATF_TP_ADD_TC(tp, t_tls_static);
93238104Sdes
94238104Sdes	return atf_no_error();
95238104Sdes}
96238104Sdes