summaryrefslogtreecommitdiffstats
path: root/ml/dlib/dlib/timer/timer_heavy.h
blob: 693b91ad9bb6a68d683d596f16e3382745a7545d (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
// Copyright (C) 2005  Davis E. King (davis@dlib.net)
// License: Boost Software License   See LICENSE.txt for the full license.
#ifndef DLIB_TIMER_KERNEl_1_
#define DLIB_TIMER_KERNEl_1_

#include "../threads.h"
#include "../algs.h"
#include "../misc_api.h"
#include "timer_abstract.h"

namespace dlib
{

    template <
        typename T
        >
    class timer_heavy
    {
        /*!
            WHAT THIS OBJECT REPRESENTS
                This is an implementation of the timer_abstract.h interface.  It is very
                simple and uses only one thread which is always alive in a timer_heavy.
                The reason this object exists is for historical reasons.  Originally, the
                dlib::timer was a multi-implementation component and the timer_heavy was
                its first implementation.  It was superseded later by the more efficient
                dlib::timer.  However, timer_heavy is still around so that
                dlib::timer::kernel_1a has something to refer to.  This way, old client
                code which somehow depends on the same thread always calling a timer action
                function isn't going to be disrupted.


            INITIAL VALUE
                - running   == false
                - delay     == 1000
                - ao        == a pointer to the action_object()
                - af        == a pointer to the action_function()
                - m         == a mutex that locks everything in this class
                - s         == a signaler for mutex m
                - stop_running == false

            CONVENTION
                - running && !stop_running == is_running()
                - delay == delay_time()
                - *ao == action_object()
                - af == action_function()    

                - if (running) then
                    - there is a thread running
                - if (is_running()) then
                    - next_time_to_run == the time when the next execution of the action
                      function should occur.  (the time is given by ts.get_timestamp())

                - stop_running is used to tell the thread to quit.  If it is
                  set to true then the thread should end.
        !*/

    public:

        typedef void (T::*af_type)();

        timer_heavy(  
            T& ao_,
            af_type af_
        );

        virtual ~timer_heavy(
        );

        void clear(
        );

        af_type action_function (
        ) const;

        const T& action_object (
        ) const;

        T& action_object (
        );

        bool is_running (
        ) const;

        unsigned long delay_time (
        ) const;

        void set_delay_time (
            unsigned long milliseconds
        );
        
        void start (            
        );

        void stop (
        );

        void stop_and_wait (
        );

    private:

        void thread (
        );
        /*!
            requires
                - is run in its own thread
            ensures
                - calls the action function for the given timer object in the manner
                  specified by timer_kernel_abstract.h
        !*/

        // data members
        T& ao;
        const af_type af;
        unsigned long delay;
        mutex m;
        signaler s;

        bool running;
        bool stop_running;
        timestamper ts;
        uint64 next_time_to_run;

        // restricted functions
        timer_heavy(const timer_heavy<T>&);        // copy constructor
        timer_heavy<T>& operator=(const timer_heavy<T>&);    // assignment operator

    };    

// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
    // member function definitions
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
    
    template <
        typename T
        >
    timer_heavy<T>::
    timer_heavy(  
        T& ao_,
        af_type af_
    ) : 
        ao(ao_),
        af(af_),
        delay(1000),
        s(m),
        running(false),
        stop_running(false)
    {
    }

// ----------------------------------------------------------------------------------------

    template <
        typename T
        >
    timer_heavy<T>::
    ~timer_heavy(
    )
    {
        stop_and_wait();
    }

// ----------------------------------------------------------------------------------------

    template <
        typename T
        >
    void timer_heavy<T>::
    clear(
    )
    {
        m.lock();
        stop_running = true;
        delay = 1000;        
        s.broadcast();
        m.unlock();
    }

// ----------------------------------------------------------------------------------------

    template <
        typename T
        >
    typename timer_heavy<T>::af_type timer_heavy<T>::
    action_function (
    ) const
    {
        return af;
    }

// ----------------------------------------------------------------------------------------

    template <
        typename T
        >
    const T& timer_heavy<T>::
    action_object (
    ) const
    {
        return ao;
    }

// ----------------------------------------------------------------------------------------

    template <
        typename T
        >
    T& timer_heavy<T>::
    action_object (
    )
    {
        return ao;
    }

// ----------------------------------------------------------------------------------------

    template <
        typename T
        >
    bool timer_heavy<T>::
    is_running (
    ) const
    {
        auto_mutex M(m);
        return running && !stop_running;
    }

// ----------------------------------------------------------------------------------------

    template <
        typename T
        >
    unsigned long timer_heavy<T>::
    delay_time (
    ) const
    {
        auto_mutex M(m);
        return delay;        
    }

// ----------------------------------------------------------------------------------------

    template <
        typename T
        >
    void timer_heavy<T>::
    set_delay_time (
        unsigned long milliseconds
    )
    {
        m.lock();

        // if (is_running()) then we should adjust next_time_to_run
        if (running && !stop_running)
        {
            next_time_to_run -= delay*1000;
            next_time_to_run += milliseconds*1000;
        }

        delay = milliseconds;
        s.broadcast();
        m.unlock();
    }

// ----------------------------------------------------------------------------------------

    template <
        typename T
        >
    void timer_heavy<T>::
    start (            
    )
    {
        auto_mutex M(m);

        // if (is_running() == false) then reset the countdown to the next call 
        // to the action_function()
        if ( (running && !stop_running) == false)
            next_time_to_run = ts.get_timestamp() + delay*1000;

        stop_running = false;
        if (running == false)
        {
            running = true;

            // start the thread
            if (create_new_thread<timer_heavy,&timer_heavy::thread>(*this) == false)
            {
                running = false;
                throw dlib::thread_error("error creating new thread in timer_heavy::start");
            }
        }
    }

// ----------------------------------------------------------------------------------------

    template <
        typename T
        >
    void timer_heavy<T>::
    stop (
    )
    {
        m.lock();
        stop_running = true;
        s.broadcast();
        m.unlock();
    }

// ----------------------------------------------------------------------------------------

    template <
        typename T
        >
    void timer_heavy<T>::
    thread (
    )
    {
        auto_mutex M(m);
        unsigned long delay_remaining;
        uint64 current_time = ts.get_timestamp();

        if (current_time < next_time_to_run)
            delay_remaining = static_cast<unsigned long>((next_time_to_run-current_time)/1000);
        else
            delay_remaining = 0;

        while (stop_running == false)
        {
            if (delay_remaining > 0)
                s.wait_or_timeout(delay_remaining);

            if (stop_running)
                break;            

            current_time = ts.get_timestamp();
            if (current_time < next_time_to_run)
            {
                // then we woke up too early so we should keep waiting
                delay_remaining = static_cast<unsigned long>((next_time_to_run-current_time)/1000);

                // rounding might make this be zero anyway.  So if it is
                // then we will say we have hit the next time to run.
                if (delay_remaining > 0)
                    continue;
            }

            // call the action function 
            m.unlock();
            (ao.*af)(); 
            m.lock();

            current_time = ts.get_timestamp();
            next_time_to_run = current_time + delay*1000;
            delay_remaining = delay;
        }
        running = false;
        stop_running = false;
        s.broadcast();
    }

// ----------------------------------------------------------------------------------------

    template <
        typename T
        >
    void timer_heavy<T>::
    stop_and_wait (
    )
    {
        m.lock();
        if (running)
        {
            // make the running thread terminate
            stop_running = true;

            s.broadcast();
            // wait for the thread to quit
            while (running)
                s.wait();          
        }
        m.unlock();
    }

// ----------------------------------------------------------------------------------------

}

#endif // DLIB_TIMER_KERNEl_1_