niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 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 | |
kjellander | d56d68c | 2015-11-02 10:12:41 | [diff] [blame] | 11 | #ifndef WEBRTC_SYSTEM_WRAPPERS_INCLUDE_EVENT_WRAPPER_H_ |
| 12 | #define WEBRTC_SYSTEM_WRAPPERS_INCLUDE_EVENT_WRAPPER_H_ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 13 | |
| 14 | namespace webrtc { |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 | [diff] [blame] | 15 | enum EventTypeWrapper { |
| 16 | kEventSignaled = 1, |
| 17 | kEventError = 2, |
| 18 | kEventTimeout = 3 |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 19 | }; |
| 20 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 21 | #define WEBRTC_EVENT_INFINITE 0xffffffff |
| 22 | |
Peter Boström | 64c0366 | 2015-04-08 09:24:19 | [diff] [blame] | 23 | class EventTimerWrapper; |
| 24 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 | [diff] [blame] | 25 | class EventWrapper { |
| 26 | public: |
| 27 | // Factory method. Constructor disabled. |
| 28 | static EventWrapper* Create(); |
Peter Boström | 64c0366 | 2015-04-08 09:24:19 | [diff] [blame] | 29 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 | [diff] [blame] | 30 | virtual ~EventWrapper() {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 31 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 | [diff] [blame] | 32 | // Releases threads who are calling Wait() and has started waiting. Please |
| 33 | // note that a thread calling Wait() will not start waiting immediately. |
| 34 | // assumptions to the contrary is a very common source of issues in |
| 35 | // multithreaded programming. |
| 36 | // Set is sticky in the sense that it will release at least one thread |
| 37 | // either immediately or some time in the future. |
| 38 | virtual bool Set() = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 39 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 | [diff] [blame] | 40 | // Puts the calling thread into a wait state. The thread may be released |
| 41 | // by a Set() call depending on if other threads are waiting and if so on |
Wan-Teh Chang | 5a8bad6 | 2015-05-29 16:41:38 | [diff] [blame] | 42 | // timing. The thread that was released will reset the event before leaving |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 | [diff] [blame] | 43 | // preventing more threads from being released. If multiple threads |
| 44 | // are waiting for the same Set(), only one (random) thread is guaranteed to |
| 45 | // be released. It is possible that multiple (random) threads are released |
| 46 | // Depending on timing. |
Wan-Teh Chang | 76cda01 | 2015-06-02 00:33:40 | [diff] [blame] | 47 | // |
| 48 | // |max_time| is the maximum time to wait in milliseconds or |
| 49 | // WEBRTC_EVENT_INFINITE to wait infinitely. |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 | [diff] [blame] | 50 | virtual EventTypeWrapper Wait(unsigned long max_time) = 0; |
Peter Boström | 64c0366 | 2015-04-08 09:24:19 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | class EventTimerWrapper : public EventWrapper { |
| 54 | public: |
| 55 | static EventTimerWrapper* Create(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 56 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 | [diff] [blame] | 57 | // Starts a timer that will call a non-sticky version of Set() either once |
| 58 | // or periodically. If the timer is periodic it ensures that there is no |
| 59 | // drift over time relative to the system clock. |
Wan-Teh Chang | 76cda01 | 2015-06-02 00:33:40 | [diff] [blame] | 60 | // |
| 61 | // |time| is in milliseconds. |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 | [diff] [blame] | 62 | virtual bool StartTimer(bool periodic, unsigned long time) = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 63 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 | [diff] [blame] | 64 | virtual bool StopTimer() = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 65 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 66 | }; |
Peter Boström | 64c0366 | 2015-04-08 09:24:19 | [diff] [blame] | 67 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 | [diff] [blame] | 68 | } // namespace webrtc |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 69 | |
kjellander | d56d68c | 2015-11-02 10:12:41 | [diff] [blame] | 70 | #endif // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_EVENT_WRAPPER_H_ |