Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions src/bthread/parking_lot.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@
#ifndef BTHREAD_PARKING_LOT_H
#define BTHREAD_PARKING_LOT_H

#include <gflags/gflags.h>
#include "butil/atomicops.h"
#include "bthread/sys_futex.h"

namespace bthread {

DECLARE_bool(parking_lot_no_signal_when_no_waiter);

// Park idle workers.
class BAIDU_CACHELINE_ALIGNMENT ParkingLot {
public:
Expand All @@ -40,13 +43,15 @@ class BAIDU_CACHELINE_ALIGNMENT ParkingLot {
int val;
};

ParkingLot() : _pending_signal(0), _waiter_num(0) {}
ParkingLot()
: _pending_signal(0), _waiter_num(0)
, _no_signal_when_no_waiter(FLAGS_parking_lot_no_signal_when_no_waiter) {}

// Wake up at most `num_task' workers.
// Returns #workers woken up.
int signal(int num_task) {
_pending_signal.fetch_add((num_task << 1), butil::memory_order_release);
if (_waiter_num.load(butil::memory_order_relaxed) == 0) {
if (_no_signal_when_no_waiter && _waiter_num.load(butil::memory_order_relaxed) == 0) {
return 0;
}
return futex_wake_private(&_pending_signal, num_task);
Expand All @@ -64,9 +69,13 @@ class BAIDU_CACHELINE_ALIGNMENT ParkingLot {
// Fast path, no need to futex_wait.
return;
}
_waiter_num.fetch_add(1, butil::memory_order_relaxed);
if (_no_signal_when_no_waiter) {
_waiter_num.fetch_add(1, butil::memory_order_relaxed);
}
futex_wait_private(&_pending_signal, expected_state.val, NULL);
_waiter_num.fetch_sub(1, butil::memory_order_relaxed);
if (_no_signal_when_no_waiter) {
_waiter_num.fetch_sub(1, butil::memory_order_relaxed);
}
}

// Wakeup suspended wait() and make them unwaitable ever.
Expand All @@ -79,6 +88,10 @@ class BAIDU_CACHELINE_ALIGNMENT ParkingLot {
// higher 31 bits for signalling, LSB for stopping.
butil::atomic<int> _pending_signal;
butil::atomic<int> _waiter_num;
// Whether to signal when there is no waiter.
// In busy worker scenarios, signal overhead
// can be reduced.
bool _no_signal_when_no_waiter;
};

} // namespace bthread
Expand Down
4 changes: 3 additions & 1 deletion src/bthread/task_control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ DEFINE_bool(task_group_set_worker_name, true,

namespace bthread {

DEFINE_bool(parking_lot_no_signal_when_no_waiter, false,
"ParkingLot doesn't signal when there is no waiter. "
"In busy worker scenarios, signal overhead can be reduced.");
DEFINE_bool(enable_bthread_priority_queue, false, "Whether to enable priority queue");

DECLARE_int32(bthread_concurrency);
Expand Down Expand Up @@ -189,7 +192,6 @@ TaskControl::TaskControl()
, _signal_per_second(&_cumulated_signal_count)
, _status(print_rq_sizes_in_the_tc, this)
, _nbthreads("bthread_count")
, _enable_priority_queue(FLAGS_enable_bthread_priority_queue)
, _priority_queues(FLAGS_task_group_ntags)
, _pl_num_of_each_tag(FLAGS_bthread_parking_lot_of_each_tag)
, _tagged_pl(FLAGS_task_group_ntags)
Expand Down
Loading