]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/netbsd-tests/lib/libpthread/t_once.c
MFC r314450,r313439:
[FreeBSD/stable/10.git] / contrib / netbsd-tests / lib / libpthread / t_once.c
1 /* $NetBSD: t_once.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $ */
2
3 /*
4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __COPYRIGHT("@(#) Copyright (c) 2008\
31  The NetBSD Foundation, inc. All rights reserved.");
32 __RCSID("$NetBSD: t_once.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $");
33
34 #ifdef __FreeBSD__
35 #include <sys/time.h> /* For itimer*, etc. */
36 #endif
37 #include <pthread.h>
38 #include <signal.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41
42 #include <atf-c.h>
43
44 #include "h_common.h"
45
46 static pthread_once_t once = PTHREAD_ONCE_INIT;
47 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
48 static int x;
49
50 #define NTHREADS 25
51
52 static void
53 ofunc(void)
54 {
55         printf("Variable x has value %d\n", x);
56         x++;
57 }
58
59 ATF_TC(once1);
60 ATF_TC_HEAD(once1, tc)
61 {
62         atf_tc_set_md_var(tc, "descr", "Checks pthread_once()");
63 }
64 ATF_TC_BODY(once1, tc)
65 {
66
67         printf("1: Test 1 of pthread_once()\n");
68
69         PTHREAD_REQUIRE(pthread_once(&once, ofunc));
70         PTHREAD_REQUIRE(pthread_once(&once, ofunc));
71
72         printf("1: X has value %d\n",x );
73         ATF_REQUIRE_EQ(x, 1);
74 }
75
76 static void
77 once2_ofunc(void)
78 {
79         x++;
80         printf("ofunc: Variable x has value %d\n", x);
81         x++;
82 }
83
84 static void *
85 once2_threadfunc(void *arg)
86 {
87         int num;
88
89         PTHREAD_REQUIRE(pthread_once(&once, once2_ofunc));
90
91         num = *(int *)arg;
92         printf("Thread %d sees x with value %d\n", num, x);
93         ATF_REQUIRE_EQ(x, 2);
94
95         return NULL;
96 }
97
98 ATF_TC(once2);
99 ATF_TC_HEAD(once2, tc)
100 {
101         atf_tc_set_md_var(tc, "descr", "Checks pthread_once()");
102 }
103 ATF_TC_BODY(once2, tc)
104 {
105         pthread_t  threads[NTHREADS];
106         int id[NTHREADS];
107         int i;
108
109         printf("1: Test 2 of pthread_once()\n");
110
111         for (i=0; i < NTHREADS; i++) {
112                 id[i] = i;
113                 PTHREAD_REQUIRE(pthread_create(&threads[i], NULL, once2_threadfunc, &id[i]));
114         }
115
116         for (i=0; i < NTHREADS; i++)
117                 PTHREAD_REQUIRE(pthread_join(threads[i], NULL));
118
119         printf("1: X has value %d\n",x );
120         ATF_REQUIRE_EQ(x, 2);
121 }
122
123 static void
124 once3_cleanup(void *m)
125 {
126         pthread_mutex_t *mu = m;
127
128         PTHREAD_REQUIRE(pthread_mutex_unlock(mu));
129 }
130
131 static void
132 once3_ofunc(void)
133 {
134         pthread_testcancel();
135 }
136
137 static void *
138 once3_threadfunc(void *arg)
139 {
140         PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
141         pthread_cleanup_push(once3_cleanup, &mutex);
142         PTHREAD_REQUIRE(pthread_once(&once, once3_ofunc));
143         pthread_cleanup_pop(1);
144
145         return NULL;
146 }
147
148 static void
149 handler(int sig, siginfo_t *info, void *ctx)
150 {
151         atf_tc_fail("Signal handler was called; "
152                 "main thread deadlocked in pthread_once()");
153 }
154
155 ATF_TC(once3);
156 ATF_TC_HEAD(once3, tc)
157 {
158         atf_tc_set_md_var(tc, "descr", "Checks pthread_once()");
159 }
160 ATF_TC_BODY(once3, tc)
161 {
162         pthread_t thread;
163         struct sigaction act;
164         struct itimerval it;
165         printf("Test 3 of pthread_once() (test versus cancellation)\n");
166
167         act.sa_sigaction = handler;
168         sigemptyset(&act.sa_mask);
169         act.sa_flags = SA_SIGINFO;
170         sigaction(SIGALRM, &act, NULL);
171
172         timerclear(&it.it_value);
173         it.it_value.tv_usec = 500000;
174         timerclear(&it.it_interval);
175         setitimer(ITIMER_REAL, &it, NULL);
176
177         PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
178         PTHREAD_REQUIRE(pthread_create(&thread, NULL, once3_threadfunc, NULL));
179         PTHREAD_REQUIRE(pthread_cancel(thread));
180         PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
181         PTHREAD_REQUIRE(pthread_join(thread, NULL));
182
183         PTHREAD_REQUIRE(pthread_once(&once, ofunc));
184
185         /* Cancel timer */
186         timerclear(&it.it_value);
187         setitimer(ITIMER_REAL, &it, NULL);
188
189         printf("Test succeeded\n");
190 }
191
192 ATF_TP_ADD_TCS(tp)
193 {
194         ATF_TP_ADD_TC(tp, once1);
195         ATF_TP_ADD_TC(tp, once2);
196         ATF_TP_ADD_TC(tp, once3);
197
198         return atf_no_error();
199 }