1 /*
2    Copyright (c) 2003, 2021, Oracle and/or its affiliates.
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License, version 2.0,
6    as published by the Free Software Foundation.
7 
8    This program is also distributed with certain software (including
9    but not limited to OpenSSL) that is licensed under separate terms,
10    as designated in a particular file or component or in included license
11    documentation.  The authors of MySQL hereby grant you an additional
12    permission to link the program and your derivative works with the
13    separately licensed software that they have included with MySQL.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License, version 2.0, for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
23 */
24 
25 #ifndef TIMER_HPP
26 #define TIMER_HPP
27 
28 /**
29  *  @class Timer
30  *  @brief A timer class that can't be fooled by NTP:ing the system clock to old time
31  *         The 'time' used by this class is 'ticks' since some platform
32  *         specific epoch start. Typically retrieved by NdbTick_getCurrentTicks()
33  */
34 class Timer {
35 public:
Timer()36   Timer() {
37     m_delay = 10;
38   };
39 
Timer(Uint64 delay_time)40   Timer(Uint64 delay_time) {
41     m_delay = delay_time;
42   }
43 
44   /**
45    *  Set/Get alarm time of timer
46    */
setDelay(Uint64 delay_time)47   inline void    setDelay(Uint64 delay_time) { m_delay = delay_time;  }
getDelay() const48   inline Uint64  getDelay() const            { return m_delay; }
49 
50   /**
51    *  Start timer
52    */
reset(NDB_TICKS now)53   inline void reset(NDB_TICKS now) {
54     m_start_time = now;
55   }
56 
check(NDB_TICKS now)57   inline bool check(NDB_TICKS now) {
58 
59     /**
60      *  Protect against time moving backwards.
61      *  In that case use 'backtick' as new start.
62      */
63     if (NdbTick_Compare(m_start_time,now) > 0)
64     {
65       m_start_time = now;
66       return false;
67     }
68 
69     /**
70      *  Standard alarm check
71      */
72     if (NdbTick_Elapsed(m_start_time,now).milliSec() > m_delay)
73        return true;
74 
75     /**
76      *  Time progressing, but it is not alarm time yet
77      */
78     return false;
79   }
80 
81 private:
82   NDB_TICKS m_start_time;
83   Uint64 m_delay;
84 };
85 
86 #endif
87