Sebastian Jansson | 7a60339 | 2019-03-20 16:50:35 +0100 | [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 | #include "rtc_base/synchronization/yield_policy.h" |
| 11 | |
| 12 | #include "absl/base/attributes.h" |
Steve Anton | fefa77c | 2019-07-19 07:03:03 -0700 | [diff] [blame] | 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 |
Sebastian Jansson | 7a60339 | 2019-03-20 16:50:35 +0100 | [diff] [blame] | 18 | |
| 19 | namespace rtc { |
| 20 | namespace { |
Steve Anton | fefa77c | 2019-07-19 07:03:03 -0700 | [diff] [blame] | 21 | |
| 22 | #if defined(ABSL_HAVE_THREAD_LOCAL) |
| 23 | |
Sebastian Jansson | 7a60339 | 2019-03-20 16:50:35 +0100 | [diff] [blame] | 24 | ABSL_CONST_INIT thread_local YieldInterface* current_yield_policy = nullptr; |
Steve Anton | fefa77c | 2019-07-19 07:03:03 -0700 | [diff] [blame] | 25 | |
| 26 | YieldInterface* GetCurrentYieldPolicy() { |
| 27 | return current_yield_policy; |
Sebastian Jansson | 7a60339 | 2019-03-20 16:50:35 +0100 | [diff] [blame] | 28 | } |
| 29 | |
Steve Anton | fefa77c | 2019-07-19 07:03:03 -0700 | [diff] [blame] | 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 | |
Sebastian Jansson | 7a60339 | 2019-03-20 16:50:35 +0100 | [diff] [blame] | 67 | ScopedYieldPolicy::ScopedYieldPolicy(YieldInterface* policy) |
Steve Anton | fefa77c | 2019-07-19 07:03:03 -0700 | [diff] [blame] | 68 | : previous_(GetCurrentYieldPolicy()) { |
| 69 | SetCurrentYieldPolicy(policy); |
Sebastian Jansson | 7a60339 | 2019-03-20 16:50:35 +0100 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | ScopedYieldPolicy::~ScopedYieldPolicy() { |
Steve Anton | fefa77c | 2019-07-19 07:03:03 -0700 | [diff] [blame] | 73 | SetCurrentYieldPolicy(previous_); |
Sebastian Jansson | 7a60339 | 2019-03-20 16:50:35 +0100 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void ScopedYieldPolicy::YieldExecution() { |
Steve Anton | fefa77c | 2019-07-19 07:03:03 -0700 | [diff] [blame] | 77 | YieldInterface* current = GetCurrentYieldPolicy(); |
| 78 | if (current) |
| 79 | current->YieldExecution(); |
Sebastian Jansson | 7a60339 | 2019-03-20 16:50:35 +0100 | [diff] [blame] | 80 | } |
Steve Anton | fefa77c | 2019-07-19 07:03:03 -0700 | [diff] [blame] | 81 | |
Sebastian Jansson | 7a60339 | 2019-03-20 16:50:35 +0100 | [diff] [blame] | 82 | } // namespace rtc |