task.cpp 3.84 KB
Newer Older
1 2 3 4 5
#include "pls/internal/scheduling/lock_free/task.h"
#include "pls/internal/scheduling/scheduler.h"

namespace pls::internal::scheduling::lock_free {

6
task *task::find_task(unsigned id, unsigned depth) {
7 8 9
  return thread_state::get().get_scheduler().thread_state_for(id).get_task_manager().get_task(depth);
}

10 11 12 13 14 15 16 17
void task::prepare_for_push(unsigned int pushing_thread_id) {
  data_structures::stamped_integer target_next;
  target_next.stamp_ = pushing_thread_id + 1;
  target_next.value_ = 0;
  resource_stack_next_.store(target_next, std::memory_order_relaxed);
}

bool task::push_task_chain(task *spare_task_chain, unsigned pushing_thread_id) {
18 19 20 21 22
  PLS_ASSERT(this->thread_id_ != spare_task_chain->thread_id_,
             "Makes no sense to push task onto itself, as it is not clean by definition.");
  PLS_ASSERT(this->depth_ == spare_task_chain->depth_,
             "Must only push tasks with correct depth.");

23
  data_structures::stamped_integer current_root = this->resource_stack_root_.load(std::memory_order_relaxed);
24
  data_structures::stamped_integer target_root;
25 26 27 28 29 30

  data_structures::stamped_integer expected_next_field;
  data_structures::stamped_integer target_next_field;
  expected_next_field.stamp_ = pushing_thread_id + 1;
  expected_next_field.value_ = 0;

31
  do {
32
    // current_root implicitly re-loaded by CAS in loop
33
    target_root.stamp_ = current_root.stamp_ + 1;
34 35
    target_root.value_ = spare_task_chain->thread_id_ + 1;

36
    // Prepare the target_next_field as required
37 38
    if (current_root.value_ == 0) {
      // Empty, simply push in with no successor.
39 40
      target_next_field.stamp_ = pushing_thread_id + 1;
      target_next_field.value_ = 0;
41 42 43
    } else {
      // Already an entry. Find it's corresponding task and set it as our successor.
      auto *current_root_task = find_task(current_root.value_ - 1, this->depth_);
44 45 46 47 48

      target_next_field.stamp_ = pushing_thread_id + 1;
      target_next_field.value_ = current_root_task->thread_id_ + 1;
    }

49 50 51
    if (!spare_task_chain->resource_stack_next_.compare_exchange_strong(expected_next_field,
                                                                        target_next_field,
                                                                        std::memory_order_relaxed)) {
52 53 54
      return false;
    } else {
      expected_next_field = target_next_field;
55 56
    }

57
  } while (!this->resource_stack_root_.compare_exchange_strong(current_root, target_root, std::memory_order_acq_rel));
58 59

  return true;
60 61
}

62
void task::reset_task_chain(task *expected_content) {
63
  data_structures::stamped_integer current_root = this->resource_stack_root_.load(std::memory_order_relaxed);
64 65
  PLS_ASSERT(current_root.value_ == expected_content->thread_id_ + 1,
             "Must only reset the task chain if we exactly know its state! (current_root.value_)");
66

67 68
  data_structures::stamped_integer target_root;
  target_root.stamp_ = current_root.stamp_ + 1;
69
  this->resource_stack_root_.store(target_root, std::memory_order_relaxed);
70 71 72
}

task *task::pop_task_chain() {
73
  data_structures::stamped_integer current_root = this->resource_stack_root_.load(std::memory_order_relaxed);
74
  data_structures::stamped_integer target_root;
75 76
  task *output_task;
  do {
77
    // current_root implicitly re-loaded by CAS in loop
78 79 80
    if (current_root.value_ == 0) {
      // Empty...
      return nullptr;
81
    } else {
82 83
      // Found something, try to pop it
      auto *current_root_task = find_task(current_root.value_ - 1, this->depth_);
84
      auto next_stack_cas = current_root_task->resource_stack_next_.load(std::memory_order_relaxed);
85

86
      target_root.stamp_ = current_root.stamp_ + 1;
87
      target_root.value_ = next_stack_cas.value_;
88

89
      output_task = current_root_task;
90
    }
91
  } while (!this->resource_stack_root_.compare_exchange_strong(current_root, target_root, std::memory_order_acq_rel));
92

93
  output_task->resource_stack_next_.store({0, 0});
94 95 96 97
  return output_task;
}

}