-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc] add spin lock family functions #100509
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
SchrodingerZhu
merged 7 commits into
llvm:main
from
SchrodingerZhu:libc/pthread_spin_lock
Aug 7, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7ada754
[libc] add spin lock family functions
SchrodingerZhu d30480d
[libc] add function spec to new header gen
SchrodingerZhu 3aceded
[libc] adjust license headers
SchrodingerZhu e580941
[libc] add integration tests
SchrodingerZhu 1bec21d
[libc] address code reviews
SchrodingerZhu 7a3b6ca
[libc] follow up changes with newest tid cache
SchrodingerZhu c81711f
[libc] address CR
SchrodingerZhu 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
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,17 @@ | ||
//===-- Definition of pthread_spinlock_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_SPINLOCK_T_H | ||
#define LLVM_LIBC_TYPES_PTHREAD_SPINLOCK_T_H | ||
#include "llvm-libc-types/pid_t.h" | ||
typedef struct { | ||
unsigned char __lockword; | ||
pid_t __owner; | ||
} pthread_spinlock_t; | ||
|
||
#endif // LLVM_LIBC_TYPES_PTHREAD_SPINLOCK_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
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,47 @@ | ||
//===-- Implementation of pthread_spin_destroy function -------------------===// | ||
// | ||
// 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/pthread/pthread_spin_destroy.h" | ||
#include "hdr/errno_macros.h" | ||
#include "src/__support/common.h" | ||
#include "src/__support/threads/spin_lock.h" | ||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
static_assert(sizeof(pthread_spinlock_t::__lockword) == sizeof(SpinLock) && | ||
alignof(decltype(pthread_spinlock_t::__lockword)) == | ||
alignof(SpinLock), | ||
"pthread_spinlock_t::__lockword and SpinLock must be of the same " | ||
"size and alignment"); | ||
|
||
LLVM_LIBC_FUNCTION(int, pthread_spin_destroy, | ||
([[maybe_unused]] pthread_spinlock_t * lock)) { | ||
// If an implementation detects that the value specified by the lock argument | ||
// to pthread_spin_lock() or pthread_spin_trylock() does not refer to an | ||
// initialized spin lock object, it is recommended that the function should | ||
// fail and report an [EINVAL] error. | ||
if (!lock) | ||
return EINVAL; | ||
auto spin_lock = reinterpret_cast<SpinLock *>(&lock->__lockword); | ||
if (!spin_lock || spin_lock->is_invalid()) | ||
return EINVAL; | ||
|
||
// If an implementation detects that the value specified by the lock argument | ||
// to pthread_spin_destroy() or pthread_spin_init() refers to a locked spin | ||
// lock object, or detects that the value specified by the lock argument to | ||
// pthread_spin_init() refers to an already initialized spin lock object, it | ||
// is recommended that the function should fail and report an [EBUSY] error. | ||
if (spin_lock->is_locked()) | ||
return EBUSY; | ||
|
||
// poison the lock | ||
spin_lock->~SpinLock(); | ||
lock->__owner = 0; | ||
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,21 @@ | ||
//===-- Implementation header for pthread_spin_destroy function --*- 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_SRC_PTHREAD_PTHREAD_SPIN_DESTROY_H | ||
#define LLVM_LIBC_SRC_PTHREAD_PTHREAD_SPIN_DESTROY_H | ||
|
||
#include "src/__support/macros/config.h" | ||
#include <pthread.h> | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
int pthread_spin_destroy(pthread_spinlock_t *lock); | ||
|
||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif // LLVM_LIBC_SRC_PTHREAD_PTHREAD_SPIN_DESTROY_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,37 @@ | ||
//===-- Implementation of pthread_spin_init function ----------------------===// | ||
// | ||
// 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/pthread/pthread_spin_init.h" | ||
#include "hdr/errno_macros.h" | ||
#include "src/__support/CPP/new.h" | ||
#include "src/__support/common.h" | ||
#include "src/__support/threads/spin_lock.h" | ||
#include <pthread.h> // for PTHREAD_PROCESS_SHARED, PTHREAD_PROCESS_PRIVATE | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
static_assert(sizeof(pthread_spinlock_t::__lockword) == sizeof(SpinLock) && | ||
alignof(decltype(pthread_spinlock_t::__lockword)) == | ||
alignof(SpinLock), | ||
"pthread_spinlock_t::__lockword and SpinLock must be of the same " | ||
"size and alignment"); | ||
|
||
LLVM_LIBC_FUNCTION(int, pthread_spin_init, | ||
(pthread_spinlock_t * lock, [[maybe_unused]] int pshared)) { | ||
if (!lock) | ||
return EINVAL; | ||
if (pshared != PTHREAD_PROCESS_SHARED && pshared != PTHREAD_PROCESS_PRIVATE) | ||
return EINVAL; | ||
// The spin lock here is a simple atomic flag, so we don't need to do any | ||
// special handling for pshared. | ||
::new (&lock->__lockword) SpinLock(); | ||
SchrodingerZhu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
lock->__owner = 0; | ||
return 0; | ||
} | ||
|
||
} // namespace LIBC_NAMESPACE_DECL |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have any issues created for pthread's proxy headers yet?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess no. Such routines are hardly used in overlay mode. That is being said, I think we should open one as many implementations do support overlay mode.