1272343Sngie/*	$NetBSD: t_tls_dynamic.c,v 1.3 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_dynamic.c,v 1.3 2012/01/17 20:34:57 joerg Exp $");
36272343Sngie
37272343Sngie#include <atf-c.h>
38272343Sngie#include <pthread.h>
39272343Sngie#include <unistd.h>
40272343Sngie
41276478Sngie#ifdef __NetBSD__
42272343Sngie#include <sys/tls.h>
43276478Sngie#endif
44272343Sngie
45272343Sngie#ifdef __HAVE_NO___THREAD
46272343Sngie#define	__thread
47272343Sngie#endif
48272343Sngie
49272343SngieATF_TC(t_tls_dynamic);
50272343Sngie
51272343SngieATF_TC_HEAD(t_tls_dynamic, tc)
52272343Sngie{
53272343Sngie	atf_tc_set_md_var(tc, "descr",
54272343Sngie	    "Test (un)initialized TLS variables in dynamic binaries");
55272343Sngie}
56272343Sngie
57272343Sngievoid testf_dso_helper(int, int);
58272343Sngie
59272343Sngieextern __thread int var1;
60272343Sngieextern __thread int var2;
61272343Sngieextern __thread pid_t (*dso_var1)(void);
62272343Sngie
63272343Sngie__thread int *var3 = &optind;
64272343Sngieint var4_helper;
65272343Sngie__thread int *var4 = &var4_helper;
66272343Sngie
67272343Sngiestatic void *
68272343Sngietestf(void *dummy)
69272343Sngie{
70272343Sngie	ATF_CHECK_EQ(var1, 1);
71272343Sngie	ATF_CHECK_EQ(var2, 0);
72272343Sngie	testf_dso_helper(2, 2);
73272343Sngie	ATF_CHECK_EQ(var1, 2);
74272343Sngie	ATF_CHECK_EQ(var2, 2);
75272343Sngie	testf_dso_helper(3, 3);
76272343Sngie	ATF_CHECK_EQ(var1, 3);
77272343Sngie	ATF_CHECK_EQ(var2, 3);
78272343Sngie	ATF_CHECK_EQ(var3, &optind);
79272343Sngie	ATF_CHECK_EQ(var4, &var4_helper);
80272343Sngie	ATF_CHECK_EQ(dso_var1, getpid);
81272343Sngie
82272343Sngie	return NULL;
83272343Sngie}
84272343Sngie
85272343SngieATF_TC_BODY(t_tls_dynamic, tc)
86272343Sngie{
87272343Sngie	pthread_t t;
88272343Sngie
89272343Sngie#ifdef __HAVE_NO___THREAD
90272343Sngie	atf_tc_skip("no TLS support on this platform");
91272343Sngie#endif
92272343Sngie
93272343Sngie	testf(NULL);
94272343Sngie
95272343Sngie	pthread_create(&t, 0, testf, 0);
96272343Sngie	pthread_join(t, NULL);
97272343Sngie
98272343Sngie	pthread_create(&t, 0, testf, 0);
99272343Sngie	pthread_join(t, NULL);
100272343Sngie}
101272343Sngie
102272343SngieATF_TP_ADD_TCS(tp)
103272343Sngie{
104272343Sngie	ATF_TP_ADD_TC(tp, t_tls_dynamic);
105272343Sngie
106272343Sngie	return atf_no_error();
107272343Sngie}
108