| 1 |
#include "pthread_impl.h" |
| 2 |
|
| 3 |
#ifndef __wasilibc_unmodified_upstream |
| 4 |
#include <common/clock.h> |
| 5 |
#endif |
| 6 |
|
| 7 |
/* |
| 8 |
* struct waiter |
| 9 |
* |
| 10 |
* Waiter objects have automatic storage on the waiting thread, and |
| 11 |
* are used in building a linked list representing waiters currently |
| 12 |
* waiting on the condition variable or a group of waiters woken |
| 13 |
* together by a broadcast or signal; in the case of signal, this is a |
| 14 |
* degenerate list of one member. |
| 15 |
* |
| 16 |
* Waiter lists attached to the condition variable itself are |
| 17 |
* protected by the lock on the cv. Detached waiter lists are never |
| 18 |
* modified again, but can only be traversed in reverse order, and are |
| 19 |
* protected by the "barrier" locks in each node, which are unlocked |
| 20 |
* in turn to control wake order. |
| 21 |
* |
| 22 |
* Since process-shared cond var semantics do not necessarily allow |
| 23 |
* one thread to see another's automatic storage (they may be in |
| 24 |
* different processes), the waiter list is not used for the |
| 25 |
* process-shared case, but the structure is still used to store data |
| 26 |
* needed by the cancellation cleanup handler. |
| 27 |
*/ |
| 28 |
|
| 29 |
struct waiter { |
| 30 |
struct waiter *prev, *next; |
| 31 |
volatile int state, barrier; |
| 32 |
volatile int *notify; |
| 33 |
}; |
| 34 |
|
| 35 |
/* Self-synchronized-destruction-safe lock functions */ |
| 36 |
|
| 37 |
static inline void lock(volatile int *l) |
| 38 |
{ |
| 39 |
if (a_cas(l, 0, 1)) { |
| 40 |
a_cas(l, 1, 2); |
| 41 |
do __wait(l, 0, 2, 1); |
| 42 |
while (a_cas(l, 0, 2)); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
static inline void unlock(volatile int *l) |
| 47 |
{ |
| 48 |
if (a_swap(l, 0)==2) |
| 49 |
__wake(l, 1, 1); |
| 50 |
} |
| 51 |
|
| 52 |
static inline void unlock_requeue(volatile int *l, volatile int *r, int w) |
| 53 |
{ |
| 54 |
a_store(l, 0); |
| 55 |
#ifdef __wasilibc_unmodified_upstream |
| 56 |
if (w) __wake(l, 1, 1); |
| 57 |
else __syscall(SYS_futex, l, FUTEX_REQUEUE|FUTEX_PRIVATE, 0, 1, r) != -ENOSYS |
| 58 |
|| __syscall(SYS_futex, l, FUTEX_REQUEUE, 0, 1, r); |
| 59 |
#else |
| 60 |
// Always wake due to lack of requeue system call in WASI |
| 61 |
// This can impact the performance, so we might need to re-visit that decision |
| 62 |
__wake(l, 1, 1); |
| 63 |
#endif |
| 64 |
} |
| 65 |
|
| 66 |
enum { |
| 67 |
WAITING, |
| 68 |
SIGNALED, |
| 69 |
LEAVING, |
| 70 |
}; |
| 71 |
|
| 72 |
int __pthread_cond_timedwait(pthread_cond_t *restrict c, pthread_mutex_t *restrict m, const struct timespec *restrict ts) |
| 73 |
{ |
| 74 |
struct waiter node = { 0 }; |
| 75 |
int e, seq, clock = c->_c_clock, cs, shared=0, oldstate, tmp; |
| 76 |
#ifndef __wasilibc_unmodified_upstream |
| 77 |
struct __clockid clock_id = { .id = clock }; |
| 78 |
#endif |
| 79 |
volatile int *fut; |
| 80 |
|
| 81 |
if ((m->_m_type&15) && (m->_m_lock&INT_MAX) != __pthread_self()->tid) |
| 82 |
return EPERM; |
| 83 |
|
| 84 |
if (ts && ts->tv_nsec >= 1000000000UL) |
| 85 |
return EINVAL; |
| 86 |
|
| 87 |
__pthread_testcancel(); |
| 88 |
|
| 89 |
if (c->_c_shared) { |
| 90 |
shared = 1; |
| 91 |
fut = &c->_c_seq; |
| 92 |
seq = c->_c_seq; |
| 93 |
a_inc(&c->_c_waiters); |
| 94 |
} else { |
| 95 |
lock(&c->_c_lock); |
| 96 |
|
| 97 |
seq = node.barrier = 2; |
| 98 |
fut = &node.barrier; |
| 99 |
node.state = WAITING; |
| 100 |
node.next = c->_c_head; |
| 101 |
c->_c_head = &node; |
| 102 |
if (!c->_c_tail) c->_c_tail = &node; |
| 103 |
else node.next->prev = &node; |
| 104 |
|
| 105 |
unlock(&c->_c_lock); |
| 106 |
} |
| 107 |
|
| 108 |
__pthread_mutex_unlock(m); |
| 109 |
|
| 110 |
__pthread_setcancelstate(PTHREAD_CANCEL_MASKED, &cs); |
| 111 |
if (cs == PTHREAD_CANCEL_DISABLE) __pthread_setcancelstate(cs, 0); |
| 112 |
|
| 113 |
#ifdef __wasilibc_unmodified_upstream |
| 114 |
do e = __timedwait_cp(fut, seq, clock, ts, !shared); |
| 115 |
#else |
| 116 |
do e = __timedwait_cp(fut, seq, &clock_id, ts, !shared); |
| 117 |
#endif |
| 118 |
while (*fut==seq && (!e || e==EINTR)); |
| 119 |
if (e == EINTR) e = 0; |
| 120 |
|
| 121 |
if (shared) { |
| 122 |
/* Suppress cancellation if a signal was potentially |
| 123 |
* consumed; this is a legitimate form of spurious |
| 124 |
* wake even if not. */ |
| 125 |
if (e == ECANCELED && c->_c_seq != seq) e = 0; |
| 126 |
if (a_fetch_add(&c->_c_waiters, -1) == -0x7fffffff) |
| 127 |
__wake(&c->_c_waiters, 1, 0); |
| 128 |
oldstate = WAITING; |
| 129 |
goto relock; |
| 130 |
} |
| 131 |
|
| 132 |
oldstate = a_cas(&node.state, WAITING, LEAVING); |
| 133 |
|
| 134 |
if (oldstate == WAITING) { |
| 135 |
/* Access to cv object is valid because this waiter was not |
| 136 |
* yet signaled and a new signal/broadcast cannot return |
| 137 |
* after seeing a LEAVING waiter without getting notified |
| 138 |
* via the futex notify below. */ |
| 139 |
|
| 140 |
lock(&c->_c_lock); |
| 141 |
|
| 142 |
if (c->_c_head == &node) c->_c_head = node.next; |
| 143 |
else if (node.prev) node.prev->next = node.next; |
| 144 |
if (c->_c_tail == &node) c->_c_tail = node.prev; |
| 145 |
else if (node.next) node.next->prev = node.prev; |
| 146 |
|
| 147 |
unlock(&c->_c_lock); |
| 148 |
|
| 149 |
if (node.notify) { |
| 150 |
if (a_fetch_add(node.notify, -1)==1) |
| 151 |
__wake(node.notify, 1, 1); |
| 152 |
} |
| 153 |
} else { |
| 154 |
/* Lock barrier first to control wake order. */ |
| 155 |
lock(&node.barrier); |
| 156 |
} |
| 157 |
|
| 158 |
relock: |
| 159 |
/* Errors locking the mutex override any existing error or |
| 160 |
* cancellation, since the caller must see them to know the |
| 161 |
* state of the mutex. */ |
| 162 |
if ((tmp = pthread_mutex_lock(m))) e = tmp; |
| 163 |
|
| 164 |
if (oldstate == WAITING) goto done; |
| 165 |
|
| 166 |
if (!node.next && !(m->_m_type & 8)) |
| 167 |
a_inc(&m->_m_waiters); |
| 168 |
|
| 169 |
/* Unlock the barrier that's holding back the next waiter, and |
| 170 |
* either wake it or requeue it to the mutex. */ |
| 171 |
if (node.prev) { |
| 172 |
int val = m->_m_lock; |
| 173 |
if (val>0) a_cas(&m->_m_lock, val, val|0x80000000); |
| 174 |
unlock_requeue(&node.prev->barrier, &m->_m_lock, m->_m_type & (8|128)); |
| 175 |
} else if (!(m->_m_type & 8)) { |
| 176 |
a_dec(&m->_m_waiters); |
| 177 |
} |
| 178 |
|
| 179 |
/* Since a signal was consumed, cancellation is not permitted. */ |
| 180 |
if (e == ECANCELED) e = 0; |
| 181 |
|
| 182 |
done: |
| 183 |
__pthread_setcancelstate(cs, 0); |
| 184 |
|
| 185 |
if (e == ECANCELED) { |
| 186 |
__pthread_testcancel(); |
| 187 |
__pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 0); |
| 188 |
} |
| 189 |
|
| 190 |
return e; |
| 191 |
} |
| 192 |
|
| 193 |
int __private_cond_signal(pthread_cond_t *c, int n) |
| 194 |
{ |
| 195 |
struct waiter *p, *first=0; |
| 196 |
volatile int ref = 0; |
| 197 |
int cur; |
| 198 |
|
| 199 |
lock(&c->_c_lock); |
| 200 |
for (p=c->_c_tail; n && p; p=p->prev) { |
| 201 |
if (a_cas(&p->state, WAITING, SIGNALED) != WAITING) { |
| 202 |
ref++; |
| 203 |
p->notify = &ref; |
| 204 |
} else { |
| 205 |
n--; |
| 206 |
if (!first) first=p; |
| 207 |
} |
| 208 |
} |
| 209 |
/* Split the list, leaving any remainder on the cv. */ |
| 210 |
if (p) { |
| 211 |
if (p->next) p->next->prev = 0; |
| 212 |
p->next = 0; |
| 213 |
} else { |
| 214 |
c->_c_head = 0; |
| 215 |
} |
| 216 |
c->_c_tail = p; |
| 217 |
unlock(&c->_c_lock); |
| 218 |
|
| 219 |
/* Wait for any waiters in the LEAVING state to remove |
| 220 |
* themselves from the list before returning or allowing |
| 221 |
* signaled threads to proceed. */ |
| 222 |
while ((cur = ref)) __wait(&ref, 0, cur, 1); |
| 223 |
|
| 224 |
/* Allow first signaled waiter, if any, to proceed. */ |
| 225 |
if (first) unlock(&first->barrier); |
| 226 |
|
| 227 |
return 0; |
| 228 |
} |
| 229 |
|
| 230 |
weak_alias(__pthread_cond_timedwait, pthread_cond_timedwait); |