Expand PMF_FN_* macros.
[netbsd-mini2440.git] / regress / lib / libpthread / mutex1 / mutex1.c
blobdbccad490e91c9dd04f85a2bfaec61a5f1bb3502
1 /* $NetBSD$ */
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_mutex_t mutex;
14 int
15 main(int argc, char *argv[])
17 int x,ret;
18 pthread_t new;
19 void *joinval;
21 printf("1: Mutex-test 1\n");
23 pthread_mutex_init(&mutex, NULL);
24 x = 1;
25 pthread_mutex_lock(&mutex);
26 ret = pthread_create(&new, NULL, threadfunc, &x);
27 if (ret != 0)
28 err(1, "pthread_create");
29 printf("1: Before changing the value.\n");
30 sleep(2);
31 x = 20;
32 printf("1: Before releasing the mutex.\n");
33 sleep(2);
34 pthread_mutex_unlock(&mutex);
35 printf("1: After releasing the mutex.\n");
36 ret = pthread_join(new, &joinval);
37 if (ret != 0)
38 err(1, "pthread_join");
40 pthread_mutex_lock(&mutex);
41 printf("1: Thread joined. X was %d. Return value (int) was %d\n",
42 x, *(int *)joinval);
43 assert(x == 21);
44 assert(*(int *)joinval == 21);
45 pthread_mutex_unlock(&mutex);
46 return 0;
49 void *
50 threadfunc(void *arg)
52 int *param;
54 printf("2: Second thread.\n");
56 param = arg;
57 printf("2: Locking mutex\n");
58 pthread_mutex_lock(&mutex);
59 printf("2: Got mutex. *param = %d\n", *param);
60 assert(*param == 20);
61 (*param)++;
63 pthread_mutex_unlock(&mutex);
65 return param;