From 4ca84bbbf79aff6c9ce33270b77fc3f263c3638e Mon Sep 17 00:00:00 2001 From: Jeroen van der Heijden Date: Mon, 20 Mar 2023 20:06:13 +0100 Subject: [PATCH] fh lock --- include/siri/file/handler.h | 3 +++ include/siri/version.h | 4 ++-- src/siri/db/shard.c | 4 ++++ src/siri/file/handler.c | 22 ++++++++++++++++++---- src/siri/siri.c | 7 +++++-- 5 files changed, 32 insertions(+), 8 deletions(-) diff --git a/include/siri/file/handler.h b/include/siri/file/handler.h index 19d813dd..08943756 100644 --- a/include/siri/file/handler.h +++ b/include/siri/file/handler.h @@ -8,8 +8,10 @@ typedef struct siri_fh_s siri_fh_t; #include #include +#include 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_ */ diff --git a/include/siri/version.h b/include/siri/version.h index 29dbc361..90f8549c 100644 --- a/include/siri/version.h +++ b/include/siri/version.h @@ -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" diff --git a/src/siri/db/shard.c b/src/siri/db/shard.c index c1552d76..01c24ec9 100644 --- a/src/siri/db/shard.c +++ b/src/siri/db/shard.c @@ -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); } diff --git a/src/siri/file/handler.c b/src/siri/file/handler.c index 9afaaeba..ff528145 100644 --- a/src/siri/file/handler.c +++ b/src/siri/file/handler.c @@ -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; } diff --git a/src/siri/siri.c b/src/siri/siri.c index 83b4fc0d..bebc47ff 100644 --- a/src/siri/siri.c +++ b/src/siri/siri.c @@ -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); } -- 2.30.2