-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathwaiters.h
More file actions
214 lines (166 loc) · 5.92 KB
/
waiters.h
File metadata and controls
214 lines (166 loc) · 5.92 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
Copyright (c) 2005-2024 Intel Corporation
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.
*/
#ifndef _TBB_waiters_H
#define _TBB_waiters_H
#include "oneapi/tbb/detail/_task.h"
#include "scheduler_common.h"
#include "arena.h"
#include "threading_control.h"
namespace tbb {
namespace detail {
namespace r1 {
inline d1::task* get_self_recall_task(arena_slot& slot);
class waiter_base {
public:
waiter_base(arena& a, int yields_multiplier = 1) : my_arena(a), my_backoff(int(a.my_num_slots), yields_multiplier) {}
bool pause() {
if (my_backoff.pause()) {
my_arena.out_of_work();
return true;
}
return false;
}
void reset_wait() {
my_backoff.reset_wait();
}
protected:
arena& my_arena;
stealing_loop_backoff my_backoff;
};
class outermost_worker_waiter : public waiter_base {
public:
using waiter_base::waiter_base;
bool continue_execution(arena_slot& slot, d1::task*& t) const {
__TBB_ASSERT(t == nullptr, nullptr);
if (is_worker_should_leave(slot)) {
if (!governor::hybrid_cpu()) {
static constexpr std::chrono::microseconds worker_wait_leave_duration(1000);
static_assert(worker_wait_leave_duration > std::chrono::steady_clock::duration(1), "Clock resolution is not enough for measured interval.");
for (auto t1 = std::chrono::steady_clock::now(), t2 = t1;
std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1) < worker_wait_leave_duration;
t2 = std::chrono::steady_clock::now())
{
if (!my_arena.is_empty() && !my_arena.is_recall_requested()) {
return true;
}
if (my_arena.my_threading_control->is_any_other_client_active()) {
break;
}
d0::yield();
}
}
// Leave dispatch loop
return false;
}
t = get_self_recall_task(slot);
return true;
}
void pause(arena_slot&) {
waiter_base::pause();
}
d1::wait_context* wait_ctx() {
return nullptr;
}
static bool postpone_execution(d1::task&) {
return false;
}
private:
using base_type = waiter_base;
bool is_worker_should_leave(arena_slot& slot) const {
bool is_top_priority_arena = my_arena.is_top_priority();
bool is_task_pool_empty = slot.task_pool.load(std::memory_order_relaxed) == EmptyTaskPool;
if (is_top_priority_arena) {
// Worker in most priority arena do not leave arena, until all work in task_pool is done
if (is_task_pool_empty && my_arena.is_recall_requested()) {
return true;
}
} else {
if (my_arena.is_recall_requested()) {
// If worker has work in task pool, we must notify other threads,
// because can appear missed wake up of other threads
if (!is_task_pool_empty) {
my_arena.advertise_new_work<arena::wakeup>();
}
return true;
}
}
return false;
}
};
class sleep_waiter : public waiter_base {
protected:
using waiter_base::waiter_base;
template <typename Pred>
void sleep(std::uintptr_t uniq_tag, Pred wakeup_condition) {
my_arena.get_waiting_threads_monitor().wait<thread_control_monitor::thread_context>(wakeup_condition,
market_context{uniq_tag, &my_arena});
reset_wait();
}
};
class external_waiter : public sleep_waiter {
public:
external_waiter(arena& a, d1::wait_context& wo)
: sleep_waiter(a, /*yields_multiplier*/10), my_wait_ctx(wo)
{}
bool continue_execution(arena_slot& slot, d1::task*& t) const {
__TBB_ASSERT(t == nullptr, nullptr);
if (!my_wait_ctx.continue_execution())
return false;
t = get_self_recall_task(slot);
return true;
}
void pause(arena_slot&) {
if (!sleep_waiter::pause()) {
return;
}
auto wakeup_condition = [&] { return !my_arena.is_empty() || !my_wait_ctx.continue_execution(); };
sleep(std::uintptr_t(&my_wait_ctx), wakeup_condition);
}
d1::wait_context* wait_ctx() {
return &my_wait_ctx;
}
static bool postpone_execution(d1::task&) {
return false;
}
private:
d1::wait_context& my_wait_ctx;
};
#if __TBB_RESUMABLE_TASKS
class coroutine_waiter : public sleep_waiter {
public:
using sleep_waiter::sleep_waiter;
bool continue_execution(arena_slot& slot, d1::task*& t) const {
__TBB_ASSERT(t == nullptr, nullptr);
t = get_self_recall_task(slot);
return true;
}
void pause(arena_slot& slot) {
if (!sleep_waiter::pause()) {
return;
}
suspend_point_type* sp = slot.default_task_dispatcher().m_suspend_point;
auto wakeup_condition = [&] { return !my_arena.is_empty() || sp->m_is_owner_recalled.load(std::memory_order_relaxed); };
sleep(std::uintptr_t(sp), wakeup_condition);
}
d1::wait_context* wait_ctx() {
return nullptr;
}
static bool postpone_execution(d1::task& t) {
return task_accessor::is_resume_task(t);
}
};
#endif // __TBB_RESUMABLE_TASKS
} // namespace r1
} // namespace detail
} // namespace tbb
#endif // _TBB_waiters_H