]> git.saurik.com Git - apple/libpthread.git/blame - tests/rwlock-22244050.c
libpthread-301.20.1.tar.gz
[apple/libpthread.git] / tests / rwlock-22244050.c
CommitLineData
3a6437e6
A
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
8pthread_t thr;
9pthread_rwlock_t lock;
10OSSpinLock slock = 0;
11int i = 0;
12
13void 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
25void* thread(void *arg)
26{
27 pthread_rwlock_rdlock(&lock);
28 pthread_rwlock_unlock(&lock);
29 return NULL;
30}
31
32int 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}