Expand PMF_FN_* macros.
[netbsd-mini2440.git] / regress / lib / libpthread / once3 / once3.c
blobde14fc73b45544054f244204e37f32228f315541
1 /* $NetBSD: once3.c,v 1.1 2005/07/16 23:02:11 nathanw Exp $ */
3 #include <pthread.h>
4 #include <signal.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <sys/time.h>
10 pthread_once_t once = PTHREAD_ONCE_INIT;
11 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
13 void ofunc(void);
14 void* threadfunc(void *);
15 void cleanup(void *);
16 void handler(int, siginfo_t *, void *);
18 int
19 main(int argc, char *argv[])
21 pthread_t thread;
22 struct sigaction act;
23 struct itimerval it;
24 printf("Test 3 of pthread_once() (test versus cancellation)\n");
26 act.sa_sigaction = handler;
27 sigemptyset(&act.sa_mask);
28 act.sa_flags = SA_SIGINFO;
29 sigaction(SIGALRM, &act, NULL);
31 timerclear(&it.it_value);
32 it.it_value.tv_usec = 500000;
33 timerclear(&it.it_interval);
34 setitimer(ITIMER_REAL, &it, NULL);
36 pthread_mutex_lock(&mutex);
37 pthread_create(&thread, NULL, threadfunc, NULL);
38 pthread_cancel(thread);
39 pthread_mutex_unlock(&mutex);
40 pthread_join(thread, NULL);
42 pthread_once(&once, ofunc);
44 /* Cancel timer */
45 timerclear(&it.it_value);
46 setitimer(ITIMER_REAL, &it, NULL);
48 printf("Test succeeded\n");
50 return 0;
53 void
54 cleanup(void *m)
56 pthread_mutex_t *mu = m;
58 pthread_mutex_unlock(mu);
61 void
62 ofunc(void)
65 pthread_testcancel();
68 void *
69 threadfunc(void *arg)
72 pthread_mutex_lock(&mutex);
73 pthread_cleanup_push(cleanup, &mutex);
74 pthread_once(&once, ofunc);
75 pthread_cleanup_pop(1);
76 return NULL;
80 void
81 handler(int sig, siginfo_t *info, void *ctx)
83 printf("Signal handler was called; main thread deadlocked in "
84 "pthread_once().\n"
85 "Test failed.\n");
86 exit(1);