Line data Source code
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 : 12 : OPENDDS_BEGIN_VERSIONED_NAMESPACE_DECL 13 : 14 : namespace OpenDDS { 15 : namespace DCPS { 16 : 17 5 : SporadicEvent::SporadicEvent(EventDispatcher_rch dispatcher, EventBase_rch event) 18 5 : : dispatcher_(dispatcher) 19 5 : , event_(event) 20 10 : , timer_id_(0) 21 : { 22 5 : } 23 : 24 13 : void SporadicEvent::schedule(const TimeDuration& duration) 25 : { 26 13 : const MonotonicTimePoint now = MonotonicTimePoint::now(); 27 13 : ACE_Guard<ACE_Thread_Mutex> guard(mutex_); 28 13 : if (timer_id_ < 1) { 29 7 : EventDispatcher_rch dispatcher = dispatcher_.lock(); 30 7 : if (dispatcher) { 31 7 : const MonotonicTimePoint expiration = now + duration; 32 7 : long id = dispatcher->schedule(rchandle_from(this), expiration); 33 7 : if (id > 0) { 34 7 : expiration_ = expiration; 35 7 : timer_id_ = id; 36 : } 37 7 : } 38 13 : } else if (now + duration < expiration_) { 39 2 : EventDispatcher_rch dispatcher = dispatcher_.lock(); 40 2 : if (dispatcher) { 41 2 : if (dispatcher->cancel(timer_id_)) { 42 2 : timer_id_ = 0; 43 2 : const MonotonicTimePoint expiration = now + duration; 44 2 : long id = dispatcher->schedule(rchandle_from(this), expiration); 45 2 : if (id > 0) { 46 2 : expiration_ = expiration; 47 2 : timer_id_ = id; 48 : } 49 2 : } 50 : } 51 2 : } 52 13 : } 53 : 54 1 : void SporadicEvent::cancel() 55 : { 56 1 : ACE_Guard<ACE_Thread_Mutex> guard(mutex_); 57 1 : if (timer_id_ > 0) { 58 1 : EventDispatcher_rch dispatcher = dispatcher_.lock(); 59 1 : if (dispatcher) { 60 1 : if (dispatcher->cancel(timer_id_)) { 61 1 : timer_id_ = 0; 62 : } 63 : } 64 1 : } 65 1 : } 66 : 67 6 : void SporadicEvent::handle_event_scheduling() 68 : { 69 6 : ACE_Guard<ACE_Thread_Mutex> guard(mutex_); 70 6 : timer_id_ = 0; 71 6 : } 72 : 73 6 : void SporadicEvent::handle_event() 74 : { 75 6 : handle_event_scheduling(); 76 6 : ACE_Guard<ACE_Thread_Mutex> guard(event_mutex_); 77 6 : if (event_) { 78 6 : RcHandle<EventBase> event_copy(event_); 79 6 : guard.release(); 80 6 : event_copy->handle_event(); 81 6 : } 82 6 : } 83 : 84 3 : void SporadicEvent::handle_cancel() 85 : { 86 3 : ACE_Guard<ACE_Thread_Mutex> guard(event_mutex_); 87 3 : if (event_) { 88 3 : RcHandle<EventBase> event_copy(event_); 89 3 : guard.release(); 90 3 : event_copy->handle_cancel(); 91 3 : } 92 3 : } 93 : 94 : } // DCPS 95 : } // OpenDDS 96 : 97 : OPENDDS_END_VERSIONED_NAMESPACE_DECL