summaryrefslogtreecommitdiff
path: root/libpthread/nptl/DESIGN-sem.txt
diff options
context:
space:
mode:
author"Steven J. Hill" <sjhill@realitydiluted.com>2005-05-07 02:04:55 +0000
committer"Steven J. Hill" <sjhill@realitydiluted.com>2005-05-07 02:04:55 +0000
commit0b3366f6a93ac0f7e2028745ea557c0acd8722c6 (patch)
tree2a87cb353c106eb55a44437d3b4d5e0d60af1d36 /libpthread/nptl/DESIGN-sem.txt
parent2274f6b2ddc7d7ea919a8fa1f9c54ef1920cb180 (diff)
Import in NPTL code from glibc. For further information please
consult the 'README.NPTL' file.
Diffstat (limited to 'libpthread/nptl/DESIGN-sem.txt')
-rw-r--r--libpthread/nptl/DESIGN-sem.txt46
1 files changed, 46 insertions, 0 deletions
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();
+}