fh lock
authorJeroen van der Heijden <jeroen@cesbit.com>
Mon, 20 Mar 2023 19:06:13 +0000 (20:06 +0100)
committerJeroen van der Heijden <jeroen@cesbit.com>
Mon, 20 Mar 2023 19:06:13 +0000 (20:06 +0100)
include/siri/file/handler.h
include/siri/version.h
src/siri/db/shard.c
src/siri/file/handler.c
src/siri/siri.c

index 19d813dd30591acc80f2846be15a4c358f27cf85..08943756f0042aa55c7e795220e6cf6e1aa9f33a 100644 (file)
@@ -8,8 +8,10 @@ typedef struct siri_fh_s siri_fh_t;
 
 #include <inttypes.h>
 #include <siri/file/pointer.h>
+#include <uv.h>
 
 siri_fh_t * siri_fh_new(uint16_t size);
+void siri_fh_close(siri_fh_t * fh);
 void siri_fh_free(siri_fh_t * fh);
 int siri_fopen(
         siri_fh_t * fh,
@@ -22,6 +24,7 @@ struct siri_fh_s
     uint16_t size;
     uint16_t idx;
     siri_fp_t ** fpointers;
+    uv_mutex_t lock_;
 };
 
 #endif  /* SIRI_FH_H_ */
index 29dbc361c3cb19d9a1adee026f4e7b9d06bb231f..90f8549c91aa4ed98fd9e869c501189bbd59e977 100644 (file)
@@ -6,7 +6,7 @@
 
 #define SIRIDB_VERSION_MAJOR 2
 #define SIRIDB_VERSION_MINOR 0
-#define SIRIDB_VERSION_PATCH 49
+#define SIRIDB_VERSION_PATCH 50
 
 /*
  * Use SIRIDB_VERSION_PRE_RELEASE for alpha release versions.
@@ -15,7 +15,7 @@
  * Note that debian alpha packages should use versions like this:
  *   2.0.34-0alpha0
  */
-#define SIRIDB_VERSION_PRE_RELEASE ""
+#define SIRIDB_VERSION_PRE_RELEASE "-alpha-1"
 
 #ifndef NDEBUG
 #define SIRIDB_VERSION_BUILD_RELEASE "+debug"
index c1552d76b79af5ab0c1c68a31ebd407ca5cbb811..01c24ec9c2227409826138122d8df0b49666075b 100644 (file)
@@ -1717,8 +1717,12 @@ void siridb__shard_free(siridb_shard_t * shard)
     }
 
     /* this will close the file, even when other references exist */
+    uv_mutex_lock(&siri.fh->lock_);
+
     siri_fp_decref(shard->fp);
 
+    uv_mutex_unlock(&siri.fh->lock_);
+
     free(shard->fn);
     free(shard);
 }
index 9afaaeba1bb8f089a6d4c161208f5e731de5641f..ff52814560762286f238780c9ffdbd88a4146ab2 100644 (file)
@@ -24,15 +24,16 @@ siri_fh_t * siri_fh_new(uint16_t size)
             free(fh);
             fh = NULL;
         }
+        uv_mutex_init(&fh->lock_);
     }
     return fh;
 }
 
 /*
- * Destroy file handler. (closes all open files in the file handler)
+ * Close file handler. (closes all open files in the file handler)
  * In case closing an open file fails, a SIGNAL will be raised.
  */
-void siri_fh_free(siri_fh_t * fh)
+void siri_fh_close(siri_fh_t * fh)
 {
     siri_fp_t ** fp;
     uint16_t i;
@@ -52,10 +53,20 @@ void siri_fh_free(siri_fh_t * fh)
         }
         siri_fp_decref(*fp);
     }
+}
+
+/*
+ * Destroy file handler. (closes all open files in the file handler)
+ * In case closing an open file fails, a SIGNAL will be raised.
+ */
+void siri_fh_free(siri_fh_t * fh)
+{
     free(fh->fpointers);
+    uv_mutex_destroy(&fh->lock_);
     free(fh);
 }
 
+
 /*
  * Returns 0 if successful or -1 in case of an error.
  */
@@ -65,7 +76,9 @@ int siri_fopen(
         const char * fn,
         const char * modes)
 {
-    siri_fp_t ** dest = fh->fpointers + fh->idx;
+    siri_fp_t ** dest;
+    uv_mutex_lock(&fh->lock_);
+    dest = fh->fpointers + fh->idx;
 
     /* close and possible free file pointer at next position */
     if (*dest != NULL)
@@ -82,12 +95,13 @@ int siri_fopen(
     if ((fp->fp = fopen(fn, modes)) == NULL)
     {
         log_critical("Cannot open file: '%s' using mode '%s'", fn, modes);
+        uv_mutex_unlock(&fh->lock_);
         return -1;
     }
 
     /* set file handler pointer to next position */
     fh->idx = (fh->idx + 1) % fh->size;
-
+    uv_mutex_unlock(&fh->lock_);
     return 0;
 }
 
index 83b4fc0d9f096e46782d369f2f73ff28a3fedf0b..bebc47ffcbcb058fe9fd381a1f3dc1768e315a58 100644 (file)
@@ -358,8 +358,8 @@ void siri_free(void)
         }
     }
 
-    /* first free the File Handler. (this will close all open shard files) */
-    siri_fh_free(siri.fh);
+    /* first close the File Handler. (this will close all open shard files) */
+    siri_fh_close(siri.fh);
 
     /* this will free each SiriDB database and the list */
     llist_free_cb(siri.siridb_list, (llist_cb) siridb_decref_cb, NULL);
@@ -376,6 +376,9 @@ void siri_free(void)
     /* free config */
     siri_cfg_destroy(&siri);
 
+    /* free the file handler */
+    siri_fh_free(siri.fh);
+
     /* free event loop */
     free(siri.loop);
 }