Danil Chapovalov | 348b08a | 2019-01-17 13:07:25 +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 "api/task_queue/task_queue_base.h" |
| 11 | |
| 12 | #include "absl/base/attributes.h" |
Danil Chapovalov | 47cf5ea | 2019-02-19 20:20:16 +0100 | [diff] [blame] | 13 | #include "absl/base/config.h" |
Danil Chapovalov | 8feb6fd | 2022-07-05 11:01:27 +0200 | [diff] [blame] | 14 | #include "absl/functional/any_invocable.h" |
| 15 | #include "api/units/time_delta.h" |
Danil Chapovalov | 47cf5ea | 2019-02-19 20:20:16 +0100 | [diff] [blame] | 16 | #include "rtc_base/checks.h" |
| 17 | |
| 18 | #if defined(ABSL_HAVE_THREAD_LOCAL) |
Danil Chapovalov | 348b08a | 2019-01-17 13:07:25 +0100 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | namespace { |
| 22 | |
| 23 | ABSL_CONST_INIT thread_local TaskQueueBase* current = nullptr; |
| 24 | |
| 25 | } // namespace |
| 26 | |
| 27 | TaskQueueBase* TaskQueueBase::Current() { |
| 28 | return current; |
| 29 | } |
| 30 | |
| 31 | TaskQueueBase::CurrentTaskQueueSetter::CurrentTaskQueueSetter( |
| 32 | TaskQueueBase* task_queue) |
| 33 | : previous_(current) { |
| 34 | current = task_queue; |
| 35 | } |
| 36 | |
| 37 | TaskQueueBase::CurrentTaskQueueSetter::~CurrentTaskQueueSetter() { |
| 38 | current = previous_; |
| 39 | } |
Danil Chapovalov | 47cf5ea | 2019-02-19 20:20:16 +0100 | [diff] [blame] | 40 | } // namespace webrtc |
| 41 | |
| 42 | #elif defined(WEBRTC_POSIX) |
| 43 | |
| 44 | #include <pthread.h> |
| 45 | |
| 46 | namespace webrtc { |
| 47 | namespace { |
| 48 | |
| 49 | ABSL_CONST_INIT pthread_key_t g_queue_ptr_tls = 0; |
| 50 | |
| 51 | void InitializeTls() { |
| 52 | RTC_CHECK(pthread_key_create(&g_queue_ptr_tls, nullptr) == 0); |
| 53 | } |
| 54 | |
| 55 | pthread_key_t GetQueuePtrTls() { |
| 56 | static pthread_once_t init_once = PTHREAD_ONCE_INIT; |
| 57 | RTC_CHECK(pthread_once(&init_once, &InitializeTls) == 0); |
| 58 | return g_queue_ptr_tls; |
| 59 | } |
| 60 | |
| 61 | } // namespace |
| 62 | |
| 63 | TaskQueueBase* TaskQueueBase::Current() { |
| 64 | return static_cast<TaskQueueBase*>(pthread_getspecific(GetQueuePtrTls())); |
| 65 | } |
| 66 | |
| 67 | TaskQueueBase::CurrentTaskQueueSetter::CurrentTaskQueueSetter( |
| 68 | TaskQueueBase* task_queue) |
| 69 | : previous_(TaskQueueBase::Current()) { |
| 70 | pthread_setspecific(GetQueuePtrTls(), task_queue); |
| 71 | } |
| 72 | |
| 73 | TaskQueueBase::CurrentTaskQueueSetter::~CurrentTaskQueueSetter() { |
| 74 | pthread_setspecific(GetQueuePtrTls(), previous_); |
| 75 | } |
Danil Chapovalov | 348b08a | 2019-01-17 13:07:25 +0100 | [diff] [blame] | 76 | |
| 77 | } // namespace webrtc |
Danil Chapovalov | 47cf5ea | 2019-02-19 20:20:16 +0100 | [diff] [blame] | 78 | |
| 79 | #else |
| 80 | #error Unsupported platform |
| 81 | #endif |