IBR-DTNSuite  0.8
daemon/src/core/Event.cpp
Go to the documentation of this file.
00001 /*
00002  * Event.cpp
00003  *
00004  *  Created on: 16.02.2010
00005  *      Author: morgenro
00006  */
00007 
00008 #include "core/Event.h"
00009 #include "core/EventSwitch.h"
00010 #include <ibrcommon/thread/MutexLock.h>
00011 
00012 namespace dtn
00013 {
00014         namespace core
00015         {
00016                 Event::Event(int p) : prio(p), _ref_count(1), _auto_delete(true), _processed(false) { }
00017                 Event::~Event() {};
00018 
00019                 void Event::raiseEvent(Event *evt, bool block_until_processed)
00020                 {
00021                         if (block_until_processed)
00022                         {
00023                                 evt->_auto_delete = false;
00024                         }
00025 
00026                         // raise the new event
00027                         dtn::core::EventSwitch::raiseEvent( evt );
00028 
00029                         // if this is a blocking event...
00030                         if (block_until_processed)
00031                         {
00032                                 {
00033                                         ibrcommon::MutexLock l(evt->_ref_count_mutex);
00034                                         while (!evt->_processed) evt->_ref_count_mutex.wait();
00035                                 }
00036 
00037                                 // ... then delete the event here
00038                                 delete evt;
00039                         }
00040                 }
00041 
00042                 void Event::set_ref_count(size_t c)
00043                 {
00044                         ibrcommon::MutexLock l(_ref_count_mutex);
00045                         _ref_count = c;
00046                 }
00047 
00048                 bool Event::decrement_ref_count()
00049                 {
00050                         ibrcommon::MutexLock l(_ref_count_mutex);
00051                         if (_ref_count == 0)
00052                         {
00053                                 throw ibrcommon::Exception("This reference count is already zero!");
00054                         }
00055 
00056                         _ref_count--;
00057                         _processed = (_ref_count == 0);
00058 
00059                         if (_auto_delete)
00060                         {
00061                                 return _processed;
00062                         }
00063                         else
00064                         {
00065                                 _ref_count_mutex.signal(true);
00066                                 return false;
00067                         }
00068                 }
00069         }
00070 }