Lines Matching refs:mutex

41 	struct pthread_mutex *mutex;  in pthread_mutex_init()  local
43 mutex = calloc(1, sizeof(*mutex)); in pthread_mutex_init()
44 if (!mutex) in pthread_mutex_init()
46 mutex->lock = _SPINLOCK_UNLOCKED; in pthread_mutex_init()
47 TAILQ_INIT(&mutex->lockers); in pthread_mutex_init()
49 mutex->type = PTHREAD_MUTEX_DEFAULT; in pthread_mutex_init()
50 mutex->prioceiling = -1; in pthread_mutex_init()
52 mutex->type = (*attr)->ma_type; in pthread_mutex_init()
53 mutex->prioceiling = (*attr)->ma_protocol == in pthread_mutex_init()
56 *mutexp = mutex; in pthread_mutex_init()
65 struct pthread_mutex *mutex; in pthread_mutex_destroy() local
68 mutex = (struct pthread_mutex *)*mutexp; in pthread_mutex_destroy()
69 if (mutex) { in pthread_mutex_destroy()
70 if (mutex->count || mutex->owner != NULL || in pthread_mutex_destroy()
71 !TAILQ_EMPTY(&mutex->lockers)) { in pthread_mutex_destroy()
77 free(mutex); in pthread_mutex_destroy()
88 struct pthread_mutex *mutex; in _rthread_mutex_lock() local
106 mutex = (struct pthread_mutex *)*mutexp; in _rthread_mutex_lock()
108 _rthread_debug(5, "%p: mutex_lock %p\n", (void *)self, (void *)mutex); in _rthread_mutex_lock()
109 _spinlock(&mutex->lock); in _rthread_mutex_lock()
110 if (mutex->owner == NULL && TAILQ_EMPTY(&mutex->lockers)) { in _rthread_mutex_lock()
111 assert(mutex->count == 0); in _rthread_mutex_lock()
112 mutex->owner = self; in _rthread_mutex_lock()
113 } else if (mutex->owner == self) { in _rthread_mutex_lock()
114 assert(mutex->count > 0); in _rthread_mutex_lock()
117 if (mutex->type != PTHREAD_MUTEX_RECURSIVE) in _rthread_mutex_lock()
120 mutex->type == PTHREAD_MUTEX_ERRORCHECK) { in _rthread_mutex_lock()
121 _spinunlock(&mutex->lock); in _rthread_mutex_lock()
126 if (mutex->type == PTHREAD_MUTEX_STRICT_NP && in _rthread_mutex_lock()
132 &mutex->lock, NULL) != EWOULDBLOCK) in _rthread_mutex_lock()
133 _spinlock(&mutex->lock); in _rthread_mutex_lock()
136 if (mutex->count == INT_MAX) { in _rthread_mutex_lock()
137 _spinunlock(&mutex->lock); in _rthread_mutex_lock()
142 _spinunlock(&mutex->lock); in _rthread_mutex_lock()
146 TAILQ_INSERT_TAIL(&mutex->lockers, self, waiting); in _rthread_mutex_lock()
147 while (mutex->owner != self) { in _rthread_mutex_lock()
149 &mutex->lock, NULL); in _rthread_mutex_lock()
150 _spinlock(&mutex->lock); in _rthread_mutex_lock()
151 assert(mutex->owner != NULL); in _rthread_mutex_lock()
153 if (mutex->owner == self) in _rthread_mutex_lock()
155 TAILQ_REMOVE(&mutex->lockers, self, waiting); in _rthread_mutex_lock()
156 _spinunlock(&mutex->lock); in _rthread_mutex_lock()
162 mutex->count++; in _rthread_mutex_lock()
163 _spinunlock(&mutex->lock); in _rthread_mutex_lock()
191 struct pthread_mutex *mutex = (struct pthread_mutex *)*mutexp; in pthread_mutex_unlock() local
194 (void *)mutex); in pthread_mutex_unlock()
196 if (mutex == NULL) in pthread_mutex_unlock()
205 if (mutex->owner != self) { in pthread_mutex_unlock()
206 if (mutex->type == PTHREAD_MUTEX_ERRORCHECK || in pthread_mutex_unlock()
207 mutex->type == PTHREAD_MUTEX_RECURSIVE) in pthread_mutex_unlock()
216 if (mutex->owner == NULL && in pthread_mutex_unlock()
217 mutex->type == PTHREAD_MUTEX_NORMAL) in pthread_mutex_unlock()
224 if (--mutex->count == 0) { in pthread_mutex_unlock()
227 _spinlock(&mutex->lock); in pthread_mutex_unlock()
228 mutex->owner = next = TAILQ_FIRST(&mutex->lockers); in pthread_mutex_unlock()
230 TAILQ_REMOVE(&mutex->lockers, next, waiting); in pthread_mutex_unlock()
231 _spinunlock(&mutex->lock); in pthread_mutex_unlock()
289 struct pthread_mutex *mutex = (struct pthread_mutex *)*mutexp; in pthread_cond_timedwait() local
304 (void *)cond, (void *)mutex); in pthread_cond_timedwait()
306 if (mutex == NULL) in pthread_cond_timedwait()
313 if (mutex->owner != self) { in pthread_cond_timedwait()
314 if (mutex->type == PTHREAD_MUTEX_ERRORCHECK) in pthread_cond_timedwait()
329 if (cond->mutex == NULL) { in pthread_cond_timedwait()
330 cond->mutex = mutex; in pthread_cond_timedwait()
332 } else if (cond->mutex != mutex) { in pthread_cond_timedwait()
333 assert(cond->mutex == mutex); in pthread_cond_timedwait()
341 mutex_count = mutex->count; in pthread_cond_timedwait()
344 _spinlock(&mutex->lock); in pthread_cond_timedwait()
350 mutex->count = 0; in pthread_cond_timedwait()
351 mutex->owner = next = TAILQ_FIRST(&mutex->lockers); in pthread_cond_timedwait()
353 TAILQ_REMOVE(&mutex->lockers, next, waiting); in pthread_cond_timedwait()
358 while (mutex->owner != self) { in pthread_cond_timedwait()
360 &mutex->lock, &self->delayed_cancel); in pthread_cond_timedwait()
369 _spinlock(&mutex->lock); in pthread_cond_timedwait()
381 _spinlock(&mutex->lock); in pthread_cond_timedwait()
402 _spinlock(&mutex->lock); in pthread_cond_timedwait()
415 assert(mutex == cond->mutex); in pthread_cond_timedwait()
417 cond->mutex = NULL; in pthread_cond_timedwait()
420 _spinlock(&mutex->lock); in pthread_cond_timedwait()
423 if (mutex->owner == NULL && in pthread_cond_timedwait()
424 TAILQ_EMPTY(&mutex->lockers)) { in pthread_cond_timedwait()
425 assert(mutex->count == 0); in pthread_cond_timedwait()
426 mutex->owner = self; in pthread_cond_timedwait()
429 TAILQ_INSERT_TAIL(&mutex->lockers, self, waiting); in pthread_cond_timedwait()
433 mutex->count = mutex_count; in pthread_cond_timedwait()
434 _spinunlock(&mutex->lock); in pthread_cond_timedwait()
445 struct pthread_mutex *mutex = (struct pthread_mutex *)*mutexp; in pthread_cond_wait() local
459 (void *)cond, (void *)mutex); in pthread_cond_wait()
461 if (mutex == NULL) in pthread_cond_wait()
468 if (mutex->owner != self) { in pthread_cond_wait()
469 if (mutex->type == PTHREAD_MUTEX_ERRORCHECK) in pthread_cond_wait()
480 if (cond->mutex == NULL) { in pthread_cond_wait()
481 cond->mutex = mutex; in pthread_cond_wait()
483 } else if (cond->mutex != mutex) { in pthread_cond_wait()
484 assert(cond->mutex == mutex); in pthread_cond_wait()
492 mutex_count = mutex->count; in pthread_cond_wait()
495 _spinlock(&mutex->lock); in pthread_cond_wait()
501 mutex->count = 0; in pthread_cond_wait()
502 mutex->owner = next = TAILQ_FIRST(&mutex->lockers); in pthread_cond_wait()
504 TAILQ_REMOVE(&mutex->lockers, next, waiting); in pthread_cond_wait()
509 while (mutex->owner != self) { in pthread_cond_wait()
510 error = __thrsleep(self, 0, NULL, &mutex->lock, in pthread_cond_wait()
521 _spinlock(&mutex->lock); in pthread_cond_wait()
541 _spinlock(&mutex->lock); in pthread_cond_wait()
552 assert(mutex == cond->mutex); in pthread_cond_wait()
554 cond->mutex = NULL; in pthread_cond_wait()
557 _spinlock(&mutex->lock); in pthread_cond_wait()
560 if (mutex->owner == NULL && in pthread_cond_wait()
561 TAILQ_EMPTY(&mutex->lockers)) { in pthread_cond_wait()
562 assert(mutex->count == 0); in pthread_cond_wait()
563 mutex->owner = self; in pthread_cond_wait()
566 TAILQ_INSERT_TAIL(&mutex->lockers, self, waiting); in pthread_cond_wait()
570 mutex->count = mutex_count; in pthread_cond_wait()
571 _spinunlock(&mutex->lock); in pthread_cond_wait()
583 struct pthread_mutex *mutex; in pthread_cond_signal() local
593 (void *)cond, (void *)cond->mutex); in pthread_cond_signal()
597 assert(cond->mutex == NULL); in pthread_cond_signal()
606 mutex = cond->mutex; in pthread_cond_signal()
607 assert(mutex != NULL); in pthread_cond_signal()
609 cond->mutex = NULL; in pthread_cond_signal()
612 _spinlock(&mutex->lock); in pthread_cond_signal()
615 wakeup = mutex->owner == NULL && TAILQ_EMPTY(&mutex->lockers); in pthread_cond_signal()
617 mutex->owner = thread; in pthread_cond_signal()
619 TAILQ_INSERT_TAIL(&mutex->lockers, thread, waiting); in pthread_cond_signal()
620 _spinunlock(&mutex->lock); in pthread_cond_signal()
631 struct pthread_mutex *mutex; in pthread_cond_broadcast() local
642 (void *)cond, (void *)cond->mutex); in pthread_cond_broadcast()
646 assert(cond->mutex == NULL); in pthread_cond_broadcast()
651 mutex = cond->mutex; in pthread_cond_broadcast()
652 assert(mutex != NULL); in pthread_cond_broadcast()
667 _spinlock(&mutex->lock); in pthread_cond_broadcast()
668 wakeup = mutex->owner == NULL && TAILQ_EMPTY(&mutex->lockers); in pthread_cond_broadcast()
669 thread->waiting.tqe_prev = mutex->lockers.tqh_last; in pthread_cond_broadcast()
670 *(mutex->lockers.tqh_last) = thread; in pthread_cond_broadcast()
673 mutex->lockers.tqh_last = cond->waiters.tqh_last; in pthread_cond_broadcast()
676 TAILQ_REMOVE(&mutex->lockers, thread, waiting); in pthread_cond_broadcast()
677 mutex->owner = thread; in pthread_cond_broadcast()
678 _spinunlock(&mutex->lock); in pthread_cond_broadcast()
681 _spinunlock(&mutex->lock); in pthread_cond_broadcast()
685 assert(cond->mutex != NULL); in pthread_cond_broadcast()
686 cond->mutex = NULL; in pthread_cond_broadcast()