diff options
author | Mitya Selivanov <automainint@guattari.tech> | 2023-09-19 14:44:12 +0200 |
---|---|---|
committer | Mitya Selivanov <automainint@guattari.tech> | 2023-09-19 14:44:12 +0200 |
commit | 7a889fd7fb01023b6ad4f673a4693742c9e05cdd (patch) | |
tree | fd78833053e2d037022a4ca8b8e7a3dd4c6d403d /source/tests/test_interprocess.c | |
parent | 5a29774fa29c03535b6df8b6ba90733f235ddb9d (diff) | |
download | kit-7a889fd7fb01023b6ad4f673a4693742c9e05cdd.zip |
shared mutex fixes
Diffstat (limited to 'source/tests/test_interprocess.c')
-rw-r--r-- | source/tests/test_interprocess.c | 97 |
1 files changed, 79 insertions, 18 deletions
diff --git a/source/tests/test_interprocess.c b/source/tests/test_interprocess.c index cf33086..b41f71f 100644 --- a/source/tests/test_interprocess.c +++ b/source/tests/test_interprocess.c @@ -1,19 +1,24 @@ #include "../kit/shared_memory.h" +#include "../kit/shared_mutex.h" #include "../kit/threads.h" #include <stdio.h> #define NAME "kit_test_interprocess" -enum { DATA_SIZE = 64 }; +enum { DATA_SIZE = 64, TIMEOUT = 3 }; + +typedef struct { + shared_mutex_t m; + i8 state; + i8 bytes[DATA_SIZE]; +} shared_data_t; enum { STATE_INIT, STATE_READY, STATE_DONE }; int run_writer() { - kit_shared_memory_clean(SZ(NAME)); - - kit_shared_memory_t mem = kit_shared_memory_open( - SZ(NAME), DATA_SIZE, KIT_SHARED_MEMORY_CREATE); + shared_memory_t mem = shared_memory_open( + SZ(NAME), sizeof(shared_data_t), SHARED_MEMORY_CREATE); if (mem.status != KIT_OK) { printf("%s: kit_shared_memory_open failed.\n", __FUNCTION__); @@ -21,13 +26,41 @@ int run_writer() { return 1; } - mem.bytes[0] = STATE_INIT; - for (int i = 1; i < DATA_SIZE; i++) mem.bytes[i] = i; - mem.bytes[0] = STATE_READY; + shared_data_t *p = (shared_data_t *) mem.bytes; + + shared_mutex_init(&p->m); + + unique_lock(&p->m); + p->state = STATE_INIT; + unique_unlock(&p->m); + + unique_lock(&p->m); + for (int i = 0; i < DATA_SIZE; i++) p->bytes[i] = i; + p->state = STATE_READY; + unique_unlock(&p->m); + + struct timespec t0; + timespec_get(&t0, TIME_UTC); + + shared_lock(&p->m); + while (p->state != STATE_DONE) { + shared_unlock(&p->m); - while (mem.bytes[0] != STATE_DONE) thrd_yield(); + struct timespec t1; + timespec_get(&t1, TIME_UTC); + + if (t1.tv_sec - t0.tv_sec > TIMEOUT) { + printf("%s: timeout.\n", __FUNCTION__); + shared_memory_close(&mem); + return 1; + } + + thrd_yield(); + shared_lock(&p->m); + } + shared_unlock(&p->m); - if (kit_shared_memory_close(&mem) != KIT_OK) { + if (shared_memory_close(&mem) != KIT_OK) { printf("%s: kit_shared_memory_close failed.\n", __FUNCTION__); fflush(stdout); return 1; @@ -37,12 +70,24 @@ int run_writer() { } int run_reader() { - kit_shared_memory_t mem; + struct timespec t0; + timespec_get(&t0, TIME_UTC); + + shared_memory_t mem; for (;;) { - mem = kit_shared_memory_open(SZ(NAME), DATA_SIZE, - KIT_SHARED_MEMORY_OPEN); + mem = shared_memory_open(SZ(NAME), sizeof(shared_data_t), + SHARED_MEMORY_OPEN); if (mem.status == KIT_OK) break; + + struct timespec t1; + timespec_get(&t1, TIME_UTC); + + if (t1.tv_sec - t0.tv_sec > TIMEOUT) { + printf("%s: timeout.\n", __FUNCTION__); + return 1; + } + thrd_yield(); } @@ -52,20 +97,31 @@ int run_reader() { return 1; } - while (mem.bytes[0] != STATE_READY) thrd_yield(); + shared_data_t *p = (shared_data_t *) mem.bytes; + + shared_lock(&p->m); + while (p->state != STATE_READY) { + shared_unlock(&p->m); + thrd_yield(); + shared_lock(&p->m); + } i32 status = 0; - for (i32 i = 1; i < DATA_SIZE; i++) - if (mem.bytes[i] != i) { + for (i32 i = 0; i < DATA_SIZE; i++) + if (p->bytes[i] != i) { printf("%s: wrong byte %d\n", __FUNCTION__, i); fflush(stdout); status = 1; } - mem.bytes[0] = STATE_DONE; + shared_unlock(&p->m); - if (kit_shared_memory_close(&mem) != KIT_OK) { + unique_lock(&p->m); + p->state = STATE_DONE; + unique_unlock(&p->m); + + if (shared_memory_close(&mem) != KIT_OK) { printf("%s: kit_shared_memory_close failed.\n", __FUNCTION__); fflush(stdout); status = 1; @@ -102,6 +158,11 @@ int main(int argc, char **argv) { if (strcmp(argv[1], "reader") == 0) return run_reader(); + if (strcmp(argv[1], "clean") == 0) { + shared_memory_clean(SZ(NAME)); + return 0; + } + printf("Invalid command line argument \"%s\"\n", argv[1]); return 1; } |