-
Notifications
You must be signed in to change notification settings - Fork 1
/
SharedMemIface.cpp
162 lines (138 loc) · 5.23 KB
/
SharedMemIface.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include"SharedMemIface.hpp"
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<cassert>
#include<cstring>
SharedMemIface::SharedMemIface(string shmem_name_, size_t shmem_size_) :
shmem_name(shmem_name_), shmem_size(shmem_size_) {
shared_memory_object::remove(shmem_name.c_str());
segment = managed_shared_memory(create_only, shmem_name.c_str(), shmem_size);
shared_struct = segment.construct<SharedStruct>("SharedStruct")();
const ShmemAllocator alloc_inst(segment.get_segment_manager());
data = segment.construct<SharedVector>("SharedVector")(alloc_inst);
shared_struct->proc_status = ProcStatus::init;
}
string SharedMemIface::print_signals(){
while(shared_struct->proc_status == ProcStatus::init);
assert(shared_struct->proc_status == ProcStatus::ready);
assert(!shared_struct->closed);
std::string ret;
shared_struct->proc_status = ProcStatus::print_signals;
wait();
assert(shared_struct->proc_status == ProcStatus::ready);
ret = (const char*)data->data();
return ret;
}
uint64_t SharedMemIface::get_signal_handle(string& handle_name){
while(shared_struct->proc_status == ProcStatus::init);
assert(shared_struct->proc_status == ProcStatus::ready);
assert(!shared_struct->closed);
data->resize(handle_name.size()+1);
memcpy(data->data(), handle_name.c_str(), handle_name.size());
data->at(handle_name.size()) = '\0';
shared_struct->proc_status = ProcStatus::get_signal_handle;
wait();
assert(shared_struct->proc_status == ProcStatus::ready);
return shared_struct->handle;
}
std::vector<uint8_t> SharedMemIface::read(uint64_t handle){
while(shared_struct->proc_status == ProcStatus::init);
assert(shared_struct->proc_status == ProcStatus::ready);
assert(!shared_struct->closed);
std::vector<uint8_t> ret_vec;
shared_struct->handle = handle;
shared_struct->proc_status = ProcStatus::read;
wait();
assert(shared_struct->proc_status == ProcStatus::ready);
ret_vec.resize(data->size());
memcpy(ret_vec.data(), data->data(), data->size());
return ret_vec;
}
uint64_t SharedMemIface::read_u64(uint64_t handle){
uint64_t ret = 0ul;
std::vector<uint8_t> read_data = this->read(handle);
size_t copy_size = std::min(8ul, read_data.size());
size_t start_orig = read_data.size()-1;
for(uint8_t i = 0; i < copy_size; i++) {
((uint8_t*) &ret)[i] = read_data[start_orig - i];
}
return ret;
}
uint32_t SharedMemIface::read_u32(uint64_t handle){
uint32_t ret = 0ul;
std::vector<uint8_t> read_data = this->read(handle);
size_t copy_size = std::min(4ul, read_data.size());
size_t start_orig = read_data.size()-1;
for(uint8_t i = 0; i < copy_size; i++) {
((uint8_t*) &ret)[i] = read_data[start_orig - i];
}
return ret;
}
void SharedMemIface::write(uint64_t handle, std::vector<uint8_t>& data_){
while(shared_struct->proc_status == ProcStatus::init);
assert(shared_struct->proc_status == ProcStatus::ready);
assert(!shared_struct->closed);
shared_struct->handle = handle;
data->resize(data_.size());
memcpy(data->data(), data_.data(), data_.size());
shared_struct->proc_status = ProcStatus::write;
wait();
assert(shared_struct->proc_status == ProcStatus::ready);
}
void SharedMemIface::write_u64(uint64_t handle, uint64_t data_){
std::vector<uint8_t> vdata;
vdata.resize(8);
for(uint8_t i = 0; i < 8; i++) vdata[7-i] = (data_ >> 8*i) & 0xFF;
this->write(handle, vdata);
}
void SharedMemIface::write_u32(uint64_t handle, uint32_t data_){
std::vector<uint8_t> vdata;
vdata.resize(4);
for(uint8_t i = 0; i < 4; i++) vdata[3-i] = (data_ >> 8*i) & 0xFF;
this->write(handle, vdata);
}
void SharedMemIface::sleep(uint64_t sleep_cycles){
while(shared_struct->proc_status == ProcStatus::init);
assert(shared_struct->proc_status == ProcStatus::ready);
assert(!shared_struct->closed);
shared_struct->sleep_cycles = sleep_cycles;
shared_struct->proc_status = ProcStatus::sleep;
wait();
}
void SharedMemIface::eval(){
this->sleep(1);
}
void SharedMemIface::close(){
while(shared_struct->proc_status == ProcStatus::init);
assert(shared_struct->proc_status == ProcStatus::ready);
assert(!shared_struct->closed);
shared_struct->proc_status = ProcStatus::close;
wait();
assert(shared_struct->closed);
}
bool SharedMemIface::error_happened(){
return shared_struct->proc_status == ProcStatus::error;
}
std::string SharedMemIface::error_string(){
return std::string((const char*)data->data());
}
SharedMemIface::~SharedMemIface(){
if(!shared_struct->closed){
close();
}
segment.destroy<SharedVector>("SharedVector");
segment.destroy<SharedStruct>("SharedStruct");
shared_memory_object::remove(shmem_name.c_str());
}
bool SharedMemIface::wait(){
assert(shared_struct->proc_status != ProcStatus::ready);
assert(shared_struct->proc_status != ProcStatus::error);
shared_struct->barrier.wait();
// Here the VPI side is doing its steps..
shared_struct->barrier.wait();
assert((shared_struct->proc_status == ProcStatus::ready) |
(shared_struct->proc_status == ProcStatus::error));
return error_happened();
}