]>
Commit | Line | Data |
---|---|---|
64f6b8f3 | 1 | /* Test for Pthreads/mutexes. |
d347a4ab | 2 | Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc. |
64f6b8f3 AJ |
3 | This file is part of the GNU C Library. |
4 | Contributed by Kurt Garloff <garloff@suse.de>, 2000. | |
5 | ||
6 | The GNU C Library is free software; you can redistribute it and/or | |
cc7375ce RM |
7 | modify it under the terms of the GNU Lesser General Public License as |
8 | published by the Free Software Foundation; either version 2.1 of the | |
64f6b8f3 AJ |
9 | License, or (at your option) any later version. |
10 | ||
11 | The GNU C Library is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
cc7375ce | 14 | Lesser General Public License for more details. |
64f6b8f3 | 15 | |
cc7375ce | 16 | You should have received a copy of the GNU Lesser General Public |
64f6b8f3 AJ |
17 | License along with the GNU C Library; see the file COPYING.LIB. If not, |
18 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
19 | Boston, MA 02111-1307, USA. */ | |
20 | ||
64f6b8f3 AJ |
21 | #include <errno.h> |
22 | #include <pthread.h> | |
3b526f89 | 23 | #include <stdio.h> |
c5ded983 | 24 | #include <stdlib.h> |
3b526f89 AJ |
25 | #include <string.h> |
26 | #include <unistd.h> | |
64f6b8f3 | 27 | |
9403ec5d AJ |
28 | static void *thread_start (void *ptr) __attribute__ ((__noreturn__)); |
29 | ||
30 | ||
64f6b8f3 AJ |
31 | struct thr_ctrl |
32 | { | |
33 | pthread_mutex_t mutex; | |
34 | pthread_cond_t cond; | |
35 | int retval; | |
36 | }; | |
37 | ||
a375a533 | 38 | static void |
64f6b8f3 AJ |
39 | dump_mut (pthread_mutex_t * mut) |
40 | { | |
160bb409 | 41 | size_t i; |
64f6b8f3 AJ |
42 | for (i = 0; i < sizeof (*mut); i++) |
43 | printf (" %02x", *((unsigned char *) mut + i)); | |
44 | printf ("\n"); | |
45 | }; | |
46 | ||
47 | /* Helper, the opposite of pthread_cond_wait (cond, mut). */ | |
a375a533 | 48 | static void |
64f6b8f3 AJ |
49 | pthr_cond_signal_mutex (pthread_cond_t * cond, pthread_mutex_t * mut) |
50 | { | |
51 | int err; | |
3b526f89 AJ |
52 | err = pthread_mutex_lock (mut); |
53 | if (err) | |
64f6b8f3 | 54 | printf ("mutex_lock : %s\n", strerror (err)); |
3b526f89 AJ |
55 | err = pthread_cond_signal (cond); |
56 | if (err) | |
64f6b8f3 | 57 | printf ("cond_signal : %s\n", strerror (err)); |
3b526f89 AJ |
58 | err = pthread_mutex_unlock (mut); |
59 | if (err) | |
64f6b8f3 AJ |
60 | printf ("mutex_unlock: %s\n", strerror (err)); |
61 | } | |
62 | ||
a375a533 | 63 | static void * |
64f6b8f3 AJ |
64 | thread_start (void *ptr) |
65 | { | |
66 | struct thr_ctrl *tc = ptr; | |
64f6b8f3 AJ |
67 | /* Do initialization. */ |
68 | /* ... */ | |
69 | /* Signal that we are ready. */ | |
70 | pthr_cond_signal_mutex (&tc->cond, &tc->mutex); | |
71 | sleep (2); | |
72 | pthr_cond_signal_mutex (&tc->cond, &tc->mutex); | |
73 | tc->retval = 0; | |
74 | pthread_exit (&tc->retval); | |
75 | } | |
76 | ||
77 | int | |
78 | main (void) | |
79 | { | |
80 | struct thr_ctrl threadctrl; | |
81 | pthread_t thread; | |
82 | int err; | |
d347a4ab | 83 | void *res = &threadctrl.retval; |
64f6b8f3 AJ |
84 | pthread_mutexattr_t mutattr; |
85 | pthread_mutexattr_init (&mutattr); | |
86 | pthread_mutex_init (&threadctrl.mutex, &mutattr); | |
87 | pthread_cond_init (&threadctrl.cond, NULL); | |
3b526f89 AJ |
88 | err = pthread_mutex_lock (&threadctrl.mutex); |
89 | if (err) | |
64f6b8f3 AJ |
90 | printf ("mutex_lock : %s\n", strerror (err)); |
91 | dump_mut (&threadctrl.mutex); | |
92 | pthread_create (&thread, NULL, thread_start, &threadctrl); | |
93 | /* Wait until it's ready. */ | |
3b526f89 AJ |
94 | err = pthread_cond_wait (&threadctrl.cond, &threadctrl.mutex); |
95 | if (err) | |
64f6b8f3 AJ |
96 | printf ("cond_wait : %s\n", strerror (err)); |
97 | /* Now, we should have acquired the mutex again! */ | |
98 | dump_mut (&threadctrl.mutex); | |
99 | sleep (1); | |
100 | dump_mut (&threadctrl.mutex); | |
3b526f89 | 101 | err = pthread_cond_wait (&threadctrl.cond, &threadctrl.mutex); |
64f6b8f3 AJ |
102 | if (err) |
103 | { | |
3b526f89 | 104 | printf ("cond_wait : %s\n", strerror (err)); |
64f6b8f3 AJ |
105 | printf ("ERROR\n"); |
106 | abort (); | |
107 | }; | |
108 | dump_mut (&threadctrl.mutex); | |
d347a4ab | 109 | pthread_join (thread, &res); |
64f6b8f3 AJ |
110 | printf ("OK\n"); |
111 | return 0; | |
112 | } |