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: #ifndef SkCondVar_DEFINED michael@0: #define SkCondVar_DEFINED michael@0: michael@0: #ifdef SK_USE_POSIX_THREADS michael@0: #include michael@0: #elif defined(SK_BUILD_FOR_WIN32) michael@0: #include michael@0: #endif michael@0: michael@0: /** michael@0: * Condition variable for blocking access to shared data from other threads and michael@0: * controlling which threads are awake. michael@0: * michael@0: * Currently only supported on platforms with posix threads and Windows Vista and michael@0: * above. michael@0: */ michael@0: class SkCondVar { michael@0: public: michael@0: SkCondVar(); michael@0: ~SkCondVar(); michael@0: michael@0: /** michael@0: * Lock a mutex. Must be done before calling the other functions on this object. michael@0: */ michael@0: void lock(); michael@0: michael@0: /** michael@0: * Unlock the mutex. michael@0: */ michael@0: void unlock(); michael@0: michael@0: /** michael@0: * Pause the calling thread. Will be awoken when signal() or broadcast() is called. michael@0: * Must be called while lock() is held (but gives it up while waiting). Once awoken, michael@0: * the calling thread will hold the lock once again. michael@0: */ michael@0: void wait(); michael@0: michael@0: /** michael@0: * Wake one thread waiting on this condition. Must be called while lock() michael@0: * is held. michael@0: */ michael@0: void signal(); michael@0: michael@0: /** michael@0: * Wake all threads waiting on this condition. Must be called while lock() michael@0: * is held. michael@0: */ michael@0: void broadcast(); michael@0: michael@0: private: michael@0: #ifdef SK_USE_POSIX_THREADS michael@0: pthread_mutex_t fMutex; michael@0: pthread_cond_t fCond; michael@0: #elif defined(SK_BUILD_FOR_WIN32) michael@0: CRITICAL_SECTION fCriticalSection; michael@0: CONDITION_VARIABLE fCondition; michael@0: #endif michael@0: }; michael@0: michael@0: #endif