1 #include "pthread_impl.h"
2 #ifdef USE_MUTEX_WAIT_OPT
3 #include "musl_opt.h"
4 #endif
5
6 #define IS32BIT(x) !((x)+0x80000000ULL>>32)
7 #define CLAMP(x) (int)(IS32BIT(x) ? (x) : 0x7fffffffU+((0ULL+(x))>>63))
8
__futex4(volatile void * addr,int op,int val,const struct timespec * to)9 static int __futex4(volatile void *addr, int op, int val, const struct timespec *to)
10 {
11 #ifdef SYS_futex_time64
12 time_t s = to ? to->tv_sec : 0;
13 long ns = to ? to->tv_nsec : 0;
14 int r = -ENOSYS;
15 if (SYS_futex == SYS_futex_time64 || !IS32BIT(s))
16 r = __syscall(SYS_futex_time64, addr, op, val,
17 to ? ((long long[]){s, ns}) : 0);
18 if (SYS_futex == SYS_futex_time64 || r!=-ENOSYS) return r;
19 to = to ? (void *)(long[]){CLAMP(s), ns} : 0;
20 #endif
21 return __syscall(SYS_futex, addr, op, val, to);
22 }
23
pthread_mutex_timedlock_pi(pthread_mutex_t * restrict m,const struct timespec * restrict at)24 static int pthread_mutex_timedlock_pi(pthread_mutex_t *restrict m, const struct timespec *restrict at)
25 {
26 int clock = m->_m_clock;
27 int type = m->_m_type;
28 int priv = (type & 128) ^ 128;
29 pthread_t self = __pthread_self();
30 int e;
31
32 if (!priv) self->robust_list.pending = &m->_m_next;
33
34 do e = -__futex4(&m->_m_lock, FUTEX_LOCK_PI|priv, 0, at);
35 while (e==EINTR);
36 if (e) self->robust_list.pending = 0;
37
38 switch (e) {
39 case 0:
40 /* Catch spurious success for non-robust mutexes. */
41 if (!(type&4) && ((m->_m_lock & 0x40000000) || m->_m_waiters)) {
42 a_store(&m->_m_waiters, -1);
43 __syscall(SYS_futex, &m->_m_lock, FUTEX_UNLOCK_PI|priv);
44 self->robust_list.pending = 0;
45 break;
46 }
47 /* Signal to trylock that we already have the lock. */
48 m->_m_count = -1;
49 return __pthread_mutex_trylock_owner(m);
50 case ETIMEDOUT:
51 return e;
52 case EDEADLK:
53 if ((type&3) == PTHREAD_MUTEX_ERRORCHECK) return e;
54 }
55 do e = __timedwait(&(int){0}, 0, clock, at, 1);
56 while (e != ETIMEDOUT);
57 return e;
58 }
59
__pthread_mutex_timedlock_inner(pthread_mutex_t * restrict m,const struct timespec * restrict at)60 int __pthread_mutex_timedlock_inner(pthread_mutex_t *restrict m, const struct timespec *restrict at)
61 {
62 int type = m->_m_type;
63 int r;
64 // PI
65 #ifndef __LITEOS_A__
66 if (type & 8) {
67 r = __pthread_mutex_trylock_owner(m);
68 if (r != EBUSY) return r;
69 return pthread_mutex_timedlock_pi(m, at);
70 }
71 #endif
72 r = __pthread_mutex_trylock(m);
73 if (r != EBUSY) return r;
74 int clock = (m->_m_clock == CLOCK_MONOTONIC) ? CLOCK_MONOTONIC : CLOCK_REALTIME;
75 int t, priv = (type & 128) ^ 128;
76
77 /* Marco: Optimazation for mutex lock wait.
78 When enabled, call function implemented in libkccl_lockopt.so.
79 Otherwise, use the default spin.
80 */
81 #ifdef USE_MUTEX_WAIT_OPT
82 lock_func(m);
83 #else
84 int spins = 100;
85 while (spins-- && m->_m_lock && !m->_m_waiters) a_spin();
86 #endif
87
88 while ((r=__pthread_mutex_trylock(m)) == EBUSY) {
89 r = m->_m_lock;
90 int own = r & 0x3fffffff;
91 if (!own && (!r || (type&4)))
92 continue;
93 if ((type&3) == PTHREAD_MUTEX_ERRORCHECK
94 && own == __pthread_self()->tid)
95 return EDEADLK;
96
97 a_inc(&m->_m_waiters);
98 t = r | 0x80000000;
99 a_cas(&m->_m_lock, r, t);
100 r = __timedwait(&m->_m_lock, t, clock, at, priv);
101 a_dec(&m->_m_waiters);
102 if (r && r != EINTR) break;
103 }
104 return r;
105 }
106
__pthread_mutex_timedlock(pthread_mutex_t * restrict m,const struct timespec * restrict at)107 int __pthread_mutex_timedlock(pthread_mutex_t *restrict m, const struct timespec *restrict at)
108 {
109 if ((m->_m_type&15) == PTHREAD_MUTEX_NORMAL
110 && !a_cas(&m->_m_lock, 0, EBUSY))
111 return 0;
112 return __pthread_mutex_timedlock_inner(m, at);
113 }
114
115 weak_alias(__pthread_mutex_timedlock, pthread_mutex_timedlock);
116