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 "ServiceEventDispatcher.h" 11 : 12 : OPENDDS_BEGIN_VERSIONED_NAMESPACE_DECL 13 : 14 : namespace OpenDDS { 15 : namespace DCPS { 16 : 17 28 : ServiceEventDispatcher::ServiceEventDispatcher(size_t count) 18 28 : : dispatcher_(make_rch<DispatchService>(count)) 19 : { 20 28 : } 21 : 22 56 : ServiceEventDispatcher::~ServiceEventDispatcher() 23 : { 24 28 : shutdown(); 25 56 : } 26 : 27 43 : void ServiceEventDispatcher::shutdown(bool immediate) 28 : { 29 43 : DispatchService_rch local; 30 : { 31 43 : ACE_Guard<ACE_Thread_Mutex> guard(mutex_); 32 43 : local.swap(dispatcher_); 33 43 : } 34 43 : if (local) { 35 28 : DispatchService::EventQueue remaining; 36 28 : local->shutdown(immediate, &remaining); 37 29 : for (DispatchService::EventQueue::iterator it = remaining.begin(), limit = remaining.end(); it != limit; ++it) { 38 1 : EventBase* ptr = static_cast<EventBase*>(it->second); 39 1 : if (ptr) { 40 1 : ptr->handle_cancel(); 41 1 : ptr->_remove_ref(); 42 : } 43 : } 44 28 : } 45 43 : } 46 : 47 2092 : bool ServiceEventDispatcher::dispatch(EventBase_rch event) 48 : { 49 2092 : ACE_Guard<ACE_Thread_Mutex> guard(mutex_); 50 2092 : if (!dispatcher_) { 51 2 : return false; 52 : } 53 2090 : event->_add_ref(); 54 2090 : const bool result = dispatcher_->dispatch(*event); 55 2090 : if (!result) { 56 0 : event->_remove_ref(); 57 : } 58 2090 : return result; 59 2092 : } 60 : 61 45 : long ServiceEventDispatcher::schedule(EventBase_rch event, const MonotonicTimePoint& expiration) 62 : { 63 45 : ACE_Guard<ACE_Thread_Mutex> guard(mutex_); 64 45 : if (!dispatcher_) { 65 1 : return -1; 66 : } 67 44 : event->_add_ref(); 68 44 : const long result = dispatcher_->schedule(*event, expiration); 69 44 : if (result < 0) { 70 0 : event->_remove_ref(); 71 : } 72 44 : return result; 73 45 : } 74 : 75 16 : size_t ServiceEventDispatcher::cancel(long id) 76 : { 77 16 : ACE_Guard<ACE_Thread_Mutex> guard(mutex_); 78 16 : if (!dispatcher_) { 79 1 : return 0; 80 : } 81 15 : void* arg = 0; 82 15 : const size_t result = dispatcher_->cancel(id, &arg); 83 15 : if (result) { 84 15 : EventBase* ptr = static_cast<EventBase*>(arg); 85 15 : if (ptr) { 86 15 : ptr->handle_cancel(); 87 15 : ptr->_remove_ref(); 88 : } 89 : } 90 15 : return result; 91 16 : } 92 : 93 : } // DCPS 94 : } // OpenDDS 95 : 96 : OPENDDS_END_VERSIONED_NAMESPACE_DECL