| 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 | #include "rtc_base/synchronization/yield_policy.h" |
| 11 |
| 12 | #include "absl/base/attributes.h" |
| 13 | #include "absl/base/config.h" |
| 14 | #include "rtc_base/checks.h" |
| 15 | #if !defined(ABSL_HAVE_THREAD_LOCAL) && defined(WEBRTC_POSIX) |
| 16 | #include <pthread.h> |
| 17 | #endif |
| 18 |
| 19 | namespace rtc { |
| 20 | namespace { |
| 21 |
| 22 | #if defined(ABSL_HAVE_THREAD_LOCAL) |
| 23 |
| 24 | ABSL_CONST_INIT thread_local YieldInterface* current_yield_policy = nullptr; |
| 25 |
| 26 | YieldInterface* GetCurrentYieldPolicy() { |
| 27 | return current_yield_policy; |
| 28 | } |
| 29 |
| 30 | void SetCurrentYieldPolicy(YieldInterface* ptr) { |
| 31 | current_yield_policy = ptr; |
| 32 | } |
| 33 |
| 34 | #elif defined(WEBRTC_POSIX) |
| 35 |
| 36 | // Emscripten does not support the C++11 thread_local keyword but does support |
| 37 | // the pthread thread-local storage API. |
| 38 | // https://github.com/emscripten-core/emscripten/issues/3502 |
| 39 |
| 40 | ABSL_CONST_INIT pthread_key_t g_current_yield_policy_tls = 0; |
| 41 |
| 42 | void InitializeTls() { |
| 43 | RTC_CHECK_EQ(pthread_key_create(&g_current_yield_policy_tls, nullptr), 0); |
| 44 | } |
| 45 |
| 46 | pthread_key_t GetCurrentYieldPolicyTls() { |
| 47 | static pthread_once_t init_once = PTHREAD_ONCE_INIT; |
| 48 | RTC_CHECK_EQ(pthread_once(&init_once, &InitializeTls), 0); |
| 49 | return g_current_yield_policy_tls; |
| 50 | } |
| 51 |
| 52 | YieldInterface* GetCurrentYieldPolicy() { |
| 53 | return static_cast<YieldInterface*>( |
| 54 | pthread_getspecific(GetCurrentYieldPolicyTls())); |
| 55 | } |
| 56 |
| 57 | void SetCurrentYieldPolicy(YieldInterface* ptr) { |
| 58 | pthread_setspecific(GetCurrentYieldPolicyTls(), ptr); |
| 59 | } |
| 60 |
| 61 | #else |
| 62 | #error Unsupported platform |
| 63 | #endif |
| 64 |
| 65 | } // namespace |
| 66 |
| 67 | ScopedYieldPolicy::ScopedYieldPolicy(YieldInterface* policy) |
| 68 | : previous_(GetCurrentYieldPolicy()) { |
| 69 | SetCurrentYieldPolicy(policy); |
| 70 | } |
| 71 |
| 72 | ScopedYieldPolicy::~ScopedYieldPolicy() { |
| 73 | SetCurrentYieldPolicy(previous_); |
| 74 | } |
| 75 |
| 76 | void ScopedYieldPolicy::YieldExecution() { |
| 77 | YieldInterface* current = GetCurrentYieldPolicy(); |
| 78 | if (current) |
| 79 | current->YieldExecution(); |
| 80 | } |
| 81 |
| 82 | } // namespace rtc |
| 83 |