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