Skip to content

Commit 7caf23b

Browse files
Yifan ZhuSchrodingerZhu
authored andcommitted
test and some fix (WIP)
1 parent a1e66fe commit 7caf23b

File tree

3 files changed

+75
-2
lines changed

3 files changed

+75
-2
lines changed

libc/src/__support/threads/linux/rwlock.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class State {
129129
LIBC_INLINE_VAR static constexpr int PENDING_WRITER_SHIFT = 1;
130130
LIBC_INLINE_VAR static constexpr int ACTIVE_READER_SHIFT = 2;
131131
LIBC_INLINE_VAR static constexpr int ACTIVE_WRITER_SHIFT =
132-
cpp::numeric_limits<int>::digits - 1;
132+
cpp::numeric_limits<int>::digits;
133133

134134
// Bitmasks to access the components of the state.
135135
LIBC_INLINE_VAR static constexpr int PENDING_READER_BIT =
@@ -156,7 +156,7 @@ class State {
156156
// Utilities to check the state of the RwLock.
157157
LIBC_INLINE constexpr bool has_active_writer() const { return state < 0; }
158158
LIBC_INLINE constexpr bool has_active_reader() const {
159-
return state > ACTIVE_READER_COUNT_UNIT;
159+
return state >= ACTIVE_READER_COUNT_UNIT;
160160
}
161161
LIBC_INLINE constexpr bool has_acitve_owner() const {
162162
return has_active_reader() || has_active_writer();

libc/test/integration/src/pthread/CMakeLists.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,31 @@ add_integration_test(
1717
libc.src.pthread.pthread_join
1818
)
1919

20+
add_integration_test(
21+
pthread_rwlock_test
22+
SUITE
23+
libc-pthread-integration-tests
24+
SRCS
25+
pthread_rwlock_test.cpp
26+
DEPENDS
27+
libc.include.pthread
28+
libc.include.time
29+
libc.include.errno
30+
libc.src.pthread.pthread_rwlock_destroy
31+
libc.src.pthread.pthread_rwlock_init
32+
libc.src.pthread.pthread_rwlock_rdlock
33+
libc.src.pthread.pthread_rwlock_tryrdlock
34+
libc.src.pthread.pthread_rwlock_timedrdlock
35+
libc.src.pthread.pthread_rwlock_wrlock
36+
libc.src.pthread.pthread_rwlock_trywrlock
37+
libc.src.pthread.pthread_rwlock_timedwrlock
38+
libc.src.pthread.pthread_rwlock_unlock
39+
libc.src.pthread.pthread_create
40+
libc.src.pthread.pthread_join
41+
libc.src.time.clock_gettime
42+
libc.src.unistd.fork
43+
)
44+
2045
add_integration_test(
2146
pthread_test
2247
SUITE
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//===-- Tests for pthread_rwlock ------------------------------------------===//
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 "src/pthread/pthread_rwlock_destroy.h"
10+
#include "src/pthread/pthread_rwlock_init.h"
11+
#include "src/pthread/pthread_rwlock_rdlock.h"
12+
#include "src/pthread/pthread_rwlock_timedrdlock.h"
13+
#include "src/pthread/pthread_rwlock_timedwrlock.h"
14+
#include "src/pthread/pthread_rwlock_tryrdlock.h"
15+
#include "src/pthread/pthread_rwlock_trywrlock.h"
16+
#include "src/pthread/pthread_rwlock_unlock.h"
17+
#include "src/pthread/pthread_rwlock_wrlock.h"
18+
19+
#include "src/pthread/pthread_create.h"
20+
#include "src/pthread/pthread_join.h"
21+
22+
#include "test/IntegrationTest/test.h"
23+
24+
#include <errno.h>
25+
#include <pthread.h>
26+
#include <stdint.h> // uintptr_t
27+
28+
static void smoke_test() {
29+
pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
30+
ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlock_init(&rwlock, nullptr), 0);
31+
ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlock_rdlock(&rwlock), 0);
32+
ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlock_tryrdlock(&rwlock), 0);
33+
ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlock_trywrlock(&rwlock), EBUSY);
34+
ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlock_unlock(&rwlock), 0);
35+
ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlock_unlock(&rwlock), 0);
36+
ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlock_wrlock(&rwlock), 0);
37+
ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlock_rdlock(&rwlock), EDEADLK);
38+
ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlock_wrlock(&rwlock), EDEADLK);
39+
ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlock_tryrdlock(&rwlock), EBUSY);
40+
ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlock_trywrlock(&rwlock), EBUSY);
41+
ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlock_unlock(&rwlock), 0);
42+
ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlock_destroy(&rwlock), 0);
43+
}
44+
45+
TEST_MAIN() {
46+
smoke_test();
47+
return 0;
48+
}

0 commit comments

Comments
 (0)