The Gaudi Framework
master (37c0b60a)
PeriodicAction.cpp
Go to the documentation of this file.
1
/***********************************************************************************\
2
* (c) Copyright 2024 CERN for the benefit of the LHCb and ATLAS collaborations *
3
* *
4
* This software is distributed under the terms of the Apache version 2 licence, *
5
* copied verbatim in the file "LICENSE". *
6
* *
7
* In applying this licence, CERN does not waive the privileges and immunities *
8
* granted to it by virtue of its status as an Intergovernmental Organization *
9
* or submit itself to any jurisdiction. *
10
\***********************************************************************************/
11
#include <
Gaudi/Utils/PeriodicAction.h
>
12
#include <utility>
13
14
using
Gaudi::Utils::PeriodicAction
;
15
16
PeriodicAction::PeriodicAction(
callback_t
callback
,
std::chrono::milliseconds
period_duration,
bool
autostart )
17
: m_callback{
std::move
(
callback
) }, m_period_duration{ period_duration } {
18
if
( autostart )
start
();
19
}
20
21
PeriodicAction::~PeriodicAction
() {
stop
(); }
22
23
void
PeriodicAction::start
() {
24
if
( !
m_thread
.
joinable
() ) {
25
// Note: we can move the callback because we are not going to use it
26
// outside of the thread
27
m_thread
=
std::thread
{ [period_duration =
m_period_duration
,
callback
=
std::move
(
m_callback
),
28
stop_signal =
m_stop_thread
.
get_future
()] {
29
auto
next_call =
clock::now
() + period_duration;
30
while
( stop_signal.wait_until( next_call ) == std::future_status::timeout ) {
31
callback
();
32
// ensure the next call is at a multiple
33
// of m_period_duration after the last one
34
const
auto
now =
clock::now
();
35
while
( next_call < now ) next_call += period_duration;
36
}
37
} };
38
}
39
}
40
41
void
PeriodicAction::stop
() {
42
if
(
m_thread
.
joinable
() ) {
43
m_stop_thread
.
set_value
();
44
m_thread
.
join
();
45
}
46
}
Gaudi::Utils::PeriodicAction::stop
void stop()
Definition:
PeriodicAction.cpp:41
Gaudi::Utils::PeriodicAction::~PeriodicAction
~PeriodicAction()
Definition:
PeriodicAction.cpp:21
std::move
T move(T... args)
Gaudi::Utils::PeriodicAction::m_stop_thread
std::promise< void > m_stop_thread
Definition:
PeriodicAction.h:46
std::chrono::milliseconds
std::promise::set_value
T set_value(T... args)
std::function< void()>
Gaudi::Utils::PeriodicAction::m_period_duration
std::chrono::milliseconds m_period_duration
Definition:
PeriodicAction.h:48
std::promise::get_future
T get_future(T... args)
IOTest.start
start
Definition:
IOTest.py:110
std::thread::joinable
T joinable(T... args)
Gaudi::Utils::PeriodicAction
Helper to periodically run asynchronous tasks.
Definition:
PeriodicAction.h:29
std::thread
STL class.
Gaudi::Utils::PeriodicAction::m_callback
callback_t m_callback
Definition:
PeriodicAction.h:47
Gaudi::Utils::PeriodicAction::m_thread
std::thread m_thread
Definition:
PeriodicAction.h:45
PeriodicAction.h
Gaudi::Utils::PeriodicAction::start
void start()
Definition:
PeriodicAction.cpp:23
std::thread::join
T join(T... args)
gaudirun.callback
callback
Definition:
gaudirun.py:202
std::chrono::system_clock::now
T now(T... args)
GaudiUtils
src
Lib
PeriodicAction.cpp
Generated on Thu Dec 19 2024 15:35:08 for The Gaudi Framework by
1.8.18