Sebastian Jansson | 7a60339 | 2019-03-20 15:50:35 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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 | #ifndef RTC_BASE_SYNCHRONIZATION_YIELD_POLICY_H_ |
| 11 | #define RTC_BASE_SYNCHRONIZATION_YIELD_POLICY_H_ |
| 12 | |
| 13 | namespace rtc { |
| 14 | class YieldInterface { |
| 15 | public: |
| 16 | virtual ~YieldInterface() = default; |
| 17 | virtual void YieldExecution() = 0; |
| 18 | }; |
| 19 | |
| 20 | // Sets the current thread-local yield policy while it's in scope and reverts |
| 21 | // to the previous policy when it leaves the scope. |
| 22 | class ScopedYieldPolicy final { |
| 23 | public: |
| 24 | explicit ScopedYieldPolicy(YieldInterface* policy); |
Sebastian Jansson | 9debe5a | 2019-03-22 14:42:38 | [diff] [blame] | 25 | ScopedYieldPolicy(const ScopedYieldPolicy&) = delete; |
| 26 | ScopedYieldPolicy& operator=(const ScopedYieldPolicy&) = delete; |
Sebastian Jansson | 7a60339 | 2019-03-20 15:50:35 | [diff] [blame] | 27 | ~ScopedYieldPolicy(); |
| 28 | // Will yield as specified by the currently active thread-local yield policy |
| 29 | // (which by default is a no-op). |
| 30 | static void YieldExecution(); |
| 31 | |
| 32 | private: |
| 33 | YieldInterface* const previous_; |
| 34 | }; |
| 35 | |
| 36 | } // namespace rtc |
| 37 | |
| 38 | #endif // RTC_BASE_SYNCHRONIZATION_YIELD_POLICY_H_ |