| 1 | //===-- Implementation of pthread_spin_destroy function -------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "src/pthread/pthread_spin_destroy.h" |
| 10 | #include "hdr/errno_macros.h" |
| 11 | #include "src/__support/common.h" |
| 12 | #include "src/__support/threads/spin_lock.h" |
| 13 | namespace LIBC_NAMESPACE_DECL { |
| 14 | |
| 15 | static_assert(sizeof(pthread_spinlock_t::__lockword) == sizeof(SpinLock) && |
| 16 | alignof(decltype(pthread_spinlock_t::__lockword)) == |
| 17 | alignof(SpinLock), |
| 18 | "pthread_spinlock_t::__lockword and SpinLock must be of the same " |
| 19 | "size and alignment" ); |
| 20 | |
| 21 | LLVM_LIBC_FUNCTION(int, pthread_spin_destroy, |
| 22 | ([[maybe_unused]] pthread_spinlock_t * lock)) { |
| 23 | // If an implementation detects that the value specified by the lock argument |
| 24 | // to pthread_spin_lock() or pthread_spin_trylock() does not refer to an |
| 25 | // initialized spin lock object, it is recommended that the function should |
| 26 | // fail and report an [EINVAL] error. |
| 27 | if (!lock) |
| 28 | return EINVAL; |
| 29 | auto spin_lock = reinterpret_cast<SpinLock *>(&lock->__lockword); |
| 30 | if (!spin_lock || spin_lock->is_invalid()) |
| 31 | return EINVAL; |
| 32 | |
| 33 | // If an implementation detects that the value specified by the lock argument |
| 34 | // to pthread_spin_destroy() or pthread_spin_init() refers to a locked spin |
| 35 | // lock object, or detects that the value specified by the lock argument to |
| 36 | // pthread_spin_init() refers to an already initialized spin lock object, it |
| 37 | // is recommended that the function should fail and report an [EBUSY] error. |
| 38 | if (spin_lock->is_locked()) |
| 39 | return EBUSY; |
| 40 | |
| 41 | // poison the lock |
| 42 | spin_lock->~SpinLock(); |
| 43 | lock->__owner = 0; |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | } // namespace LIBC_NAMESPACE_DECL |
| 48 | |