-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefault_transport.cpp
58 lines (46 loc) · 1.09 KB
/
default_transport.cpp
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
#include "default_transport.hpp"
#include <atomic>
DefaultTransport::DefaultTransport(int32_t size) :
page_size(size),
consumer_page(0),
consumer_index(0),
producer_page(0),
producer_index(0),
mtx()
{
array = new int32_t[size];
}
DefaultTransport::~DefaultTransport()
{
delete [] array;
}
void DefaultTransport::put(uint32_t x)
{
std::unique_lock<std::mutex> lk(mtx);
while (consumer_page < producer_page && consumer_index <= producer_index)
cv_space.wait(lk);
array[producer_index] = x;
producer_index++;
if (producer_index == page_size)
{
++producer_page;
producer_index = 0;
}
cv_data.notify_all();
}
uint32_t DefaultTransport::get()
{
int32_t result;
std::unique_lock<std::mutex> lk(mtx);
while (consumer_page == producer_page && consumer_index >= producer_index)
cv_data.wait(lk);
result = array[consumer_index];
consumer_index++;
if (consumer_index == page_size)
{
consumer_index = 0;
++consumer_page;
}
cv_space.notify_all();
return result;
}