[libc++][Android] Allow testing libc++ with clang-r536225 (#116149)
[llvm-project.git] / libc / src / pthread / pthread_rwlock_clockwrlock.cpp
blob8f58c7f24bb10a09838c5d14ed5fb0891927dd6a
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-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "src/pthread/pthread_rwlock_clockwrlock.h"
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"
17 #include <pthread.h>
19 namespace LIBC_NAMESPACE_DECL {
21 static_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.");
27 LLVM_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()));
42 switch (timeout.error()) {
43 case internal::AbsTimeout::Error::Invalid:
44 return EINVAL;
45 case internal::AbsTimeout::Error::BeforeEpoch:
46 return ETIMEDOUT;
48 __builtin_unreachable();
51 } // namespace LIBC_NAMESPACE_DECL