abstract_task.cpp 3.03 KB
Newer Older
1 2
#include <easy/profiler.h>

3
#include "pls/internal/scheduling/thread_state.h"
4
#include "pls/internal/scheduling/abstract_task.h"
5
#include "pls/internal/scheduling/scheduler.h"
6 7 8 9

namespace pls {
    namespace internal {
        namespace scheduling {
10
            bool abstract_task::steal_work() {
11
                EASY_FUNCTION(profiler::colors::Orange);
12 13
                auto my_state = base::this_thread::state<thread_state>();
                auto my_scheduler = my_state->scheduler_;
14

15 16 17 18
                int my_id = my_state->id_;
                for (size_t i = 1; i < my_scheduler->num_threads(); i++) {
                    size_t target = (my_id + i) % my_scheduler->num_threads();
                    auto target_state = my_scheduler->thread_state_for(target);
19 20

                    // TODO: Cleaner Locking Using std::guarded_lock
21
                    EASY_BLOCK("Acquire Thread Lock", profiler::colors::Red)
22
                    target_state->lock_.lock();
23
                    EASY_END_BLOCK;
24 25

                    // Dig down to our level
26
                    EASY_BLOCK("Go to our level")
27 28 29 30
                    abstract_task* current_task = target_state->root_task_;
                    while (current_task != nullptr && current_task->depth() < depth()) {
                        current_task = current_task->child_task_;
                    }
31
                    EASY_END_BLOCK;
32

33
                    EASY_BLOCK("Internal Steal")
34 35 36 37 38 39
                    if (current_task != nullptr) {
                        // See if it equals our type and depth of task
                        if (current_task->unique_id_ == unique_id_ &&
                            current_task->depth_ == depth_) {
                            if (internal_stealing(current_task)) {
                                // internal steal was a success, hand it back to the internal scheduler
40
                                target_state->lock_.unlock();
41 42 43 44 45 46 47
                                return true;
                            }

                            // No success, we need to steal work from a deeper level using 'top level task stealing'
                            current_task = current_task->child_task_;
                        }
                    }
48
                    EASY_END_BLOCK;
49 50 51 52


                    // Execute 'top level task steal' if possible
                    // (only try deeper tasks to keep depth restricted stealing)
53
                    EASY_BLOCK("Top Level Steal")
54
                    while (current_task != nullptr) {
55 56
                        auto lock = &target_state->lock_;
                        if (current_task->split_task(lock)) {
57 58 59 60 61 62
                            // internal steal was no success (we did a top level task steal)
                            return false;
                        }

                        current_task = current_task->child_task_;
                    }
63
                    EASY_END_BLOCK;
64
                    target_state->lock_.unlock();
65 66 67 68 69
                }

                // internal steal was no success
                return false;
            };
70 71 72
        }
    }
}