Ring Daemon
Loading...
Searching...
No Matches
threadloop.cpp
Go to the documentation of this file.
1
/*
2
* Copyright (C) 2004-2026 Savoir-faire Linux Inc.
3
*
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation, either version 3 of the License, or
7
* (at your option) any later version.
8
*
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
13
*
14
* You should have received a copy of the GNU General Public License
15
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16
*/
17
18
#include "
threadloop.h
"
19
#include "
logger.h
"
20
21
namespace
jami
{
22
23
void
24
ThreadLoop::mainloop(std::thread::id&
tid
,
25
const
std::function<
bool
()>& setup,
26
const
std::function<
void
()>& process,
27
const
std::function<
void
()>& cleanup)
28
{
29
tid
= std::this_thread::get_id();
30
try
{
31
if
(setup()) {
32
while
(state_ ==
ThreadState::RUNNING
)
33
process();
34
cleanup();
35
}
else
{
36
JAMI_ERR
(
"setup failed"
);
37
}
38
}
catch
(
const
ThreadLoopException&
e
) {
39
JAMI_ERR
(
"[threadloop:%p] ThreadLoopException: %s"
,
this
,
e
.what());
40
}
catch
(
const
std::exception&
e
) {
41
JAMI_ERR
(
"[threadloop:%p] Unwaited exception: %s"
,
this
,
e
.what());
42
}
43
stop
();
44
}
45
46
ThreadLoop::ThreadLoop
(
const
std::function<
bool
()>& setup,
47
const
std::function<
void
()>& process,
48
const
std::function<
void
()>& cleanup)
49
: setup_(setup)
50
, process_(process)
51
, cleanup_(cleanup)
52
, thread_()
53
{}
54
55
ThreadLoop::~ThreadLoop
()
56
{
57
if
(
isRunning
()) {
58
JAMI_ERR
(
"join() should be explicitly called in owner's destructor"
);
59
join
();
60
}
61
}
62
63
void
64
ThreadLoop::start
()
65
{
66
const
auto
s = state_.load();
67
68
if
(s ==
ThreadState::RUNNING
) {
69
JAMI_ERR
(
"already started"
);
70
return
;
71
}
72
73
// stop pending but not processed by thread yet?
74
if
(s ==
ThreadState::STOPPING
and
thread_.joinable()) {
75
JAMI_DBG
(
"stop pending"
);
76
thread_.join();
77
}
78
79
state_ =
ThreadState::RUNNING
;
80
thread_ = std::thread(&ThreadLoop::mainloop,
this
, std::ref(threadId_), setup_, process_, cleanup_);
81
threadId_ = thread_.get_id();
82
}
83
84
void
85
ThreadLoop::stop
()
86
{
87
auto
expected
=
ThreadState::RUNNING
;
88
state_.compare_exchange_strong(
expected
,
ThreadState::STOPPING
);
89
}
90
91
void
92
ThreadLoop::join
()
93
{
94
stop
();
95
if
(thread_.joinable())
96
thread_.join();
97
}
98
99
void
100
ThreadLoop::waitForCompletion
()
101
{
102
if
(thread_.joinable())
103
thread_.join();
104
}
105
106
void
107
ThreadLoop::exit
()
108
{
109
stop
();
110
throw
ThreadLoopException
();
111
}
112
113
bool
114
ThreadLoop::isRunning
()
const
noexcept
115
{
116
#ifdef _WIN32
117
return
state_ ==
ThreadState::RUNNING
;
118
#else
119
return
thread_.joinable()
and
state_ ==
ThreadState::RUNNING
;
120
#endif
121
}
122
123
void
124
InterruptedThreadLoop::stop
()
125
{
126
ThreadLoop::stop
();
127
cv_.notify_one();
128
}
129
}
// namespace jami
jami::InterruptedThreadLoop::stop
void stop() override
Definition
threadloop.cpp:124
jami::ThreadLoop::join
void join()
Definition
threadloop.cpp:92
jami::ThreadLoop::start
void start()
Definition
threadloop.cpp:64
jami::ThreadLoop::waitForCompletion
void waitForCompletion()
Definition
threadloop.cpp:100
jami::ThreadLoop::isRunning
bool isRunning() const noexcept
Definition
threadloop.cpp:114
jami::ThreadLoop::~ThreadLoop
virtual ~ThreadLoop()
Definition
threadloop.cpp:55
jami::ThreadLoop::ThreadState::RUNNING
@ RUNNING
jami::ThreadLoop::ThreadState::STOPPING
@ STOPPING
jami::ThreadLoop::ThreadLoop
ThreadLoop(const std::function< bool()> &setup, const std::function< void()> &process, const std::function< void()> &cleanup)
Definition
threadloop.cpp:46
jami::ThreadLoop::stop
virtual void stop()
Definition
threadloop.cpp:85
jami::ThreadLoop::exit
void exit()
Definition
threadloop.cpp:107
logger.h
JAMI_ERR
#define JAMI_ERR(...)
Definition
logger.h:230
JAMI_DBG
#define JAMI_DBG(...)
Definition
logger.h:228
jami
Definition
account.cpp:56
jami::emitSignal
void emitSignal(Args... args)
Definition
jami_signal.h:64
jami::ThreadLoopException
Definition
threadloop.h:29
threadloop.h
src
threadloop.cpp
Generated on Fri Apr 3 2026 14:23:10 for Ring Daemon by
1.9.8