Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
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 <cstdint>
29#include <mutex>
30#include <condition_variable>
31#include <thread>
32
34#include <jau/fraction_type.hpp>
35#include <jau/functional.hpp>
37
38namespace jau {
39
40 /** \addtogroup Concurrency
41 *
42 * @{
43 */
44
45 /**
46 * A simple timer for timeout and interval applications,
47 * using one dedicated service_runner thread per instance.
48 *
49 * Discussion: It is contemplated to add an implementation using a unique singleton service_runner
50 * for multiple timer instances via event loops.
51 */
53 public:
55
56 /**
57 * User defined timer function using custom granularity via fraction_i64.
58 *
59 * Function gets invoked for each timer event,
60 * i.e. after reaching the duration set earlier.
61 *
62 * @return duration in fractions of seconds for the next timer event or zero to end the timer thread.
63 */
65
66 private:
67 service_runner timer_service;
68 std::mutex mtx_timerfunc;
69 Timer_func timer_func;
70 // Note: Requires libatomic with libstdc++10
72
73 void timer_work(service_runner& sr_ref);
74
75 public:
76 /**
77 * Constructs a new service
78 * @param name thread name of this service
79 * @param service_shutdown_timeout maximum duration in fractions of seconds to wait for service to stop at stop(), where fractions_i64::zero waits infinitely
80 */
81 simple_timer(const std::string& name, const fraction_i64& service_shutdown_timeout) noexcept;
82
83 /**
84 * No copy constructor nor move constructor.
85 * @param o
86 */
87 simple_timer(const simple_timer& o) = delete;
88
89 /**
90 * Return the given name of this timer
91 */
92 const std::string& name() const noexcept { return timer_service.name(); }
93
94 /**
95 * Return the thread-id of this timer's worker thread, zero if not running.
96 */
97 pthread_t thread_id() const noexcept { return timer_service.thread_id(); }
98
99 /**
100 * Returns true if timer is running
101 */
102 bool is_running() const noexcept { return timer_service.is_running(); }
103
104 /**
105 * Returns true if timer shall stop.
106 *
107 * This flag can be used by the Timer_func function to determine whether to skip lengthly tasks.
108 */
109 bool shall_stop() const noexcept { return timer_service.shall_stop(); }
110
111 /**
112 * Start the timer with given user Timer_func function and initial duration.
113 *
114 * @param duration_ initial timer duration in fractions of seconds until next timer event
115 * @param tofunc user Timer_func to be called on next timer event
116 * @return true if timer has been started, otherwise false implies timer is already running.
117 */
118 bool start(const fraction_i64& duration_, Timer_func tofunc) noexcept;
119
120 /**
121 * Start or update the timer with given user Timer_func function and initial duration.
122 *
123 * This is faster than calling stop() and start(), however,
124 * an already started timer user Timer_func will proceed.
125 *
126 * @param duration_ initial timer duration in fractions of seconds until next timer event
127 * @param tofunc user Timer_func to be called on next timer event
128 */
129 void start_or_update(const fraction_i64& duration_, Timer_func tofunc) noexcept;
130
131 /**
132 * Stop timer, see service_runner::stop()
133 *
134 * @returns true if timer has been stopped or false if timeout has been hit
135 */
136 bool stop() noexcept { return timer_service.stop(); }
137 };
138
139 /**@}*/
140
141} /* namespace jau */
142
143#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 stop() noexcept
Stops this service, if running.
const std::string & name() const noexcept
Return the given name of this service.
bool is_running() const noexcept
Returns true if service is running.
pthread_t thread_id() const noexcept
Return the thread-id of this service service thread, zero if not running.
bool shall_stop() const noexcept
Returns true if service shall stop.
A simple timer for timeout and interval applications, using one dedicated service_runner thread per i...
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.
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