Skip to content

Commit

Permalink
[libc][pthread] fix -Wmissing-field-initializers (llvm#126314)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nickdesaulniers authored and joaosaffran committed Feb 14, 2025
1 parent c31b851 commit ff13272
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
2 changes: 2 additions & 0 deletions libc/include/llvm-libc-macros/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,8 @@ add_macro_header(
pthread_macros
HDR
pthread-macros.h
DEPENDS
.null_macro
)

add_macro_header(
Expand Down
32 changes: 30 additions & 2 deletions libc/include/llvm-libc-macros/pthread-macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions libc/test/integration/src/pthread/pthread_mutex_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit ff13272

Please sign in to comment.