• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"
13 #include "absl/base/config.h"
14 #include "rtc_base/checks.h"
15 
16 #if defined(ABSL_HAVE_THREAD_LOCAL)
17 
18 namespace webrtc {
19 namespace {
20 
21 ABSL_CONST_INIT thread_local TaskQueueBase* current = nullptr;
22 
23 }  // namespace
24 
Current()25 TaskQueueBase* TaskQueueBase::Current() {
26   return current;
27 }
28 
CurrentTaskQueueSetter(TaskQueueBase * task_queue)29 TaskQueueBase::CurrentTaskQueueSetter::CurrentTaskQueueSetter(
30     TaskQueueBase* task_queue)
31     : previous_(current) {
32   current = task_queue;
33 }
34 
~CurrentTaskQueueSetter()35 TaskQueueBase::CurrentTaskQueueSetter::~CurrentTaskQueueSetter() {
36   current = previous_;
37 }
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 
InitializeTls()49 void InitializeTls() {
50   RTC_CHECK(pthread_key_create(&g_queue_ptr_tls, nullptr) == 0);
51 }
52 
GetQueuePtrTls()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 
Current()61 TaskQueueBase* TaskQueueBase::Current() {
62   return static_cast<TaskQueueBase*>(pthread_getspecific(GetQueuePtrTls()));
63 }
64 
CurrentTaskQueueSetter(TaskQueueBase * task_queue)65 TaskQueueBase::CurrentTaskQueueSetter::CurrentTaskQueueSetter(
66     TaskQueueBase* task_queue)
67     : previous_(TaskQueueBase::Current()) {
68   pthread_setspecific(GetQueuePtrTls(), task_queue);
69 }
70 
~CurrentTaskQueueSetter()71 TaskQueueBase::CurrentTaskQueueSetter::~CurrentTaskQueueSetter() {
72   pthread_setspecific(GetQueuePtrTls(), previous_);
73 }
74 
75 }  // namespace webrtc
76 
77 #else
78 #error Unsupported platform
79 #endif
80