Expose a unique index for each event loop buffer
This can be helpful for indexing into other datastructures based on the
messages, by giving an identifier which will be unique as long as the
message is pinned.
Change-Id: I49ce18fba25a796005e64b40e5d1d5c55ca15543
diff --git a/aos/events/simulated_event_loop.cc b/aos/events/simulated_event_loop.cc
index ae053fb..c339ce0 100644
--- a/aos/events/simulated_event_loop.cc
+++ b/aos/events/simulated_event_loop.cc
@@ -33,7 +33,6 @@
Context context;
SimulatedChannel *const channel = nullptr;
- int buffer_index;
// The data.
char *data(size_t buffer_size) {
@@ -82,9 +81,10 @@
::std::deque<std::shared_ptr<SimulatedMessage>> msgs_;
- SimulatedEventLoop *simulated_event_loop_;
+ SimulatedEventLoop *const simulated_event_loop_;
+ const Channel *const channel_;
+ EventScheduler *const scheduler_;
EventHandler<SimulatedWatcher> event_;
- EventScheduler *scheduler_;
EventScheduler::Token token_;
SimulatedChannel *simulated_channel_ = nullptr;
};
@@ -247,11 +247,11 @@
SimulatedMessage::SimulatedMessage(SimulatedChannel *channel_in)
: channel(channel_in) {
- buffer_index = channel->GetBufferIndex();
+ context.buffer_index = channel->GetBufferIndex();
}
SimulatedMessage::~SimulatedMessage() {
- channel->FreeBufferIndex(buffer_index);
+ channel->FreeBufferIndex(context.buffer_index);
}
class SimulatedSender : public RawSender {
@@ -319,6 +319,12 @@
remote_queue_index);
}
+ int buffer_index() override {
+ // First, ensure message_ is allocated.
+ data();
+ return message_->context.buffer_index;
+ }
+
private:
SimulatedChannel *simulated_channel_;
EventLoop *event_loop_;
@@ -374,6 +380,9 @@
void SetMsg(std::shared_ptr<SimulatedMessage> msg) {
msg_ = msg;
context_ = msg_->context;
+ if (channel()->read_method() != ReadMethod::PIN) {
+ context_.buffer_index = -1;
+ }
if (context_.remote_queue_index == 0xffffffffu) {
context_.remote_queue_index = context_.queue_index;
}
@@ -567,6 +576,8 @@
}
}
+ int NumberBuffers(const Channel *channel) override;
+
private:
friend class SimulatedTimerHandler;
friend class SimulatedPhasedLoopHandler;
@@ -664,14 +675,19 @@
return it->second.get();
}
+int SimulatedEventLoop::NumberBuffers(const Channel *channel) {
+ return GetSimulatedChannel(channel)->number_buffers();
+}
+
SimulatedWatcher::SimulatedWatcher(
SimulatedEventLoop *simulated_event_loop, EventScheduler *scheduler,
const Channel *channel,
std::function<void(const Context &context, const void *message)> fn)
: WatcherState(simulated_event_loop, channel, std::move(fn)),
simulated_event_loop_(simulated_event_loop),
- event_(this),
+ channel_(channel),
scheduler_(scheduler),
+ event_(this),
token_(scheduler_->InvalidToken()) {}
SimulatedWatcher::~SimulatedWatcher() {
@@ -679,7 +695,7 @@
if (token_ != scheduler_->InvalidToken()) {
scheduler_->Deschedule(token_);
}
- simulated_channel_->RemoveWatcher(this);
+ CHECK_NOTNULL(simulated_channel_)->RemoveWatcher(this);
}
void SimulatedWatcher::Schedule(std::shared_ptr<SimulatedMessage> message) {
@@ -709,6 +725,9 @@
}
Context context = msgs_.front()->context;
+ if (channel_->read_method() != ReadMethod::PIN) {
+ context.buffer_index = -1;
+ }
if (context.remote_queue_index == 0xffffffffu) {
context.remote_queue_index = context.queue_index;
}