about summary refs log tree commit diff
path: root/nptl/DESIGN-sem.txt
diff options
context:
space:
mode:
Diffstat (limited to 'nptl/DESIGN-sem.txt')
-rw-r--r--nptl/DESIGN-sem.txt45
1 files changed, 45 insertions, 0 deletions
diff --git a/nptl/DESIGN-sem.txt b/nptl/DESIGN-sem.txt
new file mode 100644
index 0000000000..d25a1c281d
--- /dev/null
+++ b/nptl/DESIGN-sem.txt
@@ -0,0 +1,45 @@
+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);
+  futex_wake(&sem->count, n);
+}
+
+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();
+}