EPICS Base  7.0.6.1
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
epicsTimer.h
1 /*************************************************************************\
2 * Copyright (c) 2002 The University of Chicago, as Operator of Argonne
3 * National Laboratory.
4 * Copyright (c) 2002 The Regents of the University of California, as
5 * Operator of Los Alamos National Laboratory.
6 * SPDX-License-Identifier: EPICS
7 * EPICS Base is distributed subject to a Software License Agreement found
8 * in file LICENSE that is included with this distribution.
9 \*************************************************************************/
10 /* epicsTimer.h */
11 
12 /* Authors: Marty Kraimer, Jeff Hill */
13 
14 #ifndef epicsTimerH
15 #define epicsTimerH
16 
17 #include <float.h>
18 
19 #include "libComAPI.h"
20 #include "epicsTime.h"
21 #include "epicsThread.h"
22 
23 #ifdef __cplusplus
24 
25 /*
26  * Notes:
27  * 1) epicsTimer does not hold its lock when calling callbacks.
28  */
29 
30 /* code using a timer must implement epicsTimerNotify */
31 class LIBCOM_API epicsTimerNotify {
32 public:
33  enum restart_t { noRestart, restart };
34  class expireStatus {
35  public:
36  LIBCOM_API expireStatus ( restart_t );
37  LIBCOM_API expireStatus ( restart_t, const double & expireDelaySec );
38  LIBCOM_API bool restart () const;
39  LIBCOM_API double expirationDelay () const;
40  private:
41  double delay;
42  };
43 
44  virtual ~epicsTimerNotify () = 0;
45  /* return "noRestart" or "expireStatus ( restart, 30.0 )" */
46  virtual expireStatus expire ( const epicsTime & currentTime ) = 0;
47  virtual void show ( unsigned int level ) const;
48 };
49 
50 class LIBCOM_API epicsTimer {
51 public:
52  /* calls cancel (see warning below) and then destroys the timer */
53  virtual void destroy () = 0;
54  virtual void start ( epicsTimerNotify &, const epicsTime & ) = 0;
55  virtual void start ( epicsTimerNotify &, double delaySeconds ) = 0;
56  /* WARNING: A deadlock will occur if you hold a lock while
57  * calling this function that you also take within the timer
58  * expiration callback.
59  */
60  virtual void cancel () = 0;
61  struct expireInfo {
62  expireInfo ( bool active, const epicsTime & expireTime );
63  bool active;
64  epicsTime expireTime;
65  };
66  virtual expireInfo getExpireInfo () const = 0;
67  double getExpireDelay ();
68  virtual void show ( unsigned int level ) const = 0;
69 protected:
70  virtual ~epicsTimer () = 0; /* protected => delete() must not be called */
71 };
72 
74 public:
75  virtual epicsTimer & createTimer () = 0;
76  virtual void show ( unsigned int level ) const = 0;
77 protected:
78  LIBCOM_API virtual ~epicsTimerQueue () = 0;
79 };
80 
82  : public epicsTimerQueue {
83 public:
84  static LIBCOM_API epicsTimerQueueActive & allocate (
85  bool okToShare, unsigned threadPriority = epicsThreadPriorityMin + 10 );
86  virtual void release () = 0;
87 protected:
88  LIBCOM_API virtual ~epicsTimerQueueActive () = 0;
89 };
90 
92 public:
93  /* called when a new timer is inserted into the queue and the */
94  /* delay to the next expire has changed */
95  virtual void reschedule () = 0;
96  /* if there is a quantum in the scheduling of timer intervals */
97  /* return this quantum in seconds. If unknown then return zero. */
98  virtual double quantum () = 0;
99 protected:
100  LIBCOM_API virtual ~epicsTimerQueueNotify () = 0;
101 };
102 
104  : public epicsTimerQueue {
105 public:
106  static LIBCOM_API epicsTimerQueuePassive & create ( epicsTimerQueueNotify & );
107  LIBCOM_API virtual ~epicsTimerQueuePassive () = 0; /* ok to call delete */
108  virtual double process ( const epicsTime & currentTime ) = 0; /* returns delay to next expire */
109 };
110 
111 inline epicsTimer::expireInfo::expireInfo ( bool activeIn,
112  const epicsTime & expireTimeIn ) :
113  active ( activeIn ), expireTime ( expireTimeIn )
114 {
115 }
116 
117 inline double epicsTimer::getExpireDelay ()
118 {
119  epicsTimer::expireInfo info = this->getExpireInfo ();
120  if ( info.active ) {
121  double delay = info.expireTime - epicsTime::getCurrent ();
122  if ( delay < 0.0 ) {
123  delay = 0.0;
124  }
125  return delay;
126  }
127  return - DBL_MAX;
128 }
129 
130 extern "C" {
131 #endif /* __cplusplus */
132 
133 typedef struct epicsTimerForC * epicsTimerId;
134 typedef void ( *epicsTimerCallback ) ( void *pPrivate );
135 
136 /* thread managed timer queue */
137 typedef struct epicsTimerQueueActiveForC * epicsTimerQueueId;
138 LIBCOM_API epicsTimerQueueId epicsStdCall
139  epicsTimerQueueAllocate ( int okToShare, unsigned int threadPriority );
140 LIBCOM_API void epicsStdCall
141  epicsTimerQueueRelease ( epicsTimerQueueId );
142 LIBCOM_API epicsTimerId epicsStdCall
143  epicsTimerQueueCreateTimer ( epicsTimerQueueId queueid,
144  epicsTimerCallback callback, void *arg );
145 LIBCOM_API void epicsStdCall
146  epicsTimerQueueDestroyTimer ( epicsTimerQueueId queueid, epicsTimerId id );
147 LIBCOM_API void epicsStdCall
148  epicsTimerQueueShow ( epicsTimerQueueId id, unsigned int level );
149 
150 /* passive timer queue */
151 typedef struct epicsTimerQueuePassiveForC * epicsTimerQueuePassiveId;
152 typedef void ( * epicsTimerQueueNotifyReschedule ) ( void * pPrivate );
153 typedef double ( * epicsTimerQueueNotifyQuantum ) ( void * pPrivate );
154 LIBCOM_API epicsTimerQueuePassiveId epicsStdCall
155  epicsTimerQueuePassiveCreate ( epicsTimerQueueNotifyReschedule,
156  epicsTimerQueueNotifyQuantum, void *pPrivate );
157 LIBCOM_API void epicsStdCall
158  epicsTimerQueuePassiveDestroy ( epicsTimerQueuePassiveId );
159 LIBCOM_API epicsTimerId epicsStdCall
160  epicsTimerQueuePassiveCreateTimer (
161  epicsTimerQueuePassiveId queueid, epicsTimerCallback pCallback, void *pArg );
162 LIBCOM_API void epicsStdCall
163  epicsTimerQueuePassiveDestroyTimer ( epicsTimerQueuePassiveId queueid, epicsTimerId id );
164 LIBCOM_API double epicsStdCall
165  epicsTimerQueuePassiveProcess ( epicsTimerQueuePassiveId );
166 LIBCOM_API void epicsStdCall
167  epicsTimerQueuePassiveShow ( epicsTimerQueuePassiveId id, unsigned int level );
168 
169 /* timer */
170 LIBCOM_API void epicsStdCall
171  epicsTimerStartTime ( epicsTimerId id, const epicsTimeStamp *pTime );
172 LIBCOM_API void epicsStdCall
173  epicsTimerStartDelay ( epicsTimerId id, double delaySeconds );
174 LIBCOM_API void epicsStdCall
175  epicsTimerCancel ( epicsTimerId id );
176 LIBCOM_API double epicsStdCall
177  epicsTimerGetExpireDelay ( epicsTimerId id );
178 LIBCOM_API void epicsStdCall
179  epicsTimerShow ( epicsTimerId id, unsigned int level );
180 
181 #ifdef __cplusplus
182 }
183 #endif /* __cplusplus */
184 
185 #endif /* epicsTimerH */
C++ and C descriptions for a thread.
C++ time stamp object.
Definition: epicsTime.h:320
EPICS time stamp, for use from C code.
Definition: epicsTime.h:42
EPICS time-stamps (epicsTimeStamp), epicsTime C++ class and C functions for handling wall-clock times...
static epicsTime getCurrent()
Get current clock time.