blob: d883d42bf458573dcdbd069fbabd40c24da90fde [file] [log] [blame]
Sebastian Jansson7a603392019-03-20 16:50:35 +01001/*
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 Antonfefa77c2019-07-19 07:03:03 -070013#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 Jansson7a603392019-03-20 16:50:35 +010018
19namespace rtc {
20namespace {
Steve Antonfefa77c2019-07-19 07:03:03 -070021
22#if defined(ABSL_HAVE_THREAD_LOCAL)
23
Sebastian Jansson7a603392019-03-20 16:50:35 +010024ABSL_CONST_INIT thread_local YieldInterface* current_yield_policy = nullptr;
Steve Antonfefa77c2019-07-19 07:03:03 -070025
26YieldInterface* GetCurrentYieldPolicy() {
27 return current_yield_policy;
Sebastian Jansson7a603392019-03-20 16:50:35 +010028}
29
Steve Antonfefa77c2019-07-19 07:03:03 -070030void 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
40ABSL_CONST_INIT pthread_key_t g_current_yield_policy_tls = 0;
41
42void InitializeTls() {
43 RTC_CHECK_EQ(pthread_key_create(&g_current_yield_policy_tls, nullptr), 0);
44}
45
46pthread_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
52YieldInterface* GetCurrentYieldPolicy() {
53 return static_cast<YieldInterface*>(
54 pthread_getspecific(GetCurrentYieldPolicyTls()));
55}
56
57void SetCurrentYieldPolicy(YieldInterface* ptr) {
58 pthread_setspecific(GetCurrentYieldPolicyTls(), ptr);
59}
60
61#else
62#error Unsupported platform
63#endif
64
65} // namespace
66
Sebastian Jansson7a603392019-03-20 16:50:35 +010067ScopedYieldPolicy::ScopedYieldPolicy(YieldInterface* policy)
Steve Antonfefa77c2019-07-19 07:03:03 -070068 : previous_(GetCurrentYieldPolicy()) {
69 SetCurrentYieldPolicy(policy);
Sebastian Jansson7a603392019-03-20 16:50:35 +010070}
71
72ScopedYieldPolicy::~ScopedYieldPolicy() {
Steve Antonfefa77c2019-07-19 07:03:03 -070073 SetCurrentYieldPolicy(previous_);
Sebastian Jansson7a603392019-03-20 16:50:35 +010074}
75
76void ScopedYieldPolicy::YieldExecution() {
Steve Antonfefa77c2019-07-19 07:03:03 -070077 YieldInterface* current = GetCurrentYieldPolicy();
78 if (current)
79 current->YieldExecution();
Sebastian Jansson7a603392019-03-20 16:50:35 +010080}
Steve Antonfefa77c2019-07-19 07:03:03 -070081
Sebastian Jansson7a603392019-03-20 16:50:35 +010082} // namespace rtc