| 1 |
/* |
| 2 |
* Code for simulating pthreads API on Windows. This is Git-specific, |
| 3 |
* but it is enough for Numexpr needs too. |
| 4 |
* |
| 5 |
* Copyright (C) 2009 Andrzej K. Haczewski <[email protected]> |
| 6 |
* |
| 7 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 |
* of this software and associated documentation files (the "Software"), to deal |
| 9 |
* in the Software without restriction, including without limitation the rights |
| 10 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 |
* copies of the Software, and to permit persons to whom the Software is |
| 12 |
* furnished to do so, subject to the following conditions: |
| 13 |
* |
| 14 |
* The above copyright notice and this permission notice shall be included in |
| 15 |
* all copies or substantial portions of the Software. |
| 16 |
* |
| 17 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 |
* THE SOFTWARE. |
| 24 |
* |
| 25 |
* DISCLAIMER: The implementation is Git-specific, it is subset of original |
| 26 |
* Pthreads API, without lots of other features that Git doesn't use. |
| 27 |
* Git also makes sure that the passed arguments are valid, so there's |
| 28 |
* no need for double-checking. |
| 29 |
*/ |
| 30 |
|
| 31 |
#ifndef PTHREAD_C |
| 32 |
#define PTHREAD_C |
| 33 |
|
| 34 |
#include "pthread.h" |
| 35 |
|
| 36 |
#include "stdio.h" |
| 37 |
#include "stdlib.h" |
| 38 |
#include "process.h" |
| 39 |
#include "errno.h" |
| 40 |
#include "limits.h" |
| 41 |
|
| 42 |
|
| 43 |
#define PTHREAD_UNUSED_PARAM(x) ((void)(x)) |
| 44 |
|
| 45 |
void die(const char *err, ...) |
| 46 |
{ |
| 47 |
printf("%s", err); |
| 48 |
exit(-1); |
| 49 |
} |
| 50 |
|
| 51 |
static unsigned __stdcall win32_start_routine(void *arg) |
| 52 |
{ |
| 53 |
pthread_t *thread = (pthread_t*)arg; |
| 54 |
thread->arg = thread->start_routine(thread->arg); |
| 55 |
return 0; |
| 56 |
} |
| 57 |
|
| 58 |
int pthread_create(pthread_t *thread, const void *unused, |
| 59 |
void *(*start_routine)(void*), void *arg) |
| 60 |
{ |
| 61 |
PTHREAD_UNUSED_PARAM(unused); |
| 62 |
thread->arg = arg; |
| 63 |
thread->start_routine = start_routine; |
| 64 |
thread->handle = (HANDLE) |
| 65 |
_beginthreadex(NULL, 0, win32_start_routine, thread, 0, NULL); |
| 66 |
|
| 67 |
if (!thread->handle) |
| 68 |
return errno; |
| 69 |
else |
| 70 |
return 0; |
| 71 |
} |
| 72 |
|
| 73 |
int win32_pthread_join(pthread_t *thread, void **value_ptr) |
| 74 |
{ |
| 75 |
DWORD result = WaitForSingleObject(thread->handle, INFINITE); |
| 76 |
switch (result) { |
| 77 |
case WAIT_OBJECT_0: |
| 78 |
if (value_ptr) |
| 79 |
*value_ptr = thread->arg; |
| 80 |
CloseHandle(thread->handle); |
| 81 |
return 0; |
| 82 |
case WAIT_ABANDONED: |
| 83 |
CloseHandle(thread->handle); |
| 84 |
return EINVAL; |
| 85 |
default: |
| 86 |
return GetLastError(); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
int pthread_cond_init(pthread_cond_t *cond, const void *unused) |
| 91 |
{ |
| 92 |
PTHREAD_UNUSED_PARAM(unused); |
| 93 |
cond->waiters = 0; |
| 94 |
cond->was_broadcast = 0; |
| 95 |
InitializeCriticalSection(&cond->waiters_lock); |
| 96 |
|
| 97 |
cond->sema = CreateSemaphore(NULL, 0, LONG_MAX, NULL); |
| 98 |
if (!cond->sema) |
| 99 |
die("CreateSemaphore() failed"); |
| 100 |
|
| 101 |
cond->continue_broadcast = CreateEvent(NULL, /* security */ |
| 102 |
FALSE, /* auto-reset */ |
| 103 |
FALSE, /* not signaled */ |
| 104 |
NULL); /* name */ |
| 105 |
if (!cond->continue_broadcast) |
| 106 |
die("CreateEvent() failed"); |
| 107 |
|
| 108 |
return 0; |
| 109 |
} |
| 110 |
|
| 111 |
int pthread_cond_destroy(pthread_cond_t *cond) |
| 112 |
{ |
| 113 |
CloseHandle(cond->sema); |
| 114 |
CloseHandle(cond->continue_broadcast); |
| 115 |
DeleteCriticalSection(&cond->waiters_lock); |
| 116 |
return 0; |
| 117 |
} |
| 118 |
|
| 119 |
int pthread_cond_wait(pthread_cond_t *cond, CRITICAL_SECTION *mutex) |
| 120 |
{ |
| 121 |
int last_waiter; |
| 122 |
|
| 123 |
EnterCriticalSection(&cond->waiters_lock); |
| 124 |
cond->waiters++; |
| 125 |
LeaveCriticalSection(&cond->waiters_lock); |
| 126 |
|
| 127 |
/* |
| 128 |
* Unlock external mutex and wait for signal. |
| 129 |
* NOTE: we've held mutex locked long enough to increment |
| 130 |
* waiters count above, so there's no problem with |
| 131 |
* leaving mutex unlocked before we wait on semaphore. |
| 132 |
*/ |
| 133 |
LeaveCriticalSection(mutex); |
| 134 |
|
| 135 |
/* let's wait - ignore return value */ |
| 136 |
WaitForSingleObject(cond->sema, INFINITE); |
| 137 |
|
| 138 |
/* |
| 139 |
* Decrease waiters count. If we are the last waiter, then we must |
| 140 |
* notify the broadcasting thread that it can continue. |
| 141 |
* But if we continued due to cond_signal, we do not have to do that |
| 142 |
* because the signaling thread knows that only one waiter continued. |
| 143 |
*/ |
| 144 |
EnterCriticalSection(&cond->waiters_lock); |
| 145 |
cond->waiters--; |
| 146 |
last_waiter = cond->was_broadcast && cond->waiters == 0; |
| 147 |
LeaveCriticalSection(&cond->waiters_lock); |
| 148 |
|
| 149 |
if (last_waiter) { |
| 150 |
/* |
| 151 |
* cond_broadcast was issued while mutex was held. This means |
| 152 |
* that all other waiters have continued, but are contending |
| 153 |
* for the mutex at the end of this function because the |
| 154 |
* broadcasting thread did not leave cond_broadcast, yet. |
| 155 |
* (This is so that it can be sure that each waiter has |
| 156 |
* consumed exactly one slice of the semaphore.) |
| 157 |
* The last waiter must tell the broadcasting thread that it |
| 158 |
* can go on. |
| 159 |
*/ |
| 160 |
SetEvent(cond->continue_broadcast); |
| 161 |
/* |
| 162 |
* Now we go on to contend with all other waiters for |
| 163 |
* the mutex. Auf in den Kampf! |
| 164 |
*/ |
| 165 |
} |
| 166 |
/* lock external mutex again */ |
| 167 |
EnterCriticalSection(mutex); |
| 168 |
|
| 169 |
return 0; |
| 170 |
} |
| 171 |
|
| 172 |
/* |
| 173 |
* IMPORTANT: This implementation requires that pthread_cond_signal |
| 174 |
* is called while the mutex is held that is used in the corresponding |
| 175 |
* pthread_cond_wait calls! |
| 176 |
*/ |
| 177 |
int pthread_cond_signal(pthread_cond_t *cond) |
| 178 |
{ |
| 179 |
int have_waiters; |
| 180 |
|
| 181 |
EnterCriticalSection(&cond->waiters_lock); |
| 182 |
have_waiters = cond->waiters > 0; |
| 183 |
LeaveCriticalSection(&cond->waiters_lock); |
| 184 |
|
| 185 |
/* |
| 186 |
* Signal only when there are waiters |
| 187 |
*/ |
| 188 |
if (have_waiters) |
| 189 |
return ReleaseSemaphore(cond->sema, 1, NULL) ? |
| 190 |
0 : GetLastError(); |
| 191 |
else |
| 192 |
return 0; |
| 193 |
} |
| 194 |
|
| 195 |
/* |
| 196 |
* DOUBLY IMPORTANT: This implementation requires that pthread_cond_broadcast |
| 197 |
* is called while the mutex is held that is used in the corresponding |
| 198 |
* pthread_cond_wait calls! |
| 199 |
*/ |
| 200 |
int pthread_cond_broadcast(pthread_cond_t *cond) |
| 201 |
{ |
| 202 |
EnterCriticalSection(&cond->waiters_lock); |
| 203 |
|
| 204 |
if ((cond->was_broadcast = cond->waiters > 0)) { |
| 205 |
/* wake up all waiters */ |
| 206 |
ReleaseSemaphore(cond->sema, cond->waiters, NULL); |
| 207 |
LeaveCriticalSection(&cond->waiters_lock); |
| 208 |
/* |
| 209 |
* At this point all waiters continue. Each one takes its |
| 210 |
* slice of the semaphore. Now it's our turn to wait: Since |
| 211 |
* the external mutex is held, no thread can leave cond_wait, |
| 212 |
* yet. For this reason, we can be sure that no thread gets |
| 213 |
* a chance to eat *more* than one slice. OTOH, it means |
| 214 |
* that the last waiter must send us a wake-up. |
| 215 |
*/ |
| 216 |
WaitForSingleObject(cond->continue_broadcast, INFINITE); |
| 217 |
/* |
| 218 |
* Since the external mutex is held, no thread can enter |
| 219 |
* cond_wait, and, hence, it is safe to reset this flag |
| 220 |
* without cond->waiters_lock held. |
| 221 |
*/ |
| 222 |
cond->was_broadcast = 0; |
| 223 |
} else { |
| 224 |
LeaveCriticalSection(&cond->waiters_lock); |
| 225 |
} |
| 226 |
return 0; |
| 227 |
} |
| 228 |
|
| 229 |
#endif /* PTHREAD_C */ |