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 SkCondVar()10SkCondVar::SkCondVar() { 11 #ifdef SK_USE_POSIX_THREADS 12 pthread_mutex_init(&fMutex, NULL /* default mutex attr */); 13 pthread_cond_init(&fCond, NULL /* default cond attr */); 14 #elif defined(SK_BUILD_FOR_WIN32) 15 InitializeCriticalSection(&fCriticalSection); 16 InitializeConditionVariable(&fCondition); 17 #endif 18 } 19 ~SkCondVar()20SkCondVar::~SkCondVar() { 21 #ifdef SK_USE_POSIX_THREADS 22 pthread_mutex_destroy(&fMutex); 23 pthread_cond_destroy(&fCond); 24 #elif defined(SK_BUILD_FOR_WIN32) 25 DeleteCriticalSection(&fCriticalSection); 26 // No need to clean up fCondition. 27 #endif 28 } 29 lock()30void SkCondVar::lock() { 31 #ifdef SK_USE_POSIX_THREADS 32 pthread_mutex_lock(&fMutex); 33 #elif defined(SK_BUILD_FOR_WIN32) 34 EnterCriticalSection(&fCriticalSection); 35 #endif 36 } 37 unlock()38void SkCondVar::unlock() { 39 #ifdef SK_USE_POSIX_THREADS 40 pthread_mutex_unlock(&fMutex); 41 #elif defined(SK_BUILD_FOR_WIN32) 42 LeaveCriticalSection(&fCriticalSection); 43 #endif 44 } 45 wait()46void SkCondVar::wait() { 47 #ifdef SK_USE_POSIX_THREADS 48 pthread_cond_wait(&fCond, &fMutex); 49 #elif defined(SK_BUILD_FOR_WIN32) 50 SleepConditionVariableCS(&fCondition, &fCriticalSection, INFINITE); 51 #endif 52 } 53 signal()54void SkCondVar::signal() { 55 #ifdef SK_USE_POSIX_THREADS 56 pthread_cond_signal(&fCond); 57 #elif defined(SK_BUILD_FOR_WIN32) 58 WakeConditionVariable(&fCondition); 59 #endif 60 } 61 broadcast()62void SkCondVar::broadcast() { 63 #ifdef SK_USE_POSIX_THREADS 64 pthread_cond_broadcast(&fCond); 65 #elif defined(SK_BUILD_FOR_WIN32) 66 WakeAllConditionVariable(&fCondition); 67 #endif 68 } 69