Convert file hash lock to spinlocks
[fio.git] / filehash.c
index 1bcfc6de072a34d295e34fae04d6a2fd3eb3d6e8..97c0b3c7633d0e28ab4f44231679301a8d344e6c 100644 (file)
@@ -2,31 +2,18 @@
 #include <assert.h>
 
 #include "fio.h"
-#include "list.h"
+#include "flist.h"
 #include "crc/crc16.h"
 
+#include "spinlock.h"
+
 #define HASH_BUCKETS   512
 #define HASH_MASK      (HASH_BUCKETS - 1)
 
-unsigned int file_hash_size = HASH_BUCKETS * sizeof(struct list_head);
-
-static struct list_head *file_hash;
-static struct fio_mutex *hash_lock;
-
-static void dump_hash(void)
-{
-       struct list_head *n;
-       unsigned int i;
+unsigned int file_hash_size = HASH_BUCKETS * sizeof(struct flist_head);
 
-       for (i = 0; i < HASH_BUCKETS; i++) {
-               list_for_each(n, &file_hash[i]) {
-                       struct fio_file *f;
-
-                       f = list_entry(n, struct fio_file, hash_list);
-                       printf("%d: %s\n", i, f->file_name);
-               }
-       }
-}
+static struct flist_head *file_hash;
+static struct fio_spinlock *hash_lock;
 
 static unsigned short hash(const char *name)
 {
@@ -35,24 +22,24 @@ static unsigned short hash(const char *name)
 
 void remove_file_hash(struct fio_file *f)
 {
-       fio_mutex_down(hash_lock);
+       fio_spin_lock(hash_lock);
 
        if (f->flags & FIO_FILE_HASHED) {
-               assert(!list_empty(&f->hash_list));
-               list_del_init(&f->hash_list);
+               assert(!flist_empty(&f->hash_list));
+               flist_del_init(&f->hash_list);
                f->flags &= ~FIO_FILE_HASHED;
        }
 
-       fio_mutex_up(hash_lock);
+       fio_spin_unlock(hash_lock);
 }
 
 static struct fio_file *__lookup_file_hash(const char *name)
 {
-       struct list_head *bucket = &file_hash[hash(name)];
-       struct list_head *n;
+       struct flist_head *bucket = &file_hash[hash(name)];
+       struct flist_head *n;
 
-       list_for_each(n, bucket) {
-               struct fio_file *f = list_entry(n, struct fio_file, hash_list);
+       flist_for_each(n, bucket) {
+               struct fio_file *f = flist_entry(n, struct fio_file, hash_list);
 
                if (!strcmp(f->file_name, name)) {
                        assert(f->fd != -1);
@@ -60,7 +47,6 @@ static struct fio_file *__lookup_file_hash(const char *name)
                }
        }
 
-       dump_hash();
        return NULL;
 }
 
@@ -68,9 +54,9 @@ struct fio_file *lookup_file_hash(const char *name)
 {
        struct fio_file *f;
 
-       fio_mutex_down(hash_lock);
+       fio_spin_lock(hash_lock);
        f = __lookup_file_hash(name);
-       fio_mutex_up(hash_lock);
+       fio_spin_unlock(hash_lock);
        return f;
 }
 
@@ -81,27 +67,45 @@ struct fio_file *add_file_hash(struct fio_file *f)
        if (f->flags & FIO_FILE_HASHED)
                return NULL;
 
-       INIT_LIST_HEAD(&f->hash_list);
+       INIT_FLIST_HEAD(&f->hash_list);
 
-       fio_mutex_down(hash_lock);
+       fio_spin_lock(hash_lock);
 
        alias = __lookup_file_hash(f->file_name);
        if (!alias) {
                f->flags |= FIO_FILE_HASHED;
-               list_add_tail(&f->hash_list, &file_hash[hash(f->file_name)]);
+               flist_add_tail(&f->hash_list, &file_hash[hash(f->file_name)]);
        }
 
-       fio_mutex_up(hash_lock);
+       fio_spin_unlock(hash_lock);
        return alias;
 }
 
+void file_hash_exit(void)
+{
+       unsigned int i, has_entries = 0;
+
+       fio_spin_lock(hash_lock);
+       for (i = 0; i < HASH_BUCKETS; i++)
+               has_entries += !flist_empty(&file_hash[i]);
+
+       file_hash = NULL;
+       fio_spin_unlock(hash_lock);
+
+       if (has_entries)
+               log_err("fio: file hash not empty on exit\n");
+
+       fio_spinlock_remove(hash_lock);
+       hash_lock = NULL;
+}
+
 void file_hash_init(void *ptr)
 {
        unsigned int i;
 
        file_hash = ptr;
        for (i = 0; i < HASH_BUCKETS; i++)
-               INIT_LIST_HEAD(&file_hash[i]);
+               INIT_FLIST_HEAD(&file_hash[i]);
 
-       hash_lock = fio_mutex_init(1);
+       hash_lock = fio_spinlock_init();
 }