1 /*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28 #include <pthread.h>
29 #include <semaphore.h>
30 #include <errno.h>
31 #include <stdio.h>
32 #include <time.h>
33 #include <string.h>
34 #include <unistd.h>
35
36 static pthread_mutex_t lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER;
37 static pthread_cond_t wait = PTHREAD_COND_INITIALIZER;
38
_thread1(void * __u)39 static void* _thread1(void *__u __attribute__((unused)))
40 {
41 printf("1: obtaining mutex\n");
42 pthread_mutex_lock(&lock);
43 printf("1: waiting on condition variable\n");
44 pthread_cond_wait(&wait, &lock);
45 printf("1: releasing mutex\n");
46 pthread_mutex_unlock(&lock);
47 printf("1: exiting\n");
48 return NULL;
49 }
50
_thread2(void * __u)51 static void* _thread2(void *__u __attribute__((unused)))
52 {
53 int cnt = 2;
54 while(cnt--) {
55 printf("2: obtaining mutex\n");
56 pthread_mutex_lock(&lock);
57 printf("2: signaling\n");
58 pthread_cond_signal(&wait);
59 printf("2: releasing mutex\n");
60 pthread_mutex_unlock(&lock);
61 }
62
63 printf("2: exiting\n");
64 return NULL;
65 }
66
67 typedef void* (*thread_func)(void*);
68 static const thread_func thread_routines[] =
69 {
70 &_thread1,
71 &_thread2,
72 };
73
main(void)74 int main(void)
75 {
76 pthread_t t[2];
77 int nn;
78 int count = (int)(sizeof t/sizeof t[0]);
79
80 for (nn = 0; nn < count; nn++) {
81 printf("main: creating thread %d\n", nn+1);
82 if (pthread_create( &t[nn], NULL, thread_routines[nn], NULL) < 0) {
83 printf("main: could not create thread %d: %s\n", nn+1, strerror(errno));
84 return -2;
85 }
86 }
87
88 for (nn = 0; nn < count; nn++) {
89 printf("main: joining thread %d\n", nn+1);
90 if (pthread_join(t[nn], NULL)) {
91 printf("main: could not join thread %d: %s\n", nn+1, strerror(errno));
92 return -2;
93 }
94 }
95
96 return 0;
97 }
98