From b6791fbe47a18dda50639da48d835ba919a0376f Mon Sep 17 00:00:00 2001 From: ax-6 Date: Sat, 10 Aug 2024 01:29:56 +0800 Subject: [PATCH] Temp save. Not complete. --- aqvm/base/file/file.c | 85 +++++++++++++------ aqvm/base/file/file.h | 6 +- .../file/read_write_lock/read_write_lock.c | 3 +- .../file/read_write_lock/read_write_lock.h | 1 + aqvm/base/hash/table/table.c | 2 +- aqvm/base/io/io.c | 8 +- aqvm/base/linked_list/linked_list.c | 2 +- aqvm/base/pair.h | 2 +- aqvm/base/process/file_lock/file_lock.c | 4 +- aqvm/base/threading/file_lock/file_lock.c | 61 ++++++++++++- aqvm/base/threading/file_lock/file_lock.h | 7 +- 11 files changed, 138 insertions(+), 43 deletions(-) diff --git a/aqvm/base/file/file.c b/aqvm/base/file/file.c index 84c1ec0..b26de27 100644 --- a/aqvm/base/file/file.c +++ b/aqvm/base/file/file.c @@ -10,10 +10,11 @@ #include "aqvm/base/io/io.h" #include "aqvm/base/process/file_lock/file_lock.h" +#include "aqvm/base/threading/file_lock/file_lock.h" #include "aqvm/base/threading/mutex/mutex.h" int AqvmBaseFile_LockFile(struct AqvmBaseFile_File* stream) { - if (AqvmBaseFile_CheckStream(stream)) { + if (AqvmBaseFile_CheckStream(stream) != 0) { // TODO return -1; } @@ -27,7 +28,7 @@ int AqvmBaseFile_LockFile(struct AqvmBaseFile_File* stream) { } int AqvmBaseFile_UnlockFile(struct AqvmBaseFile_File* stream) { - if (AqvmBaseFile_CheckStream(stream)) { + if (AqvmBaseFile_CheckStream(stream) != 0) { // TODO return -1; } @@ -108,7 +109,7 @@ int AqvmBaseFile_CheckStream(struct AqvmBaseFile_File* stream) { } void AqvmBaseFile_clearerr(struct AqvmBaseFile_File* stream) { - if (AqvmBaseFile_CheckStream(stream)) { + if (AqvmBaseFile_CheckStream(stream) != 0) { // TODO return; } @@ -117,29 +118,30 @@ void AqvmBaseFile_clearerr(struct AqvmBaseFile_File* stream) { } int AqvmBaseFile_fclose(struct AqvmBaseFile_File* stream) { - if (AqvmBaseFile_CheckStream(stream)) { + if (AqvmBaseFile_CheckStream(stream) != 0) { // TODO return -1; } int result = fclose(stream->file); - /*if (AqvmBaseThreadingMutex_CloseMutex(&stream->mutex)) { + free(stream->identifier); + if (AqvmBaseThreadingFileLock_RemoveFileLock(stream) != 0) { // TODO + free(stream); return -2; - }*/ + } + free(stream); if (result != 0) { // TODO return -3; } - free(stream); - return 0; } int AqvmBaseFile_feof(struct AqvmBaseFile_File* stream) { - if (AqvmBaseFile_CheckStream(stream)) { + if (AqvmBaseFile_CheckStream(stream) != 0) { // TODO return -1; } @@ -159,7 +161,7 @@ int AqvmBaseFile_ferror(struct AqvmBaseFile_File* stream) { } int AqvmBaseFile_fflush(struct AqvmBaseFile_File* stream) { - if (AqvmBaseFile_CheckStream(stream)) { + if (AqvmBaseFile_CheckStream(stream) != 0) { // TODO return -1; } @@ -185,7 +187,7 @@ int AqvmBaseFile_fflush(struct AqvmBaseFile_File* stream) { } int AqvmBaseFile_fgetpos(struct AqvmBaseFile_File* stream, fpos_t* pos) { - if (AqvmBaseFile_CheckStream(stream) || pos == NULL) { + if (AqvmBaseFile_CheckStream(stream) != 0 || pos == NULL) { // TODO return -1; } @@ -222,26 +224,44 @@ struct AqvmBaseFile_File* AqvmBaseFile_fopen(const char* filename, // TODO return NULL; } + stream->identifier = (AqvmBaseFileIdentifier_Identifier*)malloc( + sizeof(AqvmBaseFileIdentifier_Identifier)); + if (stream->identifier == NULL) { + // TODO + free(stream); + return NULL; + } stream->file = fopen(filename, mode); if (stream->file == NULL || AqvmBaseFile_ferror(stream) != 0) { + free(stream->identifier); free(stream); // TODO return NULL; } - /*if (AqvmBaseThreadingMutex_InitializeMutex(&stream->mutex) != 0) { + if (AqvmBaseFileIdentifier_GetIdentifier(filename, &stream->identifier) != + 0) { + // TODO fclose(stream->file); + free(stream->identifier); free(stream); return NULL; - }*/ + } + + if (AqvmBaseFileThreadingFileLock_AddFileLock(stream) != 0) { + fclose(stream->file); + free(stream->identifier); + free(stream); + return NULL; + } return stream; } size_t AqvmBaseFile_fread(void* ptr, size_t size, size_t nmemb, struct AqvmBaseFile_File* stream) { - if (ptr == NULL || AqvmBaseFile_CheckStream(stream)) { + if (ptr == NULL || AqvmBaseFile_CheckStream(stream) != 0) { // TODO return 0; } @@ -263,7 +283,8 @@ size_t AqvmBaseFile_fread(void* ptr, size_t size, size_t nmemb, struct AqvmBaseFile_File* AqvmBaseFile_freopen( const char* filename, const char* mode, struct AqvmBaseFile_File* stream) { - if (filename == NULL || mode == NULL || AqvmBaseFile_CheckStream(stream)) { + if (filename == NULL || mode == NULL || + AqvmBaseFile_CheckStream(stream) != 0) { // TODO return NULL; } @@ -294,7 +315,7 @@ struct AqvmBaseFile_File* AqvmBaseFile_freopen( int AqvmBaseFile_fseek(struct AqvmBaseFile_File* stream, long int offset, int whence) { - if (AqvmBaseFile_CheckStream(stream)) { + if (AqvmBaseFile_CheckStream(stream) != 0) { // TODO return -1; } @@ -320,7 +341,7 @@ int AqvmBaseFile_fseek(struct AqvmBaseFile_File* stream, long int offset, } int AqvmBaseFile_fsetpos(struct AqvmBaseFile_File* stream, const fpos_t* pos) { - if (AqvmBaseFile_CheckStream(stream) || pos == NULL) { + if (AqvmBaseFile_CheckStream(stream) != 0 || pos == NULL) { // TODO return -1; } @@ -346,7 +367,7 @@ int AqvmBaseFile_fsetpos(struct AqvmBaseFile_File* stream, const fpos_t* pos) { } long int AqvmBaseFile_ftell(struct AqvmBaseFile_File* stream) { - if (AqvmBaseFile_CheckStream(stream)) { + if (AqvmBaseFile_CheckStream(stream) != 0) { // TODO return -1L; } @@ -373,7 +394,7 @@ long int AqvmBaseFile_ftell(struct AqvmBaseFile_File* stream) { size_t AqvmBaseFile_fwrite(const void* ptr, size_t size, size_t nmemb, struct AqvmBaseFile_File* stream) { - if (ptr == NULL || AqvmBaseFile_CheckStream(stream)) { + if (ptr == NULL || AqvmBaseFile_CheckStream(stream) != 0) { // TODO return 0; } @@ -422,7 +443,7 @@ int AqvmBaseFile_rename(const char* old_filename, const char* new_filename) { } void AqvmBaseFile_rewind(struct AqvmBaseFile_File* stream) { - if (AqvmBaseFile_CheckStream(stream)) { + if (AqvmBaseFile_CheckStream(stream) != 0) { // TODO return; } @@ -440,7 +461,7 @@ void AqvmBaseFile_rewind(struct AqvmBaseFile_File* stream) { } void AqvmBaseFile_setbuf(struct AqvmBaseFile_File* stream, char* buffer) { - if (AqvmBaseFile_CheckStream(stream)) { + if (AqvmBaseFile_CheckStream(stream) != 0) { // TODO return; } @@ -459,7 +480,7 @@ void AqvmBaseFile_setbuf(struct AqvmBaseFile_File* stream, char* buffer) { int AqvmBaseFile_setvbuf(struct AqvmBaseFile_File* stream, char* buffer, int mode, size_t size) { - if (AqvmBaseFile_CheckStream(stream)) { + if (AqvmBaseFile_CheckStream(stream) != 0) { // TODO return -1; } @@ -484,7 +505,7 @@ int AqvmBaseFile_setvbuf(struct AqvmBaseFile_File* stream, char* buffer, return result; } -struct AqvmBaseFile_File* AqvmBaseFile_tmpfile(void) { +struct AqvmBaseFile_File* AqvmBaseFile_tmpfile() { struct AqvmBaseFile_File* stream = (struct AqvmBaseFile_File*)malloc(sizeof(struct AqvmBaseFile_File)); if (stream == NULL) { @@ -498,9 +519,21 @@ struct AqvmBaseFile_File* AqvmBaseFile_tmpfile(void) { // TODO return NULL; } - - /*AqvmBaseThreadingMutex_InitializeMutex(&stream->mutex);*/ - + stream->identifier = (AqvmBaseFileIdentifier_Identifier*)malloc( + sizeof(AqvmBaseFileIdentifier_Identifier)); + if (stream->identifier == NULL) { + // TODO + fclose(stream->file); + free(stream); + return NULL; + } + if (AqvmBaseFileThreadingFileLock_AddFileLock(stream) != 0) { + // TODO + fclose(stream->file); + free(stream->identifier); + free(stream); + return NULL; + } return stream; } diff --git a/aqvm/base/file/file.h b/aqvm/base/file/file.h index 0adc73f..2543a95 100644 --- a/aqvm/base/file/file.h +++ b/aqvm/base/file/file.h @@ -13,8 +13,8 @@ struct AqvmBaseFile_File { FILE* file; - AqvmBaseFileIdentifier_Identifier identifier; - struct AqvmBaseFileReadWriteLock_ReadWriteLock lock; + AqvmBaseFileIdentifier_Identifier* identifier; + struct AqvmBaseFileReadWriteLock_ReadWriteLock* lock; }; int AqvmBaseFile_LockFile(struct AqvmBaseFile_File* stream); @@ -69,7 +69,7 @@ void AqvmBaseFile_setbuf(struct AqvmBaseFile_File* stream, char* buffer); int AqvmBaseFile_setvbuf(struct AqvmBaseFile_File* stream, char* buffer, int mode, size_t size); -struct AqvmBaseFile_File* AqvmBaseFile_tmpfile(void); +struct AqvmBaseFile_File* AqvmBaseFile_tmpfile(); char* AqvmBaseFile_tmpnam(char* str); diff --git a/aqvm/base/file/read_write_lock/read_write_lock.c b/aqvm/base/file/read_write_lock/read_write_lock.c index b08032a..7c3e8e2 100644 --- a/aqvm/base/file/read_write_lock/read_write_lock.c +++ b/aqvm/base/file/read_write_lock/read_write_lock.c @@ -40,7 +40,8 @@ int AqvmBaseFileReadWriteLock_LockReadLock( return -1; } - if (AqvmBaseThreadingMutex_LockMutex(&read_write_lock->mutex) != 0) { + if (read_write_lock->read_count == 0 && + AqvmBaseThreadingMutex_LockMutex(&read_write_lock->mutex) != 0) { // TODO return -2; } diff --git a/aqvm/base/file/read_write_lock/read_write_lock.h b/aqvm/base/file/read_write_lock/read_write_lock.h index a00aed6..d13aa30 100644 --- a/aqvm/base/file/read_write_lock/read_write_lock.h +++ b/aqvm/base/file/read_write_lock/read_write_lock.h @@ -9,6 +9,7 @@ struct AqvmBaseFileReadWriteLock_ReadWriteLock { AqvmBaseThreadingMutex_Mutex mutex; + int lock_count; int read_count; }; diff --git a/aqvm/base/hash/table/table.c b/aqvm/base/hash/table/table.c index 78c73f1..19ac32f 100644 --- a/aqvm/base/hash/table/table.c +++ b/aqvm/base/hash/table/table.c @@ -42,7 +42,7 @@ int AqvmBaseHashTable_InitializeHashTable( return -2; } for (size_t i = 0; i < capacity; i++) { - if (AqvmBaseLinkedList_InitializeLinkedList(&hash_table->data[i])) { + if (AqvmBaseLinkedList_InitializeLinkedList(&hash_table->data[i])!=0) { // TODO return -3; } diff --git a/aqvm/base/io/io.c b/aqvm/base/io/io.c index 2441423..eadb933 100644 --- a/aqvm/base/io/io.c +++ b/aqvm/base/io/io.c @@ -133,7 +133,7 @@ int AqvmBaseIo_fgetc(struct AqvmBaseFile_File* stream) { } if (result == EOF) { - if (AqvmBaseFile_feof(stream)) { + if (AqvmBaseFile_feof(stream) != 0) { return EOF; } // TODO @@ -166,7 +166,7 @@ char* AqvmBaseIo_fgets(char* str, int n, struct AqvmBaseFile_File* stream) { } if (result == NULL) { - if (AqvmBaseFile_feof(stream)) { + if (AqvmBaseFile_feof(stream) != 0) { return NULL; } // TODO @@ -326,7 +326,7 @@ int AqvmBaseIo_getc(struct AqvmBaseFile_File* stream) { } if (result == EOF) { - if (AqvmBaseFile_feof(stream)) { + if (AqvmBaseFile_feof(stream) != 0) { return EOF; } else { // TODO @@ -356,7 +356,7 @@ int AqvmBaseIo_getchar(void) { } if (result == EOF) { - if (AqvmBaseFile_feof(AqvmBaseIo_stdin)) { + if (AqvmBaseFile_feof(AqvmBaseIo_stdin) != 0) { return EOF; } else { // TODO diff --git a/aqvm/base/linked_list/linked_list.c b/aqvm/base/linked_list/linked_list.c index 456827a..3f98966 100644 --- a/aqvm/base/linked_list/linked_list.c +++ b/aqvm/base/linked_list/linked_list.c @@ -38,7 +38,7 @@ int AqvmBaseLinkedList_CloseLinkedList( } while (list->capacity > 0) { - if (AqvmBaseLinkedList_DeleteNode(list, 0)) { + if (AqvmBaseLinkedList_DeleteNode(list, 0) != 0) { // TODO return -2; } diff --git a/aqvm/base/pair.h b/aqvm/base/pair.h index 303c850..d7a5b03 100644 --- a/aqvm/base/pair.h +++ b/aqvm/base/pair.h @@ -5,7 +5,7 @@ #ifndef AQ_AQVM_BASE_PAIR_PAIR_H_ #define AQ_AQVM_BASE_PAIR_PAIR_H_ -struct AqvmBaseHashTable_Pair { +struct AqvmBase_Pair { void* key; void* value; }; diff --git a/aqvm/base/process/file_lock/file_lock.c b/aqvm/base/process/file_lock/file_lock.c index f88a944..17f6c67 100644 --- a/aqvm/base/process/file_lock/file_lock.c +++ b/aqvm/base/process/file_lock/file_lock.c @@ -14,7 +14,7 @@ int AqvmBaseProcessFileLock_LockFile(struct AqvmBaseFile_File* file) { #ifdef __unix__ - if (AqvmBaseProcessFileLockUnix_LockFile(file)) { + if (AqvmBaseProcessFileLockUnix_LockFile(file) != 0) { // TODO return -1; } @@ -33,7 +33,7 @@ int AqvmBaseProcessFileLock_LockFile(struct AqvmBaseFile_File* file) { int AqvmBaseProcessFileLock_UnlockFile(struct AqvmBaseFile_File* file) { #ifdef __unix__ - if (AqvmBaseProcessFileLockUnix_UnlockFile(file)) { + if (AqvmBaseProcessFileLockUnix_UnlockFile(file) != 0) { // TODO return -1; } diff --git a/aqvm/base/threading/file_lock/file_lock.c b/aqvm/base/threading/file_lock/file_lock.c index c404649..cf2a91b 100644 --- a/aqvm/base/threading/file_lock/file_lock.c +++ b/aqvm/base/threading/file_lock/file_lock.c @@ -4,10 +4,14 @@ #include "aqvm/base/threading/file_lock/file_lock.h" +#include +#include + #include "aqvm/base/file/file.h" #include "aqvm/base/file/identifier/identifier.h" #include "aqvm/base/file/read_write_lock/read_write_lock.h" #include "aqvm/base/hash/table/table.h" +#include "aqvm/base/linked_list/linked_list.h" #include "aqvm/base/pair.h" struct AqvmBaseHashTable_HashTable AqvmBaseThreadingFileLock_fileLockTable; @@ -31,12 +35,63 @@ int AqvmBaseThreadingFileLock_InitializeFileLockTable() { return 0; } -int AqvmBaseThreadingFileLock_InsertFileLock(struct AqvmBaseFile_File* file) { +int AqvmBaseThreadingFileLock_AddFileLock(struct AqvmBaseFile_File* file) { + if (file == NULL || file->identifier == NULL) { + // TODO + return -1; + } + + struct AqvmBase_Pair* file_lock_pair = + (struct AqvmBase_Pair*)malloc(sizeof(struct AqvmBase_Pair)); + if (file_lock_pair == NULL) { + // TODO + return -2; + } + file_lock_pair->key = file->identifier; + file_lock_pair->value = file->lock; + if (AqvmBaseLinkedList_AddNode( + &AqvmBaseThreadingFileLock_fileLockTable + .data[AqvmBaseFileIdentifier_GetIdentifierHash( + &file->identifier) % + 1024], + file_lock_pair) != 0) { + // TODO + return -3; + } + return 0; +} + +int AqvmBaseThreadingFileLock_RemoveFileLock(struct AqvmBaseFile_File* file) { if (file == NULL) { // TODO return -1; } +} - AqvmBaseThreadingFileLock_fileLockTable - .data[AqvmBaseFileIdentifier_GetIdentifierHash(&file->identifier) % 1024]; +struct AqvmBaseFileReadWriteLock_ReadWriteLock* +AqvmBaseThreadingFileLock_GetFileLock(struct AqvmBaseFile_File* file) { + if (file == NULL || file->identifier == NULL) { + // TODO + return NULL; + } + + struct AqvmBaseLinkedList_LinkedList* linked_list = + &AqvmBaseThreadingFileLock_fileLockTable + .data[AqvmBaseFileIdentifier_GetIdentifierHash(&file->identifier) % + 1024]; + struct AqvmBaseLinkedList_Node* node = linked_list->head; + + struct AqvmBaseFileReadWriteLock_ReadWriteLock* lock = NULL; + while (node != NULL) { + if (*file->identifier == + *(AqvmBaseFileIdentifier_Identifier*)((struct AqvmBase_Pair*)node->data) + ->key) { + lock = (struct + AqvmBaseFileReadWriteLock_ReadWriteLock*)((struct AqvmBase_Pair*) + node->data) + ->value; + break; + } + node = node->next; + } } \ No newline at end of file diff --git a/aqvm/base/threading/file_lock/file_lock.h b/aqvm/base/threading/file_lock/file_lock.h index 90a6de8..e240d70 100644 --- a/aqvm/base/threading/file_lock/file_lock.h +++ b/aqvm/base/threading/file_lock/file_lock.h @@ -17,6 +17,11 @@ int AqvmBaseThreadingFileLock_CloseFileLockTable(); int AqvmBaseThreadingFileLock_InitializeFileLockTable(); -int AqvmBaseThreadingFileLock_InsertFileLock(struct AqvmBaseFile_File* file); +struct AqvmBaseFileReadWriteLock_ReadWriteLock* +AqvmBaseThreadingFileLock_GetFileLock(struct AqvmBaseFile_File* file); + +int AqvmBaseThreadingFileLock_AddFileLock(struct AqvmBaseFile_File* file); + +int AqvmBaseThreadingFileLock_RemoveFileLock(struct AqvmBaseFile_File* file); #endif \ No newline at end of file