Skip to content

Commit 485f9f5

Browse files
Reland: [libc][POSIX][pthreads] implemented missing pthread_rwlockattr functions (#93622)
New pull request for #89443 The previous PR was reverted after breaking fullbuild due to a missing struct declaration, which I forgot to commit. Reverts revert and adds the missing pthread_rwlockattr_getkind_np / pthread_rwlockattr_setkind_np functions and tests respecitvely.
1 parent 5785048 commit 485f9f5

12 files changed

+163
-3
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,8 +673,10 @@ if(LLVM_LIBC_FULL_BUILD)
673673
libc.src.pthread.pthread_mutexattr_settype
674674
libc.src.pthread.pthread_once
675675
libc.src.pthread.pthread_rwlockattr_destroy
676+
libc.src.pthread.pthread_rwlockattr_getkind_np
676677
libc.src.pthread.pthread_rwlockattr_getpshared
677678
libc.src.pthread.pthread_rwlockattr_init
679+
libc.src.pthread.pthread_rwlockattr_setkind_np
678680
libc.src.pthread.pthread_rwlockattr_setpshared
679681
libc.src.pthread.pthread_setspecific
680682

libc/include/llvm-libc-types/pthread_rwlockattr_t.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
typedef struct {
1212
int pshared;
13+
int pref;
1314
} pthread_rwlockattr_t;
1415

1516
#endif // LLVM_LIBC_TYPES_PTHREAD_RWLOCKATTR_T_H

libc/include/pthread.h.def

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ enum {
3838
#define PTHREAD_PROCESS_PRIVATE 0
3939
#define PTHREAD_PROCESS_SHARED 1
4040

41+
#define PTHREAD_RWLOCK_PREFER_READER_NP 0
42+
#define PTHREAD_RWLOCK_PREFER_WRITER_NP 1
43+
#define PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP 2
44+
45+
4146
%%public_api()
4247

4348
#endif // LLVM_LIBC_PTHREAD_H

libc/spec/posix.td

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,6 +1234,11 @@ def POSIX : StandardSpec<"POSIX"> {
12341234
RetValSpec<IntType>,
12351235
[ArgSpec<PThreadRWLockAttrTPtr>]
12361236
>,
1237+
FunctionSpec<
1238+
"pthread_rwlockattr_getkind_np",
1239+
RetValSpec<IntType>,
1240+
[ArgSpec<PThreadRWLockAttrTPtr>, ArgSpec<IntPtr>]
1241+
>,
12371242
FunctionSpec<
12381243
"pthread_rwlockattr_getpshared",
12391244
RetValSpec<IntType>,
@@ -1244,6 +1249,11 @@ def POSIX : StandardSpec<"POSIX"> {
12441249
RetValSpec<IntType>,
12451250
[ArgSpec<PThreadRWLockAttrTPtr>]
12461251
>,
1252+
FunctionSpec<
1253+
"pthread_rwlockattr_setkind_np",
1254+
RetValSpec<IntType>,
1255+
[ArgSpec<PThreadRWLockAttrTPtr>, ArgSpec<IntType>]
1256+
>,
12471257
FunctionSpec<
12481258
"pthread_rwlockattr_setpshared",
12491259
RetValSpec<IntType>,

libc/src/pthread/CMakeLists.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,16 @@ add_entrypoint_object(
470470
libc.include.pthread
471471
)
472472

473+
add_entrypoint_object(
474+
pthread_rwlockattr_getkind_np
475+
SRCS
476+
pthread_rwlockattr_getkind_np.cpp
477+
HDRS
478+
pthread_rwlockattr_getkind_np.h
479+
DEPENDS
480+
libc.include.pthread
481+
)
482+
473483
add_entrypoint_object(
474484
pthread_rwlockattr_getpshared
475485
SRCS
@@ -490,6 +500,17 @@ add_entrypoint_object(
490500
libc.include.pthread
491501
)
492502

503+
add_entrypoint_object(
504+
pthread_rwlockattr_setkind_np
505+
SRCS
506+
pthread_rwlockattr_setkind_np.cpp
507+
HDRS
508+
pthread_rwlockattr_setkind_np.h
509+
DEPENDS
510+
libc.include.pthread
511+
libc.include.errno
512+
)
513+
493514
add_entrypoint_object(
494515
pthread_rwlockattr_setpshared
495516
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_getkind_np ---------------===//
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_getkind_np.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_getkind_np,
18+
(const pthread_rwlockattr_t *__restrict attr,
19+
int *__restrict pref)) {
20+
*pref = attr->pref;
21+
return 0;
22+
}
23+
24+
} // 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_getkind_np -*- 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_GETKIND_NP_H
10+
#define LLVM_LIBC_SRC_PTHREAD_PTHREAD_RWLOCKATTR_GETKIND_NP_H
11+
12+
#include <pthread.h>
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
int pthread_rwlockattr_getkind_np(const pthread_rwlockattr_t *__restrict attr,
17+
int *__restrict pref);
18+
19+
} // namespace LIBC_NAMESPACE
20+
21+
#endif // LLVM_LIBC_SRC_PTHREAD_PTHREAD_RWLOCKATTR_GETKIND_NP_H

libc/src/pthread/pthread_rwlockattr_init.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ namespace LIBC_NAMESPACE {
1717
LLVM_LIBC_FUNCTION(int, pthread_rwlockattr_init,
1818
(pthread_rwlockattr_t * attr)) {
1919
attr->pshared = PTHREAD_PROCESS_PRIVATE;
20+
attr->pref = PTHREAD_RWLOCK_PREFER_READER_NP;
2021
return 0;
2122
}
2223

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//===-- Implementation of the pthread_rwlockattr_setkind_np ---------------===//
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_setkind_np.h"
10+
11+
#include "src/__support/common.h"
12+
13+
#include <errno.h>
14+
#include <pthread.h> // pthread_rwlockattr_t
15+
16+
namespace LIBC_NAMESPACE {
17+
18+
LLVM_LIBC_FUNCTION(int, pthread_rwlockattr_setkind_np,
19+
(pthread_rwlockattr_t * attr, int pref)) {
20+
21+
if (pref != PTHREAD_RWLOCK_PREFER_READER_NP &&
22+
pref != PTHREAD_RWLOCK_PREFER_WRITER_NP &&
23+
pref != PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP)
24+
return EINVAL;
25+
26+
attr->pref = pref;
27+
return 0;
28+
}
29+
30+
} // 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_setkind_np -*- 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_SETKIND_NP_H
10+
#define LLVM_LIBC_SRC_PTHREAD_PTHREAD_RWLOCKATTR_SETKIND_NP_H
11+
12+
#include <pthread.h>
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
int pthread_rwlockattr_setkind_np(pthread_rwlockattr_t *attr, int pref);
17+
18+
} // namespace LIBC_NAMESPACE
19+
20+
#endif // LLVM_LIBC_SRC_PTHREAD_PTHREAD_RWLOCKATTR_SETKIND_NP_H

libc/test/src/pthread/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ add_libc_unittest(
6868
libc.include.errno
6969
libc.include.pthread
7070
libc.src.pthread.pthread_rwlockattr_destroy
71+
libc.src.pthread.pthread_rwlockattr_getkind_np
7172
libc.src.pthread.pthread_rwlockattr_getpshared
7273
libc.src.pthread.pthread_rwlockattr_init
74+
libc.src.pthread.pthread_rwlockattr_setkind_np
7375
libc.src.pthread.pthread_rwlockattr_setpshared
7476
)

libc/test/src/pthread/pthread_rwlockattr_test.cpp

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88

99
#include "include/llvm-libc-macros/generic-error-number-macros.h" // EINVAL
1010
#include "src/pthread/pthread_rwlockattr_destroy.h"
11+
#include "src/pthread/pthread_rwlockattr_getkind_np.h"
1112
#include "src/pthread/pthread_rwlockattr_getpshared.h"
1213
#include "src/pthread/pthread_rwlockattr_init.h"
14+
#include "src/pthread/pthread_rwlockattr_setkind_np.h"
1315
#include "src/pthread/pthread_rwlockattr_setpshared.h"
1416
#include "test/UnitTest/Test.h"
1517

@@ -25,40 +27,61 @@ TEST(LlvmLibcPThreadRWLockAttrTest, InitAndDestroy) {
2527
TEST(LlvmLibcPThreadRWLockAttrTest, GetDefaultValues) {
2628
pthread_rwlockattr_t attr;
2729

28-
// Invalid value.
30+
// Invalid values.
2931
int pshared = 42;
32+
int pref = 1337;
3033

3134
ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlockattr_init(&attr), 0);
3235
ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlockattr_getpshared(&attr, &pshared), 0);
36+
ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlockattr_getkind_np(&attr, &pref), 0);
37+
3338
ASSERT_EQ(pshared, PTHREAD_PROCESS_PRIVATE);
39+
ASSERT_EQ(pref, PTHREAD_RWLOCK_PREFER_READER_NP);
40+
3441
ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlockattr_destroy(&attr), 0);
3542
}
3643

3744
TEST(LlvmLibcPThreadRWLockAttrTest, SetGoodValues) {
3845
pthread_rwlockattr_t attr;
3946

40-
// Invalid value.
47+
// Invalid values.
4148
int pshared = 42;
49+
int pref = 1337;
4250

4351
ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlockattr_init(&attr), 0);
4452
ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlockattr_setpshared(
4553
&attr, PTHREAD_PROCESS_SHARED),
4654
0);
55+
ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlockattr_setkind_np(
56+
&attr, PTHREAD_RWLOCK_PREFER_WRITER_NP),
57+
0);
58+
4759
ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlockattr_getpshared(&attr, &pshared), 0);
60+
ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlockattr_getkind_np(&attr, &pref), 0);
61+
4862
ASSERT_EQ(pshared, PTHREAD_PROCESS_SHARED);
63+
ASSERT_EQ(pref, PTHREAD_RWLOCK_PREFER_WRITER_NP);
64+
4965
ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlockattr_destroy(&attr), 0);
5066
}
5167

5268
TEST(LlvmLibcPThreadRWLockAttrTest, SetBadValues) {
5369
pthread_rwlockattr_t attr;
5470

55-
// Invalid value.
71+
// Invalid values.
5672
int pshared = 42;
73+
int pref = 1337;
5774

5875
ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlockattr_init(&attr), 0);
5976
ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlockattr_setpshared(&attr, pshared),
6077
EINVAL);
78+
ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlockattr_setkind_np(&attr, pref), EINVAL);
79+
6180
ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlockattr_getpshared(&attr, &pshared), 0);
81+
ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlockattr_getkind_np(&attr, &pref), 0);
82+
6283
ASSERT_EQ(pshared, PTHREAD_PROCESS_PRIVATE);
84+
ASSERT_EQ(pref, PTHREAD_RWLOCK_PREFER_READER_NP);
85+
6386
ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlockattr_destroy(&attr), 0);
6487
}

0 commit comments

Comments
 (0)