blob: 7f4225c66ac1d9550e428efc8ab43979ec3f3911 [file] [log] [blame]
scroggo@google.com4177ef42012-10-31 15:52:16 +00001/*
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.smidinga9309f52014-07-09 07:25:09 -070011#include "SkTypes.h"
12
scroggo@google.com4d3c2812012-10-31 19:29:13 +000013#ifdef SK_USE_POSIX_THREADS
mtkleindb8d0e52014-11-03 17:25:54 -080014 #include <pthread.h>
scroggo@google.com4d3c2812012-10-31 19:29:13 +000015#elif defined(SK_BUILD_FOR_WIN32)
mtkleindb8d0e52014-11-03 17:25:54 -080016 #include <windows.h>
henrik.smidinga9309f52014-07-09 07:25:09 -070017#else
mtkleindb8d0e52014-11-03 17:25:54 -080018 #error "SkCondVar requires pthreads or Windows."
scroggo@google.com4d3c2812012-10-31 19:29:13 +000019#endif
scroggo@google.com4177ef42012-10-31 15:52:16 +000020
scroggo@google.com4d3c2812012-10-31 19:29:13 +000021/**
22 * Condition variable for blocking access to shared data from other threads and
23 * controlling which threads are awake.
24 *
mtkleindb8d0e52014-11-03 17:25:54 -080025 * Currently only supported on platforms with posix threads and Windows Vista and above.
scroggo@google.com4d3c2812012-10-31 19:29:13 +000026 */
scroggo@google.com4177ef42012-10-31 15:52:16 +000027class SkCondVar {
28public:
mtkleindb8d0e52014-11-03 17:25:54 -080029 /** 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.com4177ef42012-10-31 15:52:16 +000034 SkCondVar();
35 ~SkCondVar();
36
scroggo@google.com4d3c2812012-10-31 19:29:13 +000037 /**
38 * Lock a mutex. Must be done before calling the other functions on this object.
39 */
scroggo@google.com4177ef42012-10-31 15:52:16 +000040 void lock();
scroggo@google.com4d3c2812012-10-31 19:29:13 +000041
42 /**
43 * Unlock the mutex.
44 */
scroggo@google.com4177ef42012-10-31 15:52:16 +000045 void unlock();
46
47 /**
scroggo@google.com4d3c2812012-10-31 19:29:13 +000048 * 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.com4177ef42012-10-31 15:52:16 +000051 */
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
66private:
scroggo@google.com4d3c2812012-10-31 19:29:13 +000067#ifdef SK_USE_POSIX_THREADS
scroggo@google.com4177ef42012-10-31 15:52:16 +000068 pthread_mutex_t fMutex;
69 pthread_cond_t fCond;
scroggo@google.com4d3c2812012-10-31 19:29:13 +000070#elif defined(SK_BUILD_FOR_WIN32)
71 CRITICAL_SECTION fCriticalSection;
72 CONDITION_VARIABLE fCondition;
73#endif
scroggo@google.com4177ef42012-10-31 15:52:16 +000074};
75
76#endif