| 1 | //===-- Implementation of the Rwlock's clockrdlock 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 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "src/pthread/pthread_rwlock_clockrdlock.h" |
| 10 | |
| 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 | |
| 16 | #include <pthread.h> |
| 17 | |
| 18 | namespace LIBC_NAMESPACE_DECL { |
| 19 | |
| 20 | static_assert( |
| 21 | sizeof(RwLock) == sizeof(pthread_rwlock_t) && |
| 22 | alignof(RwLock) == alignof(pthread_rwlock_t), |
| 23 | "The public pthread_rwlock_t type must be of the same size and alignment " |
| 24 | "as the internal rwlock type." ); |
| 25 | |
| 26 | LLVM_LIBC_FUNCTION(int, pthread_rwlock_clockrdlock, |
| 27 | (pthread_rwlock_t * rwlock, clockid_t clockid, |
| 28 | const timespec *abstime)) { |
| 29 | if (!rwlock) |
| 30 | return EINVAL; |
| 31 | if (clockid != CLOCK_MONOTONIC && clockid != CLOCK_REALTIME) |
| 32 | return EINVAL; |
| 33 | bool is_realtime = (clockid == CLOCK_REALTIME); |
| 34 | RwLock *rw = reinterpret_cast<RwLock *>(rwlock); |
| 35 | LIBC_ASSERT(abstime && "clockrdlock called with a null timeout" ); |
| 36 | auto timeout = internal::AbsTimeout::from_timespec( |
| 37 | *abstime, /*is_realtime=*/is_realtime); |
| 38 | if (LIBC_LIKELY(timeout.has_value())) |
| 39 | return static_cast<int>(rw->read_lock(timeout.value())); |
| 40 | |
| 41 | switch (timeout.error()) { |
| 42 | case internal::AbsTimeout::Error::Invalid: |
| 43 | return EINVAL; |
| 44 | case internal::AbsTimeout::Error::BeforeEpoch: |
| 45 | return ETIMEDOUT; |
| 46 | } |
| 47 | __builtin_unreachable(); |
| 48 | } |
| 49 | |
| 50 | } // namespace LIBC_NAMESPACE_DECL |
| 51 | |