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