1b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org/* 2b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. 3b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org * 4b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org * Use of this source code is governed by a BSD-style license 5b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org * that can be found in the LICENSE file in the root of the source 6b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org * tree. An additional intellectual property rights grant can be found 7b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org * in the file PATENTS. All contributing project authors may 8b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org * be found in the AUTHORS file in the root of the source tree. 9b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org */ 10b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org 11b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_EVENT_WRAPPER_H_ 12b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_EVENT_WRAPPER_H_ 13b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org 14b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgnamespace webrtc { 1580b62056026ec31a642da519832b37e79b7eac4ephoglund@webrtc.orgenum EventTypeWrapper { 1680b62056026ec31a642da519832b37e79b7eac4ephoglund@webrtc.org kEventSignaled = 1, 1780b62056026ec31a642da519832b37e79b7eac4ephoglund@webrtc.org kEventError = 2, 1880b62056026ec31a642da519832b37e79b7eac4ephoglund@webrtc.org kEventTimeout = 3 19b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}; 20b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org 21b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#define WEBRTC_EVENT_10_SEC 10000 22b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#define WEBRTC_EVENT_INFINITE 0xffffffff 23b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org 2480b62056026ec31a642da519832b37e79b7eac4ephoglund@webrtc.orgclass EventWrapper { 2580b62056026ec31a642da519832b37e79b7eac4ephoglund@webrtc.org public: 2680b62056026ec31a642da519832b37e79b7eac4ephoglund@webrtc.org // Factory method. Constructor disabled. 2780b62056026ec31a642da519832b37e79b7eac4ephoglund@webrtc.org static EventWrapper* Create(); 2880b62056026ec31a642da519832b37e79b7eac4ephoglund@webrtc.org virtual ~EventWrapper() {} 29b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org 3080b62056026ec31a642da519832b37e79b7eac4ephoglund@webrtc.org // Releases threads who are calling Wait() and has started waiting. Please 3180b62056026ec31a642da519832b37e79b7eac4ephoglund@webrtc.org // note that a thread calling Wait() will not start waiting immediately. 3280b62056026ec31a642da519832b37e79b7eac4ephoglund@webrtc.org // assumptions to the contrary is a very common source of issues in 3380b62056026ec31a642da519832b37e79b7eac4ephoglund@webrtc.org // multithreaded programming. 3480b62056026ec31a642da519832b37e79b7eac4ephoglund@webrtc.org // Set is sticky in the sense that it will release at least one thread 3580b62056026ec31a642da519832b37e79b7eac4ephoglund@webrtc.org // either immediately or some time in the future. 3680b62056026ec31a642da519832b37e79b7eac4ephoglund@webrtc.org virtual bool Set() = 0; 37b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org 3880b62056026ec31a642da519832b37e79b7eac4ephoglund@webrtc.org // Prevents future Wait() calls from finishing without a new Set() call. 3980b62056026ec31a642da519832b37e79b7eac4ephoglund@webrtc.org virtual bool Reset() = 0; 40b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org 4180b62056026ec31a642da519832b37e79b7eac4ephoglund@webrtc.org // Puts the calling thread into a wait state. The thread may be released 4280b62056026ec31a642da519832b37e79b7eac4ephoglund@webrtc.org // by a Set() call depending on if other threads are waiting and if so on 4380b62056026ec31a642da519832b37e79b7eac4ephoglund@webrtc.org // timing. The thread that was released will call Reset() before leaving 4480b62056026ec31a642da519832b37e79b7eac4ephoglund@webrtc.org // preventing more threads from being released. If multiple threads 4580b62056026ec31a642da519832b37e79b7eac4ephoglund@webrtc.org // are waiting for the same Set(), only one (random) thread is guaranteed to 4680b62056026ec31a642da519832b37e79b7eac4ephoglund@webrtc.org // be released. It is possible that multiple (random) threads are released 4780b62056026ec31a642da519832b37e79b7eac4ephoglund@webrtc.org // Depending on timing. 4880b62056026ec31a642da519832b37e79b7eac4ephoglund@webrtc.org virtual EventTypeWrapper Wait(unsigned long max_time) = 0; 49b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org 5080b62056026ec31a642da519832b37e79b7eac4ephoglund@webrtc.org // Starts a timer that will call a non-sticky version of Set() either once 5180b62056026ec31a642da519832b37e79b7eac4ephoglund@webrtc.org // or periodically. If the timer is periodic it ensures that there is no 5280b62056026ec31a642da519832b37e79b7eac4ephoglund@webrtc.org // drift over time relative to the system clock. 5380b62056026ec31a642da519832b37e79b7eac4ephoglund@webrtc.org virtual bool StartTimer(bool periodic, unsigned long time) = 0; 54b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org 5580b62056026ec31a642da519832b37e79b7eac4ephoglund@webrtc.org virtual bool StopTimer() = 0; 56b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org 57b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}; 583b89e10f31160da35b408fd00cb8f89d2b08862dpbos@webrtc.org} // namespace webrtc 59b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org 6080b62056026ec31a642da519832b37e79b7eac4ephoglund@webrtc.org#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_EVENT_WRAPPER_H_ 61