Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Toggle main menu visibility
Loading...
Searching...
No Matches
BoostAsioThreadGroup.h
Go to the documentation of this file.
1
/*
2
* This file is part of Project SkyFire https://www.projectskyfire.org.
3
* See LICENSE.md file for Copyright information
4
*/
5
6
#ifndef SF_BOOST_ASIO_THREAD_GROUP_H
7
#define SF_BOOST_ASIO_THREAD_GROUP_H
8
9
#include "
Threading/BoostAsioThread.h
"
10
11
#include <atomic>
12
#include <cstddef>
13
#include <functional>
14
#include <memory>
15
#include <utility>
16
#include <vector>
17
18
namespace
Skyfire
19
{
20
namespace
Asio
21
{
22
class
IoContextThreadGroup
23
{
24
public
:
25
typedef
std::function<void()>
Hook
;
26
27
IoContextThreadGroup
() :
_executor
(),
_running
(false) { }
28
29
~IoContextThreadGroup
()
30
{
31
StopAndJoin
();
32
}
33
34
IoContextExecutor
&
GetExecutor
() {
return
_executor
; }
35
boost::asio::io_context&
GetIoContext
() {
return
_executor
.GetIoContext(); }
36
bool
IsRunning
()
const
{
return
_running
; }
37
38
int
Start
(std::size_t threadCount,
Hook
preRun =
Hook
(),
Hook
postRun =
Hook
())
39
{
40
if
(threadCount == 0)
41
return
-1;
42
43
bool
expected =
false
;
44
if
(!
_running
.compare_exchange_strong(expected,
true
))
45
return
-1;
46
47
_executor
.Restart();
48
_executor
.KeepAlive();
49
_preRun
= std::move(preRun);
50
_postRun
= std::move(postRun);
51
52
try
53
{
54
for
(std::size_t i = 0; i < threadCount; ++i)
55
{
56
std::unique_ptr<IoContextThread> thread(
new
IoContextThread
);
57
if
(thread->Start(
_executor
,
_preRun
,
_postRun
) == -1)
58
{
59
StopAndJoin
();
60
return
-1;
61
}
62
63
_threads
.push_back(std::move(thread));
64
}
65
}
66
catch
(...)
67
{
68
StopAndJoin
();
69
return
-1;
70
}
71
72
return
0;
73
}
74
75
void
Drain
()
76
{
77
_executor
.ResetWork();
78
}
79
80
void
Stop
()
81
{
82
_executor
.Stop();
83
_executor
.ResetWork();
84
}
85
86
void
Join
()
87
{
88
for
(std::unique_ptr<IoContextThread>& thread :
_threads
)
89
thread->Join();
90
91
_threads
.clear();
92
_preRun
=
Hook
();
93
_postRun
=
Hook
();
94
_running
=
false
;
95
}
96
97
void
DrainAndJoin
()
98
{
99
Drain
();
100
Join
();
101
}
102
103
void
StopAndJoin
()
104
{
105
Stop
();
106
Join
();
107
}
108
109
private
:
110
IoContextExecutor
_executor
;
111
std::vector<std::unique_ptr<IoContextThread> >
_threads
;
112
Hook
_preRun
;
113
Hook
_postRun
;
114
std::atomic<bool>
_running
;
115
};
116
}
117
}
118
119
#endif
BoostAsioThread.h
Skyfire::Asio::IoContextExecutor
Definition
BoostAsioExecutor.h:23
Skyfire::Asio::IoContextThreadGroup::DrainAndJoin
void DrainAndJoin()
Definition
BoostAsioThreadGroup.h:97
Skyfire::Asio::IoContextThreadGroup::_postRun
Hook _postRun
Definition
BoostAsioThreadGroup.h:113
Skyfire::Asio::IoContextThreadGroup::Drain
void Drain()
Definition
BoostAsioThreadGroup.h:75
Skyfire::Asio::IoContextThreadGroup::_threads
std::vector< std::unique_ptr< IoContextThread > > _threads
Definition
BoostAsioThreadGroup.h:111
Skyfire::Asio::IoContextThreadGroup::Start
int Start(std::size_t threadCount, Hook preRun=Hook(), Hook postRun=Hook())
Definition
BoostAsioThreadGroup.h:38
Skyfire::Asio::IoContextThreadGroup::~IoContextThreadGroup
~IoContextThreadGroup()
Definition
BoostAsioThreadGroup.h:29
Skyfire::Asio::IoContextThreadGroup::StopAndJoin
void StopAndJoin()
Definition
BoostAsioThreadGroup.h:103
Skyfire::Asio::IoContextThreadGroup::_preRun
Hook _preRun
Definition
BoostAsioThreadGroup.h:112
Skyfire::Asio::IoContextThreadGroup::GetExecutor
IoContextExecutor & GetExecutor()
Definition
BoostAsioThreadGroup.h:34
Skyfire::Asio::IoContextThreadGroup::GetIoContext
boost::asio::io_context & GetIoContext()
Definition
BoostAsioThreadGroup.h:35
Skyfire::Asio::IoContextThreadGroup::Stop
void Stop()
Definition
BoostAsioThreadGroup.h:80
Skyfire::Asio::IoContextThreadGroup::IoContextThreadGroup
IoContextThreadGroup()
Definition
BoostAsioThreadGroup.h:27
Skyfire::Asio::IoContextThreadGroup::Join
void Join()
Definition
BoostAsioThreadGroup.h:86
Skyfire::Asio::IoContextThreadGroup::_executor
IoContextExecutor _executor
Definition
BoostAsioThreadGroup.h:110
Skyfire::Asio::IoContextThreadGroup::Hook
std::function< void()> Hook
Definition
BoostAsioThreadGroup.h:25
Skyfire::Asio::IoContextThreadGroup::_running
std::atomic< bool > _running
Definition
BoostAsioThreadGroup.h:114
Skyfire::Asio::IoContextThreadGroup::IsRunning
bool IsRunning() const
Definition
BoostAsioThreadGroup.h:36
Skyfire::Asio::IoContextThread
Definition
BoostAsioThread.h:21
Skyfire::Asio
Definition
DatabaseQueue.h:18
Skyfire
Definition
AuthPatchTransfer.h:12
src
server
shared
Threading
BoostAsioThreadGroup.h
Generated on
for Project SkyFire Core by
1.17.0