1 //===-- Linux specific definitions for threads implementations. --*- C++ -*===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_LIBC_SRC_THREADS_LINUX_THREAD_UTILS_H
10 #define LLVM_LIBC_SRC_THREADS_LINUX_THREAD_UTILS_H
11 
12 #include "thread_start_args.h"
13 
14 #include <stdatomic.h>
15 #include <stdint.h>
16 
17 namespace __llvm_libc {
18 
19 // The futex data has to be exactly 4 bytes long. However, we use a uint type
20 // here as we do not want to use `_Atomic uint32_t` as the _Atomic keyword which
21 // is C only. The header stdatomic.h does not define an atomic type
22 // corresponding to `uint32_t` or to something which is exactly 4 bytes wide.
23 using FutexData = atomic_uint;
24 
25 // We use a tri-state mutex because we want to avoid making syscalls
26 // as much as possible. In `mtx_unlock` a syscall to wake waiting threads is
27 // made only if the mutex status is `MutexStatus::Waiting`.
28 enum MutexStatus : uint32_t { MS_Free, MS_Locked, MS_Waiting };
29 
30 static_assert(sizeof(atomic_uint) == 4,
31               "Size of the `atomic_uint` type is not 4 bytes on your platform. "
32               "The implementation of the standard threads library for linux "
33               "requires that size of `atomic_uint` be 4 bytes.");
34 
35 struct ThreadParams {
36   static constexpr uintptr_t DefaultStackSize = 1 << 16; // 64 KB
37   static constexpr uint32_t ClearTIDValue = 0xABCD1234;
38 };
39 
40 } // namespace __llvm_libc
41 
42 #endif // LLVM_LIBC_SRC_THREADS_LINUX_THREAD_UTILS_H
43