This is Unofficial EPICS BASE Doxygen Site
monitorEventThread.cpp
Go to the documentation of this file.
1 
11 #include "caChannel.h"
12 #include <epicsExit.h>
13 #define epicsExportSharedSymbols
14 #include "monitorEventThread.h"
15 
16 using namespace epics::pvData;
17 using namespace std;
18 
19 namespace epics {
20 namespace pvAccess {
21 namespace ca {
22 
24 {
25  static MonitorEventThreadPtr master;
26  static Mutex mutex;
27  Lock xx(mutex);
28  if(!master) {
30  master->start();
31  }
32  return master;
33 }
34 
35 MonitorEventThread::MonitorEventThread()
36 : isStop(false)
37 {
38 }
39 
40 MonitorEventThread::~MonitorEventThread()
41 {
42 //std::cout << "MonitorEventThread::~MonitorEventThread()\n";
43 }
44 
45 void MonitorEventThread::start()
46 {
47  thread = std::tr1::shared_ptr<epicsThread>(new epicsThread(
48  *this,
49  "monitorEventThread",
52  thread->start();
53 }
54 
55 void MonitorEventThread::stop()
56 {
57  {
58  Lock xx(mutex);
59  isStop = true;
60  }
61  waitForCommand.signal();
62  waitForStop.wait();
63 }
64 
65 
66 void MonitorEventThread::event(NotifyMonitorRequesterPtr const &notifyMonitorRequester)
67 {
68  {
69  Lock lock(mutex);
70  if(notifyMonitorRequester->isOnQueue) return;
71  notifyMonitorRequester->isOnQueue = true;
72  notifyMonitorQueue.push(notifyMonitorRequester);
73  }
74  waitForCommand.signal();
75 }
76 
77 void MonitorEventThread::run()
78 {
79  while(true)
80  {
81  waitForCommand.wait();
82  while(true) {
83  bool more = false;
84  NotifyMonitorRequester* notifyMonitorRequester(NULL);
85  {
86  Lock lock(mutex);
87  if(!notifyMonitorQueue.empty())
88  {
89  more = true;
90  NotifyMonitorRequesterWPtr req(notifyMonitorQueue.front());
91  notifyMonitorQueue.pop();
92  NotifyMonitorRequesterPtr reqPtr(req.lock());
93  if(reqPtr) {
94  notifyMonitorRequester = reqPtr.get();
95  reqPtr->isOnQueue = false;
96  }
97  }
98  }
99  if(!more) break;
100  if(notifyMonitorRequester!=NULL)
101  {
102  CAChannelMonitorPtr channelMonitor(notifyMonitorRequester->channelMonitor.lock());
103  if(channelMonitor) channelMonitor->notifyClient();
104  }
105  }
106  if(isStop) {
107  waitForStop.signal();
108  break;
109  }
110  }
111 }
112 
113 }}}
epicsMutexId lock
Definition: osiClockTime.c:37
std::tr1::shared_ptr< MonitorEventThread > MonitorEventThreadPtr
Definition: caChannel.h:42
Definition: memory.hpp:41
TODO only here because of the Lockable.
Definition: ntaggregate.cpp:16
A lock for multithreading.
Definition: lock.h:36
#define NULL
Definition: catime.c:38
LIBCOM_API unsigned int epicsStdCall epicsThreadGetStackSize(epicsThreadStackSizeClass size)
Definition: osdThread.c:466
pvData
Definition: monitor.h:428
epicsMutex mutex
Definition: pvAccess.cpp:71
std::tr1::shared_ptr< NotifyMonitorRequester > NotifyMonitorRequesterPtr
Definition: caChannel.h:40
#define epicsThreadPriorityLow
Definition: epicsThread.h:75
Extended replacement for the Posix exit and atexit routines.
std::tr1::shared_ptr< CAChannelMonitor > CAChannelMonitorPtr
Definition: caChannel.h:66
Definition: caget.c:48
std::tr1::weak_ptr< NotifyMonitorRequester > NotifyMonitorRequesterWPtr
epicsMutex Mutex
Definition: lock.h:28