Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / src / utils / SkCondVar.h
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 #include "SkTypes.h"
12
13 #ifdef SK_USE_POSIX_THREADS
14     #include <pthread.h>
15 #elif defined(SK_BUILD_FOR_WIN32)
16     #include <windows.h>
17 #else
18     #error "SkCondVar requires pthreads or Windows."
19 #endif
20
21 /**
22  * Condition variable for blocking access to shared data from other threads and
23  * controlling which threads are awake.
24  *
25  * Currently only supported on platforms with posix threads and Windows Vista and above.
26  */
27 class SkCondVar {
28 public:
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
34     SkCondVar();
35     ~SkCondVar();
36
37     /**
38      * Lock a mutex. Must be done before calling the other functions on this object.
39      */
40     void lock();
41
42     /**
43      * Unlock the mutex.
44      */
45     void unlock();
46
47     /**
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.
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:
67 #ifdef SK_USE_POSIX_THREADS
68     pthread_mutex_t  fMutex;
69     pthread_cond_t   fCond;
70 #elif defined(SK_BUILD_FOR_WIN32)
71     CRITICAL_SECTION   fCriticalSection;
72     CONDITION_VARIABLE fCondition;
73 #endif
74 };
75
76 #endif