Expand PMF_FN_* macros.
[netbsd-mini2440.git] / regress / lib / libpthread / mutex3 / mutex3.c
blobf698fc612b1bc0ae9a8fc21f95fc492c91e559a8
1 /* $NetBSD: mutex3.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 = PTHREAD_MUTEX_INITIALIZER;
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 3\n");
23 x = 0;
24 count = count2 = 10000000;
25 ret = pthread_mutex_lock(&mutex);
26 if (ret != 0)
27 err(1, "pthread_mutex_lock (1)");
28 ret = pthread_create(&new, NULL, threadfunc, &count2);
29 if (ret != 0)
30 err(1, "pthread_create");
32 printf("1: Thread %p\n", pthread_self());
33 ret = pthread_mutex_unlock(&mutex);
34 if (ret != 0)
35 err(1, "pthread_mutex_unlock(1)");
38 while (count--) {
39 ret = pthread_mutex_lock(&mutex);
40 if (ret != 0)
41 err(1, "pthread_mutex_lock(2)");
42 x++;
43 ret = pthread_mutex_unlock(&mutex);
44 if (ret != 0)
45 err(1, "pthread_mutex_unlock(2)");
48 ret = pthread_join(new, &joinval);
49 if (ret != 0)
50 err(1, "pthread_join");
52 pthread_mutex_lock(&mutex);
53 printf("1: Thread joined. X was %d. Return value (long) was %ld\n",
54 x, (long)joinval);
55 assert(x == 20000000);
56 return 0;
59 void *
60 threadfunc(void *arg)
62 long count = *(int *)arg;
63 int ret;
65 printf("2: Second thread (%p). Count is %ld\n", pthread_self(), count);
67 while (count--) {
68 ret = pthread_mutex_lock(&mutex);
69 if (ret != 0)
70 err(1, "pthread_mutex_lock(3)");
71 x++;
72 ret = pthread_mutex_unlock(&mutex);
73 if (ret != 0)
74 err(1, "pthread_mutex_unlock(3)");
77 return (void *)count;