| 671 | } |
| 672 | |
| 673 | void TaskGroup::ending_sched(TaskGroup** pg) { |
| 674 | TaskGroup* g = *pg; |
| 675 | bthread_t next_tid = 0; |
| 676 | // Find next task to run, if none, switch to idle thread of the group. |
| 677 | #ifndef BTHREAD_FAIR_WSQ |
| 678 | // When BTHREAD_FAIR_WSQ is defined, profiling shows that cpu cost of |
| 679 | // WSQ::steal() in example/multi_threaded_echo_c++ changes from 1.9% |
| 680 | // to 2.9% |
| 681 | const bool popped = g->_rq.pop(&next_tid); |
| 682 | #else |
| 683 | const bool popped = g->_rq.steal(&next_tid); |
| 684 | #endif |
| 685 | if (!popped && !g->steal_task(&next_tid)) { |
| 686 | // Jump to main task if there's no task to run. |
| 687 | next_tid = g->_main_tid; |
| 688 | } |
| 689 | |
| 690 | TaskMeta* const cur_meta = g->_cur_meta; |
| 691 | TaskMeta* next_meta = address_meta(next_tid); |
| 692 | if (next_meta->stack == NULL) { |
| 693 | if (next_meta->stack_type() == cur_meta->stack_type()) { |
| 694 | // Reuse the stack of the current ending task. |
| 695 | // |
| 696 | // also works with pthread_task scheduling to pthread_task, the |
| 697 | // transfered stack is just _main_stack. |
| 698 | next_meta->set_stack(cur_meta->release_stack()); |
| 699 | } else { |
| 700 | #ifdef BUTIL_USE_ASAN |
| 701 | ContextualStack* stk = get_stack( |
| 702 | next_meta->stack_type(), asan_task_runner); |
| 703 | #else |
| 704 | ContextualStack* stk = get_stack(next_meta->stack_type(), task_runner); |
| 705 | #endif // BUTIL_USE_ASAN |
| 706 | if (stk) { |
| 707 | next_meta->set_stack(stk); |
| 708 | } else { |
| 709 | // stack_type is BTHREAD_STACKTYPE_PTHREAD or out of memory, |
| 710 | // In latter case, attr is forced to be BTHREAD_STACKTYPE_PTHREAD. |
| 711 | // This basically means that if we can't allocate stack, run |
| 712 | // the task in pthread directly. |
| 713 | next_meta->attr.stack_type = BTHREAD_STACKTYPE_PTHREAD; |
| 714 | next_meta->set_stack(g->_main_stack); |
| 715 | } |
| 716 | } |
| 717 | } |
| 718 | sched_to(pg, next_meta); |
| 719 | } |
| 720 | |
| 721 | void TaskGroup::sched(TaskGroup** pg) { |
| 722 | TaskGroup* g = *pg; |
nothing calls this directly
no test coverage detected