forked from GerHobbelt/pthread-win32
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pthread_cond_destroy.c
260 lines (242 loc) · 7.72 KB
/
pthread_cond_destroy.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
* pthread_cond_destroy.c
*
* Description:
* This translation unit implements condition variables and their primitives.
*
*
* --------------------------------------------------------------------------
*
* pthreads-win32 - POSIX Threads Library for Win32
* Copyright(C) 1998 John E. Bossom
* Copyright(C) 1999-2021 pthreads-win32 / pthreads4w contributors
*
* Homepage1: http://sourceware.org/pthreads-win32/
* Homepage2: http://sourceforge.net/projects/pthreads4w/
*
* The current list of contributors is contained
* in the file CONTRIBUTORS included with the source
* code distribution. The list can also be seen at the
* following World Wide Web location:
* http://sources.redhat.com/pthreads-win32/contributors.html
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library in the file COPYING.LIB;
* if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*
* --------------------------------------------------------------------------
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "pthread.h"
#include "implement.h"
int
pthread_cond_destroy (pthread_cond_t * cond)
/*
* ------------------------------------------------------
* DOCPUBLIC
* This function destroys a condition variable
*
*
* PARAMETERS
* cond
* pointer to an instance of pthread_cond_t
*
*
* DESCRIPTION
* This function destroys a condition variable.
*
* NOTES:
* 1) A condition variable can be destroyed
* immediately after all the threads that
* are blocked on it are awakened. e.g.
*
* struct list {
* pthread_mutex_t lm;
* ...
* }
*
* struct elt {
* key k;
* int busy;
* pthread_cond_t notbusy;
* ...
* }
*
*
* struct elt *
* list_find(struct list *lp, key k)
* {
* struct elt *ep;
*
* pthread_mutex_lock(&lp->lm);
* while ((ep = find_elt(l,k) != NULL) && ep->busy)
* pthread_cond_wait(&ep->notbusy, &lp->lm);
* if (ep != NULL)
* ep->busy = 1;
* pthread_mutex_unlock(&lp->lm);
* return(ep);
* }
*
* delete_elt(struct list *lp, struct elt *ep)
* {
* pthread_mutex_lock(&lp->lm);
* assert(ep->busy);
* ... remove ep from list ...
* ep->busy = 0;
* (A) pthread_cond_broadcast(&ep->notbusy);
* pthread_mutex_unlock(&lp->lm);
* (B) pthread_cond_destroy(&rp->notbusy);
* free(ep);
* }
*
* In this example, the condition variable
* and its list element may be freed (line B)
* immediately after all threads waiting for
* it are awakened (line A), since the mutex
* and the code ensure that no other thread
* can touch the element to be deleted.
*
* RESULTS
* 0 successfully released condition variable,
* EINVAL 'cond' is invalid,
* EBUSY 'cond' is in use,
*
* ------------------------------------------------------
*/
{
pthread_cond_t cv;
int result = 0, result1 = 0, result2 = 0;
/*
* Assuming any race condition here is harmless.
*/
if (cond == NULL || *cond == NULL)
{
return EINVAL;
}
if (*cond != PTHREAD_COND_INITIALIZER)
{
ptw32_mcs_local_node_t node;
ptw32_mcs_lock_acquire(&ptw32_cond_list_lock, &node);
cv = *cond;
/*
* Close the gate; this will synchronize this thread with
* all already signaled waiters to let them retract their
* waiter status - SEE NOTE 1 ABOVE!!!
*/
if (ptw32_semwait (&(cv->semBlockLock)) != 0) /* Non-cancelable */
{
result = PTW32_GET_ERRNO();
}
else
{
/*
* !TRY! lock mtxUnblockLock; try will detect busy condition
* and will not cause a deadlock with respect to concurrent
* signal/broadcast.
*/
if ((result = pthread_mutex_trylock (&(cv->mtxUnblockLock))) != 0)
{
(void) sem_post (&(cv->semBlockLock));
}
}
if (result != 0)
{
ptw32_mcs_lock_release(&node);
return result;
}
/*
* Check whether cv is still busy (still has waiters)
*/
if (cv->nWaitersBlocked > cv->nWaitersGone)
{
if (sem_post (&(cv->semBlockLock)) != 0)
{
result = PTW32_GET_ERRNO();
}
result1 = pthread_mutex_unlock (&(cv->mtxUnblockLock));
result2 = EBUSY;
}
else
{
/*
* Now it is safe to destroy
*/
*cond = NULL;
if (sem_destroy (&(cv->semBlockLock)) != 0)
{
result = PTW32_GET_ERRNO();
}
if (sem_destroy (&(cv->semBlockQueue)) != 0)
{
result1 = PTW32_GET_ERRNO();
}
if ((result2 = pthread_mutex_unlock (&(cv->mtxUnblockLock))) == 0)
{
result2 = pthread_mutex_destroy (&(cv->mtxUnblockLock));
}
/* Unlink the CV from the list */
if (ptw32_cond_list_head == cv)
{
ptw32_cond_list_head = cv->next;
}
else
{
cv->prev->next = cv->next;
}
if (ptw32_cond_list_tail == cv)
{
ptw32_cond_list_tail = cv->prev;
}
else
{
cv->next->prev = cv->prev;
}
(void) free (cv);
}
ptw32_mcs_lock_release(&node);
}
else
{
ptw32_mcs_local_node_t node;
/*
* See notes in ptw32_cond_check_need_init() above also.
*/
ptw32_mcs_lock_acquire(&ptw32_cond_test_init_lock, &node);
/*
* Check again.
*/
if (*cond == PTHREAD_COND_INITIALIZER)
{
/*
* This is all we need to do to destroy a statically
* initialised cond that has not yet been used (initialised).
* If we get to here, another thread waiting to initialise
* this cond will get an EINVAL. That's OK.
*/
*cond = NULL;
}
else
{
/*
* The cv has been initialised while we were waiting
* so assume it's in use.
*/
result = EBUSY;
}
ptw32_mcs_lock_release(&node);
}
return ((result != 0) ? result : ((result1 != 0) ? result1 : result2));
}