chiark / gitweb /
eglibc (2.11.3-4+deb6u3) squeeze-lts; urgency=medium
[eglibc.git] / nptl / tst-once3.c
1 /* Copyright (C) 2002, 2003, 2005 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
20 #include <pthread.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <time.h>
24
25
26 #define N 100
27
28 static pthread_once_t once = PTHREAD_ONCE_INIT;
29
30 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
31 static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
32
33 static pthread_barrier_t bar;
34
35 static int global;
36 static int cl_called;
37
38 static void
39 once_handler1 (void)
40 {
41   if (pthread_mutex_lock (&mut) != 0)
42     {
43       puts ("once_handler1: mutex_lock failed");
44       exit (1);
45     }
46   puts ("once_handler1: locked");
47
48   int r = pthread_barrier_wait (&bar);
49   if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
50     {
51       puts ("once_handler1: barrier_wait failed");
52       exit (1);
53     }
54
55   puts ("once_handler1: going to wait on cond");
56
57   pthread_cond_wait (&cond, &mut);
58
59   /* We should never get here.  */
60   exit (42);
61 }
62
63 static void
64 once_handler2 (void)
65 {
66   global = 1;
67 }
68
69
70 static void
71 cl (void *arg)
72 {
73   cl_called = 1;
74 }
75
76
77 static void *
78 tf (void *arg)
79 {
80   pthread_cleanup_push (cl, NULL)
81
82   pthread_once (&once, once_handler1);
83
84   pthread_cleanup_pop (0);
85
86   /* We should never get here.  */
87   puts ("pthread_once in tf returned");
88   exit (1);
89 }
90
91
92 static int
93 do_test (void)
94 {
95   pthread_t th;
96
97   if (pthread_barrier_init (&bar, NULL, 2) != 0)
98     {
99       puts ("barrier_init failed");
100       return 1;
101     }
102
103   if (pthread_create (&th, NULL, tf, NULL) != 0)
104     {
105       puts ("first create failed");
106       return 1;
107     }
108
109   int r = pthread_barrier_wait (&bar);
110   if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
111     {
112       puts ("barrier_wait failed");
113       return 1;
114     }
115
116   if (pthread_mutex_lock (&mut) != 0)
117     {
118       puts ("mutex_lock failed");
119       return 1;
120     }
121   /* We unlock the mutex so that we catch the case where the pthread_cond_wait
122      call incorrectly resumes and tries to get the mutex.  */
123   if (pthread_mutex_unlock (&mut) != 0)
124     {
125       puts ("mutex_unlock failed");
126       return 1;
127     }
128
129   /* Cancel the thread.  */
130   puts ("going to cancel");
131   if (pthread_cancel (th) != 0)
132     {
133       puts ("cancel failed");
134       return 1;
135     }
136
137   void *result;
138   pthread_join (th, &result);
139   if (result != PTHREAD_CANCELED)
140     {
141       puts ("join didn't return PTHREAD_CANCELED");
142       return 1;
143     }
144   puts ("joined successfully");
145
146   printf ("once = %d\n", *(int *) &once);
147
148   if (cl_called != 1)
149     {
150       puts ("cleanup handler not called");
151       return 1;
152     }
153
154   pthread_once (&once, once_handler2);
155
156   if (global != 1)
157     {
158       puts ("global still 0");
159       return 1;
160     }
161
162   return 0;
163 }
164
165 #define TEST_FUNCTION do_test ()
166 #define TIMEOUT 4
167 #include "../test-skeleton.c"