Expand PMF_FN_* macros.
[netbsd-mini2440.git] / regress / lib / libpthread / sigmask3 / sigmask3.c
blob21ddb23b27b71c7316507f1ca48b6507951fbe42
1 #include <pthread.h>
2 #include <stdio.h>
3 #include <signal.h>
4 #include <stdlib.h>
5 #include <unistd.h>
7 /* Test that signal masks are respected while threads are running. */
8 volatile sig_atomic_t flag;
9 volatile sig_atomic_t flag2;
11 volatile pthread_t thr_usr1;
12 volatile pthread_t thr_usr2;
14 void handler1(int, siginfo_t *, void *);
15 void handler2(int, siginfo_t *, void *);
16 void *threadroutine(void *);
18 void
19 handler1(int sig, siginfo_t *info, void *ctx)
22 kill(getpid(), SIGUSR2);
24 * If the mask is properly set, SIGUSR2 will not be handled
25 * by the current thread until this handler returns.
27 flag = 1;
28 thr_usr1 = pthread_self();
31 void
32 handler2(int sig, siginfo_t *info, void *ctx)
34 if (flag == 1)
35 flag = 2;
36 flag2 = 1;
37 thr_usr2 = pthread_self();
40 void *
41 threadroutine(void *arg)
44 kill(getpid(), SIGUSR1);
45 sleep(1);
47 if (flag == 2)
48 printf("Success: Both handlers ran in order\n");
49 else if (flag == 1 && flag2 == 1 && thr_usr1 != thr_usr2)
50 printf("Success: Handlers were invoked by different threads\n");
51 else {
52 printf("Failure: flag=%d, flag2=%d, thr1=%p, thr2=%p\n",
53 (int)flag, (int)flag2, (void *)thr_usr1, (void *)thr_usr2);
54 exit(1);
57 return NULL;
60 int
61 main(void)
63 struct sigaction act;
64 pthread_t thread;
65 int ret;
67 act.sa_sigaction = handler1;
68 sigemptyset(&act.sa_mask);
69 sigaddset(&act.sa_mask, SIGUSR2);
70 act.sa_flags = SA_SIGINFO;
72 ret = sigaction(SIGUSR1, &act, NULL);
73 if (ret) {
74 printf("sigaction: %d\n", ret);
75 exit(1);
78 act.sa_sigaction = handler2;
79 sigemptyset(&act.sa_mask);
80 act.sa_flags = SA_SIGINFO;
81 ret = sigaction(SIGUSR2, &act, NULL);
83 ret = pthread_create(&thread, NULL, threadroutine, NULL);
84 if (ret) {
85 printf("pthread_create: %d\n", ret);
86 exit(1);
88 ret = pthread_join(thread, NULL);
89 if (ret) {
90 printf("pthread_join: %d\n", ret);
91 exit(1);
94 return 0;