From 0b3366f6a93ac0f7e2028745ea557c0acd8722c6 Mon Sep 17 00:00:00 2001 From: "\"Steven J. Hill\"" Date: Sat, 7 May 2005 02:04:55 +0000 Subject: Import in NPTL code from glibc. For further information please consult the 'README.NPTL' file. --- libpthread/nptl/DESIGN-sem.txt | 46 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 libpthread/nptl/DESIGN-sem.txt (limited to 'libpthread/nptl/DESIGN-sem.txt') diff --git a/libpthread/nptl/DESIGN-sem.txt b/libpthread/nptl/DESIGN-sem.txt new file mode 100644 index 000000000..17eb0c11c --- /dev/null +++ b/libpthread/nptl/DESIGN-sem.txt @@ -0,0 +1,46 @@ +Semaphores pseudocode +============================== + + int sem_wait(sem_t * sem); + int sem_trywait(sem_t * sem); + int sem_post(sem_t * sem); + int sem_getvalue(sem_t * sem, int * sval); + +struct sem_t { + + unsigned int count; + - current semaphore count, also used as a futex +} + +sem_wait(sem_t *sem) +{ + for (;;) { + + if (atomic_decrement_if_positive(sem->count)) + break; + + futex_wait(&sem->count, 0) + } +} + +sem_post(sem_t *sem) +{ + n = atomic_increment(sem->count); + // Pass the new value of sem->count + futex_wake(&sem->count, n + 1); +} + +sem_trywait(sem_t *sem) +{ + if (atomic_decrement_if_positive(sem->count)) { + return 0; + } else { + return EAGAIN; + } +} + +sem_getvalue(sem_t *sem, int *sval) +{ + *sval = sem->count; + read_barrier(); +} -- cgit v1.2.3