Skip to content

Commit

Permalink
Thread: Use TransientTokenRef instead of WeakRef
Browse files Browse the repository at this point in the history
  • Loading branch information
Sainan committed Dec 19, 2024
1 parent d6a79cb commit af922a4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
16 changes: 8 additions & 8 deletions soup/Thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ NAMESPACE_SOUP
detach();

auto data = new RunningData{ f, std::move(cap) };
this->running_data = WeakRef<RunningData>(data);
this->running_ref = data->transient_token;

#if SOUP_WINDOWS
handle = CreateThread(nullptr, 0, reinterpret_cast<DWORD(__stdcall*)(LPVOID)>(&threadCreateCallback), data, 0, nullptr);
SOUP_IF_UNLIKELY (handle == NULL)
{
handle = INVALID_HANDLE_VALUE;
this->running_data.reset();
this->running_ref.reset();
SOUP_THROW(Exception(format("Failed to create thread: {}", GetLastError())));
}
#else
Expand All @@ -48,7 +48,7 @@ NAMESPACE_SOUP
auto ret = pthread_create(&handle, &attr, reinterpret_cast<void*(*)(void*)>(&threadCreateCallback), data);
SOUP_IF_UNLIKELY (ret != 0)
{
this->running_data.reset();
this->running_ref.reset();
SOUP_THROW(Exception(format("Failed to create thread: {}", ret)));
}
have_handle = true;
Expand Down Expand Up @@ -83,14 +83,14 @@ NAMESPACE_SOUP
WaitForSingleObject(handle, INFINITE);
CloseHandle(handle);
handle = INVALID_HANDLE_VALUE;
running_data.reset();
running_ref.reset();
}
#else
if (have_handle)
{
pthread_join(handle, nullptr);
have_handle = false;
running_data.reset();
running_ref.reset();
}
#endif
}
Expand All @@ -114,7 +114,7 @@ NAMESPACE_SOUP
{
CloseHandle(t->handle);
t->handle = INVALID_HANDLE_VALUE;
t->running_data.reset();
t->running_ref.reset();
}
}
#else
Expand All @@ -132,14 +132,14 @@ NAMESPACE_SOUP
{
CloseHandle(handle);
handle = INVALID_HANDLE_VALUE;
running_data.reset();
running_ref.reset();
}
#else
if (have_handle)
{
pthread_detach(handle);
have_handle = false;
running_data.reset();
running_ref.reset();
}
#endif
}
Expand Down
6 changes: 3 additions & 3 deletions soup/Thread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
#include <vector>

#include "Capture.hpp"
#include "TransientToken.hpp"
#include "UniquePtr.hpp"
#include "WeakRef.hpp"

NAMESPACE_SOUP
{
Expand All @@ -34,7 +34,7 @@ NAMESPACE_SOUP
pthread_t handle{};
bool have_handle = false;
#endif
WeakRef<RunningData> running_data{};
TransientTokenRef running_ref;

explicit Thread() noexcept = default;
explicit Thread(void(*f)(Capture&&), Capture&& cap = {});
Expand All @@ -47,7 +47,7 @@ NAMESPACE_SOUP
#endif

[[nodiscard]] bool isAttached() const noexcept;
[[nodiscard]] bool isRunning() const noexcept { return running_data.isValid(); }
[[nodiscard]] bool isRunning() const noexcept { return running_ref.isValid(); }

void awaitCompletion() noexcept;
static void awaitCompletion(const std::vector<UniquePtr<Thread>>& threads) noexcept;
Expand Down

0 comments on commit af922a4

Please sign in to comment.