Expand PMF_FN_* macros.
[netbsd-mini2440.git] / regress / lib / libpthread / condcancel1 / condcancel1.c
blobe61d32f8f22aed79be575fb769fd5108213f290b
1 /* $Id: condcancel1.c,v 1.2 2003/11/21 19:25:50 nathanw Exp $ */
3 #include <err.h>
4 #include <pthread.h>
5 #include <stdio.h>
6 #include <string.h>
8 void *threadfunc(void *);
9 void unlock(void *);
11 pthread_mutex_t mutex;
12 pthread_cond_t cond;
13 int share;
15 int
16 main(int argc, char *argv[])
18 pthread_t thread;
19 int ret;
21 printf("Test of CV state after cancelling a wait\n");
23 ret = pthread_mutex_init(&mutex, NULL);
24 if (ret) errx(1, "pthread_mutex_init: %s", strerror(ret));
26 ret = pthread_cond_init(&cond, NULL);
27 if (ret) errx(1, "pthread_cond_init: %s", strerror(ret));
29 ret = pthread_mutex_lock(&mutex);
30 if (ret) errx(1, "pthread_mutex_lock: %s", strerror(ret));
32 ret = pthread_create(&thread, NULL, threadfunc, NULL);
33 if (ret) errx(1, "pthread_create: %s", strerror(ret));
35 while (share == 0) {
36 ret = pthread_cond_wait(&cond, &mutex);
37 if (ret) errx(1, "pthread_cond_wait: %s", strerror(ret));
40 ret = pthread_mutex_unlock(&mutex);
41 if (ret) errx(1, "pthread_mutex_unlock: %s", strerror(ret));
43 ret = pthread_cancel(thread);
44 if (ret) errx(1, "pthread_cancel: %s", strerror(ret));
46 ret = pthread_join(thread, NULL);
47 if (ret) errx(1, "pthread_join: %s", strerror(ret));
49 ret = pthread_cond_destroy(&cond);
50 if (ret) errx(1, "pthread_cond_destroy: %s", strerror(ret));
52 printf("CV successfully destroyed.\n");
54 ret = pthread_mutex_destroy(&mutex);
55 if (ret) errx(1, "pthread_mutex_destroy: %s", strerror(ret));
57 return 0;
60 void *
61 threadfunc(void *arg)
63 int ret;
65 ret = pthread_mutex_lock(&mutex);
66 if (ret) errx(1, "pthread_mutex_lock: %s", strerror(ret));
68 pthread_cleanup_push(unlock, &mutex);
70 while (1) {
71 share = 1;
72 ret = pthread_cond_broadcast(&cond);
73 if (ret) errx(1, "pthread_cond_broadcast: %s", strerror(ret));
74 ret = pthread_cond_wait(&cond, &mutex);
75 if (ret) errx(1, "pthread_cond_wait: %s", strerror(ret));
78 pthread_cleanup_pop(0);
79 ret = pthread_mutex_unlock(&mutex);
80 if (ret) errx(1, "pthread_mutex_unlock: %s", strerror(ret));
84 return NULL;
87 void
88 unlock(void *arg)
90 pthread_mutex_unlock((pthread_mutex_t *)arg);