blob: 6f18e1a651df9e0443d00f1a6f3ba9ae9461e5c9 [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/**
12 * Import any thread model setting from configuration files.
13 */
14#include "SkTypes.h"
15
scroggo@google.com4d3c2812012-10-31 19:29:13 +000016#ifdef SK_USE_POSIX_THREADS
scroggo@google.com4177ef42012-10-31 15:52:16 +000017#include <pthread.h>
scroggo@google.com4d3c2812012-10-31 19:29:13 +000018#elif defined(SK_BUILD_FOR_WIN32)
bungeman@google.com0d9e3da2013-12-03 15:23:37 +000019#include <windows.h>
henrik.smidinga9309f52014-07-09 07:25:09 -070020#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.com4d3c2812012-10-31 19:29:13 +000025#endif
scroggo@google.com4177ef42012-10-31 15:52:16 +000026
scroggo@google.com4d3c2812012-10-31 19:29:13 +000027/**
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.com4177ef42012-10-31 15:52:16 +000034class SkCondVar {
35public:
36 SkCondVar();
37 ~SkCondVar();
38
scroggo@google.com4d3c2812012-10-31 19:29:13 +000039 /**
40 * Lock a mutex. Must be done before calling the other functions on this object.
41 */
scroggo@google.com4177ef42012-10-31 15:52:16 +000042 void lock();
scroggo@google.com4d3c2812012-10-31 19:29:13 +000043
44 /**
45 * Unlock the mutex.
46 */
scroggo@google.com4177ef42012-10-31 15:52:16 +000047 void unlock();
48
49 /**
scroggo@google.com4d3c2812012-10-31 19:29:13 +000050 * 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.com4177ef42012-10-31 15:52:16 +000053 */
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
68private:
scroggo@google.com4d3c2812012-10-31 19:29:13 +000069#ifdef SK_USE_POSIX_THREADS
scroggo@google.com4177ef42012-10-31 15:52:16 +000070 pthread_mutex_t fMutex;
71 pthread_cond_t fCond;
scroggo@google.com4d3c2812012-10-31 19:29:13 +000072#elif defined(SK_BUILD_FOR_WIN32)
73 CRITICAL_SECTION fCriticalSection;
74 CONDITION_VARIABLE fCondition;
75#endif
scroggo@google.com4177ef42012-10-31 15:52:16 +000076};
77
78#endif