-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathautoresetevent.h
60 lines (51 loc) · 1.83 KB
/
autoresetevent.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//---------------------------------------------------------
// For conditions of distribution and use, see
// https://github.com/preshing/cpp11-on-multicore/blob/master/LICENSE
//---------------------------------------------------------
#ifndef __CPP11OM_AUTO_RESET_EVENT_H__
#define __CPP11OM_AUTO_RESET_EVENT_H__
#include <cassert>
#include <thread>
#include "sema.h"
//---------------------------------------------------------
// AutoResetEvent
//---------------------------------------------------------
class AutoResetEvent
{
private:
// m_status == 1: Event object is signaled.
// m_status == 0: Event object is reset and no threads are waiting.
// m_status == -N: Event object is reset and N threads are waiting.
std::atomic<int> m_status;
DefaultSemaphoreType m_sema;
public:
AutoResetEvent(int initialStatus = 0) : m_status(initialStatus)
{
assert(initialStatus >= 0 && initialStatus <= 1);
}
void signal()
{
int oldStatus = m_status.load(std::memory_order_relaxed);
for (;;) // Increment m_status atomically via CAS loop.
{
assert(oldStatus <= 1);
int newStatus = oldStatus < 1 ? oldStatus + 1 : 1;
if (m_status.compare_exchange_weak(oldStatus, newStatus, std::memory_order_release, std::memory_order_relaxed))
break;
// The compare-exchange failed, likely because another thread changed m_status.
// oldStatus has been updated. Retry the CAS loop.
}
if (oldStatus < 0)
m_sema.signal(); // Release one waiting thread.
}
void wait()
{
int oldStatus = m_status.fetch_sub(1, std::memory_order_acquire);
assert(oldStatus <= 1);
if (oldStatus < 1)
{
m_sema.wait();
}
}
};
#endif // __CPP11OM_AUTO_RESET_EVENT_H__