libtrbase  1.0.2
Web server and task management solution.
timer.h File Reference
#include <time.h>
#include "trbase.h"
+ Include dependency graph for timer.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define TR_timerSetNan(timer, val)   (TR_timerSet((timer), TR_TBASE_NAN, (val)))
 
#define TR_timerSetMic(timer, val)   (TR_timerSet((timer), TR_TBASE_MIC, (val)))
 
#define TR_timerSetMil(timer, val)   (TR_timerSet((timer), TR_TBASE_MIL, (val)))
 
#define TR_timerSetSec(timer, val)   (TR_timerSet((timer), TR_TBASE_SEC, (val)))
 

Enumerations

enum  TR_eTimeoutBase { TR_TBASE_NAN = 0, TR_TBASE_MIC, TR_TBASE_MIL, TR_TBASE_SEC }
 

Functions

 TR_CLASS (TR_Timer)
 
 TR_INSTANCE_INIT (TR_Timer)
 
 TR_CLASSVARS_DECL (TR_Timer)
 
void TR_timerSet (TR_Timer, TR_eTimeoutBase, unsigned long)
 
unsigned long TR_timerGet (TR_Timer, unsigned long *)
 

Detailed Description

A timer with second, millisec, microsec or nanosec granularity.

Author
Georg Hopp

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file timer.h.

Macro Definition Documentation

#define TR_timerSetMic (   timer,
  val 
)    (TR_timerSet((timer), TR_TBASE_MIC, (val)))

Definition at line 64 of file timer.h.

#define TR_timerSetMil (   timer,
  val 
)    (TR_timerSet((timer), TR_TBASE_MIL, (val)))

Definition at line 65 of file timer.h.

#define TR_timerSetNan (   timer,
  val 
)    (TR_timerSet((timer), TR_TBASE_NAN, (val)))

Definition at line 63 of file timer.h.

#define TR_timerSetSec (   timer,
  val 
)    (TR_timerSet((timer), TR_TBASE_SEC, (val)))

Definition at line 66 of file timer.h.

Enumeration Type Documentation

Enumerator
TR_TBASE_NAN 
TR_TBASE_MIC 
TR_TBASE_MIL 
TR_TBASE_SEC 

Definition at line 42 of file timer.h.

42  {
43  TR_TBASE_NAN = 0,
TR_eTimeoutBase
Definition: timer.h:42

Function Documentation

TR_CLASS ( TR_Timer  )

Definition at line 49 of file timer.h.

49  {
50  int sec;
51  int nsec;
52  TR_eTimeoutBase base;
53  unsigned long timeout;
54 };
TR_eTimeoutBase
Definition: timer.h:42
TR_CLASSVARS_DECL ( TR_Timer  )

Definition at line 57 of file timer.h.

57 {};
TR_INSTANCE_INIT ( TR_Timer  )
unsigned long TR_timerGet ( TR_Timer  ,
unsigned long *   
)

Definition at line 31 of file timer_get.c.

References TR_TBASE_MIC, TR_TBASE_MIL, TR_TBASE_NAN, and TR_TBASE_SEC.

32 {
33  struct timespec now;
34  unsigned long long elapsed;
35  unsigned long sec_factor;
36  long nan_factor;
37  unsigned long _missed;
38 
39  clock_gettime(CLOCK_REALTIME, &now);
40 
41  switch (this->base) {
42  case TR_TBASE_NAN:
43  sec_factor = 1000 * 1000 * 1000;
44  break;
45  case TR_TBASE_MIC:
46  sec_factor = 1000 * 1000;
47  break;
48  default:
49  case TR_TBASE_MIL:
50  sec_factor = 1000;
51  break;
52  case TR_TBASE_SEC:
53  sec_factor = 1;
54  break;
55  }
56 
57  nan_factor = 1000 * 1000 * 1000 / sec_factor;
58  elapsed = (now.tv_sec - this->sec) * sec_factor
59  + (now.tv_nsec - this->nsec) / nan_factor;
60 
61  _missed = elapsed / this->timeout;
62  if (_missed) {
63  this->sec += _missed * this->timeout * sec_factor / nan_factor;
64  this->nsec += _missed * this->timeout * sec_factor % nan_factor;
65  if (missed) *missed = _missed;
66  } else {
67  if (missed) *missed = 0;
68  }
69 
70  return this->timeout - (elapsed % this->timeout);
71 }
void TR_timerSet ( TR_Timer  ,
TR_eTimeoutBase  ,
unsigned  long 
)

Definition at line 30 of file timer_set.c.

Referenced by timerCtor().

31 {
32  struct timespec set;
33  clock_gettime(CLOCK_REALTIME, &set);
34  this->sec = set.tv_sec;
35  this->nsec = set.tv_nsec;
36  this->base = base;
37  this->timeout = timeout;
38 }

+ Here is the caller graph for this function: