Home | History | Annotate | Line # | Download | only in libpthread
t_once.c revision 1.2.16.1
      1  1.2.16.1  perseant /* $NetBSD: t_once.c,v 1.2.16.1 2025/08/02 05:58:09 perseant Exp $ */
      2       1.1      jmmv 
      3       1.1      jmmv /*
      4       1.1      jmmv  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5       1.1      jmmv  * All rights reserved.
      6       1.1      jmmv  *
      7       1.1      jmmv  * Redistribution and use in source and binary forms, with or without
      8       1.1      jmmv  * modification, are permitted provided that the following conditions
      9       1.1      jmmv  * are met:
     10       1.1      jmmv  * 1. Redistributions of source code must retain the above copyright
     11       1.1      jmmv  *    notice, this list of conditions and the following disclaimer.
     12       1.1      jmmv  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1      jmmv  *    notice, this list of conditions and the following disclaimer in the
     14       1.1      jmmv  *    documentation and/or other materials provided with the distribution.
     15       1.1      jmmv  *
     16       1.1      jmmv  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17       1.1      jmmv  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18       1.1      jmmv  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19       1.1      jmmv  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20       1.1      jmmv  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21       1.1      jmmv  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22       1.1      jmmv  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23       1.1      jmmv  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24       1.1      jmmv  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25       1.1      jmmv  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26       1.1      jmmv  * POSSIBILITY OF SUCH DAMAGE.
     27       1.1      jmmv  */
     28       1.1      jmmv 
     29       1.1      jmmv #include <sys/cdefs.h>
     30       1.1      jmmv __COPYRIGHT("@(#) Copyright (c) 2008\
     31       1.1      jmmv  The NetBSD Foundation, inc. All rights reserved.");
     32  1.2.16.1  perseant __RCSID("$NetBSD: t_once.c,v 1.2.16.1 2025/08/02 05:58:09 perseant Exp $");
     33       1.1      jmmv 
     34       1.2  ginsbach #include <sys/time.h>
     35  1.2.16.1  perseant #include <sys/wait.h>
     36  1.2.16.1  perseant 
     37       1.1      jmmv #include <pthread.h>
     38       1.1      jmmv #include <signal.h>
     39       1.1      jmmv #include <stdio.h>
     40       1.1      jmmv #include <stdlib.h>
     41  1.2.16.1  perseant #include <unistd.h>
     42       1.1      jmmv 
     43       1.1      jmmv #include <atf-c.h>
     44       1.1      jmmv 
     45       1.1      jmmv #include "h_common.h"
     46  1.2.16.1  perseant #include "h_macros.h"
     47       1.1      jmmv 
     48       1.1      jmmv static pthread_once_t once = PTHREAD_ONCE_INIT;
     49       1.1      jmmv static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
     50       1.1      jmmv static int x;
     51       1.1      jmmv 
     52       1.1      jmmv #define NTHREADS 25
     53       1.1      jmmv 
     54       1.1      jmmv static void
     55       1.1      jmmv ofunc(void)
     56       1.1      jmmv {
     57       1.1      jmmv 	printf("Variable x has value %d\n", x);
     58       1.1      jmmv 	x++;
     59       1.1      jmmv }
     60       1.1      jmmv 
     61  1.2.16.1  perseant static void
     62  1.2.16.1  perseant ofunc_silent(void)
     63  1.2.16.1  perseant {
     64  1.2.16.1  perseant 	x++;
     65  1.2.16.1  perseant }
     66  1.2.16.1  perseant 
     67       1.1      jmmv ATF_TC(once1);
     68       1.1      jmmv ATF_TC_HEAD(once1, tc)
     69       1.1      jmmv {
     70       1.1      jmmv 	atf_tc_set_md_var(tc, "descr", "Checks pthread_once()");
     71       1.1      jmmv }
     72       1.1      jmmv ATF_TC_BODY(once1, tc)
     73       1.1      jmmv {
     74       1.1      jmmv 
     75       1.1      jmmv 	printf("1: Test 1 of pthread_once()\n");
     76       1.1      jmmv 
     77       1.1      jmmv 	PTHREAD_REQUIRE(pthread_once(&once, ofunc));
     78       1.1      jmmv 	PTHREAD_REQUIRE(pthread_once(&once, ofunc));
     79       1.1      jmmv 
     80       1.1      jmmv 	printf("1: X has value %d\n",x );
     81       1.1      jmmv 	ATF_REQUIRE_EQ(x, 1);
     82       1.1      jmmv }
     83       1.1      jmmv 
     84       1.1      jmmv static void
     85       1.1      jmmv once2_ofunc(void)
     86       1.1      jmmv {
     87       1.1      jmmv 	x++;
     88       1.1      jmmv 	printf("ofunc: Variable x has value %d\n", x);
     89       1.1      jmmv 	x++;
     90       1.1      jmmv }
     91       1.1      jmmv 
     92       1.1      jmmv static void *
     93       1.1      jmmv once2_threadfunc(void *arg)
     94       1.1      jmmv {
     95       1.1      jmmv 	int num;
     96       1.1      jmmv 
     97       1.1      jmmv 	PTHREAD_REQUIRE(pthread_once(&once, once2_ofunc));
     98       1.1      jmmv 
     99       1.1      jmmv 	num = *(int *)arg;
    100       1.1      jmmv 	printf("Thread %d sees x with value %d\n", num, x);
    101       1.1      jmmv 	ATF_REQUIRE_EQ(x, 2);
    102       1.1      jmmv 
    103       1.1      jmmv 	return NULL;
    104       1.1      jmmv }
    105       1.1      jmmv 
    106       1.1      jmmv ATF_TC(once2);
    107       1.1      jmmv ATF_TC_HEAD(once2, tc)
    108       1.1      jmmv {
    109       1.1      jmmv 	atf_tc_set_md_var(tc, "descr", "Checks pthread_once()");
    110       1.1      jmmv }
    111       1.1      jmmv ATF_TC_BODY(once2, tc)
    112       1.1      jmmv {
    113       1.1      jmmv 	pthread_t  threads[NTHREADS];
    114       1.1      jmmv 	int id[NTHREADS];
    115       1.1      jmmv 	int i;
    116       1.1      jmmv 
    117       1.1      jmmv 	printf("1: Test 2 of pthread_once()\n");
    118       1.1      jmmv 
    119       1.1      jmmv 	for (i=0; i < NTHREADS; i++) {
    120       1.1      jmmv 		id[i] = i;
    121       1.1      jmmv 		PTHREAD_REQUIRE(pthread_create(&threads[i], NULL, once2_threadfunc, &id[i]));
    122       1.1      jmmv 	}
    123       1.1      jmmv 
    124       1.1      jmmv 	for (i=0; i < NTHREADS; i++)
    125       1.1      jmmv 		PTHREAD_REQUIRE(pthread_join(threads[i], NULL));
    126       1.1      jmmv 
    127       1.1      jmmv 	printf("1: X has value %d\n",x );
    128       1.1      jmmv 	ATF_REQUIRE_EQ(x, 2);
    129       1.1      jmmv }
    130       1.1      jmmv 
    131       1.1      jmmv static void
    132       1.1      jmmv once3_cleanup(void *m)
    133       1.1      jmmv {
    134       1.1      jmmv 	pthread_mutex_t *mu = m;
    135       1.1      jmmv 
    136       1.1      jmmv 	PTHREAD_REQUIRE(pthread_mutex_unlock(mu));
    137       1.1      jmmv }
    138       1.1      jmmv 
    139       1.1      jmmv static void
    140       1.1      jmmv once3_ofunc(void)
    141       1.1      jmmv {
    142       1.1      jmmv 	pthread_testcancel();
    143       1.1      jmmv }
    144       1.1      jmmv 
    145       1.1      jmmv static void *
    146       1.1      jmmv once3_threadfunc(void *arg)
    147       1.1      jmmv {
    148       1.1      jmmv 	PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
    149       1.1      jmmv 	pthread_cleanup_push(once3_cleanup, &mutex);
    150       1.1      jmmv 	PTHREAD_REQUIRE(pthread_once(&once, once3_ofunc));
    151       1.1      jmmv 	pthread_cleanup_pop(1);
    152       1.1      jmmv 
    153       1.1      jmmv 	return NULL;
    154       1.1      jmmv }
    155       1.1      jmmv 
    156       1.1      jmmv static void
    157       1.1      jmmv handler(int sig, siginfo_t *info, void *ctx)
    158       1.1      jmmv {
    159       1.1      jmmv 	atf_tc_fail("Signal handler was called; "
    160       1.1      jmmv 		"main thread deadlocked in pthread_once()");
    161       1.1      jmmv }
    162       1.1      jmmv 
    163       1.1      jmmv ATF_TC(once3);
    164       1.1      jmmv ATF_TC_HEAD(once3, tc)
    165       1.1      jmmv {
    166       1.1      jmmv 	atf_tc_set_md_var(tc, "descr", "Checks pthread_once()");
    167       1.1      jmmv }
    168       1.1      jmmv ATF_TC_BODY(once3, tc)
    169       1.1      jmmv {
    170       1.1      jmmv 	pthread_t thread;
    171       1.1      jmmv 	struct sigaction act;
    172       1.1      jmmv 	struct itimerval it;
    173       1.1      jmmv 	printf("Test 3 of pthread_once() (test versus cancellation)\n");
    174       1.1      jmmv 
    175       1.1      jmmv 	act.sa_sigaction = handler;
    176       1.1      jmmv 	sigemptyset(&act.sa_mask);
    177       1.1      jmmv 	act.sa_flags = SA_SIGINFO;
    178       1.1      jmmv 	sigaction(SIGALRM, &act, NULL);
    179       1.1      jmmv 
    180       1.1      jmmv 	timerclear(&it.it_value);
    181       1.1      jmmv 	it.it_value.tv_usec = 500000;
    182       1.1      jmmv 	timerclear(&it.it_interval);
    183       1.1      jmmv 	setitimer(ITIMER_REAL, &it, NULL);
    184       1.1      jmmv 
    185       1.1      jmmv 	PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
    186       1.1      jmmv 	PTHREAD_REQUIRE(pthread_create(&thread, NULL, once3_threadfunc, NULL));
    187       1.1      jmmv 	PTHREAD_REQUIRE(pthread_cancel(thread));
    188       1.1      jmmv 	PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
    189       1.1      jmmv 	PTHREAD_REQUIRE(pthread_join(thread, NULL));
    190       1.1      jmmv 
    191       1.1      jmmv 	PTHREAD_REQUIRE(pthread_once(&once, ofunc));
    192       1.1      jmmv 
    193       1.1      jmmv 	/* Cancel timer */
    194       1.1      jmmv 	timerclear(&it.it_value);
    195       1.1      jmmv 	setitimer(ITIMER_REAL, &it, NULL);
    196       1.1      jmmv 
    197       1.1      jmmv 	printf("Test succeeded\n");
    198       1.1      jmmv }
    199       1.1      jmmv 
    200  1.2.16.1  perseant static long trial;
    201  1.2.16.1  perseant 
    202  1.2.16.1  perseant static void *
    203  1.2.16.1  perseant fork_and_once(void *cookie)
    204  1.2.16.1  perseant {
    205  1.2.16.1  perseant 	pthread_barrier_t *bar = cookie;
    206  1.2.16.1  perseant 	pid_t pid, child;
    207  1.2.16.1  perseant 	int status;
    208  1.2.16.1  perseant 
    209  1.2.16.1  perseant 	(void)pthread_barrier_wait(bar);
    210  1.2.16.1  perseant 	RL(pid = fork());
    211  1.2.16.1  perseant 	if (pid == 0) {
    212  1.2.16.1  perseant 		(void)alarm(1);
    213  1.2.16.1  perseant 		(void)pthread_once(&once, &ofunc_silent);
    214  1.2.16.1  perseant 		_exit(x - 1);
    215  1.2.16.1  perseant 	}
    216  1.2.16.1  perseant 	RL(child = waitpid(pid, &status, 0));
    217  1.2.16.1  perseant 	ATF_REQUIRE_EQ_MSG(child, pid, "child=%lld pid=%lld",
    218  1.2.16.1  perseant 	    (long long)child, (long long)pid);
    219  1.2.16.1  perseant 	ATF_REQUIRE_MSG(!WIFSIGNALED(status),
    220  1.2.16.1  perseant 	    "child exited on signal %d (%s) in trial %ld",
    221  1.2.16.1  perseant 	    WTERMSIG(status), strsignal(WTERMSIG(status)), trial);
    222  1.2.16.1  perseant 	ATF_REQUIRE_MSG(WIFEXITED(status) && WEXITSTATUS(status) == 0,
    223  1.2.16.1  perseant 	    "child exited 0x%x in trial %ld", status, trial);
    224  1.2.16.1  perseant 	return NULL;
    225  1.2.16.1  perseant }
    226  1.2.16.1  perseant 
    227  1.2.16.1  perseant ATF_TC(oncefork);
    228  1.2.16.1  perseant ATF_TC_HEAD(oncefork, tc)
    229  1.2.16.1  perseant {
    230  1.2.16.1  perseant 	atf_tc_set_md_var(tc, "descr", "Test racing pthread_once with fork");
    231  1.2.16.1  perseant }
    232  1.2.16.1  perseant ATF_TC_BODY(oncefork, tc)
    233  1.2.16.1  perseant {
    234  1.2.16.1  perseant 	static pthread_once_t once0 = PTHREAD_ONCE_INIT;
    235  1.2.16.1  perseant 	pthread_barrier_t bar;
    236  1.2.16.1  perseant 	long ntrials = atf_tc_get_config_var_as_long_wd(tc,
    237  1.2.16.1  perseant 	    "pthread_once_forktrials", 0);
    238  1.2.16.1  perseant 
    239  1.2.16.1  perseant 	if (ntrials <= 0) {
    240  1.2.16.1  perseant 		atf_tc_skip("pthread_once takes thousands of fork trials"
    241  1.2.16.1  perseant 		    " on a multicore system to detect a race; set"
    242  1.2.16.1  perseant 		    " pthread_once_forktrials to the number of trials to"
    243  1.2.16.1  perseant 		    " enable this test");
    244  1.2.16.1  perseant 	}
    245  1.2.16.1  perseant 
    246  1.2.16.1  perseant 	RZ(pthread_barrier_init(&bar, NULL, 2));
    247  1.2.16.1  perseant 
    248  1.2.16.1  perseant 	for (trial = 0; trial < ntrials; trial++) {
    249  1.2.16.1  perseant 		pthread_t t;
    250  1.2.16.1  perseant 
    251  1.2.16.1  perseant 		once = once0;
    252  1.2.16.1  perseant 		x = 0;
    253  1.2.16.1  perseant 
    254  1.2.16.1  perseant 		RZ(pthread_create(&t, NULL, &fork_and_once, &bar));
    255  1.2.16.1  perseant 		(void)alarm(1);
    256  1.2.16.1  perseant 		(void)pthread_barrier_wait(&bar);
    257  1.2.16.1  perseant 		(void)pthread_once(&once, &ofunc_silent);
    258  1.2.16.1  perseant 		(void)alarm(0);
    259  1.2.16.1  perseant 		RZ(pthread_join(t, NULL));
    260  1.2.16.1  perseant 	}
    261  1.2.16.1  perseant }
    262  1.2.16.1  perseant 
    263       1.1      jmmv ATF_TP_ADD_TCS(tp)
    264       1.1      jmmv {
    265       1.1      jmmv 	ATF_TP_ADD_TC(tp, once1);
    266       1.1      jmmv 	ATF_TP_ADD_TC(tp, once2);
    267       1.1      jmmv 	ATF_TP_ADD_TC(tp, once3);
    268  1.2.16.1  perseant 	ATF_TP_ADD_TC(tp, oncefork);
    269       1.1      jmmv 
    270       1.1      jmmv 	return atf_no_error();
    271       1.1      jmmv }
    272