OpenDDS  Snapshot(2023/04/28-20:55)
SporadicEvent.cpp
Go to the documentation of this file.
1 /*
2  *
3  *
4  * Distributed under the OpenDDS License.
5  * See: http://www.opendds.org/license.html
6  */
7 
8 #include "DCPS/DdsDcps_pch.h" //Only the _pch include should start with DCPS/
9 
10 #include "SporadicEvent.h"
11 
13 
14 namespace OpenDDS {
15 namespace DCPS {
16 
18  : dispatcher_(dispatcher)
19  , event_(event)
20  , timer_id_(0)
21 {
22 }
23 
25 {
28  if (timer_id_ < 1) {
29  EventDispatcher_rch dispatcher = dispatcher_.lock();
30  if (dispatcher) {
31  const MonotonicTimePoint expiration = now + duration;
32  long id = dispatcher->schedule(rchandle_from(this), expiration);
33  if (id > 0) {
34  expiration_ = expiration;
35  timer_id_ = id;
36  }
37  }
38  } else if (now + duration < expiration_) {
39  EventDispatcher_rch dispatcher = dispatcher_.lock();
40  if (dispatcher) {
41  if (dispatcher->cancel(timer_id_)) {
42  timer_id_ = 0;
43  const MonotonicTimePoint expiration = now + duration;
44  long id = dispatcher->schedule(rchandle_from(this), expiration);
45  if (id > 0) {
46  expiration_ = expiration;
47  timer_id_ = id;
48  }
49  }
50  }
51  }
52 }
53 
55 {
57  if (timer_id_ > 0) {
58  EventDispatcher_rch dispatcher = dispatcher_.lock();
59  if (dispatcher) {
60  if (dispatcher->cancel(timer_id_)) {
61  timer_id_ = 0;
62  }
63  }
64  }
65 }
66 
68 {
70  timer_id_ = 0;
71 }
72 
74 {
77  if (event_) {
78  RcHandle<EventBase> event_copy(event_);
79  guard.release();
80  event_copy->handle_event();
81  }
82 }
83 
85 {
87  if (event_) {
88  RcHandle<EventBase> event_copy(event_);
89  guard.release();
90  event_copy->handle_cancel();
91  }
92 }
93 
94 } // DCPS
95 } // OpenDDS
96 
RcHandle< T > rchandle_from(T *pointer)
Definition: RcHandle_T.h:310
virtual size_t cancel(long id)=0
int release(void)
SporadicEvent(EventDispatcher_rch dispatcher, EventBase_rch event)
static TimePoint_T< MonotonicClock > now()
Definition: TimePoint_T.inl:41
void schedule(const TimeDuration &duration)
ACE_Thread_Mutex event_mutex_
Definition: SporadicEvent.h:69
virtual long schedule(EventBase_rch event, const MonotonicTimePoint &expiration=MonotonicTimePoint::now())=0
RcHandle< EventBase > event_
Definition: SporadicEvent.h:71
WeakRcHandle< EventDispatcher > dispatcher_
Definition: SporadicEvent.h:70
#define OPENDDS_END_VERSIONED_NAMESPACE_DECL
The Internal API and Implementation of OpenDDS.
Definition: AddressCache.h:28
MonotonicTimePoint expiration_
Definition: SporadicEvent.h:72