Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 45 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,49 @@
#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 = */ \
{ \
/* .__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 = */ \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you want these two comments to be on separate lines?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes; but dunno how to fix this quickly with clang-format.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it work if you make it like:

/* .__wait_queue_mutex = */  {0},            \
/* .__pending_readers = */ {0},               \
...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does. That makes me sad to mix comments that look like designated initializers, but only for top level members, with more aggregate initializers without such comments for nested members, but I lack the will to wrestle with the formatter.

I think we could put a .clang-format file in this dir, and use https://clang.llvm.org/docs/ClangFormatStyleOptions.html#whitespacesensitivemacros to turn off formatting for this macro.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2da1ce2 PTAL

{ \
/* .__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
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