Skip to content

[libc] add rwlock #94156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 33 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
b30a9ee
[libc] add rwlock implementation
Jun 2, 2024
e36e959
[libc] clean up headers
Jun 2, 2024
8698a18
[libc] add another trylock before operating on the timestamp
Jun 2, 2024
dd92e94
[libc] more clean ups
Jun 2, 2024
c39679a
[libc] separate out the notification part
Jun 2, 2024
26952cc
[libc] correct permission of Guard
Jun 2, 2024
b3338a8
[libc] address CRs
Jun 2, 2024
3006c9a
[libc] address CRs
Jun 2, 2024
472152e
[libc] add pthread_rwlock_init
Jun 2, 2024
084c691
[libc] add pthread_rwlock_tryrdlock
Jun 2, 2024
8da7eaf
[libc] clean up again
Jun 3, 2024
8625bb3
[libc] add pthread_rwlock_rdlock
Jun 3, 2024
f8deb2e
[libc] adjust phase order
Jun 3, 2024
40517ca
[libc] add pthread_rwlock_timedrdlock
Jun 3, 2024
d9e6cd7
[libc] add remaining entrypoint functions
Jun 3, 2024
1913585
[libc] add missing explicit ordering
Jun 3, 2024
5a6a429
[libc] remove extra new expression
Jun 3, 2024
a1e66fe
address CRs
Jun 9, 2024
7caf23b
test and some fix (WIP)
Jun 9, 2024
d41483b
add many tests
Jun 9, 2024
10689de
check status
Jun 9, 2024
53c2788
fix tests
Jun 9, 2024
bd7491b
fix tests
Jun 9, 2024
d8f9d1f
fix silly bug
Jun 9, 2024
05c5f98
fix missed pshared flags
Jun 9, 2024
860db17
fix yet another bug
Jun 9, 2024
bb3cd9b
fix all bugs
Jun 9, 2024
33ee1f8
reduce waiting time
Jun 9, 2024
47a79ab
enable rwlock on aarch64
SchrodingerZhu Jun 14, 2024
05ddf87
address CRs
SchrodingerZhu Jun 14, 2024
c713f7f
fix auto added syscall
SchrodingerZhu Jun 14, 2024
dfc7ebd
fix cross process logging
SchrodingerZhu Jun 14, 2024
635c069
remove extra braces
SchrodingerZhu Jun 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -644,6 +644,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 @@ -725,6 +725,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