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