scroggo@google.com | 4177ef4 | 2012-10-31 15:52:16 +0000 | [diff] [blame] | 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 | |
mtklein | db8d0e5 | 2014-11-03 17:25:54 -0800 | [diff] [blame^] | 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, PCRITICAL_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 is 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 | |
scroggo@google.com | 4177ef4 | 2012-10-31 15:52:16 +0000 | [diff] [blame] | 43 | SkCondVar::SkCondVar() { |
scroggo@google.com | 4d3c281 | 2012-10-31 19:29:13 +0000 | [diff] [blame] | 44 | #ifdef SK_USE_POSIX_THREADS |
scroggo@google.com | 4177ef4 | 2012-10-31 15:52:16 +0000 | [diff] [blame] | 45 | pthread_mutex_init(&fMutex, NULL /* default mutex attr */); |
| 46 | pthread_cond_init(&fCond, NULL /* default cond attr */); |
scroggo@google.com | 4d3c281 | 2012-10-31 19:29:13 +0000 | [diff] [blame] | 47 | #elif defined(SK_BUILD_FOR_WIN32) |
| 48 | InitializeCriticalSection(&fCriticalSection); |
mtklein | db8d0e5 | 2014-11-03 17:25:54 -0800 | [diff] [blame^] | 49 | SkASSERT(initialize_condition_variable); |
| 50 | initialize_condition_variable(&fCondition); |
scroggo@google.com | 4d3c281 | 2012-10-31 19:29:13 +0000 | [diff] [blame] | 51 | #endif |
scroggo@google.com | 4177ef4 | 2012-10-31 15:52:16 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | SkCondVar::~SkCondVar() { |
scroggo@google.com | 4d3c281 | 2012-10-31 19:29:13 +0000 | [diff] [blame] | 55 | #ifdef SK_USE_POSIX_THREADS |
scroggo@google.com | 4177ef4 | 2012-10-31 15:52:16 +0000 | [diff] [blame] | 56 | pthread_mutex_destroy(&fMutex); |
| 57 | pthread_cond_destroy(&fCond); |
scroggo@google.com | 4d3c281 | 2012-10-31 19:29:13 +0000 | [diff] [blame] | 58 | #elif defined(SK_BUILD_FOR_WIN32) |
| 59 | DeleteCriticalSection(&fCriticalSection); |
| 60 | // No need to clean up fCondition. |
| 61 | #endif |
scroggo@google.com | 4177ef4 | 2012-10-31 15:52:16 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void SkCondVar::lock() { |
scroggo@google.com | 4d3c281 | 2012-10-31 19:29:13 +0000 | [diff] [blame] | 65 | #ifdef SK_USE_POSIX_THREADS |
scroggo@google.com | 4177ef4 | 2012-10-31 15:52:16 +0000 | [diff] [blame] | 66 | pthread_mutex_lock(&fMutex); |
scroggo@google.com | 4d3c281 | 2012-10-31 19:29:13 +0000 | [diff] [blame] | 67 | #elif defined(SK_BUILD_FOR_WIN32) |
| 68 | EnterCriticalSection(&fCriticalSection); |
| 69 | #endif |
scroggo@google.com | 4177ef4 | 2012-10-31 15:52:16 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void SkCondVar::unlock() { |
scroggo@google.com | 4d3c281 | 2012-10-31 19:29:13 +0000 | [diff] [blame] | 73 | #ifdef SK_USE_POSIX_THREADS |
scroggo@google.com | 4177ef4 | 2012-10-31 15:52:16 +0000 | [diff] [blame] | 74 | pthread_mutex_unlock(&fMutex); |
scroggo@google.com | 4d3c281 | 2012-10-31 19:29:13 +0000 | [diff] [blame] | 75 | #elif defined(SK_BUILD_FOR_WIN32) |
| 76 | LeaveCriticalSection(&fCriticalSection); |
| 77 | #endif |
scroggo@google.com | 4177ef4 | 2012-10-31 15:52:16 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void SkCondVar::wait() { |
scroggo@google.com | 4d3c281 | 2012-10-31 19:29:13 +0000 | [diff] [blame] | 81 | #ifdef SK_USE_POSIX_THREADS |
scroggo@google.com | 4177ef4 | 2012-10-31 15:52:16 +0000 | [diff] [blame] | 82 | pthread_cond_wait(&fCond, &fMutex); |
scroggo@google.com | 4d3c281 | 2012-10-31 19:29:13 +0000 | [diff] [blame] | 83 | #elif defined(SK_BUILD_FOR_WIN32) |
mtklein | db8d0e5 | 2014-11-03 17:25:54 -0800 | [diff] [blame^] | 84 | SkASSERT(sleep_condition_variable); |
| 85 | sleep_condition_variable(&fCondition, &fCriticalSection, INFINITE); |
scroggo@google.com | 4d3c281 | 2012-10-31 19:29:13 +0000 | [diff] [blame] | 86 | #endif |
scroggo@google.com | 4177ef4 | 2012-10-31 15:52:16 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void SkCondVar::signal() { |
scroggo@google.com | 4d3c281 | 2012-10-31 19:29:13 +0000 | [diff] [blame] | 90 | #ifdef SK_USE_POSIX_THREADS |
scroggo@google.com | 4177ef4 | 2012-10-31 15:52:16 +0000 | [diff] [blame] | 91 | pthread_cond_signal(&fCond); |
scroggo@google.com | 4d3c281 | 2012-10-31 19:29:13 +0000 | [diff] [blame] | 92 | #elif defined(SK_BUILD_FOR_WIN32) |
mtklein | db8d0e5 | 2014-11-03 17:25:54 -0800 | [diff] [blame^] | 93 | SkASSERT(wake_condition_variable); |
| 94 | wake_condition_variable(&fCondition); |
scroggo@google.com | 4d3c281 | 2012-10-31 19:29:13 +0000 | [diff] [blame] | 95 | #endif |
scroggo@google.com | 4177ef4 | 2012-10-31 15:52:16 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void SkCondVar::broadcast() { |
scroggo@google.com | 4d3c281 | 2012-10-31 19:29:13 +0000 | [diff] [blame] | 99 | #ifdef SK_USE_POSIX_THREADS |
scroggo@google.com | 4177ef4 | 2012-10-31 15:52:16 +0000 | [diff] [blame] | 100 | pthread_cond_broadcast(&fCond); |
scroggo@google.com | 4d3c281 | 2012-10-31 19:29:13 +0000 | [diff] [blame] | 101 | #elif defined(SK_BUILD_FOR_WIN32) |
mtklein | db8d0e5 | 2014-11-03 17:25:54 -0800 | [diff] [blame^] | 102 | SkASSERT(wake_all_condition_variable); |
| 103 | wake_all_condition_variable(&fCondition); |
scroggo@google.com | 4d3c281 | 2012-10-31 19:29:13 +0000 | [diff] [blame] | 104 | #endif |
scroggo@google.com | 4177ef4 | 2012-10-31 15:52:16 +0000 | [diff] [blame] | 105 | } |