dol: initial dol commit
[jump.git] / dol / src / dol / visitor / PipeAndFilter / lib / Event.cpp
diff --git a/dol/src/dol/visitor/PipeAndFilter/lib/Event.cpp b/dol/src/dol/visitor/PipeAndFilter/lib/Event.cpp
new file mode 100644 (file)
index 0000000..52490b2
--- /dev/null
@@ -0,0 +1,73 @@
+#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