etc/protocols - sync with NetBSD-8
[minix.git] / tests / lib / libpthread / t_once.c
blob575d5d7ef89017c19b7b13376b078c2561582aab
1 /* $NetBSD: t_once.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $ */
3 /*
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
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.
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.
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 $");
34 #include <pthread.h>
35 #include <signal.h>
36 #include <stdio.h>
37 #include <stdlib.h>
39 #include <atf-c.h>
41 #include "h_common.h"
43 static pthread_once_t once = PTHREAD_ONCE_INIT;
44 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
45 static int x;
47 #define NTHREADS 25
49 static void
50 ofunc(void)
52 printf("Variable x has value %d\n", x);
53 x++;
56 ATF_TC(once1);
57 ATF_TC_HEAD(once1, tc)
59 atf_tc_set_md_var(tc, "descr", "Checks pthread_once()");
61 ATF_TC_BODY(once1, tc)
64 printf("1: Test 1 of pthread_once()\n");
66 PTHREAD_REQUIRE(pthread_once(&once, ofunc));
67 PTHREAD_REQUIRE(pthread_once(&once, ofunc));
69 printf("1: X has value %d\n",x );
70 ATF_REQUIRE_EQ(x, 1);
73 static void
74 once2_ofunc(void)
76 x++;
77 printf("ofunc: Variable x has value %d\n", x);
78 x++;
81 static void *
82 once2_threadfunc(void *arg)
84 int num;
86 PTHREAD_REQUIRE(pthread_once(&once, once2_ofunc));
88 num = *(int *)arg;
89 printf("Thread %d sees x with value %d\n", num, x);
90 ATF_REQUIRE_EQ(x, 2);
92 return NULL;
95 ATF_TC(once2);
96 ATF_TC_HEAD(once2, tc)
98 atf_tc_set_md_var(tc, "descr", "Checks pthread_once()");
100 ATF_TC_BODY(once2, tc)
102 pthread_t threads[NTHREADS];
103 int id[NTHREADS];
104 int i;
106 printf("1: Test 2 of pthread_once()\n");
108 for (i=0; i < NTHREADS; i++) {
109 id[i] = i;
110 PTHREAD_REQUIRE(pthread_create(&threads[i], NULL, once2_threadfunc, &id[i]));
113 for (i=0; i < NTHREADS; i++)
114 PTHREAD_REQUIRE(pthread_join(threads[i], NULL));
116 printf("1: X has value %d\n",x );
117 ATF_REQUIRE_EQ(x, 2);
120 static void
121 once3_cleanup(void *m)
123 pthread_mutex_t *mu = m;
125 PTHREAD_REQUIRE(pthread_mutex_unlock(mu));
128 static void
129 once3_ofunc(void)
131 pthread_testcancel();
134 static void *
135 once3_threadfunc(void *arg)
137 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
138 pthread_cleanup_push(once3_cleanup, &mutex);
139 PTHREAD_REQUIRE(pthread_once(&once, once3_ofunc));
140 pthread_cleanup_pop(1);
142 return NULL;
145 static void
146 handler(int sig, siginfo_t *info, void *ctx)
148 atf_tc_fail("Signal handler was called; "
149 "main thread deadlocked in pthread_once()");
152 ATF_TC(once3);
153 ATF_TC_HEAD(once3, tc)
155 atf_tc_set_md_var(tc, "descr", "Checks pthread_once()");
157 ATF_TC_BODY(once3, tc)
159 pthread_t thread;
160 struct sigaction act;
161 struct itimerval it;
162 printf("Test 3 of pthread_once() (test versus cancellation)\n");
164 act.sa_sigaction = handler;
165 sigemptyset(&act.sa_mask);
166 act.sa_flags = SA_SIGINFO;
167 sigaction(SIGALRM, &act, NULL);
169 timerclear(&it.it_value);
170 it.it_value.tv_usec = 500000;
171 timerclear(&it.it_interval);
172 setitimer(ITIMER_REAL, &it, NULL);
174 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
175 PTHREAD_REQUIRE(pthread_create(&thread, NULL, once3_threadfunc, NULL));
176 PTHREAD_REQUIRE(pthread_cancel(thread));
177 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
178 PTHREAD_REQUIRE(pthread_join(thread, NULL));
180 PTHREAD_REQUIRE(pthread_once(&once, ofunc));
182 /* Cancel timer */
183 timerclear(&it.it_value);
184 setitimer(ITIMER_REAL, &it, NULL);
186 printf("Test succeeded\n");
189 ATF_TP_ADD_TCS(tp)
191 ATF_TP_ADD_TC(tp, once1);
192 ATF_TP_ADD_TC(tp, once2);
193 ATF_TP_ADD_TC(tp, once3);
195 return atf_no_error();