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_timedwrlock(pthread_rwlock_t * restrict rw,const struct timespec * restrict at)18 int __pthread_rwlock_timedwrlock(pthread_rwlock_t *restrict rw, const struct timespec *restrict at)
19 {
20 if (rw == NULL) {
21 return EINVAL;
22 }
23 int r, t;
24 int clock = (rw->_rw_clock == CLOCK_MONOTONIC) ? CLOCK_MONOTONIC : CLOCK_REALTIME;
25
26 r = pthread_rwlock_trywrlock(rw);
27 if (r != EBUSY) return r;
28
29 int spins = 100;
30 while (spins-- && rw->_rw_lock && !rw->_rw_waiters) a_spin();
31
32 while ((r=__pthread_rwlock_trywrlock(rw))==EBUSY) {
33 if (!(r=rw->_rw_lock)) continue;
34 t = r | 0x80000000;
35 a_inc(&rw->_rw_waiters);
36 a_cas(&rw->_rw_lock, r, t);
37 r = __timedwait(&rw->_rw_lock, t, clock, at, rw->_rw_shared^128);
38 a_dec(&rw->_rw_waiters);
39 if (r && r != EINTR) return r;
40 }
41 return r;
42 }
43
44 weak_alias(__pthread_rwlock_timedwrlock, pthread_rwlock_timedwrlock);
45