Add a semaphore implementation
authorJens Axboe <jens.axboe@oracle.com>
Sat, 1 Mar 2008 17:38:12 +0000 (18:38 +0100)
committerJens Axboe <jens.axboe@oracle.com>
Sat, 1 Mar 2008 17:38:12 +0000 (18:38 +0100)
To be used by the file sharing.

Signed-off-by: Jens Axboe <jens.axboe@oracle.com>
Makefile
sem.c [new file with mode: 0644]
sem.h [new file with mode: 0644]

index e0e323de530940d4983f3f89388cddeb7319a9c8..7c79e910f560dedba4e59f7d8c082b9e233d3ba7 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -6,8 +6,8 @@ CFLAGS  = -Wwrite-strings -Wall -D_GNU_SOURCE -D_LARGEFILE_SOURCE -D_FILE_OFFSET_
 PROGS  = fio
 SCRIPTS = fio_generate_plots
 OBJS = gettime.o fio.o ioengines.o init.o stat.o log.o time.o filesetup.o \
 PROGS  = fio
 SCRIPTS = fio_generate_plots
 OBJS = gettime.o fio.o ioengines.o init.o stat.o log.o time.o filesetup.o \
-       eta.o verify.o memory.o io_u.o parse.o mutex.o options.o rbtree.o \
-       diskutil.o fifo.o blktrace.o smalloc.o
+       eta.o verify.o memory.o io_u.o parse.o mutex.o sem.o options.o \
+       rbtree.o diskutil.o fifo.o blktrace.o smalloc.o
 
 OBJS += crc/crc7.o
 OBJS += crc/crc16.o
 
 OBJS += crc/crc7.o
 OBJS += crc/crc16.o
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);
+}
diff --git a/sem.h b/sem.h
new file mode 100644 (file)
index 0000000..a7b4664
--- /dev/null
+++ b/sem.h
@@ -0,0 +1,16 @@
+#ifndef FIO_SEM_H
+#define FIO_SEM_H
+
+#include <semaphore.h>
+
+struct fio_sem {
+       sem_t sem;
+       int sem_val;
+};
+
+extern struct fio_sem *fio_sem_init(int);
+extern void fio_sem_remove(struct fio_sem *);
+extern void fio_sem_down(struct fio_sem *);
+extern void fio_sem_up(struct fio_sem *);
+
+#endif