Skip to content

Commit

Permalink
Proposal for fixing the issue skypjack#292
Browse files Browse the repository at this point in the history
Signed-off-by: Fiorentino, Stefano <stefano.fiorentino@adesso.ch>
  • Loading branch information
fiorentino-at-adesso committed Jul 10, 2023
1 parent d88bfca commit 4b89d01
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/uvw/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ namespace uvw {
UVW_INLINE thread::thread(loop::token token, std::shared_ptr<loop> ref, task t, std::shared_ptr<void> d) noexcept
: uv_type{token, std::move(ref)},
data{std::move(d)},
func{std::move(t)} {}
func{std::move(t)},
joined{false} {}

UVW_INLINE void thread::create_callback(void *arg) {
thread &curr = *(static_cast<thread *>(arg));
Expand Down Expand Up @@ -42,7 +43,11 @@ UVW_INLINE bool thread::run(create_flags opts, std::size_t stack) noexcept {
}

UVW_INLINE bool thread::join() noexcept {
return (0 == uv_thread_join(raw()));
if(!joined && 0 == uv_thread_join(raw())) {
joined = true;
return joined;
}
return false;
}

UVW_INLINE thread_local_storage::thread_local_storage(loop::token token, std::shared_ptr<loop> ref) noexcept
Expand Down
1 change: 1 addition & 0 deletions src/uvw/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class thread final: public uv_type<uv_thread_t> {
private:
std::shared_ptr<void> data;
task func;
bool joined;
};

/**
Expand Down
11 changes: 11 additions & 0 deletions test/uvw/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ TEST(Thread, Run) {
loop->run();
}

TEST(Thread, Join) {
auto loop = uvw::loop::get_default();
auto cb = [](std::shared_ptr<void> d) -> void {
};

auto handle = loop->resource<uvw::thread>(cb);
handle->run();
ASSERT_TRUE(handle->join());
ASSERT_FALSE(handle->join());
}

TEST(ThreadLocalStorage, SetGet) {
auto loop = uvw::loop::get_default();
auto localStorage = loop->resource<uvw::thread_local_storage>();
Expand Down

0 comments on commit 4b89d01

Please sign in to comment.