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) | 10 #if defined(SK_BUILD_FOR_WIN32) |
11 static void (WINAPI *initialize_condition_variable)(PCONDITION_VARIABLE); | 11 static void (WINAPI *initialize_condition_variable)(PCONDITION_VARIABLE); |
12 static BOOL (WINAPI *sleep_condition_variable)(PCONDITION_VARIABLE, PCRITICA
L_SECTION, DWORD); | 12 static BOOL (WINAPI *sleep_condition_variable)(PCONDITION_VARIABLE, PCRITICA
L_SECTION, DWORD); |
13 static void (WINAPI *wake_condition_variable)(PCONDITION_VARIABLE); | 13 static void (WINAPI *wake_condition_variable)(PCONDITION_VARIABLE); |
14 static void (WINAPI *wake_all_condition_variable)(PCONDITION_VARIABLE); | 14 static void (WINAPI *wake_all_condition_variable)(PCONDITION_VARIABLE); |
15 | 15 |
16 template <typename T> | 16 template <typename T> |
17 static void set_fn_ptr(T* ptr, FARPROC fn) { *ptr = reinterpret_cast<T>(fn);
} | 17 static void set_fn_ptr(T* ptr, FARPROC fn) { *ptr = reinterpret_cast<T>(fn);
} |
18 #endif | 18 #endif |
19 | 19 |
20 bool SkCondVar::Supported() { | 20 bool SkCondVar::Supported() { |
21 #ifdef SK_USE_POSIX_THREADS | 21 #ifdef SK_BUILD_FOR_WIN32 |
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. | 22 // If we're >= Vista we'll find these functions. Otherwise (XP) SkCondVar i
s not supported. |
25 HMODULE kernel32 = GetModuleHandleA("kernel32.dll"); | 23 HMODULE kernel32 = GetModuleHandleA("kernel32.dll"); |
26 set_fn_ptr(&initialize_condition_variable, | 24 set_fn_ptr(&initialize_condition_variable, |
27 GetProcAddress(kernel32, "InitializeConditionVariable")); | 25 GetProcAddress(kernel32, "InitializeConditionVariable")); |
28 set_fn_ptr(&sleep_condition_variable, | 26 set_fn_ptr(&sleep_condition_variable, |
29 GetProcAddress(kernel32, "SleepConditionVariableCS")); | 27 GetProcAddress(kernel32, "SleepConditionVariableCS")); |
30 set_fn_ptr(&wake_condition_variable, | 28 set_fn_ptr(&wake_condition_variable, |
31 GetProcAddress(kernel32, "WakeConditionVariable")); | 29 GetProcAddress(kernel32, "WakeConditionVariable")); |
32 set_fn_ptr(&wake_all_condition_variable, | 30 set_fn_ptr(&wake_all_condition_variable, |
33 GetProcAddress(kernel32, "WakeAllConditionVariable")); | 31 GetProcAddress(kernel32, "WakeAllConditionVariable")); |
34 return initialize_condition_variable | 32 return initialize_condition_variable |
35 && sleep_condition_variable | 33 && sleep_condition_variable |
36 && wake_condition_variable | 34 && wake_condition_variable |
37 && wake_all_condition_variable; | 35 && wake_all_condition_variable; |
38 #else | 36 #else |
39 return false; | 37 return true; |
40 #endif | 38 #endif |
41 } | 39 } |
42 | 40 |
43 SkCondVar::SkCondVar() { | 41 SkCondVar::SkCondVar() { |
44 #ifdef SK_USE_POSIX_THREADS | 42 #ifdef SK_BUILD_FOR_WIN32 |
45 pthread_mutex_init(&fMutex, NULL /* default mutex attr */); | |
46 pthread_cond_init(&fCond, NULL /* default cond attr */); | |
47 #elif defined(SK_BUILD_FOR_WIN32) | |
48 InitializeCriticalSection(&fCriticalSection); | 43 InitializeCriticalSection(&fCriticalSection); |
49 SkASSERT(initialize_condition_variable); | 44 SkASSERT(initialize_condition_variable); |
50 initialize_condition_variable(&fCondition); | 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 */); |
51 #endif | 49 #endif |
52 } | 50 } |
53 | 51 |
54 SkCondVar::~SkCondVar() { | 52 SkCondVar::~SkCondVar() { |
55 #ifdef SK_USE_POSIX_THREADS | 53 #ifdef SK_BUILD_FOR_WIN32 |
| 54 DeleteCriticalSection(&fCriticalSection); |
| 55 // No need to clean up fCondition. |
| 56 #else |
56 pthread_mutex_destroy(&fMutex); | 57 pthread_mutex_destroy(&fMutex); |
57 pthread_cond_destroy(&fCond); | 58 pthread_cond_destroy(&fCond); |
58 #elif defined(SK_BUILD_FOR_WIN32) | |
59 DeleteCriticalSection(&fCriticalSection); | |
60 // No need to clean up fCondition. | |
61 #endif | 59 #endif |
62 } | 60 } |
63 | 61 |
64 void SkCondVar::lock() { | 62 void SkCondVar::lock() { |
65 #ifdef SK_USE_POSIX_THREADS | 63 #ifdef SK_BUILD_FOR_WIN32 |
| 64 EnterCriticalSection(&fCriticalSection); |
| 65 #else |
66 pthread_mutex_lock(&fMutex); | 66 pthread_mutex_lock(&fMutex); |
67 #elif defined(SK_BUILD_FOR_WIN32) | |
68 EnterCriticalSection(&fCriticalSection); | |
69 #endif | 67 #endif |
70 } | 68 } |
71 | 69 |
72 void SkCondVar::unlock() { | 70 void SkCondVar::unlock() { |
73 #ifdef SK_USE_POSIX_THREADS | 71 #ifdef SK_BUILD_FOR_WIN32 |
| 72 LeaveCriticalSection(&fCriticalSection); |
| 73 #else |
74 pthread_mutex_unlock(&fMutex); | 74 pthread_mutex_unlock(&fMutex); |
75 #elif defined(SK_BUILD_FOR_WIN32) | |
76 LeaveCriticalSection(&fCriticalSection); | |
77 #endif | 75 #endif |
78 } | 76 } |
79 | 77 |
80 void SkCondVar::wait() { | 78 void SkCondVar::wait() { |
81 #ifdef SK_USE_POSIX_THREADS | 79 #ifdef SK_BUILD_FOR_WIN32 |
82 pthread_cond_wait(&fCond, &fMutex); | |
83 #elif defined(SK_BUILD_FOR_WIN32) | |
84 SkASSERT(sleep_condition_variable); | 80 SkASSERT(sleep_condition_variable); |
85 sleep_condition_variable(&fCondition, &fCriticalSection, INFINITE); | 81 sleep_condition_variable(&fCondition, &fCriticalSection, INFINITE); |
| 82 #else |
| 83 pthread_cond_wait(&fCond, &fMutex); |
86 #endif | 84 #endif |
87 } | 85 } |
88 | 86 |
89 void SkCondVar::signal() { | 87 void SkCondVar::signal() { |
90 #ifdef SK_USE_POSIX_THREADS | 88 #ifdef SK_BUILD_FOR_WIN32 |
91 pthread_cond_signal(&fCond); | |
92 #elif defined(SK_BUILD_FOR_WIN32) | |
93 SkASSERT(wake_condition_variable); | 89 SkASSERT(wake_condition_variable); |
94 wake_condition_variable(&fCondition); | 90 wake_condition_variable(&fCondition); |
| 91 #else |
| 92 pthread_cond_signal(&fCond); |
95 #endif | 93 #endif |
96 } | 94 } |
97 | 95 |
98 void SkCondVar::broadcast() { | 96 void SkCondVar::broadcast() { |
99 #ifdef SK_USE_POSIX_THREADS | 97 #ifdef SK_BUILD_FOR_WIN32 |
100 pthread_cond_broadcast(&fCond); | |
101 #elif defined(SK_BUILD_FOR_WIN32) | |
102 SkASSERT(wake_all_condition_variable); | 98 SkASSERT(wake_all_condition_variable); |
103 wake_all_condition_variable(&fCondition); | 99 wake_all_condition_variable(&fCondition); |
| 100 #else |
| 101 pthread_cond_broadcast(&fCond); |
104 #endif | 102 #endif |
105 } | 103 } |
OLD | NEW |