The Gaudi Framework
master (9f3f8895)
Toggle main menu visibility
Loading...
Searching...
No Matches
PeriodicAction.cpp
Go to the documentation of this file.
1
/***********************************************************************************\
2
* (c) Copyright 2024-2025 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
unsigned
int
repetitions )
18
:
m_callback
{
std
::move( callback ) },
m_period_duration
{ period_duration },
m_repetitions
{ repetitions } {
19
if
( autostart )
start
();
20
}
21
22
PeriodicAction::~PeriodicAction
() {
stop
(); }
23
24
void
PeriodicAction::start
() {
25
if
( !
m_thread
.joinable() &&
m_period_duration
.count() > 0 ) {
26
// Note: we can move the callback because we are not going to use it
27
// outside of the thread
28
m_thread
= std::thread{ [period_duration =
m_period_duration
, callback = std::move(
m_callback
),
29
stop_signal =
m_stop_thread
.get_future(), repetitions =
m_repetitions
] {
30
unsigned
int
n = 0;
31
auto
next_call = clock::now() + period_duration;
32
while
( ( repetitions == 0 || n < repetitions ) &&
33
stop_signal.wait_until( next_call ) == std::future_status::timeout ) {
34
++n;
35
callback();
36
// ensure the next call is at a multiple
37
// of m_period_duration after the last one
38
const
auto
now = clock::now();
39
while
( next_call < now ) next_call += period_duration;
40
}
41
} };
42
}
43
}
44
45
void
PeriodicAction::stop
() {
46
if
(
m_thread
.joinable() ) {
47
m_stop_thread
.set_value();
48
m_thread
.join();
49
}
50
}
PeriodicAction.h
Gaudi::Utils::PeriodicAction
Helper to periodically run asynchronous tasks.
Definition
PeriodicAction.h:29
Gaudi::Utils::PeriodicAction::m_period_duration
std::chrono::milliseconds m_period_duration
Definition
PeriodicAction.h:49
Gaudi::Utils::PeriodicAction::start
void start()
Definition
PeriodicAction.cpp:24
Gaudi::Utils::PeriodicAction::m_callback
callback_t m_callback
Definition
PeriodicAction.h:48
Gaudi::Utils::PeriodicAction::m_stop_thread
std::promise< void > m_stop_thread
Definition
PeriodicAction.h:47
Gaudi::Utils::PeriodicAction::callback_t
std::function< void()> callback_t
Definition
PeriodicAction.h:33
Gaudi::Utils::PeriodicAction::~PeriodicAction
~PeriodicAction()
Definition
PeriodicAction.cpp:22
Gaudi::Utils::PeriodicAction::stop
void stop()
Definition
PeriodicAction.cpp:45
Gaudi::Utils::PeriodicAction::m_repetitions
unsigned int m_repetitions
Definition
PeriodicAction.h:50
Gaudi::Utils::PeriodicAction::m_thread
std::thread m_thread
Definition
PeriodicAction.h:46
PeriodicAction::PeriodicAction
PeriodicAction(callback_t callback, std::chrono::milliseconds period_duration, bool autostart=true, unsigned int repetitions=0)
Definition
PeriodicAction.cpp:16
std
STL namespace.
GaudiUtils
src
Lib
PeriodicAction.cpp
Generated on
for The Gaudi Framework by
1.17.0