-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathThreadPoolTaskScheduler.cpp
More file actions
162 lines (135 loc) · 4.69 KB
/
ThreadPoolTaskScheduler.cpp
File metadata and controls
162 lines (135 loc) · 4.69 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
/*
* Copyright (C) 2019-2022 HERE Europe B.V.
*
* 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.
*
* SPDX-License-Identifier: Apache-2.0
* License-Filename: LICENSE
*/
#include "olp/core/thread/ThreadPoolTaskScheduler.h"
#if defined(PORTING_PLATFORM_QNX)
#include <process.h>
#elif defined(PORTING_PLATFORM_MAC)
#include <pthread.h>
#elif defined(PORTING_PLATFORM_LINUX) || defined(PORTING_PLATFORM_ANDROID)
#ifndef _GNU_SOURCE
#define _GNU_SOURCE 1
#endif
#include <pthread.h>
#endif
#include <string>
#include "olp/core/logging/Log.h"
#include "olp/core/porting/make_unique.h"
#include "olp/core/porting/platform.h"
#include "olp/core/thread/SyncQueue.h"
#include "olp/core/utils/WarningWorkarounds.h"
#include "thread/PriorityQueueExtended.h"
namespace olp {
namespace thread {
namespace {
constexpr auto kLogTag = "ThreadPoolTaskScheduler";
void SetCurrentThreadName(const std::string& thread_name) {
// Currently only supported for pthread users
OLP_SDK_CORE_UNUSED(thread_name);
#if defined(PORTING_PLATFORM_MAC)
// Note that in Mac based systems the pthread_setname_np takes 1 argument
// only.
pthread_setname_np(thread_name.c_str());
#elif defined(OLP_SDK_HAVE_PTHREAD_SETNAME_NP) // Linux, Android, QNX
// QNX allows 100 but Linux only 16 so select min value and apply for both.
// If maximum length is exceeded on some systems, e.g. Linux, the name is not
// set at all. So better truncate it to have at least the minimum set.
constexpr size_t kMaxThreadNameLength = 16u;
std::string truncated_name = thread_name.substr(0, kMaxThreadNameLength - 1);
pthread_setname_np(pthread_self(), truncated_name.c_str());
#endif // OLP_SDK_HAVE_PTHREAD_SETNAME_NP
}
struct PrioritizedTask {
TaskScheduler::CallFuncType function;
uint32_t priority;
};
struct ComparePrioritizedTask {
bool operator()(const PrioritizedTask& lhs,
const PrioritizedTask& rhs) const {
return lhs.priority < rhs.priority;
}
};
void SetExecutorName(std::string name) {
std::string thread_name = "OLPSDKPOOL_" + std::move(name);
SetCurrentThreadName(thread_name);
OLP_SDK_LOG_INFO_F(kLogTag, "Starting thread '%s'", thread_name.c_str());
}
} // namespace
class ThreadPoolTaskScheduler::QueueImpl {
public:
using ElementType = PrioritizedTask;
bool Pull(ElementType& element) { return sync_queue_.Pull(element); }
void Push(ElementType&& element) { sync_queue_.Push(std::move(element)); }
void Close() { sync_queue_.Close(); }
private:
using PriorityQueue =
PriorityQueueExtended<ElementType, ComparePrioritizedTask>;
SyncQueue<ElementType, PriorityQueue> sync_queue_;
};
ThreadPoolTaskScheduler::ThreadPoolTaskScheduler(size_t thread_count)
: queue_{std::make_unique<QueueImpl>()},
cancel_queue_{std::make_unique<QueueImpl>()} {
constexpr auto kCancelThreadsCount = 1;
thread_pool_.reserve(thread_count + kCancelThreadsCount);
for (size_t idx = 0; idx < thread_count; ++idx) {
std::thread executor([this, idx]() {
SetExecutorName(std::to_string(idx));
for (;;) {
PrioritizedTask task;
if (!queue_->Pull(task)) {
return;
}
task.function();
}
});
thread_pool_.push_back(std::move(executor));
}
for (size_t idx = 0; idx < kCancelThreadsCount; ++idx) {
thread_pool_.emplace_back([this, idx] {
SetExecutorName("CANCEL_" + std::to_string(idx));
for (;;) {
PrioritizedTask task;
if (!cancel_queue_->Pull(task)) {
return;
}
task.function();
}
});
}
}
ThreadPoolTaskScheduler::~ThreadPoolTaskScheduler() {
queue_->Close();
cancel_queue_->Close();
for (auto& thread : thread_pool_) {
thread.join();
}
thread_pool_.clear();
}
void ThreadPoolTaskScheduler::EnqueueTask(TaskScheduler::CallFuncType&& func) {
queue_->Push({std::move(func), thread::NORMAL});
}
void ThreadPoolTaskScheduler::EnqueueCancelTask(
TaskScheduler::CallFuncType&& func) {
cancel_queue_->Push({std::move(func), thread::NORMAL});
}
void ThreadPoolTaskScheduler::EnqueueTask(TaskScheduler::CallFuncType&& func,
uint32_t priority) {
queue_->Push({std::move(func), priority});
}
} // namespace thread
} // namespace olp