task.cpp 945 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include "pls/internal/helpers/profiler.h"

#include "pls/internal/scheduling/scheduler.h"
#include "pls/internal/scheduling/task.h"
#include "pls/internal/scheduling/thread_state.h"

namespace pls {
namespace internal {
namespace scheduling {

task::task() :
    ref_count_{0},
    parent_{nullptr},
    deque_state_{0} {}

void task::execute() {
17 18 19
  PROFILE_WORK_BLOCK("execute task")
  auto last_executing = thread_state::get()->current_task_;
  thread_state::get()->current_task_ = this;
20

21 22
  execute_internal();
  PROFILE_END_BLOCK
23 24

  wait_for_all();
25
  thread_state::get()->current_task_ = last_executing;
26 27 28 29 30 31

  if (parent_ != nullptr) {
    parent_->ref_count_--;
  }
}

32
void task::wait_for_all() {
33 34
  auto scheduler = thread_state::get()->scheduler_;

35
  while (ref_count_ > 0) {
36 37
    if (!scheduler->try_execute_local()) {
      scheduler->try_execute_stolen();
38 39 40
    }
  }
  thread_state::get()->deque_.release_memory_until(deque_state_);
41 42 43 44 45
}

}
}
}