Don't crash when using filehash lock before init
[fio.git] / filehash.c
index 1df7db0c2f2d754fd4ae7bbc674ac408d7ea7848..0d61f54cca0b2f8c526cd639f9749716069aa8ff 100644 (file)
@@ -3,7 +3,8 @@
 
 #include "fio.h"
 #include "flist.h"
-#include "crc/crc16.h"
+#include "hash.h"
+#include "filehash.h"
 
 #define HASH_BUCKETS   512
 #define HASH_MASK      (HASH_BUCKETS - 1)
@@ -15,7 +16,19 @@ static struct fio_mutex *hash_lock;
 
 static unsigned short hash(const char *name)
 {
-       return crc16((const unsigned char *) name, strlen(name)) & HASH_MASK;
+       return jhash(name, strlen(name), 0) & HASH_MASK;
+}
+
+void fio_file_hash_lock(void)
+{
+       if (hash_lock)
+               fio_mutex_down(hash_lock);
+}
+
+void fio_file_hash_unlock(void)
+{
+       if (hash_lock)
+               fio_mutex_up(hash_lock);
 }
 
 void remove_file_hash(struct fio_file *f)
@@ -107,5 +120,5 @@ void file_hash_init(void *ptr)
        for (i = 0; i < HASH_BUCKETS; i++)
                INIT_FLIST_HEAD(&file_hash[i]);
 
-       hash_lock = fio_mutex_init(1);
+       hash_lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
 }