Hackfix and re-enable strtoull and wcstoull, see bug #3798.
[sdcc.git] / sdcc / support / regression / tcc / 114_bound_signal.c
blobd11b14616c5bfb516441f99b14d4977a401af87b
1 #include <stdio.h>
2 #include <pthread.h>
3 #include <semaphore.h>
4 #include <signal.h>
5 #include <string.h>
6 #include <sys/types.h>
7 #include <sys/wait.h>
8 #include <unistd.h>
9 #include <stdlib.h>
10 #include <errno.h>
11 #include <setjmp.h>
13 static volatile int run = 1;
14 static sem_t sem;
15 static sem_t sem_child;
17 static void
18 add (int n)
20 int i;
21 int arr[n];
23 for (i = 0; i < n; i++) {
24 arr[i]++;
26 memset (&arr[0], 0, n * sizeof(int));
29 static void *
30 high_load (void *unused)
32 while (run) {
33 add(10);
34 add(20);
36 return NULL;
39 static void *
40 do_signal (void *unused)
42 while (run) {
43 kill (getpid(), SIGUSR1);
44 while (sem_wait(&sem) < 0 && errno == EINTR);
46 return NULL;
49 static void *
50 do_fork (void *unused)
52 pid_t pid;
54 while (run) {
55 switch ((pid = fork())) {
56 case 0:
57 add(1000);
58 add(2000);
59 exit(0);
60 break;
61 case -1:
62 return NULL;
63 default:
64 while (sem_wait(&sem_child) < 0 && errno == EINTR);
65 wait(NULL);
66 break;
69 return NULL;
72 static void signal_handler(int sig)
74 add(10);
75 add(20);
76 sem_post (&sem);
79 static void child_handler(int sig)
81 add(10);
82 add(20);
83 sem_post (&sem_child);
86 int
87 main (void)
89 int i;
90 pthread_t id1, id2, id3;
91 struct sigaction act;
92 sigjmp_buf sj;
93 sigset_t m;
94 time_t end;
96 memset (&act, 0, sizeof (act));
97 act.sa_handler = signal_handler;
98 act.sa_flags = 0;
99 sigemptyset (&act.sa_mask);
100 sigaction (SIGUSR1, &act, NULL);
101 act.sa_handler = child_handler;
102 sigaction (SIGCHLD, &act, NULL);
104 printf ("start\n"); fflush(stdout);
106 sem_init (&sem, 0, 0);
107 sem_init (&sem_child, 0, 0);
108 pthread_create(&id1, NULL, high_load, NULL);
109 pthread_create(&id2, NULL, do_signal, NULL);
110 #if !defined(__APPLE__)
111 pthread_create(&id3, NULL, do_fork, NULL);
112 #endif
114 /* sleep does not work !!! */
115 end = time(NULL) + 2;
116 while (time(NULL) < end) ;
117 run = 0;
119 pthread_join(id1, NULL);
120 pthread_join(id2, NULL);
121 #if !defined(__APPLE__)
122 pthread_join(id3, NULL);
123 #endif
124 sem_destroy (&sem);
125 sem_destroy (&sem_child);
127 printf ("end\n"); fflush(stdout);
129 sigemptyset (&m);
130 sigprocmask (SIG_SETMASK, &m, NULL);
131 if (sigsetjmp (sj, 0) == 0)
133 sigaddset (&m, SIGUSR1);
134 sigprocmask (SIG_SETMASK, &m, NULL);
135 siglongjmp (sj, 1);
136 printf ("failed");
137 return 1;
139 sigprocmask (SIG_SETMASK, NULL, &m);
140 if (!sigismember (&m, SIGUSR1))
141 printf ("failed");
142 return 0;