summaryrefslogtreecommitdiff
path: root/libc/src/threads/thrd_join.cpp
diff options
context:
space:
mode:
authorSiva Chandra Reddy <sivachandra@google.com>2022-04-01 06:42:14 +0000
committerSiva Chandra Reddy <sivachandra@google.com>2022-04-07 16:13:21 +0000
commit2ce09e680a7dc1201463ae74e199eac66ac52a8d (patch)
tree4b14f88eb781045a5141c6c1fb51933bfe9f78f9 /libc/src/threads/thrd_join.cpp
parent0a77e633226b5598e426603c03e54d62ed275670 (diff)
[libc] Add a linux Thread class in __support/threads.
This change is essentially a mechanical change which moves the thread creation and join implementations from src/threads/linux to src/__support/threads/linux/thread.h. The idea being that, in future, a pthread implementation can reuse the common thread implementations in src/__support/threads. Reviewed By: lntue Differential Revision: https://reviews.llvm.org/D123287
Diffstat (limited to 'libc/src/threads/thrd_join.cpp')
-rw-r--r--libc/src/threads/thrd_join.cpp30
1 files changed, 30 insertions, 0 deletions
diff --git a/libc/src/threads/thrd_join.cpp b/libc/src/threads/thrd_join.cpp
new file mode 100644
index 000000000000..20096a7be1bf
--- /dev/null
+++ b/libc/src/threads/thrd_join.cpp
@@ -0,0 +1,30 @@
+//===-- Linux implementation of the thrd_join function --------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/threads/thrd_join.h"
+#include "src/__support/common.h"
+#include "src/__support/threads/thread.h"
+
+#include <threads.h> // For thrd_* type definitions.
+
+namespace __llvm_libc {
+
+static_assert(sizeof(thrd_t) == sizeof(__llvm_libc::Thread<int>),
+ "Mismatch between thrd_t and internal Thread<int>.");
+
+LLVM_LIBC_FUNCTION(int, thrd_join, (thrd_t * th, int *retval)) {
+ auto *thread = reinterpret_cast<Thread<int> *>(th);
+ int result = thread->join();
+ if (result == 0) {
+ *retval = thread->return_value();
+ return thrd_success;
+ }
+ return thrd_error;
+}
+
+} // namespace __llvm_libc