| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright 2012 Google Inc. | |
| 3 * | |
| 4 * Use of this source code is governed by a BSD-style license that can be | |
| 5 * found in the LICENSE file. | |
| 6 */ | |
| 7 | |
| 8 #include "SkCondVar.h" | |
| 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_BUILD_FOR_WIN32 | |
| 22 // If we're >= Vista we'll find these functions. Otherwise (XP) SkCondVar i
s not supported. | |
| 23 HMODULE kernel32 = GetModuleHandleA("kernel32.dll"); | |
| 24 set_fn_ptr(&initialize_condition_variable, | |
| 25 GetProcAddress(kernel32, "InitializeConditionVariable")); | |
| 26 set_fn_ptr(&sleep_condition_variable, | |
| 27 GetProcAddress(kernel32, "SleepConditionVariableCS")); | |
| 28 set_fn_ptr(&wake_condition_variable, | |
| 29 GetProcAddress(kernel32, "WakeConditionVariable")); | |
| 30 set_fn_ptr(&wake_all_condition_variable, | |
| 31 GetProcAddress(kernel32, "WakeAllConditionVariable")); | |
| 32 return initialize_condition_variable | |
| 33 && sleep_condition_variable | |
| 34 && wake_condition_variable | |
| 35 && wake_all_condition_variable; | |
| 36 #else | |
| 37 return true; | |
| 38 #endif | |
| 39 } | |
| 40 | |
| 41 SkCondVar::SkCondVar() { | |
| 42 #ifdef SK_BUILD_FOR_WIN32 | |
| 43 InitializeCriticalSection(&fCriticalSection); | |
| 44 SkASSERT(initialize_condition_variable); | |
| 45 initialize_condition_variable(&fCondition); | |
| 46 #else | |
| 47 pthread_mutex_init(&fMutex, NULL /* default mutex attr */); | |
| 48 pthread_cond_init(&fCond, NULL /* default cond attr */); | |
| 49 #endif | |
| 50 } | |
| 51 | |
| 52 SkCondVar::~SkCondVar() { | |
| 53 #ifdef SK_BUILD_FOR_WIN32 | |
| 54 DeleteCriticalSection(&fCriticalSection); | |
| 55 // No need to clean up fCondition. | |
| 56 #else | |
| 57 pthread_mutex_destroy(&fMutex); | |
| 58 pthread_cond_destroy(&fCond); | |
| 59 #endif | |
| 60 } | |
| 61 | |
| 62 void SkCondVar::lock() { | |
| 63 #ifdef SK_BUILD_FOR_WIN32 | |
| 64 EnterCriticalSection(&fCriticalSection); | |
| 65 #else | |
| 66 pthread_mutex_lock(&fMutex); | |
| 67 #endif | |
| 68 } | |
| 69 | |
| 70 void SkCondVar::unlock() { | |
| 71 #ifdef SK_BUILD_FOR_WIN32 | |
| 72 LeaveCriticalSection(&fCriticalSection); | |
| 73 #else | |
| 74 pthread_mutex_unlock(&fMutex); | |
| 75 #endif | |
| 76 } | |
| 77 | |
| 78 void SkCondVar::wait() { | |
| 79 #ifdef SK_BUILD_FOR_WIN32 | |
| 80 SkASSERT(sleep_condition_variable); | |
| 81 sleep_condition_variable(&fCondition, &fCriticalSection, INFINITE); | |
| 82 #else | |
| 83 pthread_cond_wait(&fCond, &fMutex); | |
| 84 #endif | |
| 85 } | |
| 86 | |
| 87 void SkCondVar::signal() { | |
| 88 #ifdef SK_BUILD_FOR_WIN32 | |
| 89 SkASSERT(wake_condition_variable); | |
| 90 wake_condition_variable(&fCondition); | |
| 91 #else | |
| 92 pthread_cond_signal(&fCond); | |
| 93 #endif | |
| 94 } | |
| 95 | |
| 96 void SkCondVar::broadcast() { | |
| 97 #ifdef SK_BUILD_FOR_WIN32 | |
| 98 SkASSERT(wake_all_condition_variable); | |
| 99 wake_all_condition_variable(&fCondition); | |
| 100 #else | |
| 101 pthread_cond_broadcast(&fCond); | |
| 102 #endif | |
| 103 } | |
| OLD | NEW |