jaulib v1.4.1-17-gd77ace3-dirty
Jau Support Library (C++, Java, ..)
Loading...
Searching...
No Matches
simple_timer.hpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2022 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25#ifndef JAU_SIMPLE_TIMER_HPP_
26#define JAU_SIMPLE_TIMER_HPP_
27
28#include <mutex>
29
31#include <jau/fraction_type.hpp>
32#include <jau/functional.hpp>
34
35namespace jau {
36
37 /** \addtogroup Concurrency
38 *
39 * @{
40 */
41
42 /**
43 * A simple timer for timeout and interval applications,
44 * using one dedicated service_runner thread per instance.
45 *
46 * Discussion: It is contemplated to add an implementation using a unique singleton service_runner
47 * for multiple timer instances via event loops.
48 */
50 public:
52
53 /**
54 * User defined timer function using custom granularity via fraction_i64.
55 *
56 * Function gets invoked for each timer event,
57 * i.e. after reaching the duration set earlier.
58 *
59 * @return duration in fractions of seconds for the next timer event or zero to end the timer thread.
60 */
62
63 private:
64 service_runner timer_service;
65 std::mutex mtx_timerfunc;
66 Timer_func timer_func;
67 // Note: Requires libatomic with libstdc++10
69
70 void timer_work(service_runner& sr_ref);
71
72 public:
73 /**
74 * Constructs a new service
75 * @param name thread name of this service
76 * @param service_shutdown_timeout maximum duration in fractions of seconds to wait for service to stop at stop(), where fractions_i64::zero waits infinitely
77 */
78 simple_timer(const std::string& name, const fraction_i64& service_shutdown_timeout) noexcept;
79
80 /**
81 * No copy constructor nor move constructor.
82 * @param o
83 */
84 simple_timer(const simple_timer& o) = delete;
85
86 /**
87 * Return the given name of this timer
88 */
89 const std::string& name() const noexcept { return timer_service.name(); }
90
91 /**
92 * Return the thread-id of this timer's worker thread, zero if not running.
93 */
94 pthread_t thread_id() const noexcept { return timer_service.thread_id(); }
95
96 /**
97 * Returns true if timer is running
98 */
99 bool is_running() const noexcept { return timer_service.is_running(); }
100
101 /**
102 * Returns true if timer shall stop.
103 *
104 * This flag can be used by the Timer_func function to determine whether to skip lengthly tasks.
105 */
106 bool shall_stop() const noexcept { return timer_service.shall_stop(); }
107
108 /**
109 * Start the timer with given user Timer_func function and initial duration.
110 *
111 * @param duration_ initial timer duration in fractions of seconds until next timer event
112 * @param tofunc user Timer_func to be called on next timer event
113 * @return true if timer has been started, otherwise false implies timer is already running.
114 */
115 bool start(const fraction_i64& duration_, Timer_func tofunc) noexcept;
116
117 /**
118 * Start or update the timer with given user Timer_func function and initial duration.
119 *
120 * This is faster than calling stop() and start(), however,
121 * an already started timer user Timer_func will proceed.
122 *
123 * @param duration_ initial timer duration in fractions of seconds until next timer event
124 * @param tofunc user Timer_func to be called on next timer event
125 */
126 void start_or_update(const fraction_i64& duration_, Timer_func tofunc) noexcept;
127
128 /**
129 * Stop timer, see service_runner::stop()
130 *
131 * @returns true if timer has been stopped or false if timeout has been hit
132 */
133 bool stop() noexcept { return timer_service.stop(); }
134 };
135
136 /**@}*/
137
138} /* namespace jau */
139
140#endif /* JAU_SIMPLE_TIMER_HPP_ */
Class template jau::function is a general-purpose static-polymorphic function wrapper.
Service runner, a reusable dedicated thread performing custom user services.
bool start(const fraction_i64 &duration_, Timer_func tofunc) noexcept
Start the timer with given user Timer_func function and initial duration.
simple_timer(const std::string &name, const fraction_i64 &service_shutdown_timeout) noexcept
Constructs a new service.
const std::string & name() const noexcept
Return the given name of this timer.
function< fraction_i64(Timer0_ref)> Timer_func
User defined timer function using custom granularity via fraction_i64.
bool stop() noexcept
Stop timer, see service_runner::stop()
void start_or_update(const fraction_i64 &duration_, Timer_func tofunc) noexcept
Start or update the timer with given user Timer_func function and initial duration.
simple_timer(const simple_timer &o)=delete
No copy constructor nor move constructor.
simple_timer & Timer0_ref
pthread_t thread_id() const noexcept
Return the thread-id of this timer's worker thread, zero if not running.
bool shall_stop() const noexcept
Returns true if timer shall stop.
bool is_running() const noexcept
Returns true if timer is running.
ordered_atomic< jau::fraction_i64, std::memory_order_seq_cst > sc_atomic_fraction_i64
SC atomic integral scalar jau::fraction_i64.
fraction< int64_t > fraction_i64
fraction using int64_t as integral type
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
Definition backtrace.hpp:32