1 //===-- Implementation of pthread_spin_init function ----------------------===//
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
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
)) ==
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
)) {
28 if (pshared
!= PTHREAD_PROCESS_SHARED
&& pshared
!= PTHREAD_PROCESS_PRIVATE
)
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();
37 } // namespace LIBC_NAMESPACE_DECL