1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2024-07-22 10:47:43 +02:00

Merge pull request #20 from TREX-CoE/file-locking

Locking asked from the front to the back
This commit is contained in:
Evgeny Posenitskiy 2021-03-04 09:27:15 +01:00 committed by GitHub
commit 0328564967
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 192 additions and 32 deletions

View File

@ -31,6 +31,7 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b
trexio_t* result = NULL;
/* Allocate data structures */
switch (back_end) {
case TREXIO_TEXT:
@ -47,8 +48,10 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b
*/
}
/* TODO: Error handling */
assert (result != NULL);
assert (result != NULL); /* TODO: Error handling */
/* Data for the parent type */
result->file_name = (char*) calloc(strlen(file_name)+1,sizeof(char));
strcpy(result->file_name, file_name);
@ -57,7 +60,11 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b
int irc = pthread_mutex_init ( &(result->thread_lock), NULL);
assert (irc == 0);
trexio_exit_code rc = TREXIO_FAILURE;
trexio_exit_code rc;
/* Back end initialization */
rc = TREXIO_FAILURE;
switch (back_end) {
@ -76,6 +83,31 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b
}
if (rc != TREXIO_SUCCESS) return NULL;
/* File locking */
rc = TREXIO_FAILURE;
switch (back_end) {
case TREXIO_TEXT:
rc = trexio_text_lock(result);
break;
case TREXIO_HDF5:
rc = TREXIO_SUCCESS;
/*
rc = trexio_hdf5_lock(result);
break;
case TREXIO_JSON:
rc = trexio_json_lock(result);
break;
*/
}
if (rc != TREXIO_SUCCESS) return NULL;
return result;
}
@ -85,6 +117,7 @@ trexio_exit_code trexio_close(trexio_t* file) {
trexio_exit_code rc;
/* Terminate the back end */
switch (file->back_end) {
case TREXIO_TEXT:
@ -106,14 +139,42 @@ trexio_exit_code trexio_close(trexio_t* file) {
if (rc != TREXIO_SUCCESS) {
return TREXIO_FAILURE;
}
/* File unlocking */
rc = TREXIO_FAILURE;
switch (file->back_end) {
case TREXIO_TEXT:
rc = trexio_text_unlock(file);
break;
case TREXIO_HDF5:
rc = TREXIO_SUCCESS;
/*
rc = trexio_hdf5_unlock(file);
break;
case TREXIO_JSON:
rc = trexio_json_unlock(file);
break;
*/
}
if (rc != TREXIO_SUCCESS) return TREXIO_FAILURE;
/* Terminate front end */
free(file->file_name);
file->file_name = NULL;
int irc = pthread_mutex_destroy( &(file->thread_lock) );
assert (irc == 0);
free(file);
if (irc != 0) return TREXIO_ERRNO;
return TREXIO_SUCCESS;
}

View File

@ -21,6 +21,7 @@ typedef int32_t trexio_exit_code;
#define TREXIO_INVALID_ARG_5 ( (trexio_exit_code) 5 )
#define TREXIO_END ( (trexio_exit_code) 10 )
#define TREXIO_READONLY ( (trexio_exit_code) 11 )
#define TREXIO_ERRNO ( (trexio_exit_code) 12 )
typedef int32_t back_end_t;

View File

@ -78,6 +78,7 @@ typedef int32_t trexio_exit_code;
#define TREXIO_INVALID_ARG_5 ( (trexio_exit_code) 5 )
#define TREXIO_END ( (trexio_exit_code) 10 )
#define TREXIO_READONLY ( (trexio_exit_code) 11 )
#define TREXIO_ERRNO ( (trexio_exit_code) 12 )
#+end_src
** Back ends
@ -168,6 +169,7 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b
trexio_t* result = NULL;
/* Allocate data structures */
switch (back_end) {
case TREXIO_TEXT:
@ -184,8 +186,10 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b
,*/
}
/* TODO: Error handling */
assert (result != NULL);
assert (result != NULL); /* TODO: Error handling */
/* Data for the parent type */
result->file_name = (char*) calloc(strlen(file_name)+1,sizeof(char));
strcpy(result->file_name, file_name);
@ -194,7 +198,11 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b
int irc = pthread_mutex_init ( &(result->thread_lock), NULL);
assert (irc == 0);
trexio_exit_code rc = TREXIO_FAILURE;
trexio_exit_code rc;
/* Back end initialization */
rc = TREXIO_FAILURE;
switch (back_end) {
@ -213,6 +221,31 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b
}
if (rc != TREXIO_SUCCESS) return NULL;
/* File locking */
rc = TREXIO_FAILURE;
switch (back_end) {
case TREXIO_TEXT:
rc = trexio_text_lock(result);
break;
case TREXIO_HDF5:
rc = TREXIO_SUCCESS;
/*
rc = trexio_hdf5_lock(result);
break;
case TREXIO_JSON:
rc = trexio_json_lock(result);
break;
,*/
}
if (rc != TREXIO_SUCCESS) return NULL;
return result;
}
#+end_src
@ -230,6 +263,7 @@ trexio_exit_code trexio_close(trexio_t* file) {
trexio_exit_code rc;
/* Terminate the back end */
switch (file->back_end) {
case TREXIO_TEXT:
@ -251,14 +285,42 @@ trexio_exit_code trexio_close(trexio_t* file) {
if (rc != TREXIO_SUCCESS) {
return TREXIO_FAILURE;
}
/* File unlocking */
rc = TREXIO_FAILURE;
switch (file->back_end) {
case TREXIO_TEXT:
rc = trexio_text_unlock(file);
break;
case TREXIO_HDF5:
rc = TREXIO_SUCCESS;
/*
rc = trexio_hdf5_unlock(file);
break;
case TREXIO_JSON:
rc = trexio_json_unlock(file);
break;
,*/
}
if (rc != TREXIO_SUCCESS) return TREXIO_FAILURE;
/* Terminate front end */
free(file->file_name);
file->file_name = NULL;
int irc = pthread_mutex_destroy( &(file->thread_lock) );
assert (irc == 0);
free(file);
if (irc != 0) return TREXIO_ERRNO;
return TREXIO_SUCCESS;
}
@ -618,5 +680,4 @@ trexio_exit_code trexio_buffered_write_rdm_two_e(trexio_t* file, const int64_t o
- [ ] Error handling with errno
- [ ] HDF5 back-end
- [ ] JSON back-end
- [ ] File locking with flock
- [ ] Caching of the struct saving last modification date in structs

View File

@ -526,7 +526,7 @@ trexio_exit_code trexio_hdf5_write_nucleus_num(const trexio_t* file, const uint6
if (nucleus->num != 0) {
printf("%ld -> %ld %s \n", num, nucleus->num,
"This variable alreasy exists. Overwriting it is not supported");
"This variable already exists. Overwriting it is not supported");
trexio_hdf5_free_nucleus(nucleus);
return TREXIO_FAILURE;
}

View File

@ -38,7 +38,18 @@ trexio_exit_code trexio_text_init(trexio_t* file) {
assert (f->lock_file > 0);
free(file_name);
/* Lock the directory for the current process */
f->nucleus = NULL;
f->electron= NULL;
f->rdm = NULL;
return TREXIO_SUCCESS;
}
trexio_exit_code trexio_text_lock(trexio_t* file) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
trexio_text_t* f = (trexio_text_t*) file;
struct flock fl;
fl.l_type = F_WRLCK;
@ -50,20 +61,12 @@ trexio_exit_code trexio_text_init(trexio_t* file) {
int rc = fcntl(f->lock_file, F_SETLKW, &fl);
if (rc == -1) return TREXIO_FAILURE;
trexio_text_t* f_child = (trexio_text_t*) file;
f_child->nucleus = NULL;
f_child->electron= NULL;
f_child->rdm = NULL;
return TREXIO_SUCCESS;
}
trexio_exit_code trexio_text_finalize(trexio_t* file) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
trexio_text_t* f = (trexio_text_t*) file;
trexio_exit_code rc;
rc = trexio_text_free_nucleus( (trexio_text_t*) file);
assert (rc == TREXIO_SUCCESS);
@ -71,7 +74,14 @@ trexio_exit_code trexio_text_finalize(trexio_t* file) {
rc = trexio_text_free_rdm( (trexio_text_t*) file);
assert (rc == TREXIO_SUCCESS);
/* Release lock */
return TREXIO_SUCCESS;
}
trexio_exit_code trexio_text_unlock(trexio_t* file) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
trexio_text_t* f = (trexio_text_t*) file;
struct flock fl;
fl.l_type = F_WRLCK;

View File

@ -56,8 +56,12 @@ typedef struct trexio_text_s {
trexio_exit_code trexio_text_init(trexio_t* file);
trexio_exit_code trexio_text_lock(trexio_t* file);
trexio_exit_code trexio_text_finalize(trexio_t* file);
trexio_exit_code trexio_text_unlock(trexio_t* file);
nucleus_t* trexio_text_read_nucleus(trexio_text_t* file);
trexio_exit_code trexio_text_flush_nucleus(const trexio_text_t* file);

View File

@ -136,7 +136,25 @@ trexio_exit_code trexio_text_init(trexio_t* file) {
assert (f->lock_file > 0);
free(file_name);
/* Lock the directory for the current process */
f->nucleus = NULL;
f->electron= NULL;
f->rdm = NULL;
return TREXIO_SUCCESS;
}
#+end_src
#+begin_src c :tangle trexio_text.h
trexio_exit_code trexio_text_lock(trexio_t* file);
#+end_src
#+begin_src c :tangle trexio_text.c
trexio_exit_code trexio_text_lock(trexio_t* file) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
trexio_text_t* f = (trexio_text_t*) file;
struct flock fl;
fl.l_type = F_WRLCK;
@ -148,12 +166,6 @@ trexio_exit_code trexio_text_init(trexio_t* file) {
int rc = fcntl(f->lock_file, F_SETLKW, &fl);
if (rc == -1) return TREXIO_FAILURE;
trexio_text_t* f_child = (trexio_text_t*) file;
f_child->nucleus = NULL;
f_child->electron= NULL;
f_child->rdm = NULL;
return TREXIO_SUCCESS;
}
@ -168,8 +180,6 @@ trexio_exit_code trexio_text_finalize(trexio_t* file);
trexio_exit_code trexio_text_finalize(trexio_t* file) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
trexio_text_t* f = (trexio_text_t*) file;
trexio_exit_code rc;
rc = trexio_text_free_nucleus( (trexio_text_t*) file);
assert (rc == TREXIO_SUCCESS);
@ -177,7 +187,20 @@ trexio_exit_code trexio_text_finalize(trexio_t* file) {
rc = trexio_text_free_rdm( (trexio_text_t*) file);
assert (rc == TREXIO_SUCCESS);
/* Release lock */
return TREXIO_SUCCESS;
}
#+end_src
#+begin_src c :tangle trexio_text.h
trexio_exit_code trexio_text_unlock(trexio_t* file);
#+end_src
#+begin_src c :tangle trexio_text.c
trexio_exit_code trexio_text_unlock(trexio_t* file) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
trexio_text_t* f = (trexio_text_t*) file;
struct flock fl;
fl.l_type = F_WRLCK;