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