Skip to content

Commit

Permalink
[libc] add rwlock (llvm#94156)
Browse files Browse the repository at this point in the history
  • Loading branch information
SchrodingerZhu authored Jun 14, 2024
1 parent cccc437 commit 41fecca
Show file tree
Hide file tree
Showing 35 changed files with 1,836 additions and 5 deletions.
4 changes: 4 additions & 0 deletions libc/config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
"LIBC_CONF_RAW_MUTEX_DEFAULT_SPIN_COUNT": {
"value": 100,
"doc": "Default number of spins before blocking if a mutex is in contention (default to 100)."
},
"LIBC_CONF_RWLOCK_DEFAULT_SPIN_COUNT": {
"value": 100,
"doc": "Default number of spins before blocking if a rwlock is in contention (default to 100)."
}
}
}
9 changes: 9 additions & 0 deletions libc/config/linux/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,15 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.pthread.pthread_mutexattr_setrobust
libc.src.pthread.pthread_mutexattr_settype
libc.src.pthread.pthread_once
libc.src.pthread.pthread_rwlock_init
libc.src.pthread.pthread_rwlock_tryrdlock
libc.src.pthread.pthread_rwlock_rdlock
libc.src.pthread.pthread_rwlock_timedrdlock
libc.src.pthread.pthread_rwlock_trywrlock
libc.src.pthread.pthread_rwlock_wrlock
libc.src.pthread.pthread_rwlock_timedwrlock
libc.src.pthread.pthread_rwlock_unlock
libc.src.pthread.pthread_rwlock_destroy
libc.src.pthread.pthread_rwlockattr_destroy
libc.src.pthread.pthread_rwlockattr_getkind_np
libc.src.pthread.pthread_rwlockattr_getpshared
Expand Down
2 changes: 2 additions & 0 deletions libc/config/linux/api.td
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ def PThreadAPI : PublicAPI<"pthread.h"> {
"pthread_mutexattr_t",
"pthread_once_t",
"pthread_rwlockattr_t",
"pthread_rwlock_t",
"pthread_t",
];
}
Expand Down Expand Up @@ -270,6 +271,7 @@ def SysTypesAPI : PublicAPI<"sys/types.h"> {
"pthread_mutexattr_t",
"pthread_once_t",
"pthread_rwlockattr_t",
"pthread_rwlock_t",
"pthread_t",
"size_t",
"ssize_t",
Expand Down
9 changes: 9 additions & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,15 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.pthread.pthread_mutexattr_setrobust
libc.src.pthread.pthread_mutexattr_settype
libc.src.pthread.pthread_once
libc.src.pthread.pthread_rwlock_init
libc.src.pthread.pthread_rwlock_tryrdlock
libc.src.pthread.pthread_rwlock_rdlock
libc.src.pthread.pthread_rwlock_timedrdlock
libc.src.pthread.pthread_rwlock_trywrlock
libc.src.pthread.pthread_rwlock_wrlock
libc.src.pthread.pthread_rwlock_timedwrlock
libc.src.pthread.pthread_rwlock_unlock
libc.src.pthread.pthread_rwlock_destroy
libc.src.pthread.pthread_rwlockattr_destroy
libc.src.pthread.pthread_rwlockattr_getkind_np
libc.src.pthread.pthread_rwlockattr_getpshared
Expand Down
1 change: 1 addition & 0 deletions libc/docs/configure.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ to learn about the defaults for your platform and target.
- ``LIBC_CONF_PRINTF_FLOAT_TO_STR_USE_MEGA_LONG_DOUBLE_TABLE``: Use large table for better printf long double performance.
* **"pthread" options**
- ``LIBC_CONF_RAW_MUTEX_DEFAULT_SPIN_COUNT``: Default number of spins before blocking if a mutex is in contention (default to 100).
- ``LIBC_CONF_RWLOCK_DEFAULT_SPIN_COUNT``: Default number of spins before blocking if a rwlock is in contention (default to 100).
- ``LIBC_CONF_TIMEOUT_ENSURE_MONOTONICITY``: Automatically adjust timeout to CLOCK_MONOTONIC (default to true). POSIX API may require CLOCK_REALTIME, which can be unstable and leading to unexpected behavior. This option will convert the real-time timestamp to monotonic timestamp relative to the time of call.
* **"string" options**
- ``LIBC_CONF_MEMSET_X86_USE_SOFTWARE_PREFETCHING``: Inserts prefetch for write instructions (PREFETCHW) for memset on x86 to recover performance when hardware prefetcher is disabled.
Expand Down
1 change: 1 addition & 0 deletions libc/include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ add_gen_header(
.llvm-libc-types.pthread_mutex_t
.llvm-libc-types.pthread_mutexattr_t
.llvm-libc-types.pthread_once_t
.llvm-libc-types.pthread_rwlock_t
.llvm-libc-types.pthread_rwlockattr_t
.llvm-libc-types.pthread_t
)
Expand Down
1 change: 1 addition & 0 deletions libc/include/llvm-libc-types/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ add_header(pthread_key_t HDR pthread_key_t.h)
add_header(pthread_mutex_t HDR pthread_mutex_t.h DEPENDS .__futex_word .__mutex_type)
add_header(pthread_mutexattr_t HDR pthread_mutexattr_t.h)
add_header(pthread_once_t HDR pthread_once_t.h DEPENDS .__futex_word)
add_header(pthread_rwlock_t HDR pthread_rwlock_t.h DEPENDS .__futex_word .pid_t)
add_header(pthread_rwlockattr_t HDR pthread_rwlockattr_t.h)
add_header(pthread_t HDR pthread_t.h DEPENDS .__thread_type)
add_header(rlim_t HDR rlim_t.h)
Expand Down
26 changes: 26 additions & 0 deletions libc/include/llvm-libc-types/pthread_rwlock_t.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//===-- Definition of pthread_mutex_t type --------------------------------===//
//
// 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_TYPES_PTHREAD_RWLOCK_T_H
#define LLVM_LIBC_TYPES_PTHREAD_RWLOCK_T_H

#include "llvm-libc-types/__futex_word.h"
#include "llvm-libc-types/pid_t.h"
typedef struct {
unsigned __is_pshared : 1;
unsigned __preference : 1;
int __state;
pid_t __writer_tid;
__futex_word __wait_queue_mutex;
__futex_word __pending_readers;
__futex_word __pending_writers;
__futex_word __reader_serialization;
__futex_word __writer_serialization;
} pthread_rwlock_t;

#endif // LLVM_LIBC_TYPES_PTHREAD_RWLOCK_T_H
1 change: 1 addition & 0 deletions libc/include/pthread.h.def
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#define PTHREAD_STACK_MIN (1 << 14) // 16KB

#define PTHREAD_MUTEX_INITIALIZER {0}
#define PTHREAD_RWLOCK_INITIALIZER {}
#define PTHREAD_ONCE_INIT {0}

enum {
Expand Down
53 changes: 53 additions & 0 deletions libc/spec/posix.td
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ def POSIX : StandardSpec<"POSIX"> {
NamedType PThreadRWLockAttrTType = NamedType<"pthread_rwlockattr_t">;
PtrType PThreadRWLockAttrTPtr = PtrType<PThreadRWLockAttrTType>;
ConstType ConstPThreadRWLockAttrTPtr = ConstType<PThreadRWLockAttrTPtr>;
RestrictedPtrType RestrictedPThreadRWLockAttrTPtr = RestrictedPtrType<PThreadRWLockAttrTType>;
ConstType ConstRestrictedPThreadRWLockAttrTPtr = ConstType<RestrictedPThreadRWLockAttrTPtr>;

NamedType PThreadMutexAttrTType = NamedType<"pthread_mutexattr_t">;
PtrType PThreadMutexAttrTPtr = PtrType<PThreadMutexAttrTType>;
Expand All @@ -126,6 +128,10 @@ def POSIX : StandardSpec<"POSIX"> {
ConstType ConstPThreadMutexTPtr = ConstType<PThreadMutexTPtr>;
ConstType ConstRestrictedPThreadMutexTPtr = ConstType<RestrictedPThreadMutexTPtr>;

NamedType PThreadRWLockTType = NamedType<"pthread_rwlock_t">;
PtrType PThreadRWLockTPtr = PtrType<PThreadRWLockTType>;
RestrictedPtrType RestrictedPThreadRWLockTPtr = RestrictedPtrType<PThreadRWLockTType>;

PtrType PThreadTPtr = PtrType<PThreadTType>;
RestrictedPtrType RestrictedPThreadTPtr = RestrictedPtrType<PThreadTType>;

Expand Down Expand Up @@ -1003,6 +1009,7 @@ def POSIX : StandardSpec<"POSIX"> {
PThreadOnceCallback,
PThreadOnceT,
PThreadRWLockAttrTType,
PThreadRWLockTType,
PThreadStartT,
PThreadTSSDtorT,
PThreadTType,
Expand Down Expand Up @@ -1259,6 +1266,51 @@ def POSIX : StandardSpec<"POSIX"> {
RetValSpec<IntType>,
[ArgSpec<PThreadRWLockAttrTPtr>, ArgSpec<IntType>]
>,
FunctionSpec<
"pthread_rwlock_init",
RetValSpec<IntType>,
[ArgSpec<PThreadRWLockTPtr>, ArgSpec<ConstRestrictedPThreadRWLockAttrTPtr>]
>,
FunctionSpec<
"pthread_rwlock_tryrdlock",
RetValSpec<IntType>,
[ArgSpec<PThreadRWLockTPtr>]
>,
FunctionSpec<
"pthread_rwlock_trywrlock",
RetValSpec<IntType>,
[ArgSpec<PThreadRWLockTPtr>]
>,
FunctionSpec<
"pthread_rwlock_timedrdlock",
RetValSpec<IntType>,
[ArgSpec<RestrictedPThreadRWLockTPtr>, ArgSpec<ConstRestrictStructTimeSpecPtr>]
>,
FunctionSpec<
"pthread_rwlock_timedwrlock",
RetValSpec<IntType>,
[ArgSpec<RestrictedPThreadRWLockTPtr>, ArgSpec<ConstRestrictStructTimeSpecPtr>]
>,
FunctionSpec<
"pthread_rwlock_rdlock",
RetValSpec<IntType>,
[ArgSpec<PThreadRWLockTPtr>]
>,
FunctionSpec<
"pthread_rwlock_wrlock",
RetValSpec<IntType>,
[ArgSpec<PThreadRWLockTPtr>]
>,
FunctionSpec<
"pthread_rwlock_unlock",
RetValSpec<IntType>,
[ArgSpec<PThreadRWLockTPtr>]
>,
FunctionSpec<
"pthread_rwlock_destroy",
RetValSpec<IntType>,
[ArgSpec<PThreadRWLockTPtr>]
>,
]
>;

Expand Down Expand Up @@ -1616,6 +1668,7 @@ def POSIX : StandardSpec<"POSIX"> {
PThreadMutexTType,
PThreadOnceT,
PThreadRWLockAttrTType,
PThreadRWLockTType,
PThreadTType,
PidT,
SSizeTType,
Expand Down
2 changes: 2 additions & 0 deletions libc/spec/spec.td
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ def TimeTType : NamedType<"time_t">;
def StructTimeSpec : NamedType<"struct timespec">;
def StructTimeSpecPtr : PtrType<StructTimeSpec>;
def ConstStructTimeSpecPtr : ConstType<StructTimeSpecPtr>;
def RestrictStructTimeSpecPtr : RestrictedPtrType<StructTimeSpec>;
def ConstRestrictStructTimeSpecPtr : ConstType<RestrictStructTimeSpecPtr>;

def BSearchCompareT : NamedType<"__bsearchcompare_t">;
def QSortCompareT : NamedType<"__qsortcompare_t">;
Expand Down
6 changes: 6 additions & 0 deletions libc/src/__support/macros/attributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,10 @@
#define LIBC_CONSTINIT
#endif

#ifdef __clang__
#define LIBC_PREFERED_TYPE(TYPE) [[clang::preferred_type(TYPE)]]
#else
#define LIBC_PREFERED_TYPE(TYPE)
#endif

#endif // LLVM_LIBC_SRC___SUPPORT_MACROS_ATTRIBUTES_H
24 changes: 19 additions & 5 deletions libc/src/__support/threads/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ add_header_library(
libc.src.__support.time.linux.abs_timeout
)

set(raw_mutex_additional_flags)
set(monotonicity_flags)
if (LIBC_CONF_TIMEOUT_ENSURE_MONOTONICITY)
set(raw_mutex_additional_flags -DLIBC_COPT_TIMEOUT_ENSURE_MONOTONICITY=1)
set(monotonicity_flags -DLIBC_COPT_TIMEOUT_ENSURE_MONOTONICITY=1)
else()
set(raw_mutex_additional_flags -DLIBC_COPT_TIMEOUT_ENSURE_MONOTONICITY=0)
set(monotonicity_flags -DLIBC_COPT_TIMEOUT_ENSURE_MONOTONICITY=0)
endif()

add_header_library(
Expand All @@ -42,8 +42,22 @@ add_header_library(
libc.hdr.types.pid_t
COMPILE_OPTIONS
-DLIBC_COPT_RAW_MUTEX_DEFAULT_SPIN_COUNT=${LIBC_CONF_RAW_MUTEX_DEFAULT_SPIN_COUNT}
${raw_mutex_additional_flags}

${monotonicity_flags}
)

add_header_library(
rwlock
HDRS
rwlock.h
DEPENDS
.futex_utils
.raw_mutex
libc.src.__support.common
libc.src.__support.OSUtil.osutil
libc.src.__support.CPP.limits
COMPILE_OPTIONS
-DLIBC_COPT_RWLOCK_DEFAULT_SPIN_COUNT=${LIBC_CONF_RWLOCK_DEFAULT_SPIN_COUNT}
${monotonicity_flags}
)

add_header_library(
Expand Down
Loading

0 comments on commit 41fecca

Please sign in to comment.