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