[5ab9f5]: misc.c Maximize Restore History

Download this file

misc.c    249 lines (205 with data), 5.6 kB

  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
/*
* misc.c
*
* Description:
* This translation unit implements miscellaneous thread functions.
*/
#include <errno.h>
#include "pthread.h"
#include "implement.h"
int
pthread_once(pthread_once_t *once_control,
void (*init_routine)(void))
{
/* A flag, allocated per invocation, that indicates if the atomic
test-and-set occured. */
unsigned short flag = 0;
if (once_control == NULL || init_routine == NULL)
{
return EINVAL;
}
/* An atomic test-and-set of the "once" flag. */
pthread_mutex_lock(&once_control->lock);
if (once_control->flag == 0)
{
flag = once_control->flag = 1;
}
pthread_mutex_unlock(&once_control->lock);
if (flag)
{
/* Run the init routine. */
init_routine();
}
return 0;
}
/*
* Code contributed by John E. Bossom <JEB>.
*/
pthread_t
pthread_self (void)
/*
* ------------------------------------------------------
* DOCPUBLIC
* This function returns a reference to the current running
* thread.
*
* PARAMETERS
* N/A
*
*
* DESCRIPTION
* This function returns a reference to the current running
* thread.
*
* RESULTS
* pthread_t reference to the current thread
*
* ------------------------------------------------------
*/
{
pthread_t self = NULL;
/*
* need to ensure there always is a self
*/
if ((self = pthread_getspecific (_pthread_selfThreadKey)) == NULL)
{
/*
* Need to create an implicit 'self' for the currently
* executing thread.
*/
self = (pthread_t) calloc (1, sizeof (*self));
if (self != NULL)
{
self->implicit = 1;
self->detachState = PTHREAD_CREATE_DETACHED;
self->thread = GetCurrentThreadId ();
self->threadH = GetCurrentThread ();
}
pthread_setspecific (_pthread_selfThreadKey, self);
}
return (self);
} /* pthread_self */
int
pthread_equal (pthread_t t1, pthread_t t2)
/*
* ------------------------------------------------------
* DOCPUBLIC
* This function returns zero if t1 and t2 are equal, else
* returns nonzero
*
* PARAMETERS
* t1,
* t2
* references to an instances of thread_t
*
*
* DESCRIPTION
* This function returns zero if t1 and t2 are equal, else
* returns nonzero.
*
* RESULTS
* 0 if t1 and t2 refer to the same thread,
* non-zero t1 and t2 do not refer to the same thread
*
* ------------------------------------------------------
*/
{
int result;
result = !((t1 == t2) || (t1->thread == t2->thread));
return (result);
} /* pthread_equal */
int
pthreadCancelableWait (HANDLE waitHandle)
/*
* -------------------------------------------------------------------
* This provides an extra hook into the pthread_cancel
* mechanism that will allow you to wait on a Windows handle and make it a
* cancellation point. This function blocks until the given WIN32 handle is
* signaled or pthread_cancel has been called. It is implemented using
* WaitForMultipleObjects on 'waitHandle' and a manually reset WIN32
* event used to implement pthread_cancel.
*
* Given this hook it would be possible to implement more of the cancellation
* points.
* -------------------------------------------------------------------
*/
{
int result;
pthread_t self;
HANDLE handles[2];
DWORD nHandles = 1;
DWORD status;
handles[0] = waitHandle;
if ((self = pthread_getspecific (_pthread_selfThreadKey)) != NULL)
{
/*
* Get cancelEvent handle
*/
if (self->cancelState == PTHREAD_CANCEL_ENABLE)
{
if ((handles[1] = self->cancelEvent) != NULL)
{
nHandles++;
}
}
}
else
{
handles[1] = NULL;
}
status = WaitForMultipleObjects (
nHandles,
handles,
FALSE,
INFINITE);
if (status == WAIT_FAILED)
{
result = EINVAL;
}
else if (status == WAIT_ABANDONED_0)
{
result = EINVAL;
}
else
{
/*
* Either got the mutex or the cancel event
* was signaled
*/
switch (status - WAIT_OBJECT_0)
{
case 0:
/*
* Got the mutex
*/
result = 0;
break;
case 1:
/*
* Got cancel request
*/
ResetEvent (handles[1]);
if (self != NULL && !self->implicit)
{
/*
* Thread started with pthread_create
*/
DWORD exceptionInformation[3];
exceptionInformation[0] = (DWORD) (0);
exceptionInformation[1] = (DWORD) (0);
RaiseException (
EXCEPTION_PTHREAD_SERVICES,
0,
3,
exceptionInformation);
}
((void *) -1);
break;
default:
result = EINVAL;
break;
}
}
return (result);
} /* pthreadCancelableWait */
/* </JEB> */