From 8b4a7c860d004ea8dffc11f6232124b574fe6cd7 Mon Sep 17 00:00:00 2001 From: Jens Axboe Date: Thu, 12 Jun 2008 11:49:46 +0200 Subject: [PATCH] Add spinlock wrapper helpers Signed-off-by: Jens Axboe --- Makefile | 2 +- Makefile.FreeBSD | 2 +- Makefile.solaris | 2 +- spinlock.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ spinlock.h | 22 +++++++++++++++++++ 5 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 spinlock.c create mode 100644 spinlock.h diff --git a/Makefile b/Makefile index 11bb17e7..f05580a6 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ 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 \ - eta.o verify.o memory.o io_u.o parse.o mutex.o options.o \ + eta.o verify.o memory.o io_u.o parse.o mutex.o spinlock.o options.o \ rbtree.o diskutil.o fifo.o blktrace.o smalloc.o filehash.o OBJS += crc/crc7.o diff --git a/Makefile.FreeBSD b/Makefile.FreeBSD index 6b41db15..82b4931d 100644 --- a/Makefile.FreeBSD +++ b/Makefile.FreeBSD @@ -5,7 +5,7 @@ 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 \ - eta.o verify.o memory.o io_u.o parse.o mutex.o options.o \ + eta.o verify.o memory.o io_u.o parse.o mutex.o spinlock.o options.o \ rbtree.o smalloc.o filehash.o OBJS += crc/crc7.o diff --git a/Makefile.solaris b/Makefile.solaris index fd1029bc..b8e75f81 100644 --- a/Makefile.solaris +++ b/Makefile.solaris @@ -3,7 +3,7 @@ CFLAGS = -Wall -O2 -g -D_GNU_SOURCE -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 - 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 \ + eta.o verify.o memory.o io_u.o parse.o mutex.o spinlock.o options.o \ rbtree.o fifo.o smalloc.o filehash.o lib/strsep.o OBJS += crc/crc7.o diff --git a/spinlock.c b/spinlock.c new file mode 100644 index 00000000..38b4ddd4 --- /dev/null +++ b/spinlock.c @@ -0,0 +1,55 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "arch/arch.h" +#include "spinlock.h" + +void fio_spinlock_remove(struct fio_spinlock *lock) +{ + close(lock->lock_fd); + munmap((void *) lock, sizeof(*lock)); +} + +struct fio_spinlock *fio_spinlock_init(void) +{ + char spinlock_name[] = "/tmp/.fio_spinlock.XXXXXX"; + struct fio_spinlock *lock = NULL; + int fd; + + fd = mkstemp(spinlock_name); + if (fd < 0) { + perror("open spinlock"); + return NULL; + } + + if (ftruncate(fd, sizeof(struct fio_spinlock)) < 0) { + perror("ftruncate spinlock"); + goto err; + } + + lock = (void *) mmap(NULL, sizeof(struct fio_spinlock), + PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (lock == MAP_FAILED) { + perror("mmap spinlock"); + close(fd); + lock = NULL; + goto err; + } + + unlink(spinlock_name); + lock->lock_fd = fd; + spin_lock_init(&lock->slock); + + return lock; +err: + if (lock) + fio_spinlock_remove(lock); + + unlink(spinlock_name); + return NULL; +} diff --git a/spinlock.h b/spinlock.h new file mode 100644 index 00000000..ebc06b3d --- /dev/null +++ b/spinlock.h @@ -0,0 +1,22 @@ +#ifndef FIO_SPINLOCK_H +#define FIO_SPINLOCK_H + +struct fio_spinlock { + spinlock_t slock; + int lock_fd; +}; + +extern struct fio_spinlock *fio_spinlock_init(void); +extern void fio_spinlock_remove(struct fio_spinlock *); + +static inline void fio_spin_lock(struct fio_spinlock *lock) +{ + spin_lock(&lock->slock); +} + +static inline void fio_spin_unlock(struct fio_spinlock *lock) +{ + spin_unlock(&lock->slock); +} + +#endif -- 2.25.1