]> git.saurik.com Git - apple/libpthread.git/blob - tests/rwlock-22244050.c
libpthread-330.230.1.tar.gz
[apple/libpthread.git] / tests / rwlock-22244050.c
1 #include <pthread.h>
2 #include <stdio.h>
3 #include <signal.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <libkern/OSAtomic.h>
7
8 pthread_t thr;
9 pthread_rwlock_t lock;
10 OSSpinLock slock = 0;
11 int i = 0;
12
13 void sighandler(int sig)
14 {
15 if (sig == SIGUSR1) {
16 OSSpinLockLock(&slock);
17 OSSpinLockUnlock(&slock);
18 } else {
19 // ALARM
20 fprintf(stderr, "FAIL (%d)\n", i);
21 exit(1);
22 }
23 }
24
25 void* thread(void *arg)
26 {
27 pthread_rwlock_rdlock(&lock);
28 pthread_rwlock_unlock(&lock);
29 return NULL;
30 }
31
32 int main(int argc, const char *argv[])
33 {
34 pthread_rwlock_init(&lock, NULL);
35 signal(SIGUSR1, sighandler);
36 signal(SIGALRM, sighandler);
37
38 alarm(30);
39
40 while (i++ < 10000) {
41 pthread_rwlock_wrlock(&lock);
42 pthread_create(&thr, NULL, thread, NULL);
43 OSSpinLockLock(&slock);
44 pthread_kill(thr, SIGUSR1);
45 pthread_rwlock_unlock(&lock);
46 OSSpinLockUnlock(&slock);
47 }
48
49 fprintf(stderr, "PASS\n");
50 return 0;
51 }