michael@0: /* michael@0: * Copyright 2012 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include "SkCondVar.h" michael@0: michael@0: SkCondVar::SkCondVar() { michael@0: #ifdef SK_USE_POSIX_THREADS michael@0: pthread_mutex_init(&fMutex, NULL /* default mutex attr */); michael@0: pthread_cond_init(&fCond, NULL /* default cond attr */); michael@0: #elif defined(SK_BUILD_FOR_WIN32) michael@0: InitializeCriticalSection(&fCriticalSection); michael@0: InitializeConditionVariable(&fCondition); michael@0: #endif michael@0: } michael@0: michael@0: SkCondVar::~SkCondVar() { michael@0: #ifdef SK_USE_POSIX_THREADS michael@0: pthread_mutex_destroy(&fMutex); michael@0: pthread_cond_destroy(&fCond); michael@0: #elif defined(SK_BUILD_FOR_WIN32) michael@0: DeleteCriticalSection(&fCriticalSection); michael@0: // No need to clean up fCondition. michael@0: #endif michael@0: } michael@0: michael@0: void SkCondVar::lock() { michael@0: #ifdef SK_USE_POSIX_THREADS michael@0: pthread_mutex_lock(&fMutex); michael@0: #elif defined(SK_BUILD_FOR_WIN32) michael@0: EnterCriticalSection(&fCriticalSection); michael@0: #endif michael@0: } michael@0: michael@0: void SkCondVar::unlock() { michael@0: #ifdef SK_USE_POSIX_THREADS michael@0: pthread_mutex_unlock(&fMutex); michael@0: #elif defined(SK_BUILD_FOR_WIN32) michael@0: LeaveCriticalSection(&fCriticalSection); michael@0: #endif michael@0: } michael@0: michael@0: void SkCondVar::wait() { michael@0: #ifdef SK_USE_POSIX_THREADS michael@0: pthread_cond_wait(&fCond, &fMutex); michael@0: #elif defined(SK_BUILD_FOR_WIN32) michael@0: SleepConditionVariableCS(&fCondition, &fCriticalSection, INFINITE); michael@0: #endif michael@0: } michael@0: michael@0: void SkCondVar::signal() { michael@0: #ifdef SK_USE_POSIX_THREADS michael@0: pthread_cond_signal(&fCond); michael@0: #elif defined(SK_BUILD_FOR_WIN32) michael@0: WakeConditionVariable(&fCondition); michael@0: #endif michael@0: } michael@0: michael@0: void SkCondVar::broadcast() { michael@0: #ifdef SK_USE_POSIX_THREADS michael@0: pthread_cond_broadcast(&fCond); michael@0: #elif defined(SK_BUILD_FOR_WIN32) michael@0: WakeAllConditionVariable(&fCondition); michael@0: #endif michael@0: }