Skip to content

Commit dd79632

Browse files
[libc][POSIX][pthreads] implement pthread_rwlockattr_t functions (#89322)
Implement: - pthread_rwlockattr_destroy - pthread_rwlockattr_getpshared - pthread_rwlockattr_init - pthread_rwlockattr_setpshared
1 parent 0336116 commit dd79632

17 files changed

+352
-5
lines changed

libc/config/linux/api.td

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,12 @@ def PThreadAPI : PublicAPI<"pthread.h"> {
176176
"__pthread_tss_dtor_t",
177177
"pthread_attr_t",
178178
"pthread_condattr_t",
179+
"pthread_key_t",
179180
"pthread_mutex_t",
180181
"pthread_mutexattr_t",
181-
"pthread_t",
182-
"pthread_key_t",
183182
"pthread_once_t",
183+
"pthread_rwlockattr_t",
184+
"pthread_t",
184185
];
185186
}
186187

@@ -259,6 +260,7 @@ def SysTypesAPI : PublicAPI<"sys/types.h"> {
259260
"pthread_mutex_t",
260261
"pthread_mutexattr_t",
261262
"pthread_once_t",
263+
"pthread_rwlockattr_t",
262264
"pthread_t",
263265
"size_t",
264266
"ssize_t",

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,10 @@ if(LLVM_LIBC_FULL_BUILD)
669669
libc.src.pthread.pthread_mutexattr_setrobust
670670
libc.src.pthread.pthread_mutexattr_settype
671671
libc.src.pthread.pthread_once
672+
libc.src.pthread.pthread_rwlockattr_destroy
673+
libc.src.pthread.pthread_rwlockattr_getpshared
674+
libc.src.pthread.pthread_rwlockattr_init
675+
libc.src.pthread.pthread_rwlockattr_setpshared
672676
libc.src.pthread.pthread_setspecific
673677

674678
# sched.h entrypoints

libc/include/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,11 +322,12 @@ add_gen_header(
322322
.llvm-libc-types.__pthread_tss_dtor_t
323323
.llvm-libc-types.pthread_attr_t
324324
.llvm-libc-types.pthread_condattr_t
325+
.llvm-libc-types.pthread_key_t
325326
.llvm-libc-types.pthread_mutex_t
326327
.llvm-libc-types.pthread_mutexattr_t
327-
.llvm-libc-types.pthread_t
328-
.llvm-libc-types.pthread_key_t
329328
.llvm-libc-types.pthread_once_t
329+
.llvm-libc-types.pthread_rwlockattr_t
330+
.llvm-libc-types.pthread_t
330331
)
331332

332333
add_gen_header(

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_rwlockattr_t HDR pthread_rwlockattr_t.h)
5758
add_header(pthread_t HDR pthread_t.h DEPENDS .__thread_type)
5859
add_header(rlim_t HDR rlim_t.h)
5960
add_header(time_t HDR time_t.h)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//===-- Definition of pthread_rwlockattr_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+
#ifndef LLVM_LIBC_TYPES_PTHREAD_RWLOCKATTR_T_H
9+
#define LLVM_LIBC_TYPES_PTHREAD_RWLOCKATTR_T_H
10+
11+
typedef struct {
12+
int pshared;
13+
} pthread_rwlockattr_t;
14+
15+
#endif // LLVM_LIBC_TYPES_PTHREAD_RWLOCKATTR_T_H

libc/spec/posix.td

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ def POSIX : StandardSpec<"POSIX"> {
110110
PtrType PThreadCondAttrTPtr = PtrType<PThreadCondAttrTType>;
111111
ConstType ConstRestrictedPThreadCondAttrTPtr = ConstType<RestrictedPtrType<PThreadCondAttrTType>>;
112112

113+
NamedType PThreadRWLockAttrTType = NamedType<"pthread_rwlockattr_t">;
114+
PtrType PThreadRWLockAttrTPtr = PtrType<PThreadRWLockAttrTType>;
115+
ConstType ConstPThreadRWLockAttrTPtr = ConstType<PThreadRWLockAttrTPtr>;
116+
113117
NamedType PThreadMutexAttrTType = NamedType<"pthread_mutexattr_t">;
114118
PtrType PThreadMutexAttrTPtr = PtrType<PThreadMutexAttrTType>;
115119
RestrictedPtrType RestrictedPThreadMutexAttrTPtr = RestrictedPtrType<PThreadMutexAttrTType>;
@@ -993,6 +997,7 @@ def POSIX : StandardSpec<"POSIX"> {
993997
PThreadMutexTType,
994998
PThreadOnceCallback,
995999
PThreadOnceT,
1000+
PThreadRWLockAttrTType,
9961001
PThreadStartT,
9971002
PThreadTSSDtorT,
9981003
PThreadTType,
@@ -1219,6 +1224,26 @@ def POSIX : StandardSpec<"POSIX"> {
12191224
RetValSpec<IntType>,
12201225
[ArgSpec<PThreadOnceTPtr>, ArgSpec<PThreadOnceCallback>]
12211226
>,
1227+
FunctionSpec<
1228+
"pthread_rwlockattr_destroy",
1229+
RetValSpec<IntType>,
1230+
[ArgSpec<PThreadRWLockAttrTPtr>]
1231+
>,
1232+
FunctionSpec<
1233+
"pthread_rwlockattr_getpshared",
1234+
RetValSpec<IntType>,
1235+
[ArgSpec<ConstPThreadRWLockAttrTPtr>, ArgSpec<IntPtr>]
1236+
>,
1237+
FunctionSpec<
1238+
"pthread_rwlockattr_init",
1239+
RetValSpec<IntType>,
1240+
[ArgSpec<PThreadRWLockAttrTPtr>]
1241+
>,
1242+
FunctionSpec<
1243+
"pthread_rwlockattr_setpshared",
1244+
RetValSpec<IntType>,
1245+
[ArgSpec<PThreadRWLockAttrTPtr>, ArgSpec<IntType>]
1246+
>,
12221247
]
12231248
>;
12241249

@@ -1575,6 +1600,7 @@ def POSIX : StandardSpec<"POSIX"> {
15751600
PThreadMutexAttrTType,
15761601
PThreadMutexTType,
15771602
PThreadOnceT,
1603+
PThreadRWLockAttrTType,
15781604
PThreadTType,
15791605
PidT,
15801606
SSizeTType,

libc/src/pthread/CMakeLists.txt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,47 @@ add_entrypoint_object(
460460
libc.src.__support.threads.thread
461461
)
462462

463+
add_entrypoint_object(
464+
pthread_rwlockattr_destroy
465+
SRCS
466+
pthread_rwlockattr_destroy.cpp
467+
HDRS
468+
pthread_rwlockattr_destroy.h
469+
DEPENDS
470+
libc.include.pthread
471+
)
472+
473+
add_entrypoint_object(
474+
pthread_rwlockattr_getpshared
475+
SRCS
476+
pthread_rwlockattr_getpshared.cpp
477+
HDRS
478+
pthread_rwlockattr_getpshared.h
479+
DEPENDS
480+
libc.include.pthread
481+
)
482+
483+
add_entrypoint_object(
484+
pthread_rwlockattr_init
485+
SRCS
486+
pthread_rwlockattr_init.cpp
487+
HDRS
488+
pthread_rwlockattr_init.h
489+
DEPENDS
490+
libc.include.pthread
491+
)
492+
493+
add_entrypoint_object(
494+
pthread_rwlockattr_setpshared
495+
SRCS
496+
pthread_rwlockattr_setpshared.cpp
497+
HDRS
498+
pthread_rwlockattr_setpshared.h
499+
DEPENDS
500+
libc.include.pthread
501+
libc.include.errno
502+
)
503+
463504
add_entrypoint_object(
464505
pthread_once
465506
SRCS
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===-- Implementation of the pthread_rwlockattr_destroy ------------------===//
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+
#include "pthread_rwlockattr_destroy.h"
10+
11+
#include "src/__support/common.h"
12+
13+
#include <pthread.h> // pthread_rwlockattr_t
14+
15+
namespace LIBC_NAMESPACE {
16+
17+
LLVM_LIBC_FUNCTION(int, pthread_rwlockattr_destroy,
18+
(pthread_rwlockattr_t * attr [[gnu::unused]])) {
19+
// Initializing a pthread_rwlockattr_t acquires no resources, so this is a
20+
// no-op.
21+
return 0;
22+
}
23+
24+
} // namespace LIBC_NAMESPACE
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header for pthread_rwlockattr_destroy ----*- 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_PTHREAD_PTHREAD_RWLOCKATTR_DESTROY_H
10+
#define LLVM_LIBC_SRC_PTHREAD_PTHREAD_RWLOCKATTR_DESTROY_H
11+
12+
#include <pthread.h>
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr);
17+
18+
} // namespace LIBC_NAMESPACE
19+
20+
#endif // LLVM_LIBC_SRC_PTHREAD_PTHREAD_RWLOCKATTR_DESTROY_H
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===-- Implementation of the pthread_rwlockattr_getpshared ---------------===//
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+
#include "pthread_rwlockattr_getpshared.h"
10+
11+
#include "src/__support/common.h"
12+
13+
#include <pthread.h> // pthread_rwlockattr_t
14+
15+
namespace LIBC_NAMESPACE {
16+
17+
LLVM_LIBC_FUNCTION(int, pthread_rwlockattr_getpshared,
18+
(const pthread_rwlockattr_t *attr, int *pshared)) {
19+
*pshared = attr->pshared;
20+
return 0;
21+
}
22+
23+
} // namespace LIBC_NAMESPACE
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation header for pthread_rwlockattr_getpshared -*- 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_PTHREAD_PTHREAD_RWLOCKATTR_GETPSHARED_H
10+
#define LLVM_LIBC_SRC_PTHREAD_PTHREAD_RWLOCKATTR_GETPSHARED_H
11+
12+
#include <pthread.h>
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *attr,
17+
int *pshared);
18+
19+
} // namespace LIBC_NAMESPACE
20+
21+
#endif // LLVM_LIBC_SRC_PTHREAD_PTHREAD_RWLOCKATTR_GETPSHARED_H
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===-- Implementation of the pthread_rwlockattr_init ---------------------===//
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+
#include "pthread_rwlockattr_init.h"
10+
11+
#include "src/__support/common.h"
12+
13+
#include <pthread.h> // pthread_rwlockattr_t, PTHREAD_PROCESS_PRIVATE
14+
15+
namespace LIBC_NAMESPACE {
16+
17+
LLVM_LIBC_FUNCTION(int, pthread_rwlockattr_init,
18+
(pthread_rwlockattr_t * attr)) {
19+
attr->pshared = PTHREAD_PROCESS_PRIVATE;
20+
return 0;
21+
}
22+
23+
} // namespace LIBC_NAMESPACE
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header for pthread_rwlockattr_init ----*- 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_PTHREAD_PTHREAD_RWLOCKATTR_INIT_H
10+
#define LLVM_LIBC_SRC_PTHREAD_PTHREAD_RWLOCKATTR_INIT_H
11+
12+
#include <pthread.h>
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
int pthread_rwlockattr_init(pthread_rwlockattr_t *attr);
17+
18+
} // namespace LIBC_NAMESPACE
19+
20+
#endif // LLVM_LIBC_SRC_PTHREAD_PTHREAD_RWLOCKATTR_INIT_H
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===-- Implementation of the pthread_rwlockattr_setpshared ---------------===//
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+
#include "pthread_rwlockattr_setpshared.h"
10+
11+
#include "src/__support/common.h"
12+
13+
#include <errno.h> // EINVAL
14+
#include <pthread.h> // pthread_rwlockattr_t, PTHREAD_PROCESS_SHARED, PTHREAD_PROCESS_PRIVATE
15+
16+
namespace LIBC_NAMESPACE {
17+
18+
LLVM_LIBC_FUNCTION(int, pthread_rwlockattr_setpshared,
19+
(pthread_rwlockattr_t * attr, int pshared)) {
20+
if (pshared != PTHREAD_PROCESS_SHARED && pshared != PTHREAD_PROCESS_PRIVATE)
21+
return EINVAL;
22+
23+
attr->pshared = pshared;
24+
return 0;
25+
}
26+
27+
} // namespace LIBC_NAMESPACE
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header for pthread_rwlockattr_setpshared -*- 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_PTHREAD_PTHREAD_RWLOCKATTR_SETPSHARED_H
10+
#define LLVM_LIBC_SRC_PTHREAD_PTHREAD_RWLOCKATTR_SETPSHARED_H
11+
12+
#include <pthread.h>
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *attr, int pshared);
17+
18+
} // namespace LIBC_NAMESPACE
19+
20+
#endif // LLVM_LIBC_SRC_PTHREAD_PTHREAD_RWLOCKATTR_SETPSHARED_H

libc/test/src/pthread/CMakeLists.txt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,19 @@ add_libc_unittest(
5656
libc.src.pthread.pthread_condattr_init
5757
libc.src.pthread.pthread_condattr_setclock
5858
libc.src.pthread.pthread_condattr_setpshared
59-
)
59+
)
60+
61+
add_libc_unittest(
62+
pthread_rwlockattr_test
63+
SUITE
64+
libc_pthread_unittests
65+
SRCS
66+
pthread_rwlockattr_test.cpp
67+
DEPENDS
68+
libc.include.errno
69+
libc.include.pthread
70+
libc.src.pthread.pthread_rwlockattr_destroy
71+
libc.src.pthread.pthread_rwlockattr_getpshared
72+
libc.src.pthread.pthread_rwlockattr_init
73+
libc.src.pthread.pthread_rwlockattr_setpshared
74+
)

0 commit comments

Comments
 (0)