-
Notifications
You must be signed in to change notification settings - Fork 107
/
scoped_set_task_priority_for_current_thread.cc
111 lines (92 loc) · 3.58 KB
/
scoped_set_task_priority_for_current_thread.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/task/scoped_set_task_priority_for_current_thread.h"
#include "base/compiler_specific.h"
#include "third_party/abseil-cpp/absl/base/attributes.h"
#if defined(STARBOARD)
#include <pthread.h>
#include "base/check_op.h"
#include "starboard/thread.h"
#endif
namespace base {
namespace internal {
namespace {
#if defined(STARBOARD)
ABSL_CONST_INIT pthread_once_t s_once_flag = PTHREAD_ONCE_INIT;
ABSL_CONST_INIT SbThreadLocalKey s_thread_local_key = kSbThreadLocalKeyInvalid;
void InitThreadLocalKey() {
s_thread_local_key = SbThreadCreateLocalKey(NULL);
DCHECK(SbThreadIsValidLocalKey(s_thread_local_key));
}
void EnsureThreadLocalKeyInited() {
pthread_once(&s_once_flag, InitThreadLocalKey);
DCHECK(SbThreadIsValidLocalKey(s_thread_local_key));
}
ABSL_CONST_INIT pthread_once_t s_once_set_flag = PTHREAD_ONCE_INIT;
ABSL_CONST_INIT SbThreadLocalKey s_thread_local_set_for_thread =
kSbThreadLocalKeyInvalid;
void InitThreadLocalBoolKey() {
s_thread_local_set_for_thread = SbThreadCreateLocalKey(NULL);
DCHECK(SbThreadIsValidLocalKey(s_thread_local_set_for_thread));
}
void EnsureThreadLocalBoolKeyInited() {
pthread_once(&s_once_set_flag, InitThreadLocalBoolKey);
DCHECK(SbThreadIsValidLocalKey(s_thread_local_set_for_thread));
}
bool IsValueSetForThread() {
void* set_for_thread = SbThreadGetLocalValue(s_thread_local_set_for_thread);
return !!set_for_thread ? reinterpret_cast<intptr_t>(set_for_thread) != 0
: false;
}
#else
ABSL_CONST_INIT thread_local TaskPriority task_priority_for_current_thread =
TaskPriority::USER_BLOCKING;
#endif
} // namespace
ScopedSetTaskPriorityForCurrentThread::ScopedSetTaskPriorityForCurrentThread(
TaskPriority priority)
#if defined(STARBOARD)
{
EnsureThreadLocalKeyInited();
scoped_reset_value_ = reinterpret_cast<void*>(static_cast<intptr_t>(
static_cast<uint8_t>(GetTaskPriorityForCurrentThread())));
SbThreadSetLocalValue(s_thread_local_key,
reinterpret_cast<void*>(static_cast<intptr_t>(
static_cast<uint8_t>(priority))));
EnsureThreadLocalBoolKeyInited();
SbThreadSetLocalValue(s_thread_local_set_for_thread,
reinterpret_cast<void*>(static_cast<intptr_t>(true)));
}
#else
: resetter_(&task_priority_for_current_thread,
priority,
TaskPriority::USER_BLOCKING) {}
#endif
#if defined(STARBOARD)
ScopedSetTaskPriorityForCurrentThread::
~ScopedSetTaskPriorityForCurrentThread() {
SbThreadSetLocalValue(s_thread_local_key, scoped_reset_value_);
}
#else
ScopedSetTaskPriorityForCurrentThread::
~ScopedSetTaskPriorityForCurrentThread() = default;
#endif
TaskPriority GetTaskPriorityForCurrentThread() {
#if defined(STARBOARD)
void* task_priority_for_current_thread =
SbThreadGetLocalValue(s_thread_local_key);
return IsValueSetForThread() ? static_cast<TaskPriority>(static_cast<uint8_t>(
reinterpret_cast<intptr_t>(
task_priority_for_current_thread)))
: TaskPriority::USER_BLOCKING;
#else
// Workaround false-positive MSAN use-of-uninitialized-value on
// thread_local storage for loaded libraries:
// https://github.com/google/sanitizers/issues/1265
MSAN_UNPOISON(&task_priority_for_current_thread, sizeof(TaskPriority));
return task_priority_for_current_thread;
#endif
}
} // namespace internal
} // namespace base