Skip to content

Commit 1f33d5f

Browse files
committed
Add shared_run_loop_thread
1 parent d80713a commit 1f33d5f

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

Diff for: include/pqrs/cf/run_loop_thread.hpp

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ namespace pqrs {
1616
namespace cf {
1717
class run_loop_thread final {
1818
public:
19+
class extra {
20+
public:
21+
#include "run_loop_thread/extra/shared_run_loop_thread.hpp"
22+
};
23+
1924
run_loop_thread(const run_loop_thread&) = delete;
2025

2126
run_loop_thread(void) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#pragma once
2+
3+
// (C) Copyright Takayama Fumihiko 2023.
4+
// Distributed under the Boost Software License, Version 1.0.
5+
// (See https://www.boost.org/LICENSE_1_0.txt)
6+
7+
// `pqrs::cf::run_loop_thread::extra::shared_run_loop_thread` can be used safely in a multi-threaded environment.
8+
9+
// namespace pqrs {
10+
// namespace cf {
11+
// class run_loop_thread {
12+
// class extra {
13+
14+
class shared_run_loop_thread final {
15+
public:
16+
void initialize(void) {
17+
std::lock_guard<std::mutex> lock(mutex_);
18+
19+
if (!run_loop_thread_) {
20+
run_loop_thread_ = std::make_shared<run_loop_thread>();
21+
}
22+
}
23+
24+
void terminate(void) {
25+
std::lock_guard<std::mutex> lock(mutex_);
26+
27+
if (run_loop_thread_) {
28+
run_loop_thread_->terminate();
29+
run_loop_thread_ = nullptr;
30+
}
31+
}
32+
33+
std::shared_ptr<run_loop_thread> get_run_loop_thread(void) const {
34+
return run_loop_thread_;
35+
}
36+
37+
static std::shared_ptr<shared_run_loop_thread> get_shared_run_loop_thread(void) {
38+
static std::mutex mutex;
39+
std::lock_guard<std::mutex> lock(mutex);
40+
41+
static std::shared_ptr<shared_run_loop_thread> p;
42+
if (!p) {
43+
p = std::make_shared<shared_run_loop_thread>();
44+
}
45+
46+
return p;
47+
}
48+
49+
private:
50+
std::shared_ptr<run_loop_thread> run_loop_thread_;
51+
mutable std::mutex mutex_;
52+
};
53+
54+
static void initialize_shared_run_loop_thread(void) {
55+
auto p = shared_run_loop_thread::get_shared_run_loop_thread();
56+
p->initialize();
57+
}
58+
59+
static void terminate_shared_run_loop_thread(void) {
60+
auto p = shared_run_loop_thread::get_shared_run_loop_thread();
61+
p->terminate();
62+
}
63+
64+
static std::shared_ptr<run_loop_thread> get_shared_run_loop_thread(void) {
65+
auto p = shared_run_loop_thread::get_shared_run_loop_thread();
66+
return p->get_run_loop_thread();
67+
}

Diff for: tests/src/test.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,18 @@ int main(void) {
128128
std::cout << "never reached" << std::endl;
129129
});
130130
};
131+
132+
"shared_run_loop_thread"_test = [] {
133+
pqrs::cf::run_loop_thread::extra::initialize_shared_run_loop_thread();
134+
135+
int __block count = 0;
136+
137+
pqrs::cf::run_loop_thread::extra::get_shared_run_loop_thread()->enqueue(^{
138+
++count;
139+
});
140+
141+
pqrs::cf::run_loop_thread::extra::terminate_shared_run_loop_thread();
142+
143+
expect(count == 1_i);
144+
};
131145
}

0 commit comments

Comments
 (0)