mrfioc2  2.3.0
evrGTIF.cpp
Go to the documentation of this file.
1 /*************************************************************************\
2 * Copyright (c) 2010 Brookhaven Science Associates, as Operator of
3 * Brookhaven National Laboratory.
4 * mrfioc2 is distributed subject to a Software License Agreement found
5 * in file LICENSE that is included with this distribution.
6 \*************************************************************************/
7 /* EVR GeneralTime InterFace
8  *
9  * Author: Michael Davidsaver <mdavidsaver@gmail.com>
10  */
11 
12 #include <epicsTypes.h>
13 #include <epicsTime.h>
14 
15 #include <stdexcept>
16 #include <errlog.h>
17 #include <epicsMutex.h>
18 #include <epicsGuard.h>
19 #include <epicsTime.h>
20 #include <epicsVersion.h>
21 #include <initHooks.h>
22 
23 #include <epicsExport.h>
24 
25 #include "mrf/object.h"
26 #include "evr/evr.h"
27 #include "evrGTIF.h"
28 
29 #ifndef M_time
30 #define S_time_unsynchronized epicsTimeERROR
31 #endif
32 
33 struct priv {
34  int ok;
35  epicsTimeStamp *ts;
36  int event;
37  priv(epicsTimeStamp *t, int e) : ok(S_time_unsynchronized), ts(t), event(e) {}
38 };
39 
40 static EVR* lastSrc = 0;
41 
42 static epicsMutexId lastLock;
43 
44 epicsShareFunc
46 {
47  if(lastLock)
48  return 0;
49 
50  lastLock = epicsMutexMustCreate();
51  return 0;
52 }
53 
54 static
55 bool visitTime(mrf::Object* obj, void* raw)
56 {
57  EVR *evr = dynamic_cast<EVR*>(obj);
58  if(!evr)
59  return true;
60 
61  priv *p = (priv*)raw;
62  bool tsok=evr->getTimeStamp(p->ts, p->event);
63  if (tsok) {
64  lastSrc = evr;
65  p->ok=epicsTimeOK;
66  return false;
67  } else
68  return true;
69 }
70 
71 extern "C"
72 int EVREventTime(epicsTimeStamp *pDest, int event)
73 {
74 try {
75  epicsMutexMustLock(lastLock);
76 
77  if(lastSrc) {
78  if(lastSrc->getTimeStamp(pDest, event)) {
79  epicsMutexUnlock(lastLock);
80  return epicsTimeOK;
81  }
82  }
83  priv p(pDest, event);
84  mrf::Object::visitObjects(&visitTime, (void*)&p);
85  epicsMutexUnlock(lastLock);
86  return p.ok;
87 } catch (std::exception& e) {
88  epicsMutexUnlock(lastLock);
89  epicsPrintf("EVREventTime failed: %s\n", e.what());
90  return S_time_unsynchronized;
91 }
92 }
93 
94 extern "C"
95 int EVRCurrentTime(epicsTimeStamp *pDest)
96 {
97  return EVREventTime(pDest, epicsTimeEventCurrentTime);
98 }
99 
101 
102 #if EPICS_VERSION_INT >= VERSION_INT(3,14,9,0)
103 
104 #include <generalTimeSup.h>
105 
106 static
107 void EVRTime_Hooks(initHookState state)
108 {
109  if(state!=initHookAtBeginning)
110  return;
111 
112  int ret=0;
113  ret|=EVRInitTime();
114  // conditionally register the current (aka wall clock) time provider.
115  // May be disabled to trade off accuracy and precision for execution speed
116  // since EPICS calls this a *lot*.
117  if(mrmGTIFEnable)
118  ret|=generalTimeCurrentTpRegister("EVR", ER_PROVIDER_PRIORITY, &EVRCurrentTime);
119  else
120  epicsPrintf("EVR Current time provider NOT register\n");
121  // always register the event time provider (no fallback, and not called by default)
122  ret|=generalTimeEventTpRegister ("EVR", ER_PROVIDER_PRIORITY, &EVREventTime);
123  if (ret)
124  epicsPrintf("Failed to register EVR time provider\n");
125 }
126 
127 extern "C"
129 {
130  initHookRegister(&EVRTime_Hooks);
131 }
132 
133 #else
134 extern "C"
135 void EVRTime_Registrar() {}
136 
137 #endif
138 
139 #include <epicsExport.h>
140 extern "C"{
143 }
int EVRCurrentTime(epicsTimeStamp *pDest)
Definition: evrGTIF.cpp:95
epicsTimeStamp * ts
Definition: evrGTIF.cpp:35
int mrmGTIFEnable
Definition: evrGTIF.cpp:100
epicsShareFunc int EVRInitTime()
Definition: evrGTIF.cpp:45
int event
Definition: devEvrEvent.cpp:45
epicsExportAddress(int, mrmGTIFEnable)
epicsExportRegistrar(EVRTime_Registrar)
Base object inspection.
Definition: object.h:378
void EVRTime_Registrar()
Definition: evrGTIF.cpp:128
char obj[30]
Definition: devEvrEvent.cpp:44
static void visitObjects(bool(*)(Object *, void *), void *)
Definition: object.cpp:150
int ok
Definition: evrGTIF.cpp:34
#define S_time_unsynchronized
Definition: evrGTIF.cpp:30
#define ER_PROVIDER_PRIORITY
Priority given to EVR&#39;s timestamp/event provider.
Definition: mrfCommon.h:131
Base interface for EVRs.
Definition: evr.h:45
int EVREventTime(epicsTimeStamp *pDest, int event)
Definition: evrGTIF.cpp:72
priv(epicsTimeStamp *t, int e)
Definition: evrGTIF.cpp:37
EVR * evr
Definition: devEvrEvent.cpp:43
virtual bool getTimeStamp(epicsTimeStamp *ts, epicsUInt32 event)=0