1272343Sngie/*	$NetBSD: t_tls_static.c,v 1.2 2012/01/17 20:34:57 joerg Exp $	*/
2272343Sngie/*-
3272343Sngie * Copyright (c) 2011 The NetBSD Foundation, Inc.
4272343Sngie * All rights reserved.
5272343Sngie *
6272343Sngie * This code is derived from software contributed to The NetBSD Foundation
7272343Sngie * by Joerg Sonnenberger.
8272343Sngie *
9272343Sngie * Redistribution and use in source and binary forms, with or without
10272343Sngie * modification, are permitted provided that the following conditions
11272343Sngie * are met:
12272343Sngie *
13272343Sngie * 1. Redistributions of source code must retain the above copyright
14272343Sngie *    notice, this list of conditions and the following disclaimer.
15272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
16272343Sngie *    notice, this list of conditions and the following disclaimer in
17272343Sngie *    the documentation and/or other materials provided with the
18272343Sngie *    distribution.
19272343Sngie *
20272343Sngie * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22272343Sngie * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23272343Sngie * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
24272343Sngie * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25272343Sngie * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26272343Sngie * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27272343Sngie * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28272343Sngie * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29272343Sngie * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30272343Sngie * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31272343Sngie * SUCH DAMAGE.
32272343Sngie */
33272343Sngie
34272343Sngie#include <sys/cdefs.h>
35272343Sngie__RCSID("$NetBSD: t_tls_static.c,v 1.2 2012/01/17 20:34:57 joerg Exp $");
36272343Sngie
37272343Sngie#include <atf-c.h>
38272343Sngie#include <pthread.h>
39272343Sngie
40276478Sngie#ifdef __NetBSD__
41272343Sngie#include <sys/tls.h>
42276478Sngie#endif
43272343Sngie
44272343Sngie#ifdef __HAVE_NO___THREAD
45272343Sngie#define	__thread
46272343Sngie#endif
47272343Sngie
48272343SngieATF_TC(t_tls_static);
49272343Sngie
50272343SngieATF_TC_HEAD(t_tls_static, tc)
51272343Sngie{
52272343Sngie	atf_tc_set_md_var(tc, "descr",
53272343Sngie	    "Test (un)initialized TLS variables in static binaries");
54272343Sngie}
55272343Sngie
56272343Sngievoid testf_helper(void);
57272343Sngie
58272343Sngie__thread int var1 = 1;
59272343Sngie__thread int var2;
60272343Sngie
61272343Sngiestatic void *
62272343Sngietestf(void *dummy)
63272343Sngie{
64272343Sngie	ATF_CHECK_EQ(var1, 1);
65272343Sngie	ATF_CHECK_EQ(var2, 0);
66272343Sngie	testf_helper();
67272343Sngie	ATF_CHECK_EQ(var1, -1);
68272343Sngie	ATF_CHECK_EQ(var2, -1);
69272343Sngie
70272343Sngie	return NULL;
71272343Sngie}
72272343Sngie
73272343SngieATF_TC_BODY(t_tls_static, tc)
74272343Sngie{
75272343Sngie	pthread_t t;
76272343Sngie
77272343Sngie#ifdef __HAVE_NO___THREAD
78272343Sngie	atf_tc_skip("no TLS support on this platform");
79272343Sngie#endif
80272343Sngie
81272343Sngie	testf(NULL);
82272343Sngie
83272343Sngie	pthread_create(&t, 0, testf, 0);
84272343Sngie	pthread_join(t, NULL);
85272343Sngie
86272343Sngie	pthread_create(&t, 0, testf, 0);
87272343Sngie	pthread_join(t, NULL);
88272343Sngie}
89272343Sngie
90272343SngieATF_TP_ADD_TCS(tp)
91272343Sngie{
92272343Sngie	ATF_TP_ADD_TC(tp, t_tls_static);
93272343Sngie
94272343Sngie	return atf_no_error();
95272343Sngie}
96