1 //===-- Implementation of the Rwlock's timedrdlock 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_timedrdlock.h"
10 #include "src/__support/common.h"
11 #include "src/__support/libc_assert.h"
12 #include "src/__support/macros/config.h"
13 #include "src/__support/macros/optimization.h"
14 #include "src/__support/threads/linux/rwlock.h"
15 #include "src/__support/time/linux/abs_timeout.h"
16 #include "src/errno/libc_errno.h"
20 namespace LIBC_NAMESPACE_DECL
{
23 sizeof(RwLock
) == sizeof(pthread_rwlock_t
) &&
24 alignof(RwLock
) == alignof(pthread_rwlock_t
),
25 "The public pthread_rwlock_t type must be of the same size and alignment "
26 "as the internal rwlock type.");
28 LLVM_LIBC_FUNCTION(int, pthread_rwlock_timedrdlock
,
29 (pthread_rwlock_t
* rwlock
,
30 const struct timespec
*abstime
)) {
33 RwLock
*rw
= reinterpret_cast<RwLock
*>(rwlock
);
34 LIBC_ASSERT(abstime
&& "timedrdlock called with a null timeout");
36 internal::AbsTimeout::from_timespec(*abstime
, /*is_realtime=*/true);
37 if (LIBC_LIKELY(timeout
.has_value()))
38 return static_cast<int>(rw
->read_lock(timeout
.value()));
40 switch (timeout
.error()) {
41 case internal::AbsTimeout::Error::Invalid
:
43 case internal::AbsTimeout::Error::BeforeEpoch
:
46 __builtin_unreachable();
49 } // namespace LIBC_NAMESPACE_DECL