mailing list of musl libc
 help / color / mirror / code / Atom feed
8d65f20b58091d6012b8b0526daf3d7fe9d0c9dc blob 7487 bytes (raw)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
 
#include "pthread_impl.h"

/*
 * struct waiter
 *
 * Waiter objects have automatic storage on the waiting thread, and
 * are used in building a linked list representing waiters currently
 * waiting on the condition variable or a group of waiters woken
 * together by a broadcast or signal; in the case of signal, this is a
 * degenerate list of one member.
 *
 * Waiter lists attached to the condition variable itself are
 * protected by the lock on the cv. Detached waiter lists are never
 * modified again, but can only be traversed in reverse order, and are
 * protected by the "barrier" locks in each node, which are unlocked
 * in turn to control wake order.
 *
 * Since process-shared cond var semantics do not necessarily allow
 * one thread to see another's automatic storage (they may be in
 * different processes), the waiter list is not used for the
 * process-shared case, but the structure is still used to store data
 * needed by the cancellation cleanup handler.
 */

struct waiter {
	struct waiter *prev, *next;
	int state, barrier, mutex_ret;
	int *notify;
	pthread_mutex_t *mutex;
	pthread_cond_t *cond;
	int shared;
};

/* Self-synchronized-destruction-safe lock functions */

static inline void lock(volatile int *l)
{
	if (a_cas(l, 0, 1)) {
		a_cas(l, 1, 2);
		do __wait(l, 0, 2, 1);
		while (a_cas(l, 0, 2));
	}
}

/* Avoid taking the lock if we know it isn't necessary. */
static inline int lockRace(volatile int *l, int*volatile* notifier)
{
	int ret = 1;
	if (!*notifier && (ret = a_cas(l, 0, 1))) {
		a_cas(l, 1, 2);
		do __wait(l, 0, 2, 1);
		while (!*notifier && (ret = a_cas(l, 0, 2)));
	}
	return ret;
}

static inline void unlock(volatile int *l)
{
	if (a_swap(l, 0)==2)
		__wake(l, 1, 1);
}

static inline void unlockRace(volatile int *l, int known)
{
	int found = a_swap(l, 0);
	known += (found == 2);
	if (known > 0)
		__wake(l, known, 1);
}

static inline void unlock_requeue(volatile int *l, volatile int *r, int w)
{
	a_store(l, 0);
	if (w) __wake(l, 1, 1);
	else __syscall(SYS_futex, l, FUTEX_REQUEUE|128, 0, 1, r) != -ENOSYS
		|| __syscall(SYS_futex, l, FUTEX_REQUEUE, 0, 1, r);
}

/* Splice the node out of the current list of c and notify a signaling
   thread with whom there was contention. */
static inline void leave(struct waiter* node) {
	/* Access to cv object is valid because this waiter was not
	 * yet signaled and a new signal/broadcast cannot return
	 * after seeing a LEAVING waiter without getting notified
	 * via the futex notify below. */
	pthread_cond_t *c = node->cond;
	int locked = lockRace(&c->_c_lock, &node->notify);
	/* node->notify will only be changed while node is
	 * still in the list.*/
	int * ref = node->notify;

	if (!ref) {
		if (node->prev) node->prev->next = node->next;
		else if (c->_c_head == node) c->_c_head = node->next;
		if (node->next) node->next->prev = node->prev;
		else if (c->_c_tail == node) c->_c_tail = node->prev;

		unlock(&c->_c_lock);
	} else {
		/* A race occurred with a signaling or broadcasting thread. The call
		 * to unlockRace, there, ensures that sufficiently many waiters on _c_lock
		 * are woken up. */
		if (!locked) unlock(&c->_c_lock);

		/* There will be at most one signaling or broadcasting thread waiting on ref[0].
		 * Make sure that we don't waste a futex wake, if that thread isn't yet in futex wait. */
		if (a_fetch_add(&ref[0], -1)==1 && ref[1])
			__wake(&ref[0], 1, 1);
	}

	node->mutex_ret = pthread_mutex_lock(node->mutex);
}

static inline void enqueue(pthread_cond_t * c, struct waiter* node) {
	lock(&c->_c_lock);

	struct waiter* ohead = c->_c_head;
	node->next = ohead;
	if (ohead) ohead->prev = node;
	else c->_c_tail = node;
	c->_c_head = node;

	unlock(&c->_c_lock);
}

enum {
	WAITING,
	SIGNALED,
	LEAVING,
};

static void unwait(void *arg)
{
	struct waiter *node = arg;

	if (node->shared) {
		pthread_cond_t *c = node->cond;
		pthread_mutex_t *m = node->mutex;
		if (a_fetch_add(&c->_c_waiters, -1) == -0x7fffffff)
			__wake(&c->_c_waiters, 1, 0);
		node->mutex_ret = pthread_mutex_lock(m);
		return;
	}

	int oldstate = a_cas(&node->state, WAITING, LEAVING);

	if (oldstate == WAITING) {
		leave(node);
		return;
	}

	/* Lock barrier first to control wake order. */
	lock(&node->barrier);

	node->mutex_ret = pthread_mutex_lock(node->mutex);

	if (!node->next) a_inc(&node->mutex->_m_waiters);

	/* Unlock the barrier that's holding back the next waiter, and
	 * either wake it or requeue it to the mutex. */
	if (node->prev) {
		unlock_requeue(&node->prev->barrier,
			&node->mutex->_m_lock,
			node->mutex->_m_type & 128);
	} else {
		a_dec(&node->mutex->_m_waiters);
	}
}

int pthread_cond_timedwait(pthread_cond_t *restrict c, pthread_mutex_t *restrict m, const struct timespec *restrict ts)
{
	struct waiter node = { .cond = c, .mutex = m, .state = WAITING, .barrier = 2 };
	int e, seq, *fut, clock = c->_c_clock;

	if ((m->_m_type&15) && (m->_m_lock&INT_MAX) != __pthread_self()->tid)
		return EPERM;

	if (ts && ts->tv_nsec >= 1000000000UL)
		return EINVAL;

	pthread_testcancel();

	if (c->_c_shared) {
		node.shared = 1;
		fut = &c->_c_seq;
		seq = c->_c_seq;
		a_inc(&c->_c_waiters);
	} else {
		seq = node.barrier;
		fut = &node.barrier;

		enqueue(c, &node);
	}

	pthread_mutex_unlock(m);

	do e = __timedwait(fut, seq, clock, ts, unwait, &node, !node.shared);
	while (*fut==seq && (!e || e==EINTR));
	if (e == EINTR) e = 0;

	unwait(&node);

	return node.mutex_ret ? node.mutex_ret : e;
}

static inline int cond_signal (struct waiter * p, int* ref)
{
	int ret = a_cas(&p->state, WAITING, SIGNALED);
	if (ret != WAITING) {
		ref[0]++;
		p->notify = ref;
		if (p->prev) p->prev->next = p->next;
		if (p->next) p->next->prev = p->prev;
		p->next = 0;
		p->prev = 0;
	}
	return ret;
}

int __private_cond_signal(pthread_cond_t *c, int n)
{
	struct waiter *p, *prev, *first=0;
	int ref[2] = { 0 }, cur;

	if (n == 1) {
		lock(&c->_c_lock);
		for (p=c->_c_tail; p; p=prev) {
			prev = p->prev;
			if (!cond_signal(p, ref)) {
				first=p;
				p=prev;
				first->prev = 0;
				break;
			}
		}
		/* Split the list, leaving any remainder on the cv. */
		if (p) {
			p->next = 0;
		} else {
			c->_c_head = 0;
		}
		c->_c_tail = p;
		unlockRace(&c->_c_lock, ref[0]);
	} else {
		lock(&c->_c_lock);
                struct waiter * head = c->_c_head;
		if (head) {
			/* Signal head and tail first to reduce possible
			 * races for the cv to the beginning of the
			 * processing. */
			int headrace = cond_signal(head, ref);
			struct waiter * tail = c->_c_tail;
			p=tail->prev;
			if (tail != head) {
				if (!cond_signal(tail, ref)) first=tail;
				else while (p != head) {
					prev = p->prev;
					if (!cond_signal(p, ref)) {
						first=p;
						p=prev;
						break;
					}
					p=prev;
				}
			}
			if (!first && !headrace) first = head;
			c->_c_head = 0;
			c->_c_tail = 0;
			/* Now process the inner part of the list. */
			if (p) {
				while (p != head) {
					prev = p->prev;
					cond_signal(p, ref);
					p=prev;
				}
			}
		}
		unlockRace(&c->_c_lock, ref[0]);
	}

	/* Wait for any waiters in the LEAVING state to remove
	 * themselves from the list before returning or allowing
	 * signaled threads to proceed. */
	while ((cur = ref[0])) __wait(&ref[0], &ref[1], cur, 1);

	/* Allow first signaled waiter, if any, to proceed. */
	if (first) unlock(&first->barrier);

	return 0;
}
debug log:

solving 136fa6a ...
found 136fa6a in https://inbox.vuxu.org/musl/1409070919.8054.47.camel@eris.loria.fr/
found 2d192b0 in https://git.vuxu.org/mirror/musl/
preparing index
index prepared:
100644 2d192b07396ac9e32e69c95b3d6a539bc5b78f31	src/thread/pthread_cond_timedwait.c

applying [1/1] https://inbox.vuxu.org/musl/1409070919.8054.47.camel@eris.loria.fr/
diff --git a/src/thread/pthread_cond_timedwait.c b/src/thread/pthread_cond_timedwait.c\r
index 2d192b0..136fa6a 100644\r

Checking patch src/thread/pthread_cond_timedwait.c...
Applied patch src/thread/pthread_cond_timedwait.c cleanly.

index at:
100644 8d65f20b58091d6012b8b0526daf3d7fe9d0c9dc	src/thread/pthread_cond_timedwait.c

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).