aboutsummaryrefslogtreecommitdiffstats
path: root/include/jau/simple_timer.hpp
blob: b94d3e4611ce694006bf6fc993be516be41c8bf6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 * Author: Sven Gothel <sgothel@jausoft.com>
 * Copyright (c) 2022 Gothel Software e.K.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#ifndef JAU_SIMPLE_TIMER_HPP_
#define JAU_SIMPLE_TIMER_HPP_

#include <cstdint>
#include <mutex>
#include <condition_variable>
#include <thread>

#include <jau/ordered_atomic.hpp>
#include <jau/fraction_type.hpp>
#include <jau/functional.hpp>
#include <jau/service_runner.hpp>

namespace jau {

    /** \addtogroup Concurrency
     *
     *  @{
     */

    /**
     * A simple timer for timeout and interval applications,
     * using one dedicated service_runner thread per instance.
     *
     * Discussion: It is contemplated to add an implementation using a unique singleton service_runner
     * for multiple timer instances via event loops.
     */
    class simple_timer {
        public:
            typedef simple_timer& Timer0_ref;

            /**
             * User defined timer function using custom granularity via fraction_i64.
             *
             * Function gets invoked for each timer event,
             * i.e. after reaching the duration set earlier.
             *
             * @return duration in fractions of seconds for the next timer event or zero to end the timer thread.
             */
            typedef function<fraction_i64(Timer0_ref)> Timer_func;

        private:
            service_runner timer_service;
            std::mutex mtx_timerfunc;
            Timer_func timer_func;
            // Note: Requires libatomic with libstdc++10
            sc_atomic_fraction_i64 duration;

            void timer_work(service_runner& sr_ref);

        public:
            /**
             * Constructs a new service
             * @param name thread name of this service
             * @param service_shutdown_timeout maximum duration in fractions of seconds to wait for service to stop at stop(), where fractions_i64::zero waits infinitely
             */
            simple_timer(const std::string& name, const fraction_i64& service_shutdown_timeout) noexcept;

            /**
             * No copy constructor nor move constructor.
             * @param o
             */
            simple_timer(const simple_timer& o) = delete;

            /**
             * Return the given name of this timer
             */
            const std::string& name() const noexcept { return timer_service.name(); }

            /**
             * Return the thread-id of this timer's worker thread, zero if not running.
             */
            pthread_t thread_id() const noexcept { return timer_service.thread_id(); }

            /**
             * Returns true if timer is running
             */
            bool is_running() const noexcept { return timer_service.is_running(); }

            /**
             * Returns true if timer shall stop.
             *
             * This flag can be used by the Timer_func function to determine whether to skip lengthly tasks.
             */
            bool shall_stop() const noexcept { return timer_service.shall_stop(); }

            /**
             * Start the timer with given user Timer_func function and initial duration.
             *
             * @param duration_ initial timer duration in fractions of seconds until next timer event
             * @param tofunc user Timer_func to be called on next timer event
             * @return true if timer has been started, otherwise false implies timer is already running.
             */
            bool start(const fraction_i64& duration_, Timer_func tofunc) noexcept;

            /**
             * Start or update the timer with given user Timer_func function and initial duration.
             *
             * This is faster than calling stop() and start(), however,
             * an already started timer user Timer_func will proceed.
             *
             * @param duration_ initial timer duration in fractions of seconds until next timer event
             * @param tofunc user Timer_func to be called on next timer event
             */
            void start_or_update(const fraction_i64& duration_, Timer_func tofunc) noexcept;

            /**
             * Stop timer, see service_runner::stop()
             *
             * @returns true if timer has been stopped or false if timeout has been hit
             */
            bool stop() noexcept { return timer_service.stop(); }
    };

    /**@}*/

} /* namespace jau */

#endif /* JAU_SIMPLE_TIMER_HPP_ */