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