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 #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 GetCurrentYieldPolicy()26YieldInterface* GetCurrentYieldPolicy() { 27 return current_yield_policy; 28 } 29 SetCurrentYieldPolicy(YieldInterface * ptr)30void 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 ScopedYieldPolicy(YieldInterface * policy)67ScopedYieldPolicy::ScopedYieldPolicy(YieldInterface* policy) 68 : previous_(GetCurrentYieldPolicy()) { 69 SetCurrentYieldPolicy(policy); 70 } 71 ~ScopedYieldPolicy()72ScopedYieldPolicy::~ScopedYieldPolicy() { 73 SetCurrentYieldPolicy(previous_); 74 } 75 YieldExecution()76void ScopedYieldPolicy::YieldExecution() { 77 YieldInterface* current = GetCurrentYieldPolicy(); 78 if (current) 79 current->YieldExecution(); 80 } 81 82 } // namespace rtc 83