1 /* Test for Pthreads/mutexes.
2 Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Kurt Garloff <garloff@suse.de>, 2000.
6 The GNU C Library is free software; you can redistribute it and/or
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
9 License, or (at your option) any later version.
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
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
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. */
28 static void *thread_start (void *ptr
) __attribute__ ((__noreturn__
));
33 pthread_mutex_t mutex
;
39 dump_mut (pthread_mutex_t
* mut
)
42 for (i
= 0; i
< sizeof (*mut
); i
++)
43 printf (" %02x", *((unsigned char *) mut
+ i
));
47 /* Helper, the opposite of pthread_cond_wait (cond, mut). */
49 pthr_cond_signal_mutex (pthread_cond_t
* cond
, pthread_mutex_t
* mut
)
52 err
= pthread_mutex_lock (mut
);
54 printf ("mutex_lock : %s\n", strerror (err
));
55 err
= pthread_cond_signal (cond
);
57 printf ("cond_signal : %s\n", strerror (err
));
58 err
= pthread_mutex_unlock (mut
);
60 printf ("mutex_unlock: %s\n", strerror (err
));
64 thread_start (void *ptr
)
66 struct thr_ctrl
*tc
= ptr
;
67 /* Do initialization. */
69 /* Signal that we are ready. */
70 pthr_cond_signal_mutex (&tc
->cond
, &tc
->mutex
);
72 pthr_cond_signal_mutex (&tc
->cond
, &tc
->mutex
);
74 pthread_exit (&tc
->retval
);
80 struct thr_ctrl threadctrl
;
83 void *res
= &threadctrl
.retval
;
84 pthread_mutexattr_t mutattr
;
85 pthread_mutexattr_init (&mutattr
);
86 pthread_mutex_init (&threadctrl
.mutex
, &mutattr
);
87 pthread_cond_init (&threadctrl
.cond
, NULL
);
88 err
= pthread_mutex_lock (&threadctrl
.mutex
);
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. */
94 err
= pthread_cond_wait (&threadctrl
.cond
, &threadctrl
.mutex
);
96 printf ("cond_wait : %s\n", strerror (err
));
97 /* Now, we should have acquired the mutex again! */
98 dump_mut (&threadctrl
.mutex
);
100 dump_mut (&threadctrl
.mutex
);
101 err
= pthread_cond_wait (&threadctrl
.cond
, &threadctrl
.mutex
);
104 printf ("cond_wait : %s\n", strerror (err
));
108 dump_mut (&threadctrl
.mutex
);
109 pthread_join (thread
, &res
);