-
Notifications
You must be signed in to change notification settings - Fork 14.6k
[libc] Implement barriers for pthreads #148948
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
2d5b880
Barrier class implemented; begin work on public functions
uzairnawaz cefd7de
pthread test + public function
uzairnawaz d645f95
fix test to join threads
uzairnawaz 8cad8c0
implemented return value for barrier wait
uzairnawaz 7adf56a
formatting
uzairnawaz a349e6f
mark attr param as unused
uzairnawaz d901a37
reorganized for readability
uzairnawaz 0512e7f
used internal pthread header
uzairnawaz ecf9f6c
fix pthread_barrier_t public struct opaqueness/alignment + test cleanup
uzairnawaz 1a828f4
formatting
uzairnawaz 3122439
header typo
uzairnawaz e20d450
more header typos
uzairnawaz 4373ef7
formatting
uzairnawaz 7bbd476
make barrier linux-only for now as mutex/cndvars are only defined for it
uzairnawaz 1e80063
remove unnecesary braces
uzairnawaz 86b9074
remove extra header + force size of public barrier type to be exactly…
uzairnawaz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
//===-- Definition of macros from pthread.h -------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_LIBC_HDR_PTHREAD_MACROS_H | ||
#define LLVM_LIBC_HDR_PTHREAD_MACROS_H | ||
|
||
#ifdef LIBC_FULL_BUILD | ||
|
||
#include "include/llvm-libc-macros/pthread-macros.h" | ||
|
||
#else // Overlay mode | ||
|
||
#include <pthread.h> | ||
|
||
#endif // LLVM_LIBC_FULL_BUILD | ||
|
||
#endif // LLVM_LIBC_HDR_PTHREAD_MACROS_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
//===-- Definition of macros from pthread_barrier_t.h ---------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_LIBC_HDR_TYPES_PTHREAD_BARRIER_T_H | ||
#define LLVM_LIBC_HDR_TYPES_PTHREAD_BARRIER_T_H | ||
|
||
#ifdef LIBC_FULL_BUILD | ||
|
||
#include "include/llvm-libc-types/pthread_barrier_t.h" | ||
|
||
#else // Overlay mode | ||
|
||
#error "Cannot overlay pthread_barrier_t" | ||
|
||
#endif // LLVM_LIBC_FULL_BUILD | ||
|
||
#endif // LLVM_LIBC_HDR_TYPES_PTHREAD_BARRIER_T_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
//===-- Definition of macros from pthread_barrierattr_t.h -----------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_LIBC_HDR_TYPES_PTHREAD_BARRIERATTR_T_H | ||
#define LLVM_LIBC_HDR_TYPES_PTHREAD_BARRIERATTR_T_H | ||
|
||
#ifdef LIBC_FULL_BUILD | ||
|
||
#include "include/llvm-libc-types/pthread_barrierattr_t.h" | ||
|
||
#else // Overlay mode | ||
|
||
#error "Cannot overlay pthread_barrierattr_t" | ||
|
||
#endif // LLVM_LIBC_FULL_BUILD | ||
|
||
#endif // LLVM_LIBC_HDR_TYPES_PTHREAD_BARRIERATTR_T_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
//===-- Definition of __barrier_type type ---------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_LIBC_TYPES__BARRIER_TYPE_H | ||
#define LLVM_LIBC_TYPES__BARRIER_TYPE_H | ||
|
||
typedef struct __attribute__((aligned(8 /* alignof (Barrier) */))) { | ||
unsigned expected; | ||
unsigned waiting; | ||
bool blocking; | ||
char entering[24 /* sizeof (CndVar) */]; | ||
char exiting[24 /* sizeof (CndVar) */]; | ||
char mutex[24 /* sizeof (Mutex) */]; | ||
} __barrier_type; | ||
|
||
#endif // LLVM_LIBC_TYPES__BARRIER_TYPE_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
//===-- Definition of pthread_barrier_t type --------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_LIBC_TYPES_PTHREAD_BARRIER_T_H | ||
#define LLVM_LIBC_TYPES_PTHREAD_BARRIER_T_H | ||
|
||
#include "include/llvm-libc-types/__barrier_type.h" | ||
typedef __barrier_type pthread_barrier_t; | ||
|
||
#endif // LLVM_LIBC_TYPES_PTHREAD_BARRIER_T_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
//===-- Definition of pthread_barrierattr_t type --------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_LIBC_TYPES_PTHREAD_BARRIERATTR_T_H | ||
#define LLVM_LIBC_TYPES_PTHREAD_BARRIERATTR_T_H | ||
|
||
typedef struct { | ||
bool pshared; | ||
} pthread_barrierattr_t; | ||
|
||
#endif // LLVM_LIBC_TYPES_PTHREAD_BARRIERATTR_T_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
//===-- Implementation of Barrier class ------------- ---------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "src/__support/threads/linux/barrier.h" | ||
#include "hdr/errno_macros.h" | ||
#include "src/__support/threads/CndVar.h" | ||
#include "src/__support/threads/mutex.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
int Barrier::init(Barrier *b, | ||
[[maybe_unused]] const pthread_barrierattr_t *attr, | ||
unsigned count) { | ||
LIBC_ASSERT(attr == nullptr); // TODO implement barrierattr | ||
if (count == 0) | ||
return EINVAL; | ||
|
||
b->expected = count; | ||
b->waiting = 0; | ||
b->blocking = true; | ||
|
||
int err; | ||
err = CndVar::init(&b->entering); | ||
if (err != 0) | ||
return err; | ||
|
||
err = CndVar::init(&b->exiting); | ||
if (err != 0) | ||
return err; | ||
|
||
auto mutex_err = Mutex::init(&b->m, false, false, false, false); | ||
if (mutex_err != MutexError::NONE) | ||
return EAGAIN; | ||
|
||
return 0; | ||
} | ||
|
||
int Barrier::wait() { | ||
m.lock(); | ||
|
||
// if the barrier is emptying out threads, wait until it finishes | ||
while (!blocking) | ||
entering.wait(&m); | ||
waiting++; | ||
|
||
if (waiting < expected) { | ||
// block threads until waiting = expected | ||
while (blocking) | ||
exiting.wait(&m); | ||
} else { | ||
// this is the last thread to call wait(), so lets wake everyone up | ||
blocking = false; | ||
exiting.broadcast(); | ||
} | ||
waiting--; | ||
|
||
if (waiting == 0) { | ||
// all threads have exited the barrier, let's let the ones waiting to enter | ||
// continue | ||
blocking = true; | ||
entering.broadcast(); | ||
m.unlock(); | ||
|
||
// POSIX dictates that the barrier should return a special value to just one | ||
// thread, so we can arbitrarily choose this thread | ||
return PTHREAD_BARRIER_SERIAL_THREAD; | ||
} | ||
m.unlock(); | ||
|
||
return 0; | ||
} | ||
|
||
int Barrier::destroy(Barrier *b) { | ||
CndVar::destroy(&b->entering); | ||
CndVar::destroy(&b->exiting); | ||
Mutex::destroy(&b->m); | ||
return 0; | ||
} | ||
|
||
} // namespace LIBC_NAMESPACE_DECL |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
//===-- A platform independent abstraction layer for barriers --*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_LIBC___SUPPORT_SRC_THREADS_LINUX_BARRIER_H | ||
#define LLVM_LIBC___SUPPORT_SRC_THREADS_LINUX_BARRIER_H | ||
|
||
#include "hdr/pthread_macros.h" | ||
#include "include/llvm-libc-types/pthread_barrier_t.h" | ||
#include "include/llvm-libc-types/pthread_barrierattr_t.h" | ||
#include "src/__support/threads/CndVar.h" | ||
#include "src/__support/threads/mutex.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
// NOTE: if the size of this class changes, you must ensure that the size of | ||
// pthread_barrier_t (found in include/llvm-libc/types/pthread_barrier_t.h) is | ||
// the same size | ||
class Barrier { | ||
private: | ||
unsigned expected; | ||
unsigned waiting; | ||
bool blocking; | ||
CndVar entering; | ||
CndVar exiting; | ||
Mutex m; | ||
|
||
public: | ||
static int init(Barrier *b, const pthread_barrierattr_t *attr, | ||
unsigned count); | ||
static int destroy(Barrier *b); | ||
int wait(); | ||
}; | ||
|
||
static_assert( | ||
sizeof(Barrier) == sizeof(pthread_barrier_t), | ||
"The public pthread_barrier_t type cannot accommodate the internal " | ||
"barrier type."); | ||
|
||
static_assert(alignof(Barrier) == alignof(pthread_barrier_t), | ||
"The public pthread_barrier_t type has a different alignment " | ||
"than the internal barrier type."); | ||
|
||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif // LLVM_LIBC___SUPPORT_SRC_THREADS_LINUX_BARRIER_H |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.