@@ -183,8 +183,8 @@ TaskControl::TaskControl()
183
183
, _signal_per_second(&_cumulated_signal_count)
184
184
, _status(print_rq_sizes_in_the_tc, this )
185
185
, _nbthreads(" bthread_count" )
186
+ , _priority_qs(FLAGS_task_group_ntags)
186
187
, _pl(FLAGS_task_group_ntags)
187
- , _epoll_tid_states(FLAGS_task_group_ntags)
188
188
{}
189
189
190
190
int TaskControl::init (int concurrency) {
@@ -208,6 +208,10 @@ int TaskControl::init(int concurrency) {
208
208
_tagged_worker_usage_second.push_back (new bvar::PerSecond<bvar::PassiveStatus<double >>(
209
209
" bthread_worker_usage" , tag_str, _tagged_cumulated_worker_time[i], 1 ));
210
210
_tagged_nbthreads.push_back (new bvar::Adder<int64_t >(" bthread_count" , tag_str));
211
+ if (_priority_qs[i].init (BTHREAD_MAX_CONCURRENCY) != 0 ) {
212
+ LOG (FATAL) << " Fail to init _priority_q" ;
213
+ return -1 ;
214
+ }
211
215
}
212
216
213
217
// Make sure TimerThread is ready.
@@ -431,15 +435,9 @@ int TaskControl::_destroy_group(TaskGroup* g) {
431
435
432
436
bool TaskControl::steal_task (bthread_t * tid, size_t * seed, size_t offset) {
433
437
auto tag = tls_task_group->tag ();
434
- // epoll tid should be stolen first.
435
- for (auto &epoll_state : _epoll_tid_states[tag]) {
436
- bool expected_state = true ;
437
- if (epoll_state.second .compare_exchange_strong (
438
- expected_state, false , butil::memory_order_seq_cst,
439
- butil::memory_order_relaxed)) {
440
- *tid = epoll_state.first ;
441
- return true ;
442
- }
438
+
439
+ if (_priority_qs[tag].steal (tid)) {
440
+ return true ;
443
441
}
444
442
445
443
// 1: Acquiring fence is paired with releasing fence in _add_group to
@@ -482,30 +480,22 @@ void TaskControl::signal_task(int num_task, bthread_tag_t tag) {
482
480
if (num_task > 2 ) {
483
481
num_task = 2 ;
484
482
}
485
- if (ParkingLot::_waiting_count.load (std::memory_order_acquire) == 0 ) {
486
- if (FLAGS_bthread_min_concurrency > 0 &&
487
- _concurrency.load (butil::memory_order_relaxed) < FLAGS_bthread_concurrency) {
488
- // TODO: Reduce this lock
489
- BAIDU_SCOPED_LOCK (g_task_control_mutex);
490
- if (_concurrency.load (butil::memory_order_acquire) < FLAGS_bthread_concurrency) {
491
- add_workers (1 , tag);
492
- }
493
- } else {
494
- return ;
495
- }
496
- }
497
483
auto & pl = tag_pl (tag);
498
484
int start_index = butil::fmix64 (pthread_numeric_id ()) % PARKING_LOT_NUM;
499
- num_task -= pl[start_index].signal (1 );
500
- if (num_task > 0 ) {
485
+ // WARNING: This allow some bad case happen when wait_count is not accurente.
486
+ auto wait_count = ParkingLot::_waiting_count.load (butil::memory_order_relaxed);
487
+ if (wait_count > 0 ) {
488
+ num_task -= pl[start_index].signal (1 );
489
+ }
490
+ if (num_task > 0 && wait_count > 0 ) {
501
491
for (int i = 1 ; i < PARKING_LOT_NUM && num_task > 0 ; ++i) {
502
492
if (++start_index >= PARKING_LOT_NUM) {
503
493
start_index = 0 ;
504
494
}
505
495
num_task -= pl[start_index].signal (1 );
506
496
}
507
497
}
508
- if (num_task > 0 &&
498
+ if (num_task > 0 && wait_count > 0 &&
509
499
FLAGS_bthread_min_concurrency > 0 && // test min_concurrency for performance
510
500
_concurrency.load (butil::memory_order_relaxed) < FLAGS_bthread_concurrency) {
511
501
// TODO: Reduce this lock
@@ -600,7 +590,6 @@ bvar::LatencyRecorder* TaskControl::create_exposed_pending_time() {
600
590
}
601
591
602
592
void TaskControl::set_group_epoll_tid (bthread_tag_t tag, bthread_t tid) {
603
- _epoll_tid_states[tag][tid] = false ;
604
593
auto groups = tag_group (tag);
605
594
const size_t ngroup = tag_ngroup (tag).load (butil::memory_order_acquire);
606
595
for (size_t i = 0 ; i < ngroup; i++) {
0 commit comments