Skip to content

Commit

Permalink
Improve heave load performance in RingChannel
Browse files Browse the repository at this point in the history
Signed-off-by: Coldwings <coldwings@me.com>
  • Loading branch information
Coldwings committed Jan 31, 2024
1 parent fe8f2f7 commit 294df85
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
19 changes: 13 additions & 6 deletions common/lockfree_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -568,30 +568,37 @@ class RingChannel : public QueueType {
* parameter DEFAULT_BUSY_YIELD_TIMEOUT. Ring Channel will try busy yield
* in `busy_yield_timeout` usecs.
*/
RingChannel(uint64_t busy_yield_turn = 64,
RingChannel(uint64_t busy_yield_turn = -1UL,
uint64_t busy_yield_timeout = 1024)
: m_busy_yield_turn(busy_yield_turn),
m_busy_yield_timeout(busy_yield_timeout) {}

template <typename Pause = ThreadPause>
void send(const T& x) {
while (!push(x)) {
if (!full()) Pause::pause();
Pause::pause();
}
queue_sem.signal(idler.load(std::memory_order_acquire));
}
T recv() {
T x;
Timeout yield_timeout(m_busy_yield_timeout);
int yield_turn = m_busy_yield_turn;
if (pop(x)) return x;
// yield once if failed, so photon::now will be update
photon::thread_yield();
idler.fetch_add(1, std::memory_order_acq_rel);
DEFER(idler.fetch_sub(1, std::memory_order_acq_rel));
Timeout yield_timeout(m_busy_yield_timeout);
uint64_t yield_turn = m_busy_yield_turn;
while (!pop(x)) {
if (yield_turn > 0 && photon::now < yield_timeout.expiration()) {
if (yield_turn > 0 && !yield_timeout.expired()) {
yield_turn--;
photon::thread_yield();
} else {
queue_sem.wait(1);
// wait for 100ms
queue_sem.wait(1, 100UL * 1000);
// reset yield mark and set into busy wait
yield_turn = m_busy_yield_turn;
yield_timeout.timeout(m_busy_yield_timeout);
}
}
return x;
Expand Down
4 changes: 4 additions & 0 deletions thread/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ add_executable(perf_usleepdefer_semaphore perf_usleepdefer_semaphore.cpp)
target_link_libraries(perf_usleepdefer_semaphore PRIVATE photon_shared)
add_test(NAME perf_usleepdefer_semaphore COMMAND $<TARGET_FILE:perf_usleepdefer_semaphore>)

add_executable(perf_workpool perf_workpool.cpp)
target_link_libraries(perf_workpool PRIVATE photon_shared)
add_test(NAME perf_workpool COMMAND $<TARGET_FILE:perf_workpool>)

add_executable(test-thread test.cpp x.cpp)
target_link_libraries(test-thread PRIVATE photon_static)
add_test(NAME test-thread COMMAND $<TARGET_FILE:test-thread>)
Expand Down

0 comments on commit 294df85

Please sign in to comment.