1 //===-- Implementation of pthread_spin_unlock 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_unlock.h" 10 #include "hdr/errno_macros.h" 11 #include "src/__support/common.h" 12 #include "src/__support/threads/identifier.h" 13 #include "src/__support/threads/spin_lock.h" 14 15 namespace LIBC_NAMESPACE_DECL { 16 17 static_assert(sizeof(pthread_spinlock_t::__lockword) == sizeof(SpinLock) && 18 alignof(decltype(pthread_spinlock_t::__lockword)) == 19 alignof(SpinLock), 20 "pthread_spinlock_t::__lockword and SpinLock must be of the same " 21 "size and alignment"); 22 23 LLVM_LIBC_FUNCTION(int, pthread_spin_unlock, (pthread_spinlock_t * lock)) { 24 // If an implementation detects that the value specified by the lock argument 25 // to pthread_spin_lock() or pthread_spin_trylock() does not refer to an 26 // initialized spin lock object, it is recommended that the function should 27 // fail and report an [EINVAL] error. 28 if (!lock) 29 return EINVAL; 30 auto spin_lock = reinterpret_cast<SpinLock *>(&lock->__lockword); 31 if (!spin_lock || spin_lock->is_invalid()) 32 return EINVAL; 33 // If an implementation detects that the value specified by the lock argument 34 // to pthread_spin_unlock() refers to a spin lock object for which the current 35 // thread does not hold the lock, it is recommended that the function should 36 // fail and report an [EPERM] error. 37 if (lock->__owner != internal::gettid()) 38 return EPERM; 39 // Release the lock. 40 lock->__owner = 0; 41 spin_lock->unlock(); 42 return 0; 43 } 44 } // namespace LIBC_NAMESPACE_DECL 45