1/*
2 *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11#ifndef WEBRTC_SYSTEM_WRAPPERS_SOURCE_CONDITION_VARIABLE_WINDOWS_H_
12#define WEBRTC_SYSTEM_WRAPPERS_SOURCE_CONDITION_VARIABLE_WINDOWS_H_
13
14#include "condition_variable_wrapper.h"
15
16#include <windows.h>
17
18namespace webrtc {
19#if !defined CONDITION_VARIABLE_INIT
20    typedef struct _RTL_CONDITION_VARIABLE
21    {
22        void* Ptr;
23    } RTL_CONDITION_VARIABLE, *PRTL_CONDITION_VARIABLE;
24
25    typedef RTL_CONDITION_VARIABLE CONDITION_VARIABLE, *PCONDITION_VARIABLE;
26#endif
27
28typedef void (WINAPI *PInitializeConditionVariable)(PCONDITION_VARIABLE);
29typedef BOOL (WINAPI *PSleepConditionVariableCS)(PCONDITION_VARIABLE,
30                                                 PCRITICAL_SECTION, DWORD);
31typedef void (WINAPI *PWakeConditionVariable)(PCONDITION_VARIABLE);
32typedef void (WINAPI *PWakeAllConditionVariable)(PCONDITION_VARIABLE);
33
34
35class ConditionVariableWindows : public ConditionVariableWrapper
36{
37public:
38    ConditionVariableWindows();
39    ~ConditionVariableWindows();
40
41    void SleepCS(CriticalSectionWrapper& critSect);
42    bool SleepCS(CriticalSectionWrapper& critSect, unsigned long maxTimeInMS);
43    void Wake();
44    void WakeAll();
45
46private:
47    enum EventWakeUpType
48    {
49        WAKEALL_0   = 0,
50        WAKEALL_1   = 1,
51        WAKE        = 2,
52        EVENT_COUNT = 3
53    };
54
55private:
56    // Native support for Windows Vista+
57    static bool              _winSupportConditionVariablesPrimitive;
58    CONDITION_VARIABLE       _conditionVariable;
59
60    unsigned int     _numWaiters[2];
61    EventWakeUpType  _eventID;
62    CRITICAL_SECTION _numWaitersCritSect;
63    HANDLE           _events[EVENT_COUNT];
64};
65} // namespace webrtc
66
67#endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_CONDITION_VARIABLE_WINDOWS_H_
68