summaryrefslogtreecommitdiff
path: root/libc/src/pthread/pthread_spin_trylock.cpp
diff options
context:
space:
mode:
authorAmir Ayupov <aaupov@fb.com>2024-08-10 23:47:48 -0700
committerAmir Ayupov <aaupov@fb.com>2024-08-10 23:47:48 -0700
commit6e733cb2ee2f80ce3ca3095f5a9dd1c1d22789d7 (patch)
tree5b459e8b7d90af381df39dba6d2d430c6a2e4fa7 /libc/src/pthread/pthread_spin_trylock.cpp
parent732c302187ce0bf0279bbf75f7067cf34bc63cfc (diff)
parent242f4e85eb5caa462a9835ac85c49e4a78dc1703 (diff)
[𝘀𝗽𝗿] changes introduced through rebaseusers/aaupov/spr/main.mcprofgennfc-expand-auto-for-mcdecodedpseudoprobe
Created using spr 1.3.4 [skip ci]
Diffstat (limited to 'libc/src/pthread/pthread_spin_trylock.cpp')
-rw-r--r--libc/src/pthread/pthread_spin_trylock.cpp41
1 files changed, 41 insertions, 0 deletions
diff --git a/libc/src/pthread/pthread_spin_trylock.cpp b/libc/src/pthread/pthread_spin_trylock.cpp
new file mode 100644
index 000000000000..99b0eac41355
--- /dev/null
+++ b/libc/src/pthread/pthread_spin_trylock.cpp
@@ -0,0 +1,41 @@
+//===-- Implementation of pthread_spin_trylock function -------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/pthread/pthread_spin_trylock.h"
+#include "hdr/errno_macros.h"
+#include "src/__support/common.h"
+#include "src/__support/threads/identifier.h"
+#include "src/__support/threads/spin_lock.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+static_assert(sizeof(pthread_spinlock_t::__lockword) == sizeof(SpinLock) &&
+ alignof(decltype(pthread_spinlock_t::__lockword)) ==
+ alignof(SpinLock),
+ "pthread_spinlock_t::__lockword and SpinLock must be of the same "
+ "size and alignment");
+
+LLVM_LIBC_FUNCTION(int, pthread_spin_trylock, (pthread_spinlock_t * lock)) {
+ // If an implementation detects that the value specified by the lock argument
+ // to pthread_spin_lock() or pthread_spin_trylock() does not refer to an
+ // initialized spin lock object, it is recommended that the function should
+ // fail and report an [EINVAL] error.
+ if (!lock)
+ return EINVAL;
+ auto spin_lock = reinterpret_cast<SpinLock *>(&lock->__lockword);
+ if (!spin_lock || spin_lock->is_invalid())
+ return EINVAL;
+ // Try to acquire the lock without blocking.
+ if (!spin_lock->try_lock())
+ return EBUSY;
+ // We have acquired the lock. Update the owner field.
+ lock->__owner = internal::gettid();
+ return 0;
+}
+
+} // namespace LIBC_NAMESPACE_DECL