[libc-commits] [libc] 3e02069 - [libc][pthread] fix -Wmissing-field-initializers (#126314)
via libc-commits
libc-commits at lists.llvm.org
Wed Feb 12 14:28:32 PST 2025
Author: Nick Desaulniers
Date: 2025-02-12T14:28:29-08:00
New Revision: 3e02069afe39a3c314476141b9dd90daad5803f2
URL: https://github.com/llvm/llvm-project/commit/3e02069afe39a3c314476141b9dd90daad5803f2
DIFF: https://github.com/llvm/llvm-project/commit/3e02069afe39a3c314476141b9dd90daad5803f2.diff
LOG: [libc][pthread] fix -Wmissing-field-initializers (#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.
Added:
Modified:
libc/include/llvm-libc-macros/CMakeLists.txt
libc/include/llvm-libc-macros/pthread-macros.h
libc/test/integration/src/pthread/pthread_mutex_test.cpp
Removed:
################################################################################
diff --git a/libc/include/llvm-libc-macros/CMakeLists.txt b/libc/include/llvm-libc-macros/CMakeLists.txt
index 6124fd136ce6d..8c1f7387f3b4d 100644
--- a/libc/include/llvm-libc-macros/CMakeLists.txt
+++ b/libc/include/llvm-libc-macros/CMakeLists.txt
@@ -328,6 +328,8 @@ add_macro_header(
pthread_macros
HDR
pthread-macros.h
+ DEPENDS
+ .null_macro
)
add_macro_header(
diff --git a/libc/include/llvm-libc-macros/pthread-macros.h b/libc/include/llvm-libc-macros/pthread-macros.h
index 8a144dbd2e611..fcc6ef925e3f4 100644
--- a/libc/include/llvm-libc-macros/pthread-macros.h
+++ b/libc/include/llvm-libc-macros/pthread-macros.h
@@ -9,6 +9,8 @@
#ifndef LLVM_LIBC_MACROS_PTHREAD_MACRO_H
#define LLVM_LIBC_MACROS_PTHREAD_MACRO_H
+#include "null-macro.h"
+
#define PTHREAD_CREATE_JOINABLE 0
#define PTHREAD_CREATE_DETACHED 1
@@ -25,8 +27,34 @@
#define PTHREAD_PROCESS_PRIVATE 0
#define PTHREAD_PROCESS_SHARED 1
-#define PTHREAD_MUTEX_INITIALIZER {0}
-#define PTHREAD_RWLOCK_INITIALIZER {0}
+#ifdef __linux__
+#define PTHREAD_MUTEX_INITIALIZER \
+ { \
+ /* .__timed = */ 0, /* .__recursive = */ 0, \
+ /* .__robust = */ 0, /* .__owner = */ NULL, \
+ /* .__lock_count = */ 0, /* .__futex_word = */ {0}, \
+ }
+#else
+#define PTHREAD_MUTEX_INITIALIZER \
+ { \
+ /* .__timed = */ 0, /* .__recursive = */ 0, \
+ /* .__robust = */ 0, /* .__owner = */ NULL, \
+ /* .__lock_count = */ 0, \
+ }
+#endif
+
+#define PTHREAD_RWLOCK_INITIALIZER \
+ { \
+ /* .__is_pshared = */ 0, \
+ /* .__preference = */ 0, \
+ /* .__state = */ 0, \
+ /* .__write_tid = */ 0, \
+ /* .__wait_queue_mutex = */ {0}, \
+ /* .__pending_readers = */ {0}, \
+ /* .__pending_writers = */ {0}, \
+ /* .__reader_serialization = */ {0}, \
+ /* .__writer_serialization = */ {0}, \
+ }
// glibc extensions
#define PTHREAD_STACK_MIN (1 << 14) // 16KB
diff --git a/libc/test/integration/src/pthread/pthread_mutex_test.cpp b/libc/test/integration/src/pthread/pthread_mutex_test.cpp
index ce2a3538924da..137daed6bd283 100644
--- a/libc/test/integration/src/pthread/pthread_mutex_test.cpp
+++ b/libc/test/integration/src/pthread/pthread_mutex_test.cpp
@@ -186,6 +186,10 @@ void multiple_waiters() {
LIBC_NAMESPACE::pthread_mutex_destroy(&counter_lock);
}
+// Test the initializer
+[[maybe_unused]]
+static pthread_mutex_t test_initializer = PTHREAD_MUTEX_INITIALIZER;
+
TEST_MAIN() {
relay_counter();
wait_and_step();
More information about the libc-commits
mailing list