1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "pthread_impl.h"
17
__pthread_rwlock_timedrdlock(pthread_rwlock_t * restrict rw,const struct timespec * restrict at)18 int __pthread_rwlock_timedrdlock(pthread_rwlock_t *restrict rw, const struct timespec *restrict at)
19 {
20 int r, t;
21 int clock = (rw->_rw_clock == CLOCK_MONOTONIC) ? CLOCK_MONOTONIC : CLOCK_REALTIME;
22
23 r = pthread_rwlock_tryrdlock(rw);
24 if (r != EBUSY) return r;
25
26 int spins = 100;
27 while (spins-- && rw->_rw_lock && !rw->_rw_waiters) a_spin();
28
29 while ((r=__pthread_rwlock_tryrdlock(rw))==EBUSY) {
30 if (!(r=rw->_rw_lock) || (r&0x7fffffff)!=0x7fffffff) continue;
31 t = r | 0x80000000;
32 a_inc(&rw->_rw_waiters);
33 a_cas(&rw->_rw_lock, r, t);
34 r = __timedwait(&rw->_rw_lock, t, clock, at, rw->_rw_shared^128);
35 a_dec(&rw->_rw_waiters);
36 if (r && r != EINTR) return r;
37 }
38 return r;
39 }
40
41 weak_alias(__pthread_rwlock_timedrdlock, pthread_rwlock_timedrdlock);