(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / linuxthreads / Examples / ex13.c
blob14add6c7733ed445cd825c4e0a30cd2733ad4c61
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. */
21 #include <errno.h>
22 #include <pthread.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
28 static void *thread_start (void *ptr) __attribute__ ((__noreturn__));
31 struct thr_ctrl
33 pthread_mutex_t mutex;
34 pthread_cond_t cond;
35 int retval;
38 static void
39 dump_mut (pthread_mutex_t * mut)
41 size_t i;
42 for (i = 0; i < sizeof (*mut); i++)
43 printf (" %02x", *((unsigned char *) mut + i));
44 printf ("\n");
47 /* Helper, the opposite of pthread_cond_wait (cond, mut). */
48 static void
49 pthr_cond_signal_mutex (pthread_cond_t * cond, pthread_mutex_t * mut)
51 int err;
52 err = pthread_mutex_lock (mut);
53 if (err)
54 printf ("mutex_lock : %s\n", strerror (err));
55 err = pthread_cond_signal (cond);
56 if (err)
57 printf ("cond_signal : %s\n", strerror (err));
58 err = pthread_mutex_unlock (mut);
59 if (err)
60 printf ("mutex_unlock: %s\n", strerror (err));
63 static void *
64 thread_start (void *ptr)
66 struct thr_ctrl *tc = ptr;
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);
77 int
78 main (void)
80 struct thr_ctrl threadctrl;
81 pthread_t thread;
82 int err;
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);
89 if (err)
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);
95 if (err)
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);
101 err = pthread_cond_wait (&threadctrl.cond, &threadctrl.mutex);
102 if (err)
104 printf ("cond_wait : %s\n", strerror (err));
105 printf ("ERROR\n");
106 abort ();
108 dump_mut (&threadctrl.mutex);
109 pthread_join (thread, &res);
110 printf ("OK\n");
111 return 0;