diff options
| author | Schrodinger ZHU Yifan <yifanzhu@rochester.edu> | 2024-08-06 21:02:54 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-06 21:02:54 -0700 |
| commit | 03841e7ab847b279d65be707a8e0f2799fd69f50 (patch) | |
| tree | 9972900e054d9ab67ee998ebb5a88893773521c5 /libc/src/pthread/pthread_spin_destroy.cpp | |
| parent | 81d8273978ace8b9a2b8cfd811b94e95f9560ac5 (diff) | |
[libc] add spin lock family functions (#100509)
This PR:
- add entrypoints for `pthread_spin_*`
- additionally, the fixes a typo that has been disabling lock related
tests
Diffstat (limited to 'libc/src/pthread/pthread_spin_destroy.cpp')
| -rw-r--r-- | libc/src/pthread/pthread_spin_destroy.cpp | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/libc/src/pthread/pthread_spin_destroy.cpp b/libc/src/pthread/pthread_spin_destroy.cpp new file mode 100644 index 000000000000..7d93dd967afe --- /dev/null +++ b/libc/src/pthread/pthread_spin_destroy.cpp @@ -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 |
