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