1 //===-- Implementation of the Rwlock's clockwrlock 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_rwlock_clockwrlock.h"
11 #include "hdr/errno_macros.h"
12 #include "src/__support/common.h"
13 #include "src/__support/macros/config.h"
14 #include "src/__support/threads/linux/rwlock.h"
15 #include "src/__support/time/linux/abs_timeout.h"
19 namespace LIBC_NAMESPACE_DECL
{
22 sizeof(RwLock
) == sizeof(pthread_rwlock_t
) &&
23 alignof(RwLock
) == alignof(pthread_rwlock_t
),
24 "The public pthread_rwlock_t type must be of the same size and alignment "
25 "as the internal rwlock type.");
27 LLVM_LIBC_FUNCTION(int, pthread_rwlock_clockwrlock
,
28 (pthread_rwlock_t
* rwlock
, clockid_t clockid
,
29 const timespec
*abstime
)) {
32 if (clockid
!= CLOCK_MONOTONIC
&& clockid
!= CLOCK_REALTIME
)
34 bool is_realtime
= (clockid
== CLOCK_REALTIME
);
35 RwLock
*rw
= reinterpret_cast<RwLock
*>(rwlock
);
36 LIBC_ASSERT(abstime
&& "clockwrlock called with a null timeout");
37 auto timeout
= internal::AbsTimeout::from_timespec(
38 *abstime
, /*is_realtime=*/is_realtime
);
39 if (LIBC_LIKELY(timeout
.has_value()))
40 return static_cast<int>(rw
->write_lock(timeout
.value()));
42 switch (timeout
.error()) {
43 case internal::AbsTimeout::Error::Invalid
:
45 case internal::AbsTimeout::Error::BeforeEpoch
:
48 __builtin_unreachable();
51 } // namespace LIBC_NAMESPACE_DECL