yield_policy.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. namespace rtc {
  13. class YieldInterface {
  14. public:
  15. virtual ~YieldInterface() = default;
  16. virtual void YieldExecution() = 0;
  17. };
  18. // Sets the current thread-local yield policy while it's in scope and reverts
  19. // to the previous policy when it leaves the scope.
  20. class ScopedYieldPolicy final {
  21. public:
  22. explicit ScopedYieldPolicy(YieldInterface* policy);
  23. ScopedYieldPolicy(const ScopedYieldPolicy&) = delete;
  24. ScopedYieldPolicy& operator=(const ScopedYieldPolicy&) = delete;
  25. ~ScopedYieldPolicy();
  26. // Will yield as specified by the currently active thread-local yield policy
  27. // (which by default is a no-op).
  28. static void YieldExecution();
  29. private:
  30. YieldInterface* const previous_;
  31. };
  32. } // namespace rtc
  33. #endif // RTC_BASE_SYNCHRONIZATION_YIELD_POLICY_H_