aboutsummaryrefslogtreecommitdiff
path: root/works/life/operating-system-experiment/Thread.cpp
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-06-09 22:31:11 +0800
committercrupest <crupest@outlook.com>2021-06-09 22:31:11 +0800
commit59be99aad9d7ad36e3edf648dbbd6b5b215c125c (patch)
tree73cf88107c915ed1657d6e2a6b9584cc48c52113 /works/life/operating-system-experiment/Thread.cpp
parent8268724a054a42f9446736c42d8f04a0207aa12d (diff)
parent8bdb5db67fa3eeda755511f91fd257bde548a18f (diff)
downloadcrupest-59be99aad9d7ad36e3edf648dbbd6b5b215c125c.tar.gz
crupest-59be99aad9d7ad36e3edf648dbbd6b5b215c125c.tar.bz2
crupest-59be99aad9d7ad36e3edf648dbbd6b5b215c125c.zip
import(life): Merge branch 'main' of https://github.com/crupest/life
Diffstat (limited to 'works/life/operating-system-experiment/Thread.cpp')
-rw-r--r--works/life/operating-system-experiment/Thread.cpp53
1 files changed, 48 insertions, 5 deletions
diff --git a/works/life/operating-system-experiment/Thread.cpp b/works/life/operating-system-experiment/Thread.cpp
index b91ca69..8230c1c 100644
--- a/works/life/operating-system-experiment/Thread.cpp
+++ b/works/life/operating-system-experiment/Thread.cpp
@@ -2,21 +2,36 @@
#include <cassert>
#include <exception>
+#include <pthread.h>
#include <utility>
namespace cru {
Thread::Thread(Thread &&other) noexcept
- : joined_(other.joined_), thread_handle_(other.thread_handle_) {
+ : detached_(other.detached_), joined_(other.joined_),
+#ifdef CRU_WINDOWS
+ thread_handle_(other.thread_handle_)
+#else
+ thread_(std::move(other.thread_))
+#endif
+{
other.joined_ = false;
+#ifdef CRU_WINDOWS
other.thread_handle_ = nullptr;
-}
+#endif
+} // namespace cru
Thread &Thread::operator=(Thread &&other) noexcept {
if (this != &other) {
+ detached_ = other.detached_;
joined_ = other.joined_;
+#ifdef CRU_WINDOWS
thread_handle_ = other.thread_handle_;
- other.joined_ = false;
other.thread_handle_ = nullptr;
+#else
+ thread_ = std::move(other.thread_);
+#endif
+ other.detached_ = false;
+ other.joined_ = false;
}
return *this;
@@ -25,13 +40,23 @@ Thread &Thread::operator=(Thread &&other) noexcept {
Thread::~Thread() { Destroy(); }
void Thread::Join() {
- assert(thread_handle_);
joined_ = true;
+#ifdef CRU_WINDOWS
+ assert(thread_handle_);
WaitForSingleObject(thread_handle_, INFINITE);
+#else
+ assert(thread_);
+ auto c = pthread_join(*thread_, nullptr);
+ assert(c == 0);
+#endif
}
void Thread::Detach() {
+#ifdef CRU_WINDOWS
assert(thread_handle_);
+#else
+ assert(thread_);
+#endif
detached_ = true;
}
@@ -45,11 +70,22 @@ void Thread::swap(Thread &other) noexcept {
}
void Thread::Destroy() noexcept {
- if (!detached_ && !joined_ && thread_handle_ != nullptr) {
+ if (!detached_ && !joined_ &&
+#ifdef CRU_WINDOWS
+ thread_handle_ != nullptr
+#else
+ thread_ != nullptr
+#endif
+ ) {
std::terminate();
} else {
+ detached_ = false;
joined_ = false;
+#ifdef CRU_WINDOWS
thread_handle_ = nullptr;
+#else
+ thread_ = nullptr;
+#endif
}
}
@@ -62,6 +98,13 @@ DWORD WINAPI ThreadProc(_In_ LPVOID lpParameter) {
return 0;
}
#else
+void *ThreadProc(void *data) {
+ auto p = static_cast<std::function<void()> *>(data);
+ (*p)();
+ delete p;
+ return nullptr;
+}
+
#endif
} // namespace details
} // namespace cru \ No newline at end of file