| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| //===--- A platform independent indirection for a thread class --*- C++ -*-===// | ||
| // | ||
| // 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef LLVM_LIBC_SRC_SUPPORT_THREADS_THREAD_H | ||
| #define LLVM_LIBC_SRC_SUPPORT_THREADS_THREAD_H | ||
|
|
||
| #include <stddef.h> | ||
|
|
||
| // The platform specific implemnetations are pulled via the following include. | ||
| // The idea is for the platform implementation to implement a class named Thread | ||
| // in the namespace __llvm_libc with the following properties: | ||
| // | ||
| // 1. Has a defaulted default constructor (not a default constructor). | ||
| // | ||
| // 2. Has a "run" method with the following signature: | ||
| // | ||
| // int run(ThreadRunner *f, void *arg, void *stack, size_t size); | ||
| // | ||
| // Returns: | ||
| // 0 on success and an error value on failure. | ||
| // Args: | ||
| // arg - The argument to be passed to the thread runner after the thread | ||
| // is created. | ||
| // stack - The stack to use for the thread. | ||
| // size - The stack size. | ||
| // | ||
| // If callers pass a non-null |stack| value, then it will assumed that | ||
| // 1. The clean up the stack memory is their responsibility | ||
| // 2. The guard area is setup appropriately by the caller. | ||
| // | ||
| // 3. Has a "join" method with the following signature: | ||
| // ErrorOr<ReturnType> join(); | ||
| // The "join" method should return 0 on success and set retcode to the | ||
| // threads return value. On failure, an appropriate errno value should be | ||
| // returned. | ||
| // | ||
| // 4. Has an operator== for comparison between two threads. | ||
| #ifdef __unix__ | ||
| #include "linux/thread.h" | ||
| #endif // __unix__ | ||
|
|
||
| #endif // LLVM_LIBC_SRC_SUPPORT_THREADS_THREAD_H |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| //===--- A data type for thread attributes ----------------------*- C++ -*-===// | ||
| // | ||
| // 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef LLVM_LIBC_SRC_SUPPORT_THREADS_THREAD_ATTRIB_H | ||
| #define LLVM_LIBC_SRC_SUPPORT_THREADS_THREAD_ATTRIB_H | ||
|
|
||
| namespace __llvm_libc { | ||
|
|
||
| // A data type to hold common thread attributes which have to be stored as | ||
| // thread state. A platform thread implementation should store the attrib object | ||
| // in its Thread data structure. Note that this is different from public | ||
| // attribute types like pthread_attr which contain information which need not | ||
| // be saved as part of a thread's state. For example, the stack guard size. | ||
| template <typename ReturnType> struct ThreadAttributes { | ||
| void *stack; // Pointer to the thread stack | ||
| unsigned long long stack_size; // Size of the stack | ||
| unsigned char owned_stack; // Indicates if the thread owns this stack memory | ||
| ReturnType retval; // The return value of thread runner is saved here | ||
| int tid; | ||
| }; | ||
|
|
||
| } // namespace __llvm_libc | ||
|
|
||
| #endif // LLVM_LIBC_SRC_SUPPORT_THREADS_THREAD_ATTRIB_H |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| //===-- Linux implementation of the thrd_create 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_create.h" | ||
| #include "src/__support/common.h" | ||
| #include "src/__support/threads/thread.h" | ||
|
|
||
| #include <errno.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_create, | ||
| (thrd_t * th, thrd_start_t func, void *arg)) { | ||
| auto *thread = reinterpret_cast<__llvm_libc::Thread<int> *>(th); | ||
| int result = thread->run(func, arg, nullptr, 0); | ||
| if (result == 0) | ||
| return thrd_success; | ||
| else if (result == ENOMEM) | ||
| return thrd_nomem; | ||
| else | ||
| return thrd_error; | ||
| } | ||
|
|
||
| } // namespace __llvm_libc |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |