1 /* Linuxthreads - a simple clone()-based implementation of Posix */
2 /* threads for Linux. */
3 /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr) */
5 /* This program is free software; you can redistribute it and/or */
6 /* modify it under the terms of the GNU Library General Public License */
7 /* as published by the Free Software Foundation; either version 2 */
8 /* of the License, or (at your option) any later version. */
10 /* This program is distributed in the hope that it will be useful, */
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
13 /* GNU Library General Public License for more details. */
18 #include <bits/libc-lock.h>
26 #include "internals.h"
31 #include <l4/util/atomic.h>
32 #include <l4/plr/pthread_rep.h>
35 int pthread_mutex_lock_rep(pthread_mutex_t * mutex);
36 int pthread_mutex_unlock_rep(pthread_mutex_t * mutex);
39 #define LOCKli(li, mtx) (li)->locks[(mtx)->__m_reserved]
40 #define ACQ(li, mtx) lock_li( (li), (mtx)->__m_reserved)
41 #define REL(li, mtx) unlock_li((li), (mtx)->__m_reserved)
43 #define YIELD() yield()
44 #define BARRIER() asm volatile ("" : : : "memory");
45 #define ASSERT42(cond, msg) do { /*if (cond) enter_kdebug42(msg);*/ } while (0)
50 * The generated code uses registers to access and modify data in
51 * the lock info page. This page is shared between all replicas, but
52 * the counts written to it may differ between replicas, which in turn
53 * may lead to the master process detecting state deviation if the values
54 * remain in those registers.
56 * To fix that, we store the original values of EBX, ECX, and EDX in
57 * thread-private storage and restore them before we a) leave the function or
58 * b) perform a system call that would be observed by the master.
60 * XXX: This assumes that we will not cause a page fault or
61 * any other exception during execution, because then we might end
62 * up with differing register values as well.
64 static inline void rep_function_save_regs(void)
67 asm volatile ("mov %%ebx, %0\t\n"
73 : "=m" (thread_self()->ebx),
74 "=m" (thread_self()->ecx),
75 "=m" (thread_self()->edx)/*,
76 "=m" (thread_self()->esi),
77 "=m" (thread_self()->edi)*/
84 static inline void rep_function_restore_regs(void)
88 asm volatile ("mov %0, %%ebx\t\n"
95 : "m" (thread_self()->ebx),
96 "m" (thread_self()->ecx),
97 "m" (thread_self()->edx)/*,
98 "m" (thread_self()->esi),
99 "m" (thread_self()->edi)*/
104 static inline int yield()
106 rep_function_restore_regs();
107 asm volatile ("ud2" : : : "edx", "ecx", "ebx", "memory");
108 rep_function_save_regs();
112 static inline void lock_rep_wait(pthread_mutex_t* mutex)
115 * Go to sleep. This is a system call and will be checked by the master. Therefore,
116 * we need to load the ECX and EDX values we pushed in the beginning, so that the
117 * master process sees a consistent state here.
119 rep_function_restore_regs();
122 "mov $0xA020, %%eax\t\n"
128 rep_function_save_regs();
133 static inline void lock_rep_post(pthread_mutex_t* mutex)
136 * Send the actual notification. This is a special case in the master,
137 * because here only one replica performs the system call while all
138 * others continue untouched.
141 asm volatile ("push %0\t\n"
142 "mov $0xA040, %%eax\t\n"
144 "pop %0\t\n": : "r" (mutex) : "eax", "memory");
148 static void init_replica_mutex(pthread_mutex_t* mtx)
151 lock_info* li = get_lock_info();
153 rep_function_save_regs();
156 * find either the respective lock (if it has been registered by another
157 * replica yet) or a free slot to use
159 for (i = 0; i < NUM_LOCKS; ++i) {
163 // slot already acquired by another replica
164 if (li->locks[i].lockdesc == (l4_addr_t)mtx) {
165 mtx->__m_reserved = i;
170 // free slot -> we can use it as we do not release
172 if (li->locks[i].owner == lock_entry_free) {
173 li->locks[i].lockdesc = (l4_addr_t)mtx;
174 li->locks[i].owner = lock_unowned;
175 mtx->__m_reserved = i;
183 if (i >= NUM_LOCKS) {
184 enter_kdebug("out of locks");
187 rep_function_restore_regs();
192 pthread_mutex_lock_rep(pthread_mutex_t * mutex)
194 rep_function_save_regs();
197 * not initialized yet? -> happens for statically initialized
198 * locks as those don't call mutex_init(). And as we need to
199 * handle this anyway, we simply don't overwrite mutex_init()
202 if (!mutex->__m_reserved) {
203 init_replica_mutex(mutex);
206 unsigned retry_counter = 0;
207 lock_info* li = get_lock_info();
208 thread_self()->p_epoch += 1;
210 /*outstring("lock() "); outhex32(thread_self()->p_epoch); outstring("\n");*/
216 if (LOCKli(li, mutex).owner == lock_unowned)
218 ASSERT42(LOCKli(li, mutex).wait_count != 0, "wait count != 0");
219 ASSERT42(LOCKli(li, mutex).acq_count != 0, "acq count != 0");
220 ASSERT42(LOCKli(li, mutex).wake_count != 0, "wake count != 0");
222 LOCKli(li, mutex).owner = (l4_addr_t)thread_self();
223 LOCKli(li, mutex).owner_epoch = thread_self()->p_epoch;
224 LOCKli(li, mutex).acq_count = li->replica_count;
227 else if (LOCKli(li, mutex).owner == (l4_addr_t)thread_self())
229 if (LOCKli(li, mutex).owner_epoch != thread_self()->p_epoch) {
230 //outchar42('.'); outchar42(' '); outhex42(thread_self());
235 // XXX allow multiple subsequent lock acquisitions */
237 outhex42(LOCKli(li, mutex).owner_epoch); outchar42(' ');
238 outhex42(thread_self()->p_epoch); outchar42('\n');
239 enter_kdebug42("epoch mismatch");
244 //enter_kdebug42("mtx owned by me");
249 * XXX: Spin for a short while?
252 LOCKli(li, mutex).wait_count += 1;
255 lock_rep_wait(mutex);
259 LOCKli(li, mutex).wake_count -= 1;
260 LOCKli(li, mutex).wait_count -= 1;
262 if (LOCKli(li, mutex).wake_count == 0) {
263 ASSERT42(LOCKli(li, mutex).acq_count != 0, "acq count != 0");
264 LOCKli(li, mutex).owner = (l4_addr_t)thread_self();
265 LOCKli(li, mutex).owner_epoch = thread_self()->p_epoch;
266 LOCKli(li, mutex).acq_count = li->replica_count;
277 //enter_kdebug42("mtx: other owner");
283 rep_function_restore_regs();
288 int pthread_mutex_unlock_rep(pthread_mutex_t * mutex);
292 pthread_mutex_unlock_rep(pthread_mutex_t * mutex)
294 rep_function_save_regs();
295 lock_info *li = get_lock_info();
300 ASSERT42(LOCKli(li, mutex).owner != (l4_addr_t)thread_self(), "unlock not by owner");
301 ASSERT42(LOCKli(li, mutex).acq_count == 0, "acq count == 0");
303 LOCKli(li, mutex).acq_count -= 1;
304 if (LOCKli(li, mutex).acq_count == 0) {
306 if (LOCKli(li, mutex).wait_count != 0) {
307 LOCKli(li, mutex).wake_count = li->replica_count;
308 lock_rep_post(mutex);
309 /* don't reset owner */
311 LOCKli(li, mutex).owner = lock_unowned;
314 LOCKli(li, mutex).owner = lock_unowned;
319 rep_function_restore_regs();
321 //if (thread_self()->p_epoch % 10 == 0)
331 __pthread_mutex_init(pthread_mutex_t * mutex,
332 const pthread_mutexattr_t * mutex_attr)
334 __pthread_init_lock(&mutex->__m_lock);
336 mutex_attr == NULL ? PTHREAD_MUTEX_TIMED_NP : mutex_attr->__mutexkind;
337 mutex->__m_count = 0;
338 mutex->__m_owner = NULL;
341 strong_alias (__pthread_mutex_init, pthread_mutex_init)
345 __pthread_mutex_destroy(pthread_mutex_t * mutex)
347 switch (mutex->__m_kind) {
348 case PTHREAD_MUTEX_ADAPTIVE_NP:
349 case PTHREAD_MUTEX_RECURSIVE_NP:
350 if ((mutex->__m_lock.__status & 1) != 0)
353 case PTHREAD_MUTEX_ERRORCHECK_NP:
354 case PTHREAD_MUTEX_TIMED_NP:
355 if (mutex->__m_lock.__status != 0)
362 strong_alias (__pthread_mutex_destroy, pthread_mutex_destroy)
366 __pthread_mutex_trylock(pthread_mutex_t * mutex)
371 switch(mutex->__m_kind) {
372 case PTHREAD_MUTEX_ADAPTIVE_NP:
373 retcode = __pthread_trylock(&mutex->__m_lock);
375 case PTHREAD_MUTEX_RECURSIVE_NP:
376 self = thread_self();
377 if (mutex->__m_owner == self) {
381 retcode = __pthread_trylock(&mutex->__m_lock);
383 mutex->__m_owner = self;
384 mutex->__m_count = 0;
387 case PTHREAD_MUTEX_ERRORCHECK_NP:
388 retcode = __pthread_alt_trylock(&mutex->__m_lock);
390 mutex->__m_owner = thread_self();
393 case PTHREAD_MUTEX_TIMED_NP:
394 retcode = __pthread_alt_trylock(&mutex->__m_lock);
400 strong_alias (__pthread_mutex_trylock, pthread_mutex_trylock)
404 __pthread_mutex_lock(pthread_mutex_t * mutex)
408 switch(mutex->__m_kind) {
409 case PTHREAD_MUTEX_ADAPTIVE_NP:
411 __pthread_lock(&mutex->__m_lock, NULL);
413 pthread_mutex_lock_rep(mutex);
416 case PTHREAD_MUTEX_RECURSIVE_NP:
417 self = thread_self();
418 if (mutex->__m_owner == self) {
423 __pthread_lock(&mutex->__m_lock, self);
425 pthread_mutex_lock_rep(mutex);
427 mutex->__m_owner = self;
428 mutex->__m_count = 0;
430 case PTHREAD_MUTEX_ERRORCHECK_NP:
431 self = thread_self();
432 if (mutex->__m_owner == self) return EDEADLK;
434 __pthread_alt_lock(&mutex->__m_lock, self);
436 pthread_mutex_lock_rep(mutex);
438 mutex->__m_owner = self;
440 case PTHREAD_MUTEX_TIMED_NP:
442 __pthread_alt_lock(&mutex->__m_lock, NULL);
444 pthread_mutex_lock_rep(mutex);
452 strong_alias (__pthread_mutex_lock, pthread_mutex_lock)
457 __pthread_mutex_timedlock (pthread_mutex_t *mutex,
458 const struct timespec *abstime)
463 if (__builtin_expect (abstime->tv_nsec, 0) < 0
464 || __builtin_expect (abstime->tv_nsec, 0) >= 1000000000)
467 switch(mutex->__m_kind) {
468 case PTHREAD_MUTEX_ADAPTIVE_NP:
469 __pthread_lock(&mutex->__m_lock, NULL);
471 case PTHREAD_MUTEX_RECURSIVE_NP:
472 self = thread_self();
473 if (mutex->__m_owner == self) {
477 __pthread_lock(&mutex->__m_lock, self);
478 mutex->__m_owner = self;
479 mutex->__m_count = 0;
481 case PTHREAD_MUTEX_ERRORCHECK_NP:
482 self = thread_self();
483 if (mutex->__m_owner == self) return EDEADLK;
484 res = __pthread_alt_timedlock(&mutex->__m_lock, self, abstime);
487 mutex->__m_owner = self;
491 case PTHREAD_MUTEX_TIMED_NP:
492 /* Only this type supports timed out lock. */
493 return (__pthread_alt_timedlock(&mutex->__m_lock, NULL, abstime)
499 strong_alias (__pthread_mutex_timedlock, pthread_mutex_timedlock)
503 __pthread_mutex_unlock(pthread_mutex_t * mutex)
505 switch (mutex->__m_kind) {
506 case PTHREAD_MUTEX_ADAPTIVE_NP:
508 __pthread_unlock(&mutex->__m_lock);
510 pthread_mutex_unlock_rep(mutex);
513 case PTHREAD_MUTEX_RECURSIVE_NP:
514 if (mutex->__m_owner != thread_self())
516 if (mutex->__m_count > 0) {
520 mutex->__m_owner = NULL;
522 __pthread_unlock(&mutex->__m_lock);
524 pthread_mutex_unlock_rep(mutex);
527 case PTHREAD_MUTEX_ERRORCHECK_NP:
528 if (mutex->__m_owner != thread_self() || mutex->__m_lock.__status == 0)
530 mutex->__m_owner = NULL;
532 __pthread_alt_unlock(&mutex->__m_lock);
534 pthread_mutex_unlock_rep(mutex);
537 case PTHREAD_MUTEX_TIMED_NP:
539 __pthread_alt_unlock(&mutex->__m_lock);
541 pthread_mutex_unlock_rep(mutex);
548 strong_alias (__pthread_mutex_unlock, pthread_mutex_unlock)
552 __pthread_mutexattr_init(pthread_mutexattr_t *attr)
554 attr->__mutexkind = PTHREAD_MUTEX_TIMED_NP;
557 strong_alias (__pthread_mutexattr_init, pthread_mutexattr_init)
561 __pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
565 strong_alias (__pthread_mutexattr_destroy, pthread_mutexattr_destroy)
569 __pthread_mutexattr_settype(pthread_mutexattr_t *attr, int kind)
571 if (kind != PTHREAD_MUTEX_ADAPTIVE_NP
572 && kind != PTHREAD_MUTEX_RECURSIVE_NP
573 && kind != PTHREAD_MUTEX_ERRORCHECK_NP
574 && kind != PTHREAD_MUTEX_TIMED_NP)
576 attr->__mutexkind = kind;
579 weak_alias (__pthread_mutexattr_settype, pthread_mutexattr_settype)
580 strong_alias ( __pthread_mutexattr_settype, __pthread_mutexattr_setkind_np)
581 weak_alias (__pthread_mutexattr_setkind_np, pthread_mutexattr_setkind_np)
585 __pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *kind)
587 *kind = attr->__mutexkind;
590 weak_alias (__pthread_mutexattr_gettype, pthread_mutexattr_gettype)
591 strong_alias (__pthread_mutexattr_gettype, __pthread_mutexattr_getkind_np)
592 weak_alias (__pthread_mutexattr_getkind_np, pthread_mutexattr_getkind_np)
596 __pthread_mutexattr_getpshared (const pthread_mutexattr_t *attr,
599 *pshared = PTHREAD_PROCESS_PRIVATE;
602 weak_alias (__pthread_mutexattr_getpshared, pthread_mutexattr_getpshared)
606 __pthread_mutexattr_setpshared (pthread_mutexattr_t *attr, int pshared)
608 if (pshared != PTHREAD_PROCESS_PRIVATE && pshared != PTHREAD_PROCESS_SHARED)
611 /* For now it is not possible to shared a conditional variable. */
612 if (pshared != PTHREAD_PROCESS_PRIVATE)
617 weak_alias (__pthread_mutexattr_setpshared, pthread_mutexattr_setpshared)
619 /* Once-only execution */
621 static pthread_mutex_t once_masterlock = PTHREAD_MUTEX_INITIALIZER;
622 static pthread_cond_t once_finished = PTHREAD_COND_INITIALIZER;
623 static int fork_generation = 0; /* Child process increments this after fork. */
625 enum { NEVER = 0, IN_PROGRESS = 1, DONE = 2 };
627 /* If a thread is canceled while calling the init_routine out of
628 pthread once, this handler will reset the once_control variable
629 to the NEVER state. */
631 static void pthread_once_cancelhandler(void *arg)
633 pthread_once_t *once_control = arg;
635 pthread_mutex_lock(&once_masterlock);
636 *once_control = NEVER;
637 pthread_mutex_unlock(&once_masterlock);
638 pthread_cond_broadcast(&once_finished);
643 __pthread_once(pthread_once_t * once_control, void (*init_routine)(void))
645 /* flag for doing the condition broadcast outside of mutex */
648 /* Test without locking first for speed */
649 if (*once_control == DONE) {
650 READ_MEMORY_BARRIER();
653 /* Lock and test again */
657 pthread_mutex_lock(&once_masterlock);
659 /* If this object was left in an IN_PROGRESS state in a parent
660 process (indicated by stale generation field), reset it to NEVER. */
661 if ((*once_control & 3) == IN_PROGRESS && (*once_control & ~3) != fork_generation)
662 *once_control = NEVER;
664 /* If init_routine is being called from another routine, wait until
666 while ((*once_control & 3) == IN_PROGRESS) {
667 pthread_cond_wait(&once_finished, &once_masterlock);
669 /* Here *once_control is stable and either NEVER or DONE. */
670 if (*once_control == NEVER) {
671 *once_control = IN_PROGRESS | fork_generation;
672 pthread_mutex_unlock(&once_masterlock);
673 pthread_cleanup_push(pthread_once_cancelhandler, once_control);
675 pthread_cleanup_pop(0);
676 pthread_mutex_lock(&once_masterlock);
677 WRITE_MEMORY_BARRIER();
678 *once_control = DONE;
681 pthread_mutex_unlock(&once_masterlock);
684 pthread_cond_broadcast(&once_finished);
688 strong_alias (__pthread_once, pthread_once)
691 * Handle the state of the pthread_once mechanism across forks. The
692 * once_masterlock is acquired in the parent process prior to a fork to ensure
693 * that no thread is in the critical region protected by the lock. After the
694 * fork, the lock is released. In the child, the lock and the condition
695 * variable are simply reset. The child also increments its generation
696 * counter which lets pthread_once calls detect stale IN_PROGRESS states
697 * and reset them back to NEVER.
702 __pthread_once_fork_prepare(void)
704 pthread_mutex_lock(&once_masterlock);
709 __pthread_once_fork_parent(void)
711 pthread_mutex_unlock(&once_masterlock);
716 __pthread_once_fork_child(void)
718 pthread_mutex_init(&once_masterlock, NULL);
719 pthread_cond_init(&once_finished, NULL);
720 if (fork_generation <= INT_MAX - 4)
721 fork_generation += 4; /* leave least significant two bits zero */