dol: initial dol commit
[jump.git] / dol / src / dol / visitor / PipeAndFilter / lib / Event.cpp
1 #include "Event.h"\r
2 \r
3 Event::Event() {\r
4     _name = "Event";\r
5     //std::cout << "Create " << _name << "." << std::endl;\r
6     _mutex = new Mutex();\r
7     _condition = new Condition(_mutex);\r
8     _waitMutex = new Mutex();\r
9     _waitCondition = new Condition(_waitMutex);\r
10     _pendingWait = false;\r
11 }\r
12 \r
13 \r
14 Event::Event(std::string name) {\r
15     _name = "Event " + name;\r
16     //std::cout << "Create " << _name << "." << std::endl;\r
17     _mutex = new Mutex();\r
18     _condition = new Condition(_mutex);\r
19     _waitMutex = new Mutex();\r
20     _waitCondition = new Condition(_waitMutex);\r
21     _pendingWait = false;\r
22 }\r
23 \r
24 \r
25 Event::~Event() {\r
26     //std::cout << "Delete " << _name << "." << std::endl;\r
27     delete _condition;\r
28     delete _waitCondition;\r
29     delete _mutex;\r
30     delete _waitMutex;\r
31     //std::cout << "Deleted " << _name << "." << std::endl;\r
32 }\r
33 \r
34 \r
35 void Event::notify() {\r
36     _mutex->lock();\r
37     _condition->notify();\r
38     _mutex->unlock();\r
39 }\r
40 \r
41 \r
42 void Event::notifyAll() {\r
43     _mutex->lock();\r
44     _condition->notifyAll();\r
45     _mutex->unlock();\r
46 }\r
47 \r
48 \r
49 void Event::wait() {\r
50     _mutex->lock();\r
51     _pendingWait = true;\r
52     _waitMutex->lock();\r
53     _waitCondition->notify();\r
54     _waitMutex->unlock();\r
55     _condition->wait();\r
56     _pendingWait = false;\r
57     _mutex->unlock();\r
58 }\r
59 \r
60 \r
61 void Event::notifyAfterWait() {\r
62     _mutex->lock();\r
63 \r
64     if (!_pendingWait) {\r
65         _waitMutex->lock();\r
66         _mutex->unlock();\r
67         _waitCondition->wait();\r
68         _mutex->lock();\r
69         _waitMutex->unlock();\r
70     }\r
71     _condition->notify();\r
72     _mutex->unlock();\r
73 }\r