diff options
author | Mitya Selivanov <automainint@guattari.tech> | 2023-09-15 02:02:57 +0200 |
---|---|---|
committer | Mitya Selivanov <automainint@guattari.tech> | 2023-09-15 02:02:57 +0200 |
commit | 93bf289d04b1f029787f51824fbc42136a8285b0 (patch) | |
tree | a809e324bd294647dd9345b7e91bd9c9ab58e73a /source/tests | |
parent | b2666b7ec2c923c2d33627f75d032eb4dbbf2937 (diff) | |
download | kit-93bf289d04b1f029787f51824fbc42136a8285b0.zip |
shared_memory posix impl
Diffstat (limited to 'source/tests')
-rw-r--r-- | source/tests/test_interprocess.c | 121 |
1 files changed, 45 insertions, 76 deletions
diff --git a/source/tests/test_interprocess.c b/source/tests/test_interprocess.c index df2a375..30a8779 100644 --- a/source/tests/test_interprocess.c +++ b/source/tests/test_interprocess.c @@ -1,90 +1,60 @@ +#include "../kit/shared_memory.h" +#include "../kit/threads.h" + +#include <stdio.h> + #ifdef _WIN32 int main() { return 0; } #else -# include "../kit/threads.h" +# define NAME "kit_test_interprocess" -# include <stdio.h> -# include <string.h> -# include <sys/mman.h> -# include <sys/stat.h> -# include <fcntl.h> -# include <unistd.h> - -# define NAME "/kit_test_interprocess" +enum { SIZE = 64 }; enum { STATE_INIT, STATE_READY, STATE_DONE }; -enum { SIZE = 64 }; - int run_writer() { - int f = shm_open(NAME, O_RDWR | O_CREAT, 0660); + kit_shared_memory_t mem = kit_shared_memory_create(SZ(NAME), SIZE); - if (f == -1) { - printf("%s: shm_open failed.\n", __FUNCTION__); - return 1; - } - - if (ftruncate(f, SIZE) == -1) { - printf("%s: ftruncate failed.\n", __FUNCTION__); + if (mem.status != KIT_OK) { + printf("%s: kit_shared_memory_create failed.\n", __FUNCTION__); fflush(stdout); - return 1; - } - - void *p = mmap(NULL, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, f, - 0); - - if (p == MAP_FAILED) { - printf("%s: mmap failed.\n", __FUNCTION__); - fflush(stdout); - return 1; + return mem.status; } - unsigned char *bytes = (unsigned char *) p; + mem.bytes[0] = STATE_INIT; + for (int i = 1; i < SIZE; i++) mem.bytes[i] = i; + mem.bytes[0] = STATE_READY; - bytes[0] = STATE_INIT; - for (int i = 1; i < SIZE; i++) bytes[i] = i; - bytes[0] = STATE_READY; + while (mem.bytes[0] != STATE_DONE) thrd_yield(); - while (bytes[0] != STATE_DONE) thrd_yield(); - - shm_unlink(NAME); - - return 0; + return kit_shared_memory_close(&mem); } int run_reader() { - int f = -1; - - while (f == -1) { - f = shm_open(NAME, O_RDWR, 0); - thrd_yield(); - } - - void *p = mmap(NULL, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, f, - 0); + kit_shared_memory_t mem = kit_shared_memory_open(SZ(NAME), SIZE); - if (p == MAP_FAILED) { - printf("%s: mmap failed.\n", __FUNCTION__); + if (mem.status != KIT_OK) { + printf("%s: kit_shared_memory_open failed.\n", __FUNCTION__); fflush(stdout); - return 1; + return mem.status; } - unsigned char *bytes = (unsigned char *) p; - - while (bytes[0] != STATE_READY) thrd_yield(); + while (mem.bytes[0] != STATE_READY) thrd_yield(); - int status = 0; + i32 status = 0; - for (int i = 1; i < SIZE; i++) - if (bytes[i] != i) { + for (i32 i = 1; i < SIZE; i++) + if (mem.bytes[i] != i) { printf("%s: wrong byte %d\n", __FUNCTION__, i); fflush(stdout); status = 1; } - bytes[0] = STATE_DONE; + mem.bytes[0] = STATE_DONE; + + status |= kit_shared_memory_close(&mem); return status; } @@ -95,30 +65,29 @@ int main(int argc, char **argv) { return 1; } - int status = 0; + if (strcmp(argv[1], "writer") == 0) { + struct timespec t0; + timespec_get(&t0, TIME_UTC); - struct timespec t0; - timespec_get(&t0, TIME_UTC); + i32 status = run_writer(); - if (strcmp(argv[1], "writer") == 0) - status = run_writer(); - else if (strcmp(argv[1], "reader") == 0) - status = run_reader(); - else { - printf("Invalid command line argument \"%s\"\n", argv[1]); - return 1; - } + struct timespec t1; + timespec_get(&t1, TIME_UTC); - struct timespec t1; - timespec_get(&t1, TIME_UTC); + i64 sec = t1.tv_sec - t0.tv_sec; + i64 nsec = t1.tv_nsec - t0.tv_nsec; - long long sec = t1.tv_sec - t0.tv_sec; - long long nsec = t1.tv_nsec - t0.tv_nsec; + printf("Done in %.2lf msec\n", + (sec * 1000000000 + nsec) * 0.000001); + fflush(stdout); - printf("%s: done in %.2lf msec\n", argv[1], - (sec * 1000000000 + nsec) * 0.000001); - fflush(stdout); + return status; + } - return status; + if (strcmp(argv[1], "reader") == 0) + return run_reader(); + + printf("Invalid command line argument \"%s\"\n", argv[1]); + return 1; } #endif |