Expand PMF_FN_* macros.
[netbsd-mini2440.git] / regress / lib / libpthread / mutex2 / mutex2.c
blobaf4de16986aab9004f92dcff13d9b946cbf4e222
1 /* $NetBSD: mutex2.c,v 1.1 2003/01/30 18:05:26 thorpej Exp $ */
3 #include <assert.h>
4 #include <err.h>
5 #include <pthread.h>
6 #include <stdio.h>
7 #include <stdlib.h>
9 void *threadfunc(void *arg);
11 pthread_mutex_t mutex;
12 int x;
14 int
15 main(int argc, char *argv[])
17 int count, count2, ret;
18 pthread_t new;
19 void *joinval;
21 printf("1: Mutex-test 2\n");
23 ret = pthread_mutex_init(&mutex, NULL);
24 if (ret != 0)
25 err(1, "pthread_mutex_init");
26 x = 0;
27 count = count2 = 10000000;
28 ret = pthread_mutex_lock(&mutex);
29 if (ret != 0)
30 err(1, "pthread_mutex_lock (1)");
31 ret = pthread_create(&new, NULL, threadfunc, &count2);
32 if (ret != 0)
33 err(1, "pthread_create");
35 printf("1: Thread %p\n", pthread_self());
36 ret = pthread_mutex_unlock(&mutex);
37 if (ret != 0)
38 err(1, "pthread_mutex_unlock(1)");
41 while (count--) {
42 ret = pthread_mutex_lock(&mutex);
43 if (ret != 0)
44 err(1, "pthread_mutex_lock(2)");
45 x++;
46 ret = pthread_mutex_unlock(&mutex);
47 if (ret != 0)
48 err(1, "pthread_mutex_unlock(2)");
51 ret = pthread_join(new, &joinval);
52 if (ret != 0)
53 err(1, "pthread_join");
55 pthread_mutex_lock(&mutex);
56 printf("1: Thread joined. X was %d. Return value (long) was %ld\n",
57 x, (long)joinval);
58 assert(x == 20000000);
59 return 0;
62 void *
63 threadfunc(void *arg)
65 long count = *(int *)arg;
66 int ret;
68 printf("2: Second thread (%p). Count is %ld\n", pthread_self(), count);
70 while (count--) {
71 ret = pthread_mutex_lock(&mutex);
72 if (ret != 0)
73 err(1, "pthread_mutex_lock(3)");
74 x++;
75 ret = pthread_mutex_unlock(&mutex);
76 if (ret != 0)
77 err(1, "pthread_mutex_unlock(3)");
80 return (void *)count;