-
Notifications
You must be signed in to change notification settings - Fork 10k
Expand file tree
/
Copy pathcondition_notifier.cc
More file actions
209 lines (177 loc) · 5.17 KB
/
Copy pathcondition_notifier.cc
File metadata and controls
209 lines (177 loc) · 5.17 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/******************************************************************************
* Copyright 2018 The Apollo Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*****************************************************************************/
#include "cyber/transport/shm/condition_notifier.h"
#include <sys/ipc.h>
#include <sys/shm.h>
#include <thread>
#include "cyber/common/log.h"
#include "cyber/common/util.h"
namespace apollo {
namespace cyber {
namespace transport {
using common::Hash;
ConditionNotifier::ConditionNotifier() {
key_ = static_cast<key_t>(Hash("/apollo/cyber/transport/shm/notifier"));
ADEBUG << "condition notifier key: " << key_;
shm_size_ = sizeof(Indicator);
if (!Init()) {
AERROR << "fail to init condition notifier.";
is_shutdown_.store(true);
return;
}
next_seq_ = indicator_->next_seq.load();
ADEBUG << "next_seq: " << next_seq_;
}
ConditionNotifier::~ConditionNotifier() { Shutdown(); }
void ConditionNotifier::Shutdown() {
if (is_shutdown_.exchange(true)) {
return;
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
Reset();
}
bool ConditionNotifier::Notify(const ReadableInfo& info) {
if (is_shutdown_.load()) {
ADEBUG << "notifier is shutdown.";
return false;
}
uint64_t seq = indicator_->next_seq.fetch_add(1);
uint64_t idx = seq % kBufLength;
indicator_->infos[idx] = info;
indicator_->seqs[idx].store(seq, std::memory_order_release);
return true;
}
bool ConditionNotifier::Listen(int timeout_ms, ReadableInfo* info) {
if (info == nullptr) {
AERROR << "info nullptr.";
return false;
}
if (is_shutdown_.load()) {
ADEBUG << "notifier is shutdown.";
return false;
}
int timeout_us = timeout_ms * 1000;
while (!is_shutdown_.load()) {
uint64_t seq = indicator_->next_seq.load();
if (seq != next_seq_) {
auto idx = next_seq_ % kBufLength;
auto actual_seq = indicator_->seqs[idx].load(std::memory_order_acquire);
if (actual_seq >= next_seq_) {
next_seq_ = actual_seq;
*info = indicator_->infos[idx];
++next_seq_;
return true;
} else {
ADEBUG << "seq[" << next_seq_ << "] is writing, can not read now.";
}
}
if (timeout_us > 0) {
std::this_thread::sleep_for(std::chrono::microseconds(50));
timeout_us -= 50;
} else {
return false;
}
}
return false;
}
bool ConditionNotifier::Init() { return OpenOrCreate(); }
bool ConditionNotifier::OpenOrCreate() {
// create managed_shm_
int retry = 0;
int shmid = 0;
while (retry < 2) {
shmid = shmget(key_, shm_size_, 0644 | IPC_CREAT | IPC_EXCL);
if (shmid != -1) {
break;
}
if (EINVAL == errno) {
AINFO << "need larger space, recreate.";
Reset();
Remove();
++retry;
} else if (EEXIST == errno) {
ADEBUG << "shm already exist, open only.";
return OpenOnly();
} else {
break;
}
}
if (shmid == -1) {
AERROR << "create shm failed, error code: " << strerror(errno);
return false;
}
// attach managed_shm_
managed_shm_ = shmat(shmid, nullptr, 0);
if (managed_shm_ == reinterpret_cast<void*>(-1)) {
AERROR << "attach shm failed.";
shmctl(shmid, IPC_RMID, 0);
return false;
}
// create indicator_
indicator_ = new (managed_shm_) Indicator();
if (indicator_ == nullptr) {
AERROR << "create indicator failed.";
shmdt(managed_shm_);
managed_shm_ = nullptr;
shmctl(shmid, IPC_RMID, 0);
return false;
}
ADEBUG << "open or create true.";
return true;
}
bool ConditionNotifier::OpenOnly() {
// get managed_shm_
int shmid = shmget(key_, 0, 0644);
if (shmid == -1) {
AERROR << "get shm failed, error: " << strerror(errno);
return false;
}
// attach managed_shm_
managed_shm_ = shmat(shmid, nullptr, 0);
if (managed_shm_ == reinterpret_cast<void*>(-1)) {
AERROR << "attach shm failed, error: " << strerror(errno);
return false;
}
// get indicator_
indicator_ = reinterpret_cast<Indicator*>(managed_shm_);
if (indicator_ == nullptr) {
AERROR << "get indicator failed.";
shmdt(managed_shm_);
managed_shm_ = nullptr;
return false;
}
ADEBUG << "open true.";
return true;
}
bool ConditionNotifier::Remove() {
int shmid = shmget(key_, 0, 0644);
if (shmid == -1 || shmctl(shmid, IPC_RMID, 0) == -1) {
AERROR << "remove shm failed, error code: " << strerror(errno);
return false;
}
ADEBUG << "remove success.";
return true;
}
void ConditionNotifier::Reset() {
indicator_ = nullptr;
if (managed_shm_ != nullptr) {
shmdt(managed_shm_);
managed_shm_ = nullptr;
}
}
} // namespace transport
} // namespace cyber
} // namespace apollo