Refactor #includes and headers
[fio.git] / fio_sem.c
index b9a1bf1e988d550f8ad439983bc7c014b22cc32a..3b48061cd266cc9f6629a5d52df0dc6e55317f54 100644 (file)
--- a/fio_sem.c
+++ b/fio_sem.c
@@ -1,8 +1,13 @@
+#include <stdio.h>
 #include <string.h>
 #include <sys/mman.h>
 #include <assert.h>
+#ifdef CONFIG_VALGRIND_DEV
+#include <valgrind/valgrind.h>
+#else
+#define RUNNING_ON_VALGRIND 0
+#endif
 
-#include "log.h"
 #include "fio_sem.h"
 #include "pshared.h"
 #include "os/os.h"
 void __fio_sem_remove(struct fio_sem *sem)
 {
        assert(sem->magic == FIO_SEM_MAGIC);
+       pthread_mutex_destroy(&sem->lock);
        pthread_cond_destroy(&sem->cond);
 
        /*
-        * Ensure any subsequent attempt to grab this semaphore will fail
-        * with an assert, instead of just silently hanging.
-        */
-       memset(sem, 0, sizeof(*sem));
+        * When not running on Valgrind, ensure any subsequent attempt to grab
+        * this semaphore will fail with an assert, instead of just silently
+        * hanging. When running on Valgrind, let Valgrind detect
+        * use-after-free.
+         */
+       if (!RUNNING_ON_VALGRIND)
+               memset(sem, 0, sizeof(*sem));
 }
 
 void fio_sem_remove(struct fio_sem *sem)
@@ -32,6 +41,8 @@ int __fio_sem_init(struct fio_sem *sem, int value)
        int ret;
 
        sem->value = value;
+       /* Initialize .waiters explicitly for Valgrind. */
+       sem->waiters = 0;
        sem->magic = FIO_SEM_MAGIC;
 
        ret = mutex_cond_init_pshared(&sem->lock, &sem->cond);