Add a semaphore implementation
[fio.git] / sem.c
diff --git a/sem.c b/sem.c
new file mode 100644 (file)
index 0000000..f94fa25
--- /dev/null
+++ b/sem.c
@@ -0,0 +1,44 @@
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <sys/mman.h>
+#include <assert.h>
+
+#include "sem.h"
+#include "smalloc.h"
+
+void fio_sem_remove(struct fio_sem *sem)
+{
+       sfree(sem);
+}
+
+struct fio_sem *fio_sem_init(int value)
+{
+       struct fio_sem *sem;
+
+       sem = smalloc(sizeof(*sem));
+       if (!sem)
+               return NULL;
+
+       sem->sem_val = value;
+
+       if (!sem_init(&sem->sem, 1, value))
+               return sem;
+
+       perror("sem_init");
+       sfree(sem);
+       return NULL;
+}
+
+void fio_sem_down(struct fio_sem *sem)
+{
+       sem_wait(&sem->sem);
+}
+
+void fio_sem_up(struct fio_sem *sem)
+{
+       sem_post(&sem->sem);
+}