@@ -51,6 +51,8 @@ DEFINE_string(cpu_set, "",
5151 " Set of CPUs to which cores are bound. "
5252 " for example, 0-3,5,7; default: disable" );
5353
54+ namespace brpc { DECLARE_int32(event_dispatcher_num); }
55+
5456namespace bthread {
5557
5658DEFINE_bool (parking_lot_no_signal_when_no_waiter, false ,
@@ -205,11 +207,28 @@ TaskControl::TaskControl()
205207 , _status(print_rq_sizes_in_the_tc, this )
206208 , _nbthreads(" bthread_count" )
207209 , _enable_priority_queue(FLAGS_enable_bthread_priority_queue)
208- , _priority_queues(FLAGS_task_group_ntags)
210+ , _pq_num_of_each_tag(brpc::FLAGS_event_dispatcher_num)
211+ , _priority_queues(FLAGS_task_group_ntags * brpc::FLAGS_event_dispatcher_num)
209212 , _pl_num_of_each_tag(FLAGS_bthread_parking_lot_of_each_tag)
210213 , _tagged_pl(FLAGS_task_group_ntags)
211214{}
212215
216+ int TaskControl::init_priority_queues () {
217+ if (!_enable_priority_queue) {
218+ return 0 ;
219+ }
220+ for (int i = 0 ; i < FLAGS_task_group_ntags; ++i) {
221+ for (int j = 0 ; j < _pq_num_of_each_tag; ++j) {
222+ if (priority_queue (i, j).init (BTHREAD_MAX_CONCURRENCY) != 0 ) {
223+ LOG (ERROR) << " Fail to init priority queue for tag=" << i
224+ << " ed=" << j;
225+ return -1 ;
226+ }
227+ }
228+ }
229+ return 0 ;
230+ }
231+
213232int TaskControl::init (int concurrency) {
214233 if (_concurrency != 0 ) {
215234 LOG (ERROR) << " Already initialized" ;
@@ -238,10 +257,10 @@ int TaskControl::init(int concurrency) {
238257 _tagged_worker_usage_second.push_back (new bvar::PerSecond<bvar::PassiveStatus<double >>(
239258 " bthread_worker_usage" , tag_str, _tagged_cumulated_worker_time[i], 1 ));
240259 _tagged_nbthreads.push_back (new bvar::Adder<int64_t >(" bthread_count" , tag_str));
241- if (_priority_queues[i]. init (BTHREAD_MAX_CONCURRENCY) != 0 ) {
242- LOG (ERROR) << " Fail to init _priority_q " ;
243- return - 1 ;
244- }
260+ }
261+
262+ if ( init_priority_queues () != 0 ) {
263+ return - 1 ;
245264 }
246265
247266 // Make sure TimerThread is ready.
@@ -445,7 +464,7 @@ TaskControl::~TaskControl() {
445464 _switch_per_second.hide ();
446465 _signal_per_second.hide ();
447466 _status.hide ();
448-
467+
449468 stop_and_join ();
450469}
451470
@@ -528,8 +547,12 @@ int TaskControl::_destroy_group(TaskGroup* g) {
528547bool TaskControl::steal_task (bthread_t * tid, size_t * seed, size_t offset) {
529548 auto tag = tls_task_group->tag ();
530549
531- if (_priority_queues[tag].steal (tid)) {
532- return true ;
550+ if (_enable_priority_queue) {
551+ for (int i = 0 ; i < _pq_num_of_each_tag; ++i) {
552+ if (priority_queue (tag, i).steal (tid)) {
553+ return true ;
554+ }
555+ }
533556 }
534557
535558 // 1: Acquiring fence is paired with releasing fence in _add_group to
@@ -689,4 +712,5 @@ std::vector<bthread_t> TaskControl::get_living_bthreads() {
689712 return living_bthread_ids;
690713}
691714
715+
692716} // namespace bthread
0 commit comments