Skip to content

Commit 3e02069

Browse files
[libc][pthread] fix -Wmissing-field-initializers (llvm#126314)
Fixes: llvm-project/libc/test/integration/src/pthread/pthread_rwlock_test.cpp:59:29: warning: missing field '__preference' initializer [-Wmissing-field-initializers] 59 | pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER; | ^ Also, add a test that demonstrates the same issue for PTHREAD_MUTEX_INITIALIZER, and fix that, too. PTHREAD_ONCE_INIT does not have this issue and does have test coverage.
1 parent 37952ef commit 3e02069

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,8 @@ add_macro_header(
328328
pthread_macros
329329
HDR
330330
pthread-macros.h
331+
DEPENDS
332+
.null_macro
331333
)
332334

333335
add_macro_header(

libc/include/llvm-libc-macros/pthread-macros.h

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#ifndef LLVM_LIBC_MACROS_PTHREAD_MACRO_H
1010
#define LLVM_LIBC_MACROS_PTHREAD_MACRO_H
1111

12+
#include "null-macro.h"
13+
1214
#define PTHREAD_CREATE_JOINABLE 0
1315
#define PTHREAD_CREATE_DETACHED 1
1416

@@ -25,8 +27,34 @@
2527
#define PTHREAD_PROCESS_PRIVATE 0
2628
#define PTHREAD_PROCESS_SHARED 1
2729

28-
#define PTHREAD_MUTEX_INITIALIZER {0}
29-
#define PTHREAD_RWLOCK_INITIALIZER {0}
30+
#ifdef __linux__
31+
#define PTHREAD_MUTEX_INITIALIZER \
32+
{ \
33+
/* .__timed = */ 0, /* .__recursive = */ 0, \
34+
/* .__robust = */ 0, /* .__owner = */ NULL, \
35+
/* .__lock_count = */ 0, /* .__futex_word = */ {0}, \
36+
}
37+
#else
38+
#define PTHREAD_MUTEX_INITIALIZER \
39+
{ \
40+
/* .__timed = */ 0, /* .__recursive = */ 0, \
41+
/* .__robust = */ 0, /* .__owner = */ NULL, \
42+
/* .__lock_count = */ 0, \
43+
}
44+
#endif
45+
46+
#define PTHREAD_RWLOCK_INITIALIZER \
47+
{ \
48+
/* .__is_pshared = */ 0, \
49+
/* .__preference = */ 0, \
50+
/* .__state = */ 0, \
51+
/* .__write_tid = */ 0, \
52+
/* .__wait_queue_mutex = */ {0}, \
53+
/* .__pending_readers = */ {0}, \
54+
/* .__pending_writers = */ {0}, \
55+
/* .__reader_serialization = */ {0}, \
56+
/* .__writer_serialization = */ {0}, \
57+
}
3058

3159
// glibc extensions
3260
#define PTHREAD_STACK_MIN (1 << 14) // 16KB

libc/test/integration/src/pthread/pthread_mutex_test.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ void multiple_waiters() {
186186
LIBC_NAMESPACE::pthread_mutex_destroy(&counter_lock);
187187
}
188188

189+
// Test the initializer
190+
[[maybe_unused]]
191+
static pthread_mutex_t test_initializer = PTHREAD_MUTEX_INITIALIZER;
192+
189193
TEST_MAIN() {
190194
relay_counter();
191195
wait_and_step();

0 commit comments

Comments
 (0)