xref: /aosp_15_r20/external/llvm-libc/src/pthread/pthread_rwlock_clockrdlock.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1*71db0c75SAndroid Build Coastguard Worker //===-- Implementation of the Rwlock's clockrdlock function ---------------===//
2*71db0c75SAndroid Build Coastguard Worker //
3*71db0c75SAndroid Build Coastguard Worker // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*71db0c75SAndroid Build Coastguard Worker // See https://llvm.org/LICENSE.txt for license information.
5*71db0c75SAndroid Build Coastguard Worker // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*71db0c75SAndroid Build Coastguard Worker //
7*71db0c75SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
8*71db0c75SAndroid Build Coastguard Worker 
9*71db0c75SAndroid Build Coastguard Worker #include "src/pthread/pthread_rwlock_clockrdlock.h"
10*71db0c75SAndroid Build Coastguard Worker 
11*71db0c75SAndroid Build Coastguard Worker #include "hdr/errno_macros.h"
12*71db0c75SAndroid Build Coastguard Worker #include "src/__support/common.h"
13*71db0c75SAndroid Build Coastguard Worker #include "src/__support/macros/config.h"
14*71db0c75SAndroid Build Coastguard Worker #include "src/__support/threads/linux/rwlock.h"
15*71db0c75SAndroid Build Coastguard Worker 
16*71db0c75SAndroid Build Coastguard Worker #include <pthread.h>
17*71db0c75SAndroid Build Coastguard Worker 
18*71db0c75SAndroid Build Coastguard Worker namespace LIBC_NAMESPACE_DECL {
19*71db0c75SAndroid Build Coastguard Worker 
20*71db0c75SAndroid Build Coastguard Worker static_assert(
21*71db0c75SAndroid Build Coastguard Worker     sizeof(RwLock) == sizeof(pthread_rwlock_t) &&
22*71db0c75SAndroid Build Coastguard Worker         alignof(RwLock) == alignof(pthread_rwlock_t),
23*71db0c75SAndroid Build Coastguard Worker     "The public pthread_rwlock_t type must be of the same size and alignment "
24*71db0c75SAndroid Build Coastguard Worker     "as the internal rwlock type.");
25*71db0c75SAndroid Build Coastguard Worker 
26*71db0c75SAndroid Build Coastguard Worker LLVM_LIBC_FUNCTION(int, pthread_rwlock_clockrdlock,
27*71db0c75SAndroid Build Coastguard Worker                    (pthread_rwlock_t * rwlock, clockid_t clockid,
28*71db0c75SAndroid Build Coastguard Worker                     const timespec *abstime)) {
29*71db0c75SAndroid Build Coastguard Worker   if (!rwlock)
30*71db0c75SAndroid Build Coastguard Worker     return EINVAL;
31*71db0c75SAndroid Build Coastguard Worker   if (clockid != CLOCK_MONOTONIC && clockid != CLOCK_REALTIME)
32*71db0c75SAndroid Build Coastguard Worker     return EINVAL;
33*71db0c75SAndroid Build Coastguard Worker   bool is_realtime = (clockid == CLOCK_REALTIME);
34*71db0c75SAndroid Build Coastguard Worker   RwLock *rw = reinterpret_cast<RwLock *>(rwlock);
35*71db0c75SAndroid Build Coastguard Worker   LIBC_ASSERT(abstime && "clockrdlock called with a null timeout");
36*71db0c75SAndroid Build Coastguard Worker   auto timeout = internal::AbsTimeout::from_timespec(
37*71db0c75SAndroid Build Coastguard Worker       *abstime, /*is_realtime=*/is_realtime);
38*71db0c75SAndroid Build Coastguard Worker   if (LIBC_LIKELY(timeout.has_value()))
39*71db0c75SAndroid Build Coastguard Worker     return static_cast<int>(rw->read_lock(timeout.value()));
40*71db0c75SAndroid Build Coastguard Worker 
41*71db0c75SAndroid Build Coastguard Worker   switch (timeout.error()) {
42*71db0c75SAndroid Build Coastguard Worker   case internal::AbsTimeout::Error::Invalid:
43*71db0c75SAndroid Build Coastguard Worker     return EINVAL;
44*71db0c75SAndroid Build Coastguard Worker   case internal::AbsTimeout::Error::BeforeEpoch:
45*71db0c75SAndroid Build Coastguard Worker     return ETIMEDOUT;
46*71db0c75SAndroid Build Coastguard Worker   }
47*71db0c75SAndroid Build Coastguard Worker   __builtin_unreachable();
48*71db0c75SAndroid Build Coastguard Worker }
49*71db0c75SAndroid Build Coastguard Worker 
50*71db0c75SAndroid Build Coastguard Worker } // namespace LIBC_NAMESPACE_DECL
51