Skip to content

Commit 41fecca

Browse files
[libc] add rwlock (#94156)
1 parent cccc437 commit 41fecca

35 files changed

+1836
-5
lines changed

libc/config/config.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@
4949
"LIBC_CONF_RAW_MUTEX_DEFAULT_SPIN_COUNT": {
5050
"value": 100,
5151
"doc": "Default number of spins before blocking if a mutex is in contention (default to 100)."
52+
},
53+
"LIBC_CONF_RWLOCK_DEFAULT_SPIN_COUNT": {
54+
"value": 100,
55+
"doc": "Default number of spins before blocking if a rwlock is in contention (default to 100)."
5256
}
5357
}
5458
}

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,15 @@ if(LLVM_LIBC_FULL_BUILD)
645645
libc.src.pthread.pthread_mutexattr_setrobust
646646
libc.src.pthread.pthread_mutexattr_settype
647647
libc.src.pthread.pthread_once
648+
libc.src.pthread.pthread_rwlock_init
649+
libc.src.pthread.pthread_rwlock_tryrdlock
650+
libc.src.pthread.pthread_rwlock_rdlock
651+
libc.src.pthread.pthread_rwlock_timedrdlock
652+
libc.src.pthread.pthread_rwlock_trywrlock
653+
libc.src.pthread.pthread_rwlock_wrlock
654+
libc.src.pthread.pthread_rwlock_timedwrlock
655+
libc.src.pthread.pthread_rwlock_unlock
656+
libc.src.pthread.pthread_rwlock_destroy
648657
libc.src.pthread.pthread_rwlockattr_destroy
649658
libc.src.pthread.pthread_rwlockattr_getkind_np
650659
libc.src.pthread.pthread_rwlockattr_getpshared

libc/config/linux/api.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ def PThreadAPI : PublicAPI<"pthread.h"> {
181181
"pthread_mutexattr_t",
182182
"pthread_once_t",
183183
"pthread_rwlockattr_t",
184+
"pthread_rwlock_t",
184185
"pthread_t",
185186
];
186187
}
@@ -270,6 +271,7 @@ def SysTypesAPI : PublicAPI<"sys/types.h"> {
270271
"pthread_mutexattr_t",
271272
"pthread_once_t",
272273
"pthread_rwlockattr_t",
274+
"pthread_rwlock_t",
273275
"pthread_t",
274276
"size_t",
275277
"ssize_t",

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,15 @@ if(LLVM_LIBC_FULL_BUILD)
726726
libc.src.pthread.pthread_mutexattr_setrobust
727727
libc.src.pthread.pthread_mutexattr_settype
728728
libc.src.pthread.pthread_once
729+
libc.src.pthread.pthread_rwlock_init
730+
libc.src.pthread.pthread_rwlock_tryrdlock
731+
libc.src.pthread.pthread_rwlock_rdlock
732+
libc.src.pthread.pthread_rwlock_timedrdlock
733+
libc.src.pthread.pthread_rwlock_trywrlock
734+
libc.src.pthread.pthread_rwlock_wrlock
735+
libc.src.pthread.pthread_rwlock_timedwrlock
736+
libc.src.pthread.pthread_rwlock_unlock
737+
libc.src.pthread.pthread_rwlock_destroy
729738
libc.src.pthread.pthread_rwlockattr_destroy
730739
libc.src.pthread.pthread_rwlockattr_getkind_np
731740
libc.src.pthread.pthread_rwlockattr_getpshared

libc/docs/configure.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ to learn about the defaults for your platform and target.
3636
- ``LIBC_CONF_PRINTF_FLOAT_TO_STR_USE_MEGA_LONG_DOUBLE_TABLE``: Use large table for better printf long double performance.
3737
* **"pthread" options**
3838
- ``LIBC_CONF_RAW_MUTEX_DEFAULT_SPIN_COUNT``: Default number of spins before blocking if a mutex is in contention (default to 100).
39+
- ``LIBC_CONF_RWLOCK_DEFAULT_SPIN_COUNT``: Default number of spins before blocking if a rwlock is in contention (default to 100).
3940
- ``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.
4041
* **"string" options**
4142
- ``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.

libc/include/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ add_gen_header(
332332
.llvm-libc-types.pthread_mutex_t
333333
.llvm-libc-types.pthread_mutexattr_t
334334
.llvm-libc-types.pthread_once_t
335+
.llvm-libc-types.pthread_rwlock_t
335336
.llvm-libc-types.pthread_rwlockattr_t
336337
.llvm-libc-types.pthread_t
337338
)

libc/include/llvm-libc-types/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ add_header(pthread_key_t HDR pthread_key_t.h)
5454
add_header(pthread_mutex_t HDR pthread_mutex_t.h DEPENDS .__futex_word .__mutex_type)
5555
add_header(pthread_mutexattr_t HDR pthread_mutexattr_t.h)
5656
add_header(pthread_once_t HDR pthread_once_t.h DEPENDS .__futex_word)
57+
add_header(pthread_rwlock_t HDR pthread_rwlock_t.h DEPENDS .__futex_word .pid_t)
5758
add_header(pthread_rwlockattr_t HDR pthread_rwlockattr_t.h)
5859
add_header(pthread_t HDR pthread_t.h DEPENDS .__thread_type)
5960
add_header(rlim_t HDR rlim_t.h)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//===-- Definition of pthread_mutex_t type --------------------------------===//
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_TYPES_PTHREAD_RWLOCK_T_H
10+
#define LLVM_LIBC_TYPES_PTHREAD_RWLOCK_T_H
11+
12+
#include "llvm-libc-types/__futex_word.h"
13+
#include "llvm-libc-types/pid_t.h"
14+
typedef struct {
15+
unsigned __is_pshared : 1;
16+
unsigned __preference : 1;
17+
int __state;
18+
pid_t __writer_tid;
19+
__futex_word __wait_queue_mutex;
20+
__futex_word __pending_readers;
21+
__futex_word __pending_writers;
22+
__futex_word __reader_serialization;
23+
__futex_word __writer_serialization;
24+
} pthread_rwlock_t;
25+
26+
#endif // LLVM_LIBC_TYPES_PTHREAD_RWLOCK_T_H

libc/include/pthread.h.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#define PTHREAD_STACK_MIN (1 << 14) // 16KB
1818

1919
#define PTHREAD_MUTEX_INITIALIZER {0}
20+
#define PTHREAD_RWLOCK_INITIALIZER {}
2021
#define PTHREAD_ONCE_INIT {0}
2122

2223
enum {

libc/spec/posix.td

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ def POSIX : StandardSpec<"POSIX"> {
113113
NamedType PThreadRWLockAttrTType = NamedType<"pthread_rwlockattr_t">;
114114
PtrType PThreadRWLockAttrTPtr = PtrType<PThreadRWLockAttrTType>;
115115
ConstType ConstPThreadRWLockAttrTPtr = ConstType<PThreadRWLockAttrTPtr>;
116+
RestrictedPtrType RestrictedPThreadRWLockAttrTPtr = RestrictedPtrType<PThreadRWLockAttrTType>;
117+
ConstType ConstRestrictedPThreadRWLockAttrTPtr = ConstType<RestrictedPThreadRWLockAttrTPtr>;
116118

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

131+
NamedType PThreadRWLockTType = NamedType<"pthread_rwlock_t">;
132+
PtrType PThreadRWLockTPtr = PtrType<PThreadRWLockTType>;
133+
RestrictedPtrType RestrictedPThreadRWLockTPtr = RestrictedPtrType<PThreadRWLockTType>;
134+
129135
PtrType PThreadTPtr = PtrType<PThreadTType>;
130136
RestrictedPtrType RestrictedPThreadTPtr = RestrictedPtrType<PThreadTType>;
131137

@@ -1003,6 +1009,7 @@ def POSIX : StandardSpec<"POSIX"> {
10031009
PThreadOnceCallback,
10041010
PThreadOnceT,
10051011
PThreadRWLockAttrTType,
1012+
PThreadRWLockTType,
10061013
PThreadStartT,
10071014
PThreadTSSDtorT,
10081015
PThreadTType,
@@ -1259,6 +1266,51 @@ def POSIX : StandardSpec<"POSIX"> {
12591266
RetValSpec<IntType>,
12601267
[ArgSpec<PThreadRWLockAttrTPtr>, ArgSpec<IntType>]
12611268
>,
1269+
FunctionSpec<
1270+
"pthread_rwlock_init",
1271+
RetValSpec<IntType>,
1272+
[ArgSpec<PThreadRWLockTPtr>, ArgSpec<ConstRestrictedPThreadRWLockAttrTPtr>]
1273+
>,
1274+
FunctionSpec<
1275+
"pthread_rwlock_tryrdlock",
1276+
RetValSpec<IntType>,
1277+
[ArgSpec<PThreadRWLockTPtr>]
1278+
>,
1279+
FunctionSpec<
1280+
"pthread_rwlock_trywrlock",
1281+
RetValSpec<IntType>,
1282+
[ArgSpec<PThreadRWLockTPtr>]
1283+
>,
1284+
FunctionSpec<
1285+
"pthread_rwlock_timedrdlock",
1286+
RetValSpec<IntType>,
1287+
[ArgSpec<RestrictedPThreadRWLockTPtr>, ArgSpec<ConstRestrictStructTimeSpecPtr>]
1288+
>,
1289+
FunctionSpec<
1290+
"pthread_rwlock_timedwrlock",
1291+
RetValSpec<IntType>,
1292+
[ArgSpec<RestrictedPThreadRWLockTPtr>, ArgSpec<ConstRestrictStructTimeSpecPtr>]
1293+
>,
1294+
FunctionSpec<
1295+
"pthread_rwlock_rdlock",
1296+
RetValSpec<IntType>,
1297+
[ArgSpec<PThreadRWLockTPtr>]
1298+
>,
1299+
FunctionSpec<
1300+
"pthread_rwlock_wrlock",
1301+
RetValSpec<IntType>,
1302+
[ArgSpec<PThreadRWLockTPtr>]
1303+
>,
1304+
FunctionSpec<
1305+
"pthread_rwlock_unlock",
1306+
RetValSpec<IntType>,
1307+
[ArgSpec<PThreadRWLockTPtr>]
1308+
>,
1309+
FunctionSpec<
1310+
"pthread_rwlock_destroy",
1311+
RetValSpec<IntType>,
1312+
[ArgSpec<PThreadRWLockTPtr>]
1313+
>,
12621314
]
12631315
>;
12641316

@@ -1616,6 +1668,7 @@ def POSIX : StandardSpec<"POSIX"> {
16161668
PThreadMutexTType,
16171669
PThreadOnceT,
16181670
PThreadRWLockAttrTType,
1671+
PThreadRWLockTType,
16191672
PThreadTType,
16201673
PidT,
16211674
SSizeTType,

libc/spec/spec.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ def TimeTType : NamedType<"time_t">;
126126
def StructTimeSpec : NamedType<"struct timespec">;
127127
def StructTimeSpecPtr : PtrType<StructTimeSpec>;
128128
def ConstStructTimeSpecPtr : ConstType<StructTimeSpecPtr>;
129+
def RestrictStructTimeSpecPtr : RestrictedPtrType<StructTimeSpec>;
130+
def ConstRestrictStructTimeSpecPtr : ConstType<RestrictStructTimeSpecPtr>;
129131

130132
def BSearchCompareT : NamedType<"__bsearchcompare_t">;
131133
def QSortCompareT : NamedType<"__qsortcompare_t">;

libc/src/__support/macros/attributes.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,10 @@
4242
#define LIBC_CONSTINIT
4343
#endif
4444

45+
#ifdef __clang__
46+
#define LIBC_PREFERED_TYPE(TYPE) [[clang::preferred_type(TYPE)]]
47+
#else
48+
#define LIBC_PREFERED_TYPE(TYPE)
49+
#endif
50+
4551
#endif // LLVM_LIBC_SRC___SUPPORT_MACROS_ATTRIBUTES_H

libc/src/__support/threads/linux/CMakeLists.txt

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ add_header_library(
2222
libc.src.__support.time.linux.abs_timeout
2323
)
2424

25-
set(raw_mutex_additional_flags)
25+
set(monotonicity_flags)
2626
if (LIBC_CONF_TIMEOUT_ENSURE_MONOTONICITY)
27-
set(raw_mutex_additional_flags -DLIBC_COPT_TIMEOUT_ENSURE_MONOTONICITY=1)
27+
set(monotonicity_flags -DLIBC_COPT_TIMEOUT_ENSURE_MONOTONICITY=1)
2828
else()
29-
set(raw_mutex_additional_flags -DLIBC_COPT_TIMEOUT_ENSURE_MONOTONICITY=0)
29+
set(monotonicity_flags -DLIBC_COPT_TIMEOUT_ENSURE_MONOTONICITY=0)
3030
endif()
3131

3232
add_header_library(
@@ -42,8 +42,22 @@ add_header_library(
4242
libc.hdr.types.pid_t
4343
COMPILE_OPTIONS
4444
-DLIBC_COPT_RAW_MUTEX_DEFAULT_SPIN_COUNT=${LIBC_CONF_RAW_MUTEX_DEFAULT_SPIN_COUNT}
45-
${raw_mutex_additional_flags}
46-
45+
${monotonicity_flags}
46+
)
47+
48+
add_header_library(
49+
rwlock
50+
HDRS
51+
rwlock.h
52+
DEPENDS
53+
.futex_utils
54+
.raw_mutex
55+
libc.src.__support.common
56+
libc.src.__support.OSUtil.osutil
57+
libc.src.__support.CPP.limits
58+
COMPILE_OPTIONS
59+
-DLIBC_COPT_RWLOCK_DEFAULT_SPIN_COUNT=${LIBC_CONF_RWLOCK_DEFAULT_SPIN_COUNT}
60+
${monotonicity_flags}
4761
)
4862

4963
add_header_library(

0 commit comments

Comments
 (0)