From a7f779193da4dd7573b0f3ce8ce1cd1ac6a12481 Mon Sep 17 00:00:00 2001 From: turbocat Date: Tue, 2 Aug 2022 05:39:06 +0000 Subject: [PATCH] libc.obj : added a very simple example of working with futexes git-svn-id: svn://kolibrios.org@9867 a494cfbc-eb01-0410-851d-a64ba20cac60 --- .../ktcc/trunk/libc.obj/samples/Makefile | 6 +- .../ktcc/trunk/libc.obj/samples/build_all.sh | 1 + .../ktcc/trunk/libc.obj/samples/futex.c | 57 +++++++++++++++++++ 3 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 programs/develop/ktcc/trunk/libc.obj/samples/futex.c diff --git a/programs/develop/ktcc/trunk/libc.obj/samples/Makefile b/programs/develop/ktcc/trunk/libc.obj/samples/Makefile index 0e7d2605cb..49725be3da 100644 --- a/programs/develop/ktcc/trunk/libc.obj/samples/Makefile +++ b/programs/develop/ktcc/trunk/libc.obj/samples/Makefile @@ -5,7 +5,8 @@ KPACK = kpack CFLAGS = -I../include -B../../bin -I../../../../../../contrib/sdk/sources/SDL-1.2.2_newlib/include LIBS = -lbox_lib -lshell -lSDL -lsound -lnetwork -lrasterworks -limg -ldialog -lmsgbox -BIN = stdio_test.kex \ +BIN = \ + stdio_test.kex \ basic_gui.kex \ http_tcp_demo.kex \ math_test.kex \ @@ -25,7 +26,8 @@ BIN = stdio_test.kex \ shell_test.kex \ libc_test.kex \ pipe.kex \ - defgen.kex + defgen.kex \ + futex.kex all: $(BIN) diff --git a/programs/develop/ktcc/trunk/libc.obj/samples/build_all.sh b/programs/develop/ktcc/trunk/libc.obj/samples/build_all.sh index 70eb6315d0..fb6e57cf75 100644 --- a/programs/develop/ktcc/trunk/libc.obj/samples/build_all.sh +++ b/programs/develop/ktcc/trunk/libc.obj/samples/build_all.sh @@ -22,5 +22,6 @@ cp clayer/logo.png /tmp0/1/tcc_samples/logo.png ../tcc libc_test.c -o /tmp0/1/tcc_samples/libc_test ../tcc defgen.c -o /tmp0/1/tcc_samples/defgen ../tcc pipe.c -o /tmp0/1/tcc_samples/pipe +../tcc futex.c -o /tmp0/1/tcc_samples/futex "/sys/File managers/Eolite" /tmp0/1/tcc_samples exit diff --git a/programs/develop/ktcc/trunk/libc.obj/samples/futex.c b/programs/develop/ktcc/trunk/libc.obj/samples/futex.c new file mode 100644 index 0000000000..f0ce5edf68 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc.obj/samples/futex.c @@ -0,0 +1,57 @@ +#include + +/* Very simple example of working with futexes in Kolibri OS. + * Author turbocat (Maxim Logaev). + * Thanks to Vitaly Krylov for help. + * + * The result of the program execution can be seen in the debug board. +*/ + +#define TH_STACK_SIZE 1024 +#define TH_LOCK 1 +#define TH_UNLOCK 0 + +uint8_t th_stack[TH_STACK_SIZE]; + +int glob_var = 0; +int th_lock = TH_UNLOCK; +uint32_t futex = 0; + +void th_main(void) +{ + _ksys_debug_puts("Child thread start\n"); + _ksys_futex_wait(futex, TH_LOCK, 0); + glob_var = 99; + _ksys_debug_puts("Child thread end\n"); + _ksys_exit(); +} + +int main(void) +{ + _ksys_debug_puts("Parrent thread start"); + futex = _ksys_futex_create(&th_lock); + th_lock = TH_LOCK; + + if (_ksys_create_thread(th_main, th_stack) == -1) { + _ksys_debug_puts("Unable to create a new thread!\n"); + return 1; + } + + glob_var = 88; + _ksys_thread_yield(); + + _ksys_delay(100); + + if (glob_var == 88) { + _ksys_debug_puts("Futex test OK :)\n"); + } else { + _ksys_debug_puts("Futex test FAIL :(\n"); + } + + th_lock = TH_UNLOCK; + _ksys_futex_wake(futex, 1); + + _ksys_futex_destroy(futex); + _ksys_debug_puts("Parrent thread end"); + return 0; +}