Expand PMF_FN_* macros.
[netbsd-mini2440.git] / regress / lib / libpthread / barrier1 / barrier1.c
blob7aa88eeaec78a1f0c4b23c84bae3bd5785738fe2
1 /* $NetBSD: barrier1.c,v 1.2 2006/12/23 17:44:12 ad Exp $ */
3 #include <assert.h>
4 #include <err.h>
5 #include <pthread.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <unistd.h>
10 void *threadfunc(void *arg);
12 pthread_barrier_t barrier;
13 pthread_mutex_t mutex;
14 int serial_count;
15 int after_barrier_count;
17 #define COUNT 10
19 int
20 main(int argc, char *argv[])
22 int i, ret;
23 pthread_t new[COUNT];
24 void *joinval;
26 pthread_mutex_init(&mutex, NULL);
28 ret = pthread_barrier_init(&barrier, NULL, COUNT);
29 if (ret != 0)
30 err(1, "pthread_barrier_init");
32 for (i = 0; i < COUNT; i++) {
33 pthread_mutex_lock(&mutex);
34 assert(after_barrier_count == 0);
35 pthread_mutex_unlock(&mutex);
36 ret = pthread_create(&new[i], NULL, threadfunc,
37 (void *)(long)i);
38 if (ret != 0)
39 err(1, "pthread_create");
40 sleep(2);
43 for (i = 0; i < COUNT; i++) {
44 ret = pthread_join(new[i], &joinval);
45 if (ret != 0)
46 err(1, "pthread_join: %d", ret);
47 pthread_mutex_lock(&mutex);
48 assert(after_barrier_count > i);
49 pthread_mutex_unlock(&mutex);
50 printf("main joined with thread %d\n", i);
53 pthread_mutex_lock(&mutex);
54 assert(after_barrier_count == COUNT);
55 pthread_mutex_unlock(&mutex);
56 assert(serial_count == 1);
58 return 0;
61 void *
62 threadfunc(void *arg)
64 int which = (int)(long)arg;
65 int ret;
67 printf("thread %d entering barrier\n", which);
68 ret = pthread_barrier_wait(&barrier);
69 printf("thread %d leaving barrier -> %d\n", which, ret);
71 pthread_mutex_lock(&mutex);
72 after_barrier_count++;
73 if (ret == PTHREAD_BARRIER_SERIAL_THREAD)
74 serial_count++;
75 pthread_mutex_unlock(&mutex);
77 return NULL;