OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkCondVar.h" | 8 #include "SkCondVar.h" |
9 | 9 |
| 10 #if defined(SK_BUILD_FOR_WIN32) |
| 11 static void (WINAPI *initialize_condition_variable)(PCONDITION_VARIABLE); |
| 12 static BOOL (WINAPI *sleep_condition_variable)(PCONDITION_VARIABLE, PCRITICA
L_SECTION, DWORD); |
| 13 static void (WINAPI *wake_condition_variable)(PCONDITION_VARIABLE); |
| 14 static void (WINAPI *wake_all_condition_variable)(PCONDITION_VARIABLE); |
| 15 |
| 16 template <typename T> |
| 17 static void set_fn_ptr(T* ptr, FARPROC fn) { *ptr = reinterpret_cast<T>(fn);
} |
| 18 #endif |
| 19 |
| 20 bool SkCondVar::Supported() { |
| 21 #ifdef SK_USE_POSIX_THREADS |
| 22 return true; |
| 23 #elif defined(SK_BUILD_FOR_WIN32) |
| 24 // If we're >= Vista we'll find these functions. Otherwise (XP) SkCondVar i
s not supported. |
| 25 HMODULE kernel32 = GetModuleHandleA("kernel32.dll"); |
| 26 set_fn_ptr(&initialize_condition_variable, |
| 27 GetProcAddress(kernel32, "InitializeConditionVariable")); |
| 28 set_fn_ptr(&sleep_condition_variable, |
| 29 GetProcAddress(kernel32, "SleepConditionVariableCS")); |
| 30 set_fn_ptr(&wake_condition_variable, |
| 31 GetProcAddress(kernel32, "WakeConditionVariable")); |
| 32 set_fn_ptr(&wake_all_condition_variable, |
| 33 GetProcAddress(kernel32, "WakeAllConditionVariable")); |
| 34 return initialize_condition_variable |
| 35 && sleep_condition_variable |
| 36 && wake_condition_variable |
| 37 && wake_all_condition_variable; |
| 38 #else |
| 39 return false; |
| 40 #endif |
| 41 } |
| 42 |
10 SkCondVar::SkCondVar() { | 43 SkCondVar::SkCondVar() { |
11 #ifdef SK_USE_POSIX_THREADS | 44 #ifdef SK_USE_POSIX_THREADS |
12 pthread_mutex_init(&fMutex, NULL /* default mutex attr */); | 45 pthread_mutex_init(&fMutex, NULL /* default mutex attr */); |
13 pthread_cond_init(&fCond, NULL /* default cond attr */); | 46 pthread_cond_init(&fCond, NULL /* default cond attr */); |
14 #elif defined(SK_BUILD_FOR_WIN32) | 47 #elif defined(SK_BUILD_FOR_WIN32) |
15 InitializeCriticalSection(&fCriticalSection); | 48 InitializeCriticalSection(&fCriticalSection); |
16 InitializeConditionVariable(&fCondition); | 49 SkASSERT(initialize_condition_variable); |
| 50 initialize_condition_variable(&fCondition); |
17 #endif | 51 #endif |
18 } | 52 } |
19 | 53 |
20 SkCondVar::~SkCondVar() { | 54 SkCondVar::~SkCondVar() { |
21 #ifdef SK_USE_POSIX_THREADS | 55 #ifdef SK_USE_POSIX_THREADS |
22 pthread_mutex_destroy(&fMutex); | 56 pthread_mutex_destroy(&fMutex); |
23 pthread_cond_destroy(&fCond); | 57 pthread_cond_destroy(&fCond); |
24 #elif defined(SK_BUILD_FOR_WIN32) | 58 #elif defined(SK_BUILD_FOR_WIN32) |
25 DeleteCriticalSection(&fCriticalSection); | 59 DeleteCriticalSection(&fCriticalSection); |
26 // No need to clean up fCondition. | 60 // No need to clean up fCondition. |
(...skipping 13 matching lines...) Expand all Loading... |
40 pthread_mutex_unlock(&fMutex); | 74 pthread_mutex_unlock(&fMutex); |
41 #elif defined(SK_BUILD_FOR_WIN32) | 75 #elif defined(SK_BUILD_FOR_WIN32) |
42 LeaveCriticalSection(&fCriticalSection); | 76 LeaveCriticalSection(&fCriticalSection); |
43 #endif | 77 #endif |
44 } | 78 } |
45 | 79 |
46 void SkCondVar::wait() { | 80 void SkCondVar::wait() { |
47 #ifdef SK_USE_POSIX_THREADS | 81 #ifdef SK_USE_POSIX_THREADS |
48 pthread_cond_wait(&fCond, &fMutex); | 82 pthread_cond_wait(&fCond, &fMutex); |
49 #elif defined(SK_BUILD_FOR_WIN32) | 83 #elif defined(SK_BUILD_FOR_WIN32) |
50 SleepConditionVariableCS(&fCondition, &fCriticalSection, INFINITE); | 84 SkASSERT(sleep_condition_variable); |
| 85 sleep_condition_variable(&fCondition, &fCriticalSection, INFINITE); |
51 #endif | 86 #endif |
52 } | 87 } |
53 | 88 |
54 void SkCondVar::signal() { | 89 void SkCondVar::signal() { |
55 #ifdef SK_USE_POSIX_THREADS | 90 #ifdef SK_USE_POSIX_THREADS |
56 pthread_cond_signal(&fCond); | 91 pthread_cond_signal(&fCond); |
57 #elif defined(SK_BUILD_FOR_WIN32) | 92 #elif defined(SK_BUILD_FOR_WIN32) |
58 WakeConditionVariable(&fCondition); | 93 SkASSERT(wake_condition_variable); |
| 94 wake_condition_variable(&fCondition); |
59 #endif | 95 #endif |
60 } | 96 } |
61 | 97 |
62 void SkCondVar::broadcast() { | 98 void SkCondVar::broadcast() { |
63 #ifdef SK_USE_POSIX_THREADS | 99 #ifdef SK_USE_POSIX_THREADS |
64 pthread_cond_broadcast(&fCond); | 100 pthread_cond_broadcast(&fCond); |
65 #elif defined(SK_BUILD_FOR_WIN32) | 101 #elif defined(SK_BUILD_FOR_WIN32) |
66 WakeAllConditionVariable(&fCondition); | 102 SkASSERT(wake_all_condition_variable); |
| 103 wake_all_condition_variable(&fCondition); |
67 #endif | 104 #endif |
68 } | 105 } |
OLD | NEW |