[libc++][Android] Allow testing libc++ with clang-r536225 (#116149)
[llvm-project.git] / libc / src / pthread / pthread_spin_init.cpp
blob54972475288bcab03c91ddf14880d84c5d6e238d
1 //===-- Implementation of pthread_spin_init 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 //===----------------------------------------------------------------------===//
9 #include "src/pthread/pthread_spin_init.h"
10 #include "hdr/errno_macros.h"
11 #include "src/__support/CPP/new.h"
12 #include "src/__support/common.h"
13 #include "src/__support/threads/spin_lock.h"
14 #include <pthread.h> // for PTHREAD_PROCESS_SHARED, PTHREAD_PROCESS_PRIVATE
16 namespace LIBC_NAMESPACE_DECL {
18 static_assert(sizeof(pthread_spinlock_t::__lockword) == sizeof(SpinLock) &&
19 alignof(decltype(pthread_spinlock_t::__lockword)) ==
20 alignof(SpinLock),
21 "pthread_spinlock_t::__lockword and SpinLock must be of the same "
22 "size and alignment");
24 LLVM_LIBC_FUNCTION(int, pthread_spin_init,
25 (pthread_spinlock_t * lock, [[maybe_unused]] int pshared)) {
26 if (!lock)
27 return EINVAL;
28 if (pshared != PTHREAD_PROCESS_SHARED && pshared != PTHREAD_PROCESS_PRIVATE)
29 return EINVAL;
30 // The spin lock here is a simple atomic flag, so we don't need to do any
31 // special handling for pshared.
32 ::new (&lock->__lockword) SpinLock();
33 lock->__owner = 0;
34 return 0;
37 } // namespace LIBC_NAMESPACE_DECL