From 6ef84f257c414f7f564add2579c8f98fb342bf82 Mon Sep 17 00:00:00 2001 From: Nick Desaulniers Date: Fri, 7 Feb 2025 13:38:52 -0800 Subject: [PATCH 1/5] [libc][pthread] fix -Wmissing-field-initializers 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. --- libc/include/llvm-libc-macros/pthread-macros.h | 4 ++-- libc/test/integration/src/pthread/pthread_mutex_test.cpp | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/libc/include/llvm-libc-macros/pthread-macros.h b/libc/include/llvm-libc-macros/pthread-macros.h index 8a144dbd2e611e7..2b1440f9308ca7f 100644 --- a/libc/include/llvm-libc-macros/pthread-macros.h +++ b/libc/include/llvm-libc-macros/pthread-macros.h @@ -25,8 +25,8 @@ #define PTHREAD_PROCESS_PRIVATE 0 #define PTHREAD_PROCESS_SHARED 1 -#define PTHREAD_MUTEX_INITIALIZER {0} -#define PTHREAD_RWLOCK_INITIALIZER {0} +#define PTHREAD_MUTEX_INITIALIZER {} +#define PTHREAD_RWLOCK_INITIALIZER {} // 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 ce2a3538924da84..ec8488da4ead98f 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 +[[gnu::unused]] +static pthread_mutex_t test_initializer = PTHREAD_MUTEX_INITIALIZER; + TEST_MAIN() { relay_counter(); wait_and_step(); From c380f97ad3c866e4dda5bfd22b5f7d0fc4324c3f Mon Sep 17 00:00:00 2001 From: Nick Desaulniers Date: Mon, 10 Feb 2025 09:52:03 -0800 Subject: [PATCH 2/5] switch to aggregate initialization --- .../include/llvm-libc-macros/pthread-macros.h | 53 ++++++++++++++++++- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/libc/include/llvm-libc-macros/pthread-macros.h b/libc/include/llvm-libc-macros/pthread-macros.h index 2b1440f9308ca7f..46e0025894288af 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 "include/llvm-libc-macros/null-macro.h" + #define PTHREAD_CREATE_JOINABLE 0 #define PTHREAD_CREATE_DETACHED 1 @@ -25,8 +27,55 @@ #define PTHREAD_PROCESS_PRIVATE 0 #define PTHREAD_PROCESS_SHARED 1 -#define PTHREAD_MUTEX_INITIALIZER {} -#define PTHREAD_RWLOCK_INITIALIZER {} +#ifdef __linux__ +#define PTHREAD_MUTEX_INITIALIZER \ + { \ + /* .__timed = */ 0, \ + /* .__recursive = */ 0, \ + /* .__robust = */ 0, \ + /* .__owner = */ NULL, \ + /* .__lock_count = */ 0, \ + /* .__futex_word = */ \ + { \ + /* .__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 = */ \ + { \ + /* .__word = */ 0, \ + }, \ + /* .__pending_readers = */ \ + { \ + /* .__word = */ 0, \ + }, \ + /* .__pending_writers = */ \ + { \ + /* .__word = */ 0, \ + }, \ + /* .__reader_serialization = */ \ + { \ + /* .__word = */ 0, \ + }, \ + /* .__writer_serialization = */ \ + { \ + /* .__word = */ 0, \ + }, \ + } // glibc extensions #define PTHREAD_STACK_MIN (1 << 14) // 16KB From bfc84ca4ff1e58948ed985bb1792f0e416e73171 Mon Sep 17 00:00:00 2001 From: Nick Desaulniers Date: Mon, 10 Feb 2025 09:53:01 -0800 Subject: [PATCH 3/5] s/gnu::unused/maybe_unused/ --- libc/test/integration/src/pthread/pthread_mutex_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libc/test/integration/src/pthread/pthread_mutex_test.cpp b/libc/test/integration/src/pthread/pthread_mutex_test.cpp index ec8488da4ead98f..137daed6bd283f6 100644 --- a/libc/test/integration/src/pthread/pthread_mutex_test.cpp +++ b/libc/test/integration/src/pthread/pthread_mutex_test.cpp @@ -187,7 +187,7 @@ void multiple_waiters() { } // Test the initializer -[[gnu::unused]] +[[maybe_unused]] static pthread_mutex_t test_initializer = PTHREAD_MUTEX_INITIALIZER; TEST_MAIN() { From 868122b70a1a93b8cbac7390181043ba3bd924cc Mon Sep 17 00:00:00 2001 From: Nick Desaulniers Date: Mon, 10 Feb 2025 09:58:12 -0800 Subject: [PATCH 4/5] reformat --- libc/include/llvm-libc-macros/pthread-macros.h | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/libc/include/llvm-libc-macros/pthread-macros.h b/libc/include/llvm-libc-macros/pthread-macros.h index 46e0025894288af..16204a1e708e027 100644 --- a/libc/include/llvm-libc-macros/pthread-macros.h +++ b/libc/include/llvm-libc-macros/pthread-macros.h @@ -34,8 +34,7 @@ /* .__recursive = */ 0, \ /* .__robust = */ 0, \ /* .__owner = */ NULL, \ - /* .__lock_count = */ 0, \ - /* .__futex_word = */ \ + /* .__lock_count = */ 0, /* .__futex_word = */ \ { \ /* .__word = */ 0, \ }, \ @@ -54,24 +53,19 @@ /* .__is_pshared = */ 0, \ /* .__preference = */ 0, \ /* .__state = */ 0, \ - /* .__write_tid = */ 0, \ - /* .__wait_queue_mutex = */ \ + /* .__write_tid = */ 0, /* .__wait_queue_mutex = */ \ { \ /* .__word = */ 0, \ - }, \ - /* .__pending_readers = */ \ + }, /* .__pending_readers = */ \ { \ /* .__word = */ 0, \ - }, \ - /* .__pending_writers = */ \ + }, /* .__pending_writers = */ \ { \ /* .__word = */ 0, \ - }, \ - /* .__reader_serialization = */ \ + }, /* .__reader_serialization = */ \ { \ /* .__word = */ 0, \ - }, \ - /* .__writer_serialization = */ \ + }, /* .__writer_serialization = */ \ { \ /* .__word = */ 0, \ }, \ From 0f16df9d00816fc384d7bc22e1feefddac151048 Mon Sep 17 00:00:00 2001 From: Nick Desaulniers Date: Mon, 10 Feb 2025 11:01:06 -0800 Subject: [PATCH 5/5] fix include path --- libc/include/llvm-libc-macros/pthread-macros.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libc/include/llvm-libc-macros/pthread-macros.h b/libc/include/llvm-libc-macros/pthread-macros.h index 16204a1e708e027..68c0a5e53fbe24b 100644 --- a/libc/include/llvm-libc-macros/pthread-macros.h +++ b/libc/include/llvm-libc-macros/pthread-macros.h @@ -9,7 +9,7 @@ #ifndef LLVM_LIBC_MACROS_PTHREAD_MACRO_H #define LLVM_LIBC_MACROS_PTHREAD_MACRO_H -#include "include/llvm-libc-macros/null-macro.h" +#include "null-macro.h" #define PTHREAD_CREATE_JOINABLE 0 #define PTHREAD_CREATE_DETACHED 1