From ae626d4ead6416adf464cf209cdf3e8b85d58190 Mon Sep 17 00:00:00 2001 From: Bart Van Assche Date: Wed, 14 Mar 2018 08:26:15 -0700 Subject: [PATCH] Split mutex.c and .h each into three files Create separate header and source files for the struct fio_mutex functions, for the struct fio_rwlock functions and for the helper functions for initializing process shared mutexes and condition variables. Replace an #include directive by a forward declaration in fio.h. Minimize the #include directives in mutex.c, rwlock.c and pshared.c. This patch does not change any functionality. Signed-off-by: Bart Van Assche --- Makefile | 13 ++-- backend.c | 1 + diskutil.h | 1 + filesetup.c | 1 + fio.h | 3 +- helper_thread.c | 1 + iolog.c | 1 + mutex.c | 157 +----------------------------------------------- mutex.h | 16 ----- pshared.c | 76 +++++++++++++++++++++++ pshared.h | 10 +++ rwlock.c | 82 +++++++++++++++++++++++++ rwlock.h | 19 ++++++ workqueue.c | 1 + 14 files changed, 204 insertions(+), 178 deletions(-) create mode 100644 pshared.c create mode 100644 pshared.h create mode 100644 rwlock.c create mode 100644 rwlock.h diff --git a/Makefile b/Makefile index d73b944f..ce1deedc 100644 --- a/Makefile +++ b/Makefile @@ -41,7 +41,8 @@ endif SOURCE := $(sort $(patsubst $(SRCDIR)/%,%,$(wildcard $(SRCDIR)/crc/*.c)) \ $(patsubst $(SRCDIR)/%,%,$(wildcard $(SRCDIR)/lib/*.c))) \ gettime.c ioengines.c init.c stat.c log.c time.c filesetup.c \ - eta.c verify.c memory.c io_u.c parse.c mutex.c options.c \ + eta.c verify.c memory.c io_u.c parse.c mutex.c rwlock.c \ + pshared.c options.c \ smalloc.c filehash.c profile.c debug.c engines/cpu.c \ engines/mmap.c engines/sync.c engines/null.c engines/net.c \ engines/ftruncate.c engines/filecreate.c \ @@ -211,7 +212,8 @@ endif -include $(OBJS:.o=.d) T_SMALLOC_OBJS = t/stest.o -T_SMALLOC_OBJS += gettime.o mutex.o smalloc.o t/log.o t/debug.o t/arch.o +T_SMALLOC_OBJS += gettime.o mutex.o pshared.o smalloc.o t/log.o t/debug.o \ + t/arch.o T_SMALLOC_PROGS = t/stest T_IEEE_OBJS = t/ieee754.o @@ -244,9 +246,10 @@ T_BTRACE_FIO_PROGS = t/fio-btrace2fio endif T_DEDUPE_OBJS = t/dedupe.o -T_DEDUPE_OBJS += lib/rbtree.o t/log.o mutex.o smalloc.o gettime.o crc/md5.o \ - lib/memalign.o lib/bloom.o t/debug.o crc/xxhash.o t/arch.o \ - crc/murmur3.o crc/crc32c.o crc/crc32c-intel.o crc/crc32c-arm64.o crc/fnv.o +T_DEDUPE_OBJS += lib/rbtree.o t/log.o mutex.o pshared.o smalloc.o gettime.o \ + crc/md5.o lib/memalign.o lib/bloom.o t/debug.o crc/xxhash.o \ + t/arch.o crc/murmur3.o crc/crc32c.o crc/crc32c-intel.o \ + crc/crc32c-arm64.o crc/fnv.o T_DEDUPE_PROGS = t/fio-dedupe T_VS_OBJS = t/verify-state.o t/log.o crc/crc32c.o crc/crc32c-intel.o crc/crc32c-arm64.o t/debug.o diff --git a/backend.c b/backend.c index b4a09aca..69220e45 100644 --- a/backend.c +++ b/backend.c @@ -58,6 +58,7 @@ #include "lib/mountcheck.h" #include "rate-submit.h" #include "helper_thread.h" +#include "pshared.h" static struct fio_mutex *startup_mutex; static struct flist_head *cgroup_list; diff --git a/diskutil.h b/diskutil.h index 91b42020..ee5c0afd 100644 --- a/diskutil.h +++ b/diskutil.h @@ -5,6 +5,7 @@ #include "lib/output_buffer.h" #include "helper_thread.h" +#include "mutex.h" struct disk_util_stats { uint64_t ios[2]; diff --git a/filesetup.c b/filesetup.c index 1a187ff2..4b0f40d6 100644 --- a/filesetup.c +++ b/filesetup.c @@ -15,6 +15,7 @@ #include "os/os.h" #include "hash.h" #include "lib/axmap.h" +#include "rwlock.h" #ifdef CONFIG_LINUX_FALLOCATE #include diff --git a/fio.h b/fio.h index 85546c55..0f6dfb6e 100644 --- a/fio.h +++ b/fio.h @@ -20,7 +20,6 @@ #include "fifo.h" #include "arch/arch.h" #include "os/os.h" -#include "mutex.h" #include "log.h" #include "debug.h" #include "file.h" @@ -63,6 +62,8 @@ #include #endif +struct fio_mutex; + /* * offset generator types */ diff --git a/helper_thread.c b/helper_thread.c index b05f8212..1d89d3a1 100644 --- a/helper_thread.c +++ b/helper_thread.c @@ -2,6 +2,7 @@ #include "smalloc.h" #include "helper_thread.h" #include "steadystate.h" +#include "pshared.h" static struct helper_data { volatile int exit; diff --git a/iolog.c b/iolog.c index 7d5a136c..460d7a2e 100644 --- a/iolog.c +++ b/iolog.c @@ -20,6 +20,7 @@ #include "filelock.h" #include "smalloc.h" #include "blktrace.h" +#include "pshared.h" static int iolog_flush(struct io_log *log); diff --git a/mutex.c b/mutex.c index acc88dc3..03291c7f 100644 --- a/mutex.c +++ b/mutex.c @@ -1,20 +1,11 @@ -#include #include -#include -#include -#include -#include -#include -#include #include #include -#include "fio.h" #include "log.h" #include "mutex.h" -#include "arch/arch.h" +#include "pshared.h" #include "os/os.h" -#include "helpers.h" #include "fio_time.h" #include "gettime.h" @@ -36,78 +27,6 @@ void fio_mutex_remove(struct fio_mutex *mutex) munmap((void *) mutex, sizeof(*mutex)); } -int cond_init_pshared(pthread_cond_t *cond) -{ - pthread_condattr_t cattr; - int ret; - - ret = pthread_condattr_init(&cattr); - if (ret) { - log_err("pthread_condattr_init: %s\n", strerror(ret)); - return ret; - } - -#ifdef CONFIG_PSHARED - ret = pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_SHARED); - if (ret) { - log_err("pthread_condattr_setpshared: %s\n", strerror(ret)); - return ret; - } -#endif - ret = pthread_cond_init(cond, &cattr); - if (ret) { - log_err("pthread_cond_init: %s\n", strerror(ret)); - return ret; - } - - return 0; -} - -int mutex_init_pshared(pthread_mutex_t *mutex) -{ - pthread_mutexattr_t mattr; - int ret; - - ret = pthread_mutexattr_init(&mattr); - if (ret) { - log_err("pthread_mutexattr_init: %s\n", strerror(ret)); - return ret; - } - - /* - * Not all platforms support process shared mutexes (FreeBSD) - */ -#ifdef CONFIG_PSHARED - ret = pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED); - if (ret) { - log_err("pthread_mutexattr_setpshared: %s\n", strerror(ret)); - return ret; - } -#endif - ret = pthread_mutex_init(mutex, &mattr); - if (ret) { - log_err("pthread_mutex_init: %s\n", strerror(ret)); - return ret; - } - - return 0; -} - -int mutex_cond_init_pshared(pthread_mutex_t *mutex, pthread_cond_t *cond) -{ - int ret; - - ret = mutex_init_pshared(mutex); - if (ret) - return ret; - - ret = cond_init_pshared(cond); - if (ret) - return ret; - - return 0; -} - int __fio_mutex_init(struct fio_mutex *mutex, int value) { int ret; @@ -246,77 +165,3 @@ void fio_mutex_up(struct fio_mutex *mutex) pthread_mutex_unlock(&mutex->lock); } - -void fio_rwlock_write(struct fio_rwlock *lock) -{ - assert(lock->magic == FIO_RWLOCK_MAGIC); - pthread_rwlock_wrlock(&lock->lock); -} - -void fio_rwlock_read(struct fio_rwlock *lock) -{ - assert(lock->magic == FIO_RWLOCK_MAGIC); - pthread_rwlock_rdlock(&lock->lock); -} - -void fio_rwlock_unlock(struct fio_rwlock *lock) -{ - assert(lock->magic == FIO_RWLOCK_MAGIC); - pthread_rwlock_unlock(&lock->lock); -} - -void fio_rwlock_remove(struct fio_rwlock *lock) -{ - assert(lock->magic == FIO_RWLOCK_MAGIC); - munmap((void *) lock, sizeof(*lock)); -} - -struct fio_rwlock *fio_rwlock_init(void) -{ - struct fio_rwlock *lock; - pthread_rwlockattr_t attr; - int ret; - - lock = (void *) mmap(NULL, sizeof(struct fio_rwlock), - PROT_READ | PROT_WRITE, - OS_MAP_ANON | MAP_SHARED, -1, 0); - if (lock == MAP_FAILED) { - perror("mmap rwlock"); - lock = NULL; - goto err; - } - - lock->magic = FIO_RWLOCK_MAGIC; - - ret = pthread_rwlockattr_init(&attr); - if (ret) { - log_err("pthread_rwlockattr_init: %s\n", strerror(ret)); - goto err; - } -#ifdef CONFIG_PSHARED - ret = pthread_rwlockattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); - if (ret) { - log_err("pthread_rwlockattr_setpshared: %s\n", strerror(ret)); - goto destroy_attr; - } - - ret = pthread_rwlock_init(&lock->lock, &attr); -#else - ret = pthread_rwlock_init(&lock->lock, NULL); -#endif - - if (ret) { - log_err("pthread_rwlock_init: %s\n", strerror(ret)); - goto destroy_attr; - } - - pthread_rwlockattr_destroy(&attr); - - return lock; -destroy_attr: - pthread_rwlockattr_destroy(&attr); -err: - if (lock) - fio_rwlock_remove(lock); - return NULL; -} diff --git a/mutex.h b/mutex.h index 54009bae..0c39873d 100644 --- a/mutex.h +++ b/mutex.h @@ -5,7 +5,6 @@ #include "lib/types.h" #define FIO_MUTEX_MAGIC 0x4d555445U -#define FIO_RWLOCK_MAGIC 0x52574c4fU struct fio_mutex { pthread_mutex_t lock; @@ -15,11 +14,6 @@ struct fio_mutex { int magic; }; -struct fio_rwlock { - pthread_rwlock_t lock; - int magic; -}; - enum { FIO_MUTEX_LOCKED = 0, FIO_MUTEX_UNLOCKED = 1, @@ -34,14 +28,4 @@ extern void fio_mutex_down(struct fio_mutex *); extern bool fio_mutex_down_trylock(struct fio_mutex *); extern int fio_mutex_down_timeout(struct fio_mutex *, unsigned int); -extern void fio_rwlock_read(struct fio_rwlock *); -extern void fio_rwlock_write(struct fio_rwlock *); -extern void fio_rwlock_unlock(struct fio_rwlock *); -extern struct fio_rwlock *fio_rwlock_init(void); -extern void fio_rwlock_remove(struct fio_rwlock *); - -extern int mutex_init_pshared(pthread_mutex_t *); -extern int cond_init_pshared(pthread_cond_t *); -extern int mutex_cond_init_pshared(pthread_mutex_t *, pthread_cond_t *); - #endif diff --git a/pshared.c b/pshared.c new file mode 100644 index 00000000..74812ede --- /dev/null +++ b/pshared.c @@ -0,0 +1,76 @@ +#include + +#include "log.h" +#include "pshared.h" + +int cond_init_pshared(pthread_cond_t *cond) +{ + pthread_condattr_t cattr; + int ret; + + ret = pthread_condattr_init(&cattr); + if (ret) { + log_err("pthread_condattr_init: %s\n", strerror(ret)); + return ret; + } + +#ifdef CONFIG_PSHARED + ret = pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_SHARED); + if (ret) { + log_err("pthread_condattr_setpshared: %s\n", strerror(ret)); + return ret; + } +#endif + ret = pthread_cond_init(cond, &cattr); + if (ret) { + log_err("pthread_cond_init: %s\n", strerror(ret)); + return ret; + } + + return 0; +} + +int mutex_init_pshared(pthread_mutex_t *mutex) +{ + pthread_mutexattr_t mattr; + int ret; + + ret = pthread_mutexattr_init(&mattr); + if (ret) { + log_err("pthread_mutexattr_init: %s\n", strerror(ret)); + return ret; + } + + /* + * Not all platforms support process shared mutexes (FreeBSD) + */ +#ifdef CONFIG_PSHARED + ret = pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED); + if (ret) { + log_err("pthread_mutexattr_setpshared: %s\n", strerror(ret)); + return ret; + } +#endif + ret = pthread_mutex_init(mutex, &mattr); + if (ret) { + log_err("pthread_mutex_init: %s\n", strerror(ret)); + return ret; + } + + return 0; +} + +int mutex_cond_init_pshared(pthread_mutex_t *mutex, pthread_cond_t *cond) +{ + int ret; + + ret = mutex_init_pshared(mutex); + if (ret) + return ret; + + ret = cond_init_pshared(cond); + if (ret) + return ret; + + return 0; +} diff --git a/pshared.h b/pshared.h new file mode 100644 index 00000000..a58df6fe --- /dev/null +++ b/pshared.h @@ -0,0 +1,10 @@ +#ifndef FIO_PSHARED_H +#define FIO_PSHARED_H + +#include + +extern int mutex_init_pshared(pthread_mutex_t *); +extern int cond_init_pshared(pthread_cond_t *); +extern int mutex_cond_init_pshared(pthread_mutex_t *, pthread_cond_t *); + +#endif diff --git a/rwlock.c b/rwlock.c new file mode 100644 index 00000000..7d9ad731 --- /dev/null +++ b/rwlock.c @@ -0,0 +1,82 @@ +#include +#include +#include +#include + +#include "log.h" +#include "rwlock.h" +#include "os/os.h" + +void fio_rwlock_write(struct fio_rwlock *lock) +{ + assert(lock->magic == FIO_RWLOCK_MAGIC); + pthread_rwlock_wrlock(&lock->lock); +} + +void fio_rwlock_read(struct fio_rwlock *lock) +{ + assert(lock->magic == FIO_RWLOCK_MAGIC); + pthread_rwlock_rdlock(&lock->lock); +} + +void fio_rwlock_unlock(struct fio_rwlock *lock) +{ + assert(lock->magic == FIO_RWLOCK_MAGIC); + pthread_rwlock_unlock(&lock->lock); +} + +void fio_rwlock_remove(struct fio_rwlock *lock) +{ + assert(lock->magic == FIO_RWLOCK_MAGIC); + munmap((void *) lock, sizeof(*lock)); +} + +struct fio_rwlock *fio_rwlock_init(void) +{ + struct fio_rwlock *lock; + pthread_rwlockattr_t attr; + int ret; + + lock = (void *) mmap(NULL, sizeof(struct fio_rwlock), + PROT_READ | PROT_WRITE, + OS_MAP_ANON | MAP_SHARED, -1, 0); + if (lock == MAP_FAILED) { + perror("mmap rwlock"); + lock = NULL; + goto err; + } + + lock->magic = FIO_RWLOCK_MAGIC; + + ret = pthread_rwlockattr_init(&attr); + if (ret) { + log_err("pthread_rwlockattr_init: %s\n", strerror(ret)); + goto err; + } +#ifdef CONFIG_PSHARED + ret = pthread_rwlockattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); + if (ret) { + log_err("pthread_rwlockattr_setpshared: %s\n", strerror(ret)); + goto destroy_attr; + } + + ret = pthread_rwlock_init(&lock->lock, &attr); +#else + ret = pthread_rwlock_init(&lock->lock, NULL); +#endif + + if (ret) { + log_err("pthread_rwlock_init: %s\n", strerror(ret)); + goto destroy_attr; + } + + pthread_rwlockattr_destroy(&attr); + + return lock; +destroy_attr: + pthread_rwlockattr_destroy(&attr); +err: + if (lock) + fio_rwlock_remove(lock); + return NULL; +} diff --git a/rwlock.h b/rwlock.h new file mode 100644 index 00000000..2968eed9 --- /dev/null +++ b/rwlock.h @@ -0,0 +1,19 @@ +#ifndef FIO_RWLOCK_H +#define FIO_RWLOCK_H + +#include + +#define FIO_RWLOCK_MAGIC 0x52574c4fU + +struct fio_rwlock { + pthread_rwlock_t lock; + int magic; +}; + +extern void fio_rwlock_read(struct fio_rwlock *); +extern void fio_rwlock_write(struct fio_rwlock *); +extern void fio_rwlock_unlock(struct fio_rwlock *); +extern struct fio_rwlock *fio_rwlock_init(void); +extern void fio_rwlock_remove(struct fio_rwlock *); + +#endif diff --git a/workqueue.c b/workqueue.c index 18ec198b..841dbb9f 100644 --- a/workqueue.c +++ b/workqueue.c @@ -10,6 +10,7 @@ #include "flist.h" #include "workqueue.h" #include "smalloc.h" +#include "pshared.h" enum { SW_F_IDLE = 1 << 0, -- 2.25.1