blob: ca1b56d13d7f722bcb5828dc7cfd2b0180bd784a (
plain)
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
|
// Copyright 2018 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/threading/platform_thread.h"
#include "base/no_destructor.h"
#include "base/task/current_thread.h"
#include "base/threading/thread_local_storage.h"
namespace base {
namespace {
// Returns ThreadLocalStorage slot used to store type of the current thread.
// The value is stored as an integer value converted to a pointer. 1 is added to
// the integer value in order to distinguish the case when the TLS slot is not
// initialized.
base::ThreadLocalStorage::Slot* GetThreadTypeTlsSlot() {
static base::NoDestructor<base::ThreadLocalStorage::Slot> tls_slot;
return tls_slot.get();
}
void SaveThreadTypeToTls(ThreadType thread_type) {
GetThreadTypeTlsSlot()->Set(
reinterpret_cast<void*>(static_cast<uintptr_t>(thread_type) + 1));
}
ThreadType GetThreadTypeFromTls() {
uintptr_t value = reinterpret_cast<uintptr_t>(GetThreadTypeTlsSlot()->Get());
// Thread type is set to kNormal by default.
if (value == 0)
return ThreadType::kDefault;
DCHECK_LE(value - 1, static_cast<uintptr_t>(ThreadType::kMaxValue));
return static_cast<ThreadType>(value - 1);
}
} // namespace
// static
void PlatformThread::SetCurrentThreadType(ThreadType thread_type) {
MessagePumpType message_pump_type = MessagePumpType::DEFAULT;
if (CurrentIOThread::IsSet()) {
message_pump_type = MessagePumpType::IO;
}
#if !BUILDFLAG(IS_NACL)
else if (CurrentUIThread::IsSet()) {
message_pump_type = MessagePumpType::UI;
}
#endif
internal::SetCurrentThreadType(thread_type, message_pump_type);
}
// static
ThreadType PlatformThread::GetCurrentThreadType() {
return GetThreadTypeFromTls();
}
namespace internal {
void SetCurrentThreadType(ThreadType thread_type,
MessagePumpType pump_type_hint) {
SetCurrentThreadTypeImpl(thread_type, pump_type_hint);
SaveThreadTypeToTls(thread_type);
}
} // namespace internal
} // namespace base
|