-
Notifications
You must be signed in to change notification settings - Fork 1
/
ks_apartment.cpp
112 lines (88 loc) · 3.55 KB
/
ks_apartment.cpp
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
112
/* Copyright 2024 The Kingsoft's ks-async Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#include "ks_apartment.h"
#include "ks_single_thread_apartment_imp.h"
#include "ks_thread_pool_apartment_imp.h"
#include <thread>
void __forcelink_to_ks_apartment_cpp() {}
static ks_apartment* g_ui_sta = nullptr;
static ks_apartment* g_master_sta = nullptr;
static thread_local ks_apartment* tls_current_thread_apartment = nullptr;
ks_apartment* ks_apartment::ui_sta() {
ASSERT(g_ui_sta != nullptr);
return g_ui_sta;
}
ks_apartment* ks_apartment::master_sta() {
ASSERT(g_master_sta != nullptr);
return g_master_sta;
}
ks_apartment* ks_apartment::background_sta() {
static ks_single_thread_apartment_imp g_background_sta(true);
return &g_background_sta;
}
ks_apartment* ks_apartment::default_mta() {
struct _default_mta_options {
static size_t max_thread_count() {
size_t cpu_count = (size_t)std::thread::hardware_concurrency();
size_t max_thread_count = cpu_count * 2;
if (max_thread_count < 4)
max_thread_count = 4;
return max_thread_count;
}
};
static ks_thread_pool_apartment_imp g_default_mta(_default_mta_options::max_thread_count());
return &g_default_mta;
}
ks_apartment* ks_apartment::current_thread_apartment() {
return tls_current_thread_apartment;
}
ks_apartment* ks_apartment::current_thread_apartment_or_default_mta() {
ks_apartment* cur_apartment = tls_current_thread_apartment;
if (cur_apartment == nullptr)
cur_apartment = default_mta();
return cur_apartment;
}
ks_apartment* ks_apartment::current_thread_apartment_or(ks_apartment* or_apartment) {
ks_apartment* cur_apartment = tls_current_thread_apartment;
if (cur_apartment == nullptr)
cur_apartment = or_apartment;
return cur_apartment;
}
bool ks_apartment::__current_thread_apartment_try_pump_once() {
ks_apartment* cur_apartment = tls_current_thread_apartment;
ASSERT(cur_apartment != nullptr);
return cur_apartment->__try_pump_once();
}
void ks_apartment::__set_ui_sta(ks_apartment* ui_sta) {
ASSERT(g_ui_sta == nullptr || ui_sta == nullptr);
ASSERT(g_ui_sta != nullptr || ui_sta != nullptr);
g_ui_sta = ui_sta;
}
void ks_apartment::__set_master_sta(ks_apartment* master_sta) {
ASSERT(g_master_sta == nullptr || master_sta == nullptr);
ASSERT(g_master_sta != nullptr || master_sta != nullptr);
g_master_sta = master_sta;
}
void ks_apartment::__tls_set_current_thread_apartment(ks_apartment* current_thread_apartment) {
ASSERT(tls_current_thread_apartment == nullptr || current_thread_apartment == nullptr);
ASSERT(tls_current_thread_apartment != nullptr || current_thread_apartment != nullptr);
tls_current_thread_apartment = current_thread_apartment;
}
ks_apartment* ks_apartment::__get_ui_sta() {
return g_ui_sta;
}
ks_apartment* ks_apartment::__get_master_sta() {
return g_master_sta;
}
ks_apartment* ks_apartment::__tls_get_current_thread_apartment() {
return tls_current_thread_apartment;
}