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
11/**
12 * Import any thread model setting from configuration files.
13 */
14#include "SkTypes.h"
15
16#ifdef SK_USE_POSIX_THREADS
17#include <pthread.h>
18#elif defined(SK_BUILD_FOR_WIN32)
19#include <windows.h>
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!"
25#endif
26
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 */
34class SkCondVar {
35public:
36    SkCondVar();
37    ~SkCondVar();
38
39    /**
40     * Lock a mutex. Must be done before calling the other functions on this object.
41     */
42    void lock();
43
44    /**
45     * Unlock the mutex.
46     */
47    void unlock();
48
49    /**
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.
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
68private:
69#ifdef SK_USE_POSIX_THREADS
70    pthread_mutex_t  fMutex;
71    pthread_cond_t   fCond;
72#elif defined(SK_BUILD_FOR_WIN32)
73    CRITICAL_SECTION   fCriticalSection;
74    CONDITION_VARIABLE fCondition;
75#endif
76};
77
78#endif
79