14177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com/*
24177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com * Copyright 2012 Google Inc.
34177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com *
44177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com * Use of this source code is governed by a BSD-style license that can be
54177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com * found in the LICENSE file.
64177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com */
74177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com
84177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com#include "SkCondVar.h"
94177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com
104177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.comSkCondVar::SkCondVar() {
114d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com#ifdef SK_USE_POSIX_THREADS
124177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com    pthread_mutex_init(&fMutex, NULL /* default mutex attr */);
134177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com    pthread_cond_init(&fCond, NULL /* default cond attr */);
144d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com#elif defined(SK_BUILD_FOR_WIN32)
154d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com    InitializeCriticalSection(&fCriticalSection);
164d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com    InitializeConditionVariable(&fCondition);
174d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com#endif
184177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com}
194177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com
204177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.comSkCondVar::~SkCondVar() {
214d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com#ifdef SK_USE_POSIX_THREADS
224177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com    pthread_mutex_destroy(&fMutex);
234177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com    pthread_cond_destroy(&fCond);
244d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com#elif defined(SK_BUILD_FOR_WIN32)
254d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com    DeleteCriticalSection(&fCriticalSection);
264d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com    // No need to clean up fCondition.
274d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com#endif
284177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com}
294177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com
304177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.comvoid SkCondVar::lock() {
314d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com#ifdef SK_USE_POSIX_THREADS
324177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com    pthread_mutex_lock(&fMutex);
334d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com#elif defined(SK_BUILD_FOR_WIN32)
344d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com    EnterCriticalSection(&fCriticalSection);
354d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com#endif
364177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com}
374177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com
384177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.comvoid SkCondVar::unlock() {
394d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com#ifdef SK_USE_POSIX_THREADS
404177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com    pthread_mutex_unlock(&fMutex);
414d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com#elif defined(SK_BUILD_FOR_WIN32)
424d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com    LeaveCriticalSection(&fCriticalSection);
434d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com#endif
444177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com}
454177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com
464177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.comvoid SkCondVar::wait() {
474d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com#ifdef SK_USE_POSIX_THREADS
484177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com    pthread_cond_wait(&fCond, &fMutex);
494d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com#elif defined(SK_BUILD_FOR_WIN32)
504d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com    SleepConditionVariableCS(&fCondition, &fCriticalSection, INFINITE);
514d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com#endif
524177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com}
534177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com
544177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.comvoid SkCondVar::signal() {
554d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com#ifdef SK_USE_POSIX_THREADS
564177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com    pthread_cond_signal(&fCond);
574d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com#elif defined(SK_BUILD_FOR_WIN32)
584d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com    WakeConditionVariable(&fCondition);
594d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com#endif
604177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com}
614177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com
624177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.comvoid SkCondVar::broadcast() {
634d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com#ifdef SK_USE_POSIX_THREADS
644177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com    pthread_cond_broadcast(&fCond);
654d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com#elif defined(SK_BUILD_FOR_WIN32)
664d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com    WakeAllConditionVariable(&fCondition);
674d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com#endif
684177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com}
69