50 lines · cpp
1//===-- Implementation of the Rwlock's timedrdlock 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-exception6//7//===----------------------------------------------------------------------===//8 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/libc_errno.h"13#include "src/__support/macros/config.h"14#include "src/__support/macros/optimization.h"15#include "src/__support/threads/linux/rwlock.h"16#include "src/__support/time/linux/abs_timeout.h"17 18#include <pthread.h>19 20namespace LIBC_NAMESPACE_DECL {21 22static_assert(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.");27 28LLVM_LIBC_FUNCTION(int, pthread_rwlock_timedrdlock,29 (pthread_rwlock_t * rwlock,30 const struct timespec *abstime)) {31 if (!rwlock)32 return EINVAL;33 RwLock *rw = reinterpret_cast<RwLock *>(rwlock);34 LIBC_ASSERT(abstime && "timedrdlock called with a null timeout");35 auto 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()));39 40 switch (timeout.error()) {41 case internal::AbsTimeout::Error::Invalid:42 return EINVAL;43 case internal::AbsTimeout::Error::BeforeEpoch:44 return ETIMEDOUT;45 }46 __builtin_unreachable();47}48 49} // namespace LIBC_NAMESPACE_DECL50