Sync usage with man page.
[netbsd-mini2440.git] / regress / lib / libpthread / mutex4 / mutex4.c
blobb73e5301aa4829f0a8326c95a42d6b500a0d6ab7
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 pthread_mutexattr_t mattr;
20 void *joinval;
22 printf("1: Mutex-test 4\n");
24 pthread_mutexattr_init(&mattr);
25 pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_RECURSIVE);
27 pthread_mutex_init(&mutex, &mattr);
29 pthread_mutexattr_destroy(&mattr);
31 x = 1;
32 pthread_mutex_lock(&mutex);
33 ret = pthread_create(&new, NULL, threadfunc, &x);
34 if (ret != 0)
35 err(1, "pthread_create");
37 printf("1: Before recursively acquiring the mutex.\n");
38 ret = pthread_mutex_lock(&mutex);
39 if (ret != 0)
40 err(1, "pthread_mutex_lock recursive");
42 printf("1: Before releasing the mutex once.\n");
43 sleep(2);
44 pthread_mutex_unlock(&mutex);
45 printf("1: After releasing the mutex once.\n");
47 x = 20;
49 printf("1: Before releasing the mutex twice.\n");
50 sleep(2);
51 pthread_mutex_unlock(&mutex);
52 printf("1: After releasing the mutex twice.\n");
54 ret = pthread_join(new, &joinval);
55 if (ret != 0)
56 err(1, "pthread_join");
58 pthread_mutex_lock(&mutex);
59 printf("1: Thread joined. X was %d. Return value (int) was %d\n",
60 x, *(int *)joinval);
61 assert(x == 21);
62 assert(*(int *)joinval == 21);
63 pthread_mutex_unlock(&mutex);
64 return 0;
67 void *
68 threadfunc(void *arg)
70 int *param;
72 printf("2: Second thread.\n");
74 param = arg;
75 printf("2: Locking mutex\n");
76 pthread_mutex_lock(&mutex);
77 printf("2: Got mutex. *param = %d\n", *param);
78 (*param)++;
80 pthread_mutex_unlock(&mutex);
82 return param;