Split mutex.c and .h each into three files
authorBart Van Assche <bart.vanassche@wdc.com>
Wed, 14 Mar 2018 15:26:15 +0000 (08:26 -0700)
committerBart Van Assche <bart.vanassche@wdc.com>
Thu, 15 Mar 2018 16:54:43 +0000 (09:54 -0700)
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 <bart.vanassche@wdc.com>
14 files changed:
Makefile
backend.c
diskutil.h
filesetup.c
fio.h
helper_thread.c
iolog.c
mutex.c
mutex.h
pshared.c [new file with mode: 0644]
pshared.h [new file with mode: 0644]
rwlock.c [new file with mode: 0644]
rwlock.h [new file with mode: 0644]
workqueue.c

index d73b944fa20f35efc252ca4216c66ea3cae8608b..ce1deedc9239838a50b90e87d7f585898130aabd 100644 (file)
--- 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
index b4a09acae5ab0e2ccc2515a5b1251342f1dc34d9..69220e4597d61c97413ab6b7fdd6af9f644a5cdc 100644 (file)
--- 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;
index 91b42020a8077024607a3818c9314e7882189390..ee5c0afd75984535da56fae52b9788f8c3e591f2 100644 (file)
@@ -5,6 +5,7 @@
 
 #include "lib/output_buffer.h"
 #include "helper_thread.h"
+#include "mutex.h"
 
 struct disk_util_stats {
        uint64_t ios[2];
index 1a187ff2648d35c933740852751cd147c2ec0a36..4b0f40d6f9e458071446ab64becf007260188f1e 100644 (file)
@@ -15,6 +15,7 @@
 #include "os/os.h"
 #include "hash.h"
 #include "lib/axmap.h"
+#include "rwlock.h"
 
 #ifdef CONFIG_LINUX_FALLOCATE
 #include <linux/falloc.h>
diff --git a/fio.h b/fio.h
index 85546c55c5e9b5999292ce0c7444b99c68eb6cb2..0f6dfb6e6e8d93d8c35e97aaca5c5dd3d0c59044 100644 (file)
--- 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 <cuda.h>
 #endif
 
+struct fio_mutex;
+
 /*
  * offset generator types
  */
index b05f8212a6210e4cd9f00f6147759e3e5418249a..1d89d3a17ce070aa70d781e2b1fe1cb1af67c72f 100644 (file)
@@ -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 7d5a136cacc7972f855b62ec518909dc0acb7b20..460d7a2e51af8ada55d8b324547d6fb1dc12b74b 100644 (file)
--- 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 acc88dc33b98f0b7bb015df7676a15de6e16eb28..03291c7faf9392a62d373ea0cc5e55763fbba728 100644 (file)
--- a/mutex.c
+++ b/mutex.c
@@ -1,20 +1,11 @@
-#include <stdio.h>
 #include <string.h>
-#include <unistd.h>
-#include <stdlib.h>
-#include <fcntl.h>
-#include <time.h>
-#include <errno.h>
-#include <pthread.h>
 #include <sys/mman.h>
 #include <assert.h>
 
-#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 54009bae4ae081094172da149814dcea6e4e7eec..0c39873d83af28a8f418ec663ce627ff156a6499 100644 (file)
--- 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 (file)
index 0000000..74812ed
--- /dev/null
+++ b/pshared.c
@@ -0,0 +1,76 @@
+#include <string.h>
+
+#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 (file)
index 0000000..a58df6f
--- /dev/null
+++ b/pshared.h
@@ -0,0 +1,10 @@
+#ifndef FIO_PSHARED_H
+#define FIO_PSHARED_H
+
+#include <pthread.h>
+
+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 (file)
index 0000000..7d9ad73
--- /dev/null
+++ b/rwlock.c
@@ -0,0 +1,82 @@
+#include <stdio.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <assert.h>
+
+#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 (file)
index 0000000..2968eed
--- /dev/null
+++ b/rwlock.h
@@ -0,0 +1,19 @@
+#ifndef FIO_RWLOCK_H
+#define FIO_RWLOCK_H
+
+#include <pthread.h>
+
+#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
index 18ec198bee7e9de9c81db22758ad5c1e28ec62e4..841dbb9ffffa6de6252990592e7d750e876442b5 100644 (file)
@@ -10,6 +10,7 @@
 #include "flist.h"
 #include "workqueue.h"
 #include "smalloc.h"
+#include "pshared.h"
 
 enum {
        SW_F_IDLE       = 1 << 0,