xref: /aosp_15_r20/external/llvm-libc/src/pthread/pthread_spin_init.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1*71db0c75SAndroid Build Coastguard Worker //===-- Implementation of pthread_spin_init 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_init.h"
10*71db0c75SAndroid Build Coastguard Worker #include "hdr/errno_macros.h"
11*71db0c75SAndroid Build Coastguard Worker #include "src/__support/CPP/new.h"
12*71db0c75SAndroid Build Coastguard Worker #include "src/__support/common.h"
13*71db0c75SAndroid Build Coastguard Worker #include "src/__support/threads/spin_lock.h"
14*71db0c75SAndroid Build Coastguard Worker #include <pthread.h> // for PTHREAD_PROCESS_SHARED, PTHREAD_PROCESS_PRIVATE
15*71db0c75SAndroid Build Coastguard Worker 
16*71db0c75SAndroid Build Coastguard Worker namespace LIBC_NAMESPACE_DECL {
17*71db0c75SAndroid Build Coastguard Worker 
18*71db0c75SAndroid Build Coastguard Worker static_assert(sizeof(pthread_spinlock_t::__lockword) == sizeof(SpinLock) &&
19*71db0c75SAndroid Build Coastguard Worker                   alignof(decltype(pthread_spinlock_t::__lockword)) ==
20*71db0c75SAndroid Build Coastguard Worker                       alignof(SpinLock),
21*71db0c75SAndroid Build Coastguard Worker               "pthread_spinlock_t::__lockword and SpinLock must be of the same "
22*71db0c75SAndroid Build Coastguard Worker               "size and alignment");
23*71db0c75SAndroid Build Coastguard Worker 
24*71db0c75SAndroid Build Coastguard Worker LLVM_LIBC_FUNCTION(int, pthread_spin_init,
25*71db0c75SAndroid Build Coastguard Worker                    (pthread_spinlock_t * lock, [[maybe_unused]] int pshared)) {
26*71db0c75SAndroid Build Coastguard Worker   if (!lock)
27*71db0c75SAndroid Build Coastguard Worker     return EINVAL;
28*71db0c75SAndroid Build Coastguard Worker   if (pshared != PTHREAD_PROCESS_SHARED && pshared != PTHREAD_PROCESS_PRIVATE)
29*71db0c75SAndroid Build Coastguard Worker     return EINVAL;
30*71db0c75SAndroid Build Coastguard Worker   // The spin lock here is a simple atomic flag, so we don't need to do any
31*71db0c75SAndroid Build Coastguard Worker   // special handling for pshared.
32*71db0c75SAndroid Build Coastguard Worker   ::new (&lock->__lockword) SpinLock();
33*71db0c75SAndroid Build Coastguard Worker   lock->__owner = 0;
34*71db0c75SAndroid Build Coastguard Worker   return 0;
35*71db0c75SAndroid Build Coastguard Worker }
36*71db0c75SAndroid Build Coastguard Worker 
37*71db0c75SAndroid Build Coastguard Worker } // namespace LIBC_NAMESPACE_DECL
38