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