scroggo@google.com | 4177ef4 | 2012-10-31 15:52:16 +0000 | [diff] [blame] | 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 | |
scroggo@google.com | 4d3c281 | 2012-10-31 19:29:13 +0000 | [diff] [blame] | 11 | #ifdef SK_USE_POSIX_THREADS |
scroggo@google.com | 4177ef4 | 2012-10-31 15:52:16 +0000 | [diff] [blame] | 12 | #include <pthread.h> |
scroggo@google.com | 4d3c281 | 2012-10-31 19:29:13 +0000 | [diff] [blame] | 13 | #elif defined(SK_BUILD_FOR_WIN32) |
| 14 | #include <Windows.h> |
| 15 | #endif |
scroggo@google.com | 4177ef4 | 2012-10-31 15:52:16 +0000 | [diff] [blame] | 16 | |
scroggo@google.com | 4d3c281 | 2012-10-31 19:29:13 +0000 | [diff] [blame] | 17 | /** |
| 18 | * Condition variable for blocking access to shared data from other threads and |
| 19 | * controlling which threads are awake. |
| 20 | * |
| 21 | * Currently only supported on platforms with posix threads and Windows Vista and |
| 22 | * above. |
| 23 | */ |
scroggo@google.com | 4177ef4 | 2012-10-31 15:52:16 +0000 | [diff] [blame] | 24 | class SkCondVar { |
| 25 | public: |
| 26 | SkCondVar(); |
| 27 | ~SkCondVar(); |
| 28 | |
scroggo@google.com | 4d3c281 | 2012-10-31 19:29:13 +0000 | [diff] [blame] | 29 | /** |
| 30 | * Lock a mutex. Must be done before calling the other functions on this object. |
| 31 | */ |
scroggo@google.com | 4177ef4 | 2012-10-31 15:52:16 +0000 | [diff] [blame] | 32 | void lock(); |
scroggo@google.com | 4d3c281 | 2012-10-31 19:29:13 +0000 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Unlock the mutex. |
| 36 | */ |
scroggo@google.com | 4177ef4 | 2012-10-31 15:52:16 +0000 | [diff] [blame] | 37 | void unlock(); |
| 38 | |
| 39 | /** |
scroggo@google.com | 4d3c281 | 2012-10-31 19:29:13 +0000 | [diff] [blame] | 40 | * Pause the calling thread. Will be awoken when signal() or broadcast() is called. |
| 41 | * Must be called while lock() is held (but gives it up while waiting). Once awoken, |
| 42 | * the calling thread will hold the lock once again. |
scroggo@google.com | 4177ef4 | 2012-10-31 15:52:16 +0000 | [diff] [blame] | 43 | */ |
| 44 | void wait(); |
| 45 | |
| 46 | /** |
| 47 | * Wake one thread waiting on this condition. Must be called while lock() |
| 48 | * is held. |
| 49 | */ |
| 50 | void signal(); |
| 51 | |
| 52 | /** |
| 53 | * Wake all threads waiting on this condition. Must be called while lock() |
| 54 | * is held. |
| 55 | */ |
| 56 | void broadcast(); |
| 57 | |
| 58 | private: |
scroggo@google.com | 4d3c281 | 2012-10-31 19:29:13 +0000 | [diff] [blame] | 59 | #ifdef SK_USE_POSIX_THREADS |
scroggo@google.com | 4177ef4 | 2012-10-31 15:52:16 +0000 | [diff] [blame] | 60 | pthread_mutex_t fMutex; |
| 61 | pthread_cond_t fCond; |
scroggo@google.com | 4d3c281 | 2012-10-31 19:29:13 +0000 | [diff] [blame] | 62 | #elif defined(SK_BUILD_FOR_WIN32) |
| 63 | CRITICAL_SECTION fCriticalSection; |
| 64 | CONDITION_VARIABLE fCondition; |
| 65 | #endif |
scroggo@google.com | 4177ef4 | 2012-10-31 15:52:16 +0000 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | #endif |