Skip to content

Commit

Permalink
[Thread pool] Add wait_stop/request_stop (#38)
Browse files Browse the repository at this point in the history
* thread pool now operates on queues and threads instead workers
* task_queue satisfies executor concept now
* request stop + wait_stop methods for thread pool
---------

Co-authored-by: Андрей Будиловский [email protected]
  • Loading branch information
sabudilovskiy authored Nov 26, 2024
1 parent 27462f8 commit 0ec41fe
Show file tree
Hide file tree
Showing 6 changed files with 319 additions and 117 deletions.
1 change: 1 addition & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
SortIncludes: false
IndentPPDirectives: BeforeHash
RemoveBracesLLVM: true
8 changes: 8 additions & 0 deletions include/kelcoro/executor_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ struct task_node {
task_node* next = nullptr;
std::coroutine_handle<> task = nullptr;
schedule_errc status = schedule_errc::ok;

static task_node deadpill() {
return task_node{.next = nullptr, .task = nullptr};
}

bool is_deadpill() const noexcept {
return task == nullptr;
}
};

template <typename T>
Expand Down
91 changes: 91 additions & 0 deletions include/kelcoro/noexport/fixed_array.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#pragma once

#include <cassert>
#include <memory>
#include <memory_resource>
#include <type_traits>
#include <utility>

namespace dd::noexport {

template <typename T>
struct fixed_array {
private:
T* arr = nullptr;
size_t n = 0;
std::pmr::memory_resource* resource = std::pmr::new_delete_resource();

public:
static_assert(std::is_same_v<T, std::decay_t<T>>);
fixed_array() = default;
fixed_array(fixed_array&& rhs) noexcept {
*this = std::move(rhs);
}
fixed_array& operator=(fixed_array&& rhs) noexcept {
std::swap(arr, rhs.arr);
std::swap(n, rhs.n);
std::swap(resource, rhs.resource);
return *this;
}
fixed_array(size_t n, std::pmr::memory_resource& resource = *std::pmr::new_delete_resource()) : n(n) {
if (n == 0)
return;
arr = (T*)resource.allocate(sizeof(T) * n, alignof(T));
std::uninitialized_default_construct_n(arr, n);
}

std::pmr::memory_resource* get_resource() const {
assert(resource);
return resource;
}

T* data() {
return arr;
}

T* data() const {
return arr;
}

size_t size() const noexcept {
return n;
}

T* begin() noexcept {
return arr;
}

T* end() noexcept {
return arr + n;
}

const T* begin() const noexcept {
return arr;
}

const T* end() const noexcept {
return arr + n;
}

T& operator[](size_t i) noexcept {
assert(arr);
assert(i < n);
return arr[i];
}

const T& operator[](size_t i) const noexcept {
assert(arr);
assert(i < n);
return arr[i];
}

~fixed_array() {
if (arr) {
std::destroy_n(arr, n);
// assume noexcept
resource->deallocate(arr, sizeof(T) * n, alignof(T));
}
}
};

} // namespace dd::noexport
1 change: 1 addition & 0 deletions include/kelcoro/operation_hash.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <coroutine>
#include <memory>

#include "noexport/macro.hpp"

Expand Down
Loading

0 comments on commit 0ec41fe

Please sign in to comment.