Skip to content
Snippets Groups Projects
Commit ca6280c4 authored by Nils-Arne Dreier's avatar Nils-Arne Dreier
Browse files

fix: avoid deconstruction of std::function in tsqueue

parent a5e7be0c
No related branches found
No related tags found
No related merge requests found
......@@ -104,7 +104,14 @@ PYBIND11_MODULE(coyote, m) {
m.def("get_field_metadata", &get_field_metadata);
m.def("run", &coyote_run, py::call_guard<py::gil_scoped_release>());
m.def("init", &coyote_init, "group_name"_a, "nthreads"_a = 1);
m.def("add_task", &coyote_add_task, "task"_a, "prio"_a = 0);
m.def(
"add_task",
[](std::function<void()> task, int prio) {
py::gil_scoped_release release;
coyote_add_task(task, prio);
},
"task"_a,
"prio"_a = 0);
m.def("start_datetime", &coyote_start_datetime);
m.def("end_datetime", &coyote_end_datetime);
m.def("group_comm", []() { return MPI_Comm_c2f(coyote_group_comm()); });
......
......@@ -160,6 +160,6 @@ namespace coyote {
}
void CoyoteEnv::add_task(std::function<void()> task, int prio) {
_task_queue.enqueue(task, {prio, ""});
_task_queue.enqueue(std::move(task), {prio, ""});
}
} // namespace coyote
......@@ -13,34 +13,37 @@ class QueueTerminated {};
template <class T, class P, class Compare = std::less<P>>
class TSQueue {
private:
typedef std::optional<std::tuple<P, T>> elem_type;
typedef std::tuple<P, std::shared_ptr<T>> elem_type;
public:
TSQueue() : q(), m(), c() {}
~TSQueue() {}
void enqueue(T t, P prio) {
void enqueue(T&& t, P prio) {
std::lock_guard<std::mutex> lock(m);
q.push(std::make_tuple(prio, t));
q.push(std::make_tuple(prio, std::make_shared<T>(std::forward<T>(t))));
c.notify_one();
}
void end() {
std::lock_guard<std::mutex> lock(m);
q.push(std::nullopt);
q.push({});
c.notify_all();
}
T dequeue() {
std::unique_lock<std::mutex> lock(m);
c.wait(lock, [&] { return !q.empty(); });
elem_type val = q.top();
if (!val) {
throw QueueTerminated();
elem_type val;
{
std::unique_lock<std::mutex> lock(m);
c.wait(lock, [&] { return !q.empty(); });
val = q.top();
if (!std::get<1>(val)) {
throw QueueTerminated();
}
q.pop();
}
q.pop();
return std::get<1>(val.value());
return *std::get<1>(val);
}
bool terminated() {
......@@ -58,11 +61,11 @@ class TSQueue {
struct _Compare {
Compare compare;
bool operator()(const elem_type& a, const elem_type& b) const {
if (!a)
if (!std::get<1>(a))
return true;
if (!b)
if (!std::get<1>(b))
return false;
return compare(std::get<0>(a.value()), std::get<0>(b.value()));
return compare(std::get<0>(a), std::get<0>(b));
}
};
std::priority_queue<elem_type, std::vector<elem_type>, _Compare> q;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment