|
| 1 | +// Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <cassert> |
| 16 | +#include <mutex> |
| 17 | +#include <iostream> |
| 18 | +#include "dali/core/exec/tasking/scheduler.h" |
| 19 | + |
| 20 | +namespace dali::tasking { |
| 21 | + |
| 22 | +bool Scheduler::AcquireAllAndMoveToReady(SharedTask &task) noexcept { |
| 23 | + assert(task->state_ <= TaskState::Pending); |
| 24 | + |
| 25 | + // All or nothing - first we check that all preconditions are met |
| 26 | + for (auto &w : task->preconditions_) |
| 27 | + if (!w->IsAcquirable()) |
| 28 | + return false; // at least one unmet |
| 29 | + // If they are, we acquire them - this must succeed |
| 30 | + for (auto &w : task->preconditions_) |
| 31 | + if (!w->TryAcquire(task)) { |
| 32 | + std::cerr |
| 33 | + << "Internal error - resource acquisition failed for a resource known to be available" |
| 34 | + << std::endl; |
| 35 | + std::abort(); |
| 36 | + } |
| 37 | + |
| 38 | + task->preconditions_.clear(); |
| 39 | + task->state_ = TaskState::Ready; |
| 40 | + pending_.Remove(task); |
| 41 | + ready_.push(std::move(task)); |
| 42 | + return true; |
| 43 | +} |
| 44 | + |
| 45 | +void Scheduler::Notify(Waitable *w) { |
| 46 | + bool is_completion_event = dynamic_cast<CompletionEvent *>(w) != nullptr; |
| 47 | + bool is_task = is_completion_event && dynamic_cast<Task *>(w); |
| 48 | + |
| 49 | + int new_ready = 0; |
| 50 | + { |
| 51 | + std::lock_guard g(mtx_); |
| 52 | + if (is_task) |
| 53 | + task_done_.notify_all(); |
| 54 | + |
| 55 | + SmallVector<SharedTask, 8> waiting; |
| 56 | + int n = w->waiting_.size(); |
| 57 | + waiting.reserve(n); |
| 58 | + for (int i = 0; i < n; i++) |
| 59 | + waiting.emplace_back(w->waiting_[i]); |
| 60 | + |
| 61 | + for (auto &task : waiting) { |
| 62 | + // If the waitable is a completion event, it will never become unacquirable again. |
| 63 | + // Otherwise, we have to re-check it. |
| 64 | + if (!is_completion_event && !w->IsAcquirable()) |
| 65 | + break; |
| 66 | + if (task->Ready()) |
| 67 | + continue; |
| 68 | + |
| 69 | + // If the task has only one precondition or the waitable is a completion event, |
| 70 | + // then we can just try to acquire that waitable on behalf of the task. |
| 71 | + // A completion event, once complete, is never un-completed and all waiting threads |
| 72 | + // will be able to acquire it. This menas that we can eagerly acquire it without risking |
| 73 | + // deadlocks. This imposes less overhead than re-checking all preconditions each time. |
| 74 | + if (is_completion_event || |
| 75 | + (task->preconditions_.size() == 1 && task->preconditions_.begin()->get() == w)) { |
| 76 | + // try acquire - the only way this can fail is that the task was |
| 77 | + // re-checked in another thread and marked as ready... |
| 78 | + if (!w->TryAcquire(task)) { |
| 79 | + assert(task->preconditions_.size() != 1 || task->preconditions_.begin()->get() != w); |
| 80 | + continue; // ... if so, nothing to do |
| 81 | + } |
| 82 | + auto it = std::find_if(task->preconditions_.begin(), task->preconditions_.end(), |
| 83 | + [w](auto &pre) { return pre.get() == w; }); |
| 84 | + assert(it != task->preconditions_.end()); |
| 85 | + task->preconditions_.erase(it); |
| 86 | + if (task->Ready()) { |
| 87 | + pending_.Remove(task); |
| 88 | + task->state_ = TaskState::Ready; |
| 89 | + ready_.push(std::move(task)); |
| 90 | + new_ready++; |
| 91 | + // OK, the task is ready, we're done with it |
| 92 | + continue; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + if (AcquireAllAndMoveToReady(task)) |
| 97 | + new_ready++; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + |
| 102 | + if (new_ready == 1) |
| 103 | + this->task_ready_.notify_one(); |
| 104 | + else if (new_ready > 1) |
| 105 | + this->task_ready_.notify_all(); |
| 106 | +} |
| 107 | + |
| 108 | +} // namespace dali::tasking |
0 commit comments