+#include "Event.h"\r
+\r
+Event::Event() {\r
+ _name = "Event";\r
+ //std::cout << "Create " << _name << "." << std::endl;\r
+ _mutex = new Mutex();\r
+ _condition = new Condition(_mutex);\r
+ _waitMutex = new Mutex();\r
+ _waitCondition = new Condition(_waitMutex);\r
+ _pendingWait = false;\r
+}\r
+\r
+\r
+Event::Event(std::string name) {\r
+ _name = "Event " + name;\r
+ //std::cout << "Create " << _name << "." << std::endl;\r
+ _mutex = new Mutex();\r
+ _condition = new Condition(_mutex);\r
+ _waitMutex = new Mutex();\r
+ _waitCondition = new Condition(_waitMutex);\r
+ _pendingWait = false;\r
+}\r
+\r
+\r
+Event::~Event() {\r
+ //std::cout << "Delete " << _name << "." << std::endl;\r
+ delete _condition;\r
+ delete _waitCondition;\r
+ delete _mutex;\r
+ delete _waitMutex;\r
+ //std::cout << "Deleted " << _name << "." << std::endl;\r
+}\r
+\r
+\r
+void Event::notify() {\r
+ _mutex->lock();\r
+ _condition->notify();\r
+ _mutex->unlock();\r
+}\r
+\r
+\r
+void Event::notifyAll() {\r
+ _mutex->lock();\r
+ _condition->notifyAll();\r
+ _mutex->unlock();\r
+}\r
+\r
+\r
+void Event::wait() {\r
+ _mutex->lock();\r
+ _pendingWait = true;\r
+ _waitMutex->lock();\r
+ _waitCondition->notify();\r
+ _waitMutex->unlock();\r
+ _condition->wait();\r
+ _pendingWait = false;\r
+ _mutex->unlock();\r
+}\r
+\r
+\r
+void Event::notifyAfterWait() {\r
+ _mutex->lock();\r
+\r
+ if (!_pendingWait) {\r
+ _waitMutex->lock();\r
+ _mutex->unlock();\r
+ _waitCondition->wait();\r
+ _mutex->lock();\r
+ _waitMutex->unlock();\r
+ }\r
+ _condition->notify();\r
+ _mutex->unlock();\r
+}\r