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 #ifndef SkCondVar_DEFINED 9 #define SkCondVar_DEFINED 10 11 #include "SkTypes.h" 12 13 #ifdef SK_BUILD_FOR_WIN32 14 #include <windows.h> 15 #else 16 #include <pthread.h> 17 #endif 18 19 /** 20 * Condition variable for blocking access to shared data from other threads and 21 * controlling which threads are awake. 22 * 23 * Currently only supported on platforms with posix threads and Windows Vista and above. 24 */ 25 class SkCondVar { 26 public: 27 /** Returns true if it makes sense to create and use SkCondVars. 28 * You _MUST_ call this method and it must return true before creating any SkCondVars. 29 */ 30 static bool Supported(); 31 32 SkCondVar(); 33 ~SkCondVar(); 34 35 /** 36 * Lock a mutex. Must be done before calling the other functions on this object. 37 */ 38 void lock(); 39 40 /** 41 * Unlock the mutex. 42 */ 43 void unlock(); 44 45 /** 46 * Pause the calling thread. Will be awoken when signal() or broadcast() is called. 47 * Must be called while lock() is held (but gives it up while waiting). Once awoken, 48 * the calling thread will hold the lock once again. 49 */ 50 void wait(); 51 52 /** 53 * Wake one thread waiting on this condition. Must be called while lock() 54 * is held. 55 */ 56 void signal(); 57 58 /** 59 * Wake all threads waiting on this condition. Must be called while lock() 60 * is held. 61 */ 62 void broadcast(); 63 64 private: 65 #ifdef SK_BUILD_FOR_WIN32 66 CRITICAL_SECTION fCriticalSection; 67 CONDITION_VARIABLE fCondition; 68 #else 69 pthread_mutex_t fMutex; 70 pthread_cond_t fCond; 71 #endif 72 }; 73 74 #endif 75