source: trunk/src/libctests/glibc/nptl/tst-once3.c@ 2036

Last change on this file since 2036 was 2036, checked in by bird, 20 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 3.3 KB
Line 
1/* Copyright (C) 2002, 2003 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
28static pthread_once_t once = PTHREAD_ONCE_INIT;
29
30static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
31static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
32
33static pthread_barrier_t bar;
34
35static int global;
36static int cl_called;
37
38static void
39once_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 pthread_cond_wait (&cond, &mut);
56
57 /* We should never get here. */
58 exit (42);
59}
60
61static void
62once_handler2 (void)
63{
64 global = 1;
65}
66
67
68static void
69cl (void *arg)
70{
71 cl_called = 1;
72}
73
74
75static void *
76tf (void *arg)
77{
78 pthread_cleanup_push (cl, NULL)
79
80 pthread_once (&once, once_handler1);
81
82 pthread_cleanup_pop (0);
83
84 /* We should never get here. */
85 puts ("pthread_once in tf returned");
86 exit (1);
87}
88
89
90static int
91do_test (void)
92{
93 pthread_t th;
94
95 if (pthread_barrier_init (&bar, NULL, 2) != 0)
96 {
97 puts ("barrier_init failed");
98 return 1;
99 }
100
101 if (pthread_create (&th, NULL, tf, NULL) != 0)
102 {
103 puts ("first create failed");
104 return 1;
105 }
106
107 int r = pthread_barrier_wait (&bar);
108 if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
109 {
110 puts ("barrier_wait failed");
111 return 1;
112 }
113
114 if (pthread_mutex_lock (&mut) != 0)
115 {
116 puts ("mutex_lock failed");
117 return 1;
118 }
119 /* We unlock the mutex so that we catch the case where the pthread_cond_wait
120 call incorrectly resumes and tries to get the mutex. */
121 if (pthread_mutex_unlock (&mut) != 0)
122 {
123 puts ("mutex_unlock failed");
124 return 1;
125 }
126
127 /* Cancel the thread. */
128 puts ("going to cancel");
129 if (pthread_cancel (th) != 0)
130 {
131 puts ("cancel failed");
132 return 1;
133 }
134
135 void *result;
136 pthread_join (th, &result);
137 if (result != PTHREAD_CANCELED)
138 {
139 puts ("join didn't return PTHREAD_CANCELED");
140 return 1;
141 }
142
143 if (cl_called != 1)
144 {
145 puts ("cleanup handler not called");
146 return 1;
147 }
148
149 pthread_once (&once, once_handler2);
150
151 if (global != 1)
152 {
153 puts ("global still 0");
154 return 1;
155 }
156
157 return 0;
158}
159
160#define TEST_FUNCTION do_test ()
161#define TIMEOUT 4
162#include "../test-skeleton.c"
Note: See TracBrowser for help on using the repository browser.