Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright 2009-2012 Niels Provos and Nick Mathewson |
michael@0 | 3 | * |
michael@0 | 4 | * Redistribution and use in source and binary forms, with or without |
michael@0 | 5 | * modification, are permitted provided that the following conditions |
michael@0 | 6 | * are met: |
michael@0 | 7 | * 1. Redistributions of source code must retain the above copyright |
michael@0 | 8 | * notice, this list of conditions and the following disclaimer. |
michael@0 | 9 | * 2. Redistributions in binary form must reproduce the above copyright |
michael@0 | 10 | * notice, this list of conditions and the following disclaimer in the |
michael@0 | 11 | * documentation and/or other materials provided with the distribution. |
michael@0 | 12 | * 3. The name of the author may not be used to endorse or promote products |
michael@0 | 13 | * derived from this software without specific prior written permission. |
michael@0 | 14 | * |
michael@0 | 15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
michael@0 | 16 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
michael@0 | 17 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
michael@0 | 18 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
michael@0 | 19 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
michael@0 | 20 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 21 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 22 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
michael@0 | 24 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 25 | */ |
michael@0 | 26 | #include "event2/event-config.h" |
michael@0 | 27 | |
michael@0 | 28 | #ifdef WIN32 |
michael@0 | 29 | #ifndef _WIN32_WINNT |
michael@0 | 30 | /* Minimum required for InitializeCriticalSectionAndSpinCount */ |
michael@0 | 31 | #define _WIN32_WINNT 0x0403 |
michael@0 | 32 | #endif |
michael@0 | 33 | #include <winsock2.h> |
michael@0 | 34 | #define WIN32_LEAN_AND_MEAN |
michael@0 | 35 | #include <windows.h> |
michael@0 | 36 | #undef WIN32_LEAN_AND_MEAN |
michael@0 | 37 | #include <sys/locking.h> |
michael@0 | 38 | #endif |
michael@0 | 39 | |
michael@0 | 40 | struct event_base; |
michael@0 | 41 | #include "event2/thread.h" |
michael@0 | 42 | |
michael@0 | 43 | #include "mm-internal.h" |
michael@0 | 44 | #include "evthread-internal.h" |
michael@0 | 45 | |
michael@0 | 46 | #define SPIN_COUNT 2000 |
michael@0 | 47 | |
michael@0 | 48 | static void * |
michael@0 | 49 | evthread_win32_lock_create(unsigned locktype) |
michael@0 | 50 | { |
michael@0 | 51 | CRITICAL_SECTION *lock = mm_malloc(sizeof(CRITICAL_SECTION)); |
michael@0 | 52 | if (!lock) |
michael@0 | 53 | return NULL; |
michael@0 | 54 | if (InitializeCriticalSectionAndSpinCount(lock, SPIN_COUNT) == 0) { |
michael@0 | 55 | mm_free(lock); |
michael@0 | 56 | return NULL; |
michael@0 | 57 | } |
michael@0 | 58 | return lock; |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | static void |
michael@0 | 62 | evthread_win32_lock_free(void *_lock, unsigned locktype) |
michael@0 | 63 | { |
michael@0 | 64 | CRITICAL_SECTION *lock = _lock; |
michael@0 | 65 | DeleteCriticalSection(lock); |
michael@0 | 66 | mm_free(lock); |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | static int |
michael@0 | 70 | evthread_win32_lock(unsigned mode, void *_lock) |
michael@0 | 71 | { |
michael@0 | 72 | CRITICAL_SECTION *lock = _lock; |
michael@0 | 73 | if ((mode & EVTHREAD_TRY)) { |
michael@0 | 74 | return ! TryEnterCriticalSection(lock); |
michael@0 | 75 | } else { |
michael@0 | 76 | EnterCriticalSection(lock); |
michael@0 | 77 | return 0; |
michael@0 | 78 | } |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | static int |
michael@0 | 82 | evthread_win32_unlock(unsigned mode, void *_lock) |
michael@0 | 83 | { |
michael@0 | 84 | CRITICAL_SECTION *lock = _lock; |
michael@0 | 85 | LeaveCriticalSection(lock); |
michael@0 | 86 | return 0; |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | static unsigned long |
michael@0 | 90 | evthread_win32_get_id(void) |
michael@0 | 91 | { |
michael@0 | 92 | return (unsigned long) GetCurrentThreadId(); |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | #ifdef WIN32_HAVE_CONDITION_VARIABLES |
michael@0 | 96 | static void WINAPI (*InitializeConditionVariable_fn)(PCONDITION_VARIABLE) |
michael@0 | 97 | = NULL; |
michael@0 | 98 | static BOOL WINAPI (*SleepConditionVariableCS_fn)( |
michael@0 | 99 | PCONDITION_VARIABLE, PCRITICAL_SECTION, DWORD) = NULL; |
michael@0 | 100 | static void WINAPI (*WakeAllConditionVariable_fn)(PCONDITION_VARIABLE) = NULL; |
michael@0 | 101 | static void WINAPI (*WakeConditionVariable_fn)(PCONDITION_VARIABLE) = NULL; |
michael@0 | 102 | |
michael@0 | 103 | static int |
michael@0 | 104 | evthread_win32_condvar_init(void) |
michael@0 | 105 | { |
michael@0 | 106 | HANDLE lib; |
michael@0 | 107 | |
michael@0 | 108 | lib = GetModuleHandle(TEXT("kernel32.dll")); |
michael@0 | 109 | if (lib == NULL) |
michael@0 | 110 | return 0; |
michael@0 | 111 | |
michael@0 | 112 | #define LOAD(name) \ |
michael@0 | 113 | name##_fn = GetProcAddress(lib, #name) |
michael@0 | 114 | LOAD(InitializeConditionVariable); |
michael@0 | 115 | LOAD(SleepConditionVariable); |
michael@0 | 116 | LOAD(WakeAllConditionVariable); |
michael@0 | 117 | LOAD(WakeConditionVariable); |
michael@0 | 118 | |
michael@0 | 119 | return InitializeConditionVariable_fn && SleepConditionVariableCS_fn && |
michael@0 | 120 | WakeAllConditionVariable_fn && WakeConditionVariable_fn; |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | /* XXXX Even if we can build this, we don't necessarily want to: the functions |
michael@0 | 124 | * in question didn't exist before Vista, so we'd better LoadProc them. */ |
michael@0 | 125 | static void * |
michael@0 | 126 | evthread_win32_condvar_alloc(unsigned condflags) |
michael@0 | 127 | { |
michael@0 | 128 | CONDITION_VARIABLE *cond = mm_malloc(sizeof(CONDITION_VARIABLE)); |
michael@0 | 129 | if (!cond) |
michael@0 | 130 | return NULL; |
michael@0 | 131 | InitializeConditionVariable_fn(cond); |
michael@0 | 132 | return cond; |
michael@0 | 133 | } |
michael@0 | 134 | |
michael@0 | 135 | static void |
michael@0 | 136 | evthread_win32_condvar_free(void *_cond) |
michael@0 | 137 | { |
michael@0 | 138 | CONDITION_VARIABLE *cond = _cond; |
michael@0 | 139 | /* There doesn't _seem_ to be a cleaup fn here... */ |
michael@0 | 140 | mm_free(cond); |
michael@0 | 141 | } |
michael@0 | 142 | |
michael@0 | 143 | static int |
michael@0 | 144 | evthread_win32_condvar_signal(void *_cond, int broadcast) |
michael@0 | 145 | { |
michael@0 | 146 | CONDITION_VARIABLE *cond = _cond; |
michael@0 | 147 | if (broadcast) |
michael@0 | 148 | WakeAllConditionVariable_fn(cond); |
michael@0 | 149 | else |
michael@0 | 150 | WakeConditionVariable_fn(cond); |
michael@0 | 151 | return 0; |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | static int |
michael@0 | 155 | evthread_win32_condvar_wait(void *_cond, void *_lock, const struct timeval *tv) |
michael@0 | 156 | { |
michael@0 | 157 | CONDITION_VARIABLE *cond = _cond; |
michael@0 | 158 | CRITICAL_SECTION *lock = _lock; |
michael@0 | 159 | DWORD ms, err; |
michael@0 | 160 | BOOL result; |
michael@0 | 161 | |
michael@0 | 162 | if (tv) |
michael@0 | 163 | ms = evutil_tv_to_msec(tv); |
michael@0 | 164 | else |
michael@0 | 165 | ms = INFINITE; |
michael@0 | 166 | result = SleepConditionVariableCS_fn(cond, lock, ms); |
michael@0 | 167 | if (result) { |
michael@0 | 168 | if (GetLastError() == WAIT_TIMEOUT) |
michael@0 | 169 | return 1; |
michael@0 | 170 | else |
michael@0 | 171 | return -1; |
michael@0 | 172 | } else { |
michael@0 | 173 | return 0; |
michael@0 | 174 | } |
michael@0 | 175 | } |
michael@0 | 176 | #endif |
michael@0 | 177 | |
michael@0 | 178 | struct evthread_win32_cond { |
michael@0 | 179 | HANDLE event; |
michael@0 | 180 | |
michael@0 | 181 | CRITICAL_SECTION lock; |
michael@0 | 182 | int n_waiting; |
michael@0 | 183 | int n_to_wake; |
michael@0 | 184 | int generation; |
michael@0 | 185 | }; |
michael@0 | 186 | |
michael@0 | 187 | static void * |
michael@0 | 188 | evthread_win32_cond_alloc(unsigned flags) |
michael@0 | 189 | { |
michael@0 | 190 | struct evthread_win32_cond *cond; |
michael@0 | 191 | if (!(cond = mm_malloc(sizeof(struct evthread_win32_cond)))) |
michael@0 | 192 | return NULL; |
michael@0 | 193 | if (InitializeCriticalSectionAndSpinCount(&cond->lock, SPIN_COUNT)==0) { |
michael@0 | 194 | mm_free(cond); |
michael@0 | 195 | return NULL; |
michael@0 | 196 | } |
michael@0 | 197 | if ((cond->event = CreateEvent(NULL,TRUE,FALSE,NULL)) == NULL) { |
michael@0 | 198 | DeleteCriticalSection(&cond->lock); |
michael@0 | 199 | mm_free(cond); |
michael@0 | 200 | return NULL; |
michael@0 | 201 | } |
michael@0 | 202 | cond->n_waiting = cond->n_to_wake = cond->generation = 0; |
michael@0 | 203 | return cond; |
michael@0 | 204 | } |
michael@0 | 205 | |
michael@0 | 206 | static void |
michael@0 | 207 | evthread_win32_cond_free(void *_cond) |
michael@0 | 208 | { |
michael@0 | 209 | struct evthread_win32_cond *cond = _cond; |
michael@0 | 210 | DeleteCriticalSection(&cond->lock); |
michael@0 | 211 | CloseHandle(cond->event); |
michael@0 | 212 | mm_free(cond); |
michael@0 | 213 | } |
michael@0 | 214 | |
michael@0 | 215 | static int |
michael@0 | 216 | evthread_win32_cond_signal(void *_cond, int broadcast) |
michael@0 | 217 | { |
michael@0 | 218 | struct evthread_win32_cond *cond = _cond; |
michael@0 | 219 | EnterCriticalSection(&cond->lock); |
michael@0 | 220 | if (broadcast) |
michael@0 | 221 | cond->n_to_wake = cond->n_waiting; |
michael@0 | 222 | else |
michael@0 | 223 | ++cond->n_to_wake; |
michael@0 | 224 | cond->generation++; |
michael@0 | 225 | SetEvent(cond->event); |
michael@0 | 226 | LeaveCriticalSection(&cond->lock); |
michael@0 | 227 | return 0; |
michael@0 | 228 | } |
michael@0 | 229 | |
michael@0 | 230 | static int |
michael@0 | 231 | evthread_win32_cond_wait(void *_cond, void *_lock, const struct timeval *tv) |
michael@0 | 232 | { |
michael@0 | 233 | struct evthread_win32_cond *cond = _cond; |
michael@0 | 234 | CRITICAL_SECTION *lock = _lock; |
michael@0 | 235 | int generation_at_start; |
michael@0 | 236 | int waiting = 1; |
michael@0 | 237 | int result = -1; |
michael@0 | 238 | DWORD ms = INFINITE, ms_orig = INFINITE, startTime, endTime; |
michael@0 | 239 | if (tv) |
michael@0 | 240 | ms_orig = ms = evutil_tv_to_msec(tv); |
michael@0 | 241 | |
michael@0 | 242 | EnterCriticalSection(&cond->lock); |
michael@0 | 243 | ++cond->n_waiting; |
michael@0 | 244 | generation_at_start = cond->generation; |
michael@0 | 245 | LeaveCriticalSection(&cond->lock); |
michael@0 | 246 | |
michael@0 | 247 | LeaveCriticalSection(lock); |
michael@0 | 248 | |
michael@0 | 249 | startTime = GetTickCount(); |
michael@0 | 250 | do { |
michael@0 | 251 | DWORD res; |
michael@0 | 252 | res = WaitForSingleObject(cond->event, ms); |
michael@0 | 253 | EnterCriticalSection(&cond->lock); |
michael@0 | 254 | if (cond->n_to_wake && |
michael@0 | 255 | cond->generation != generation_at_start) { |
michael@0 | 256 | --cond->n_to_wake; |
michael@0 | 257 | --cond->n_waiting; |
michael@0 | 258 | result = 0; |
michael@0 | 259 | waiting = 0; |
michael@0 | 260 | goto out; |
michael@0 | 261 | } else if (res != WAIT_OBJECT_0) { |
michael@0 | 262 | result = (res==WAIT_TIMEOUT) ? 1 : -1; |
michael@0 | 263 | --cond->n_waiting; |
michael@0 | 264 | waiting = 0; |
michael@0 | 265 | goto out; |
michael@0 | 266 | } else if (ms != INFINITE) { |
michael@0 | 267 | endTime = GetTickCount(); |
michael@0 | 268 | if (startTime + ms_orig <= endTime) { |
michael@0 | 269 | result = 1; /* Timeout */ |
michael@0 | 270 | --cond->n_waiting; |
michael@0 | 271 | waiting = 0; |
michael@0 | 272 | goto out; |
michael@0 | 273 | } else { |
michael@0 | 274 | ms = startTime + ms_orig - endTime; |
michael@0 | 275 | } |
michael@0 | 276 | } |
michael@0 | 277 | /* If we make it here, we are still waiting. */ |
michael@0 | 278 | if (cond->n_to_wake == 0) { |
michael@0 | 279 | /* There is nobody else who should wake up; reset |
michael@0 | 280 | * the event. */ |
michael@0 | 281 | ResetEvent(cond->event); |
michael@0 | 282 | } |
michael@0 | 283 | out: |
michael@0 | 284 | LeaveCriticalSection(&cond->lock); |
michael@0 | 285 | } while (waiting); |
michael@0 | 286 | |
michael@0 | 287 | EnterCriticalSection(lock); |
michael@0 | 288 | |
michael@0 | 289 | EnterCriticalSection(&cond->lock); |
michael@0 | 290 | if (!cond->n_waiting) |
michael@0 | 291 | ResetEvent(cond->event); |
michael@0 | 292 | LeaveCriticalSection(&cond->lock); |
michael@0 | 293 | |
michael@0 | 294 | return result; |
michael@0 | 295 | } |
michael@0 | 296 | |
michael@0 | 297 | int |
michael@0 | 298 | evthread_use_windows_threads(void) |
michael@0 | 299 | { |
michael@0 | 300 | struct evthread_lock_callbacks cbs = { |
michael@0 | 301 | EVTHREAD_LOCK_API_VERSION, |
michael@0 | 302 | EVTHREAD_LOCKTYPE_RECURSIVE, |
michael@0 | 303 | evthread_win32_lock_create, |
michael@0 | 304 | evthread_win32_lock_free, |
michael@0 | 305 | evthread_win32_lock, |
michael@0 | 306 | evthread_win32_unlock |
michael@0 | 307 | }; |
michael@0 | 308 | |
michael@0 | 309 | |
michael@0 | 310 | struct evthread_condition_callbacks cond_cbs = { |
michael@0 | 311 | EVTHREAD_CONDITION_API_VERSION, |
michael@0 | 312 | evthread_win32_cond_alloc, |
michael@0 | 313 | evthread_win32_cond_free, |
michael@0 | 314 | evthread_win32_cond_signal, |
michael@0 | 315 | evthread_win32_cond_wait |
michael@0 | 316 | }; |
michael@0 | 317 | #ifdef WIN32_HAVE_CONDITION_VARIABLES |
michael@0 | 318 | struct evthread_condition_callbacks condvar_cbs = { |
michael@0 | 319 | EVTHREAD_CONDITION_API_VERSION, |
michael@0 | 320 | evthread_win32_condvar_alloc, |
michael@0 | 321 | evthread_win32_condvar_free, |
michael@0 | 322 | evthread_win32_condvar_signal, |
michael@0 | 323 | evthread_win32_condvar_wait |
michael@0 | 324 | }; |
michael@0 | 325 | #endif |
michael@0 | 326 | |
michael@0 | 327 | evthread_set_lock_callbacks(&cbs); |
michael@0 | 328 | evthread_set_id_callback(evthread_win32_get_id); |
michael@0 | 329 | #ifdef WIN32_HAVE_CONDITION_VARIABLES |
michael@0 | 330 | if (evthread_win32_condvar_init()) { |
michael@0 | 331 | evthread_set_condition_callbacks(&condvar_cbs); |
michael@0 | 332 | return 0; |
michael@0 | 333 | } |
michael@0 | 334 | #endif |
michael@0 | 335 | evthread_set_condition_callbacks(&cond_cbs); |
michael@0 | 336 | |
michael@0 | 337 | return 0; |
michael@0 | 338 | } |
michael@0 | 339 |