Skip to content

Commit 6b0d0a4

Browse files
committed
rename pushRingBuffer to processAnyTask and add comments
1 parent 46e0398 commit 6b0d0a4

File tree

3 files changed

+30
-22
lines changed

3 files changed

+30
-22
lines changed

orchagent/orch.cpp

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -335,36 +335,44 @@ void Consumer::execute()
335335
auto entries = std::make_shared<std::deque<KeyOpFieldsValuesTuple>>();
336336
getConsumerTable()->pops(*entries);
337337

338-
pushRingBuffer([=](){
339-
addToSync(entries);
340-
});
341-
342-
pushRingBuffer([=](){
343-
drain();
344-
});
338+
processAnyTask(
339+
// bundle tasks into a lambda function which takes no argument and returns void
340+
// this lambda captures variables by value from the surrounding scope
341+
[=](){
342+
addToSync(entries);
343+
drain();
344+
}
345+
);
345346
}
346347

347-
void Executor::pushRingBuffer(AnyTask&& task)
348+
void Executor::processAnyTask(AnyTask&& task)
348349
{
350+
// if either gRingBuffer isn't initialized or the ring thread isn't created
349351
if (!gRingBuffer || !gRingBuffer->thread_created)
350352
{
351-
// execute the task right now in this thread if gRingBuffer is not initialized
352-
// or the ring thread is not created, or this executor is not served by gRingBuffer
353+
// execute the input task immediately
353354
task();
354355
}
355-
else if (!gRingBuffer->serves(getName())) // not served by ring thread
356+
357+
// Ring Buffer Logic
358+
359+
// if this executor isn't served by ring buffer
360+
else if (!gRingBuffer->serves(getName()))
356361
{
362+
// this executor should execute the input task in the main thread
363+
// but to avoid thread issue, it should wait when the ring buffer is actively working
357364
while (!gRingBuffer->IsEmpty() || !gRingBuffer->IsIdle()) {
358365
gRingBuffer->notify();
359366
std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_MSECONDS));
360367
}
361-
// if ring thread is enabled, make sure to execute task after the ring finishes its work
368+
// execute task()
362369
task();
363370
}
364371
else
365372
{
366-
// if this executor is served by gRingBuffer, push the task to gRingBuffer
367-
// and notify the ring thread to flush gRingBuffer
373+
// if this executor is served by ring buffer,
374+
// push the task to gRingBuffer
375+
// this task would be executed in the ring thread, not here
368376
while (!gRingBuffer->push(task)) {
369377
gRingBuffer->notify();
370378
SWSS_LOG_WARN("ring is full...push again");

orchagent/orch.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ typedef std::pair<std::string, int> table_name_with_pri_t;
9393

9494
class Orch;
9595

96-
using AnyTask = std::function<void()>;
96+
using AnyTask = std::function<void()>; // represents a function with no argument and returns void
9797

9898
class RingBuffer;
9999

@@ -135,7 +135,7 @@ class Executor : public swss::Selectable
135135

136136
Orch *getOrch() const { return m_orch; }
137137
static std::shared_ptr<RingBuffer> gRingBuffer;
138-
void pushRingBuffer(AnyTask&& func);
138+
void processAnyTask(AnyTask&& func);
139139

140140
protected:
141141
swss::Selectable *m_selectable;

tests/mock_tests/orchdaemon_ut.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,18 +141,18 @@ namespace orchdaemon_test
141141
EXPECT_FALSE(gRingBuffer->serves("OTHER_TABLE"));
142142

143143
int x = 0;
144-
route_consumer->pushRingBuffer([&](){x=3;});
145-
// verify `pushRingBuffer` is equivalent to executing the task immediately
144+
route_consumer->processAnyTask([&](){x=3;});
145+
// verify `processAnyTask` is equivalent to executing the task immediately
146146
EXPECT_TRUE(gRingBuffer->IsEmpty() && gRingBuffer->IsIdle() && !gRingBuffer->thread_created && x==3);
147147

148148
gRingBuffer->thread_created = true; // set the flag to assume the ring thread is created (actually not)
149149

150-
// verify `pushRingBuffer` is equivalent to executing the task immediately when ring is empty and idle
151-
other_consumer->pushRingBuffer([&](){x=4;});
150+
// verify `processAnyTask` is equivalent to executing the task immediately when ring is empty and idle
151+
other_consumer->processAnyTask([&](){x=4;});
152152
EXPECT_TRUE(gRingBuffer->IsEmpty() && gRingBuffer->IsIdle() && x==4);
153153

154-
route_consumer->pushRingBuffer([&](){x=5;});
155-
// verify `pushRingBuffer` would not execute the task if thread_created is true
154+
route_consumer->processAnyTask([&](){x=5;});
155+
// verify `processAnyTask` would not execute the task if thread_created is true
156156
// it only pushes the task to the ring buffer, without executing it
157157
EXPECT_TRUE(!gRingBuffer->IsEmpty() && x==4);
158158

0 commit comments

Comments
 (0)