From 1ec4c35ae4b5f10c6d3db7a5db6c4cfe6053c72a Mon Sep 17 00:00:00 2001 From: beerpsi Date: Mon, 1 Jan 2024 16:32:55 +0700 Subject: [PATCH] even more atomic shit baby --- chuniio/src/chuniio.c | 18 ++++++++---------- chuniio/src/servers/android.c | 28 ++++++++++++++++------------ chuniio/src/servers/ios.c | 13 ++++++------- 3 files changed, 30 insertions(+), 29 deletions(-) diff --git a/chuniio/src/chuniio.c b/chuniio/src/chuniio.c index 96a9908..99edd38 100644 --- a/chuniio/src/chuniio.c +++ b/chuniio/src/chuniio.c @@ -5,6 +5,7 @@ #include "chuniio.h" #include +#include #include "arch.h" #include "config.h" @@ -91,7 +92,7 @@ static bool chuni_io_coin; static uint16_t chuni_io_coins; static uint8_t chuni_io_hand_pos; static HANDLE chuni_io_slider_thread; -static bool chuni_io_slider_stop_flag; +static volatile atomic_flag chuni_io_slider_run_flag; static struct chuni_io_config chuni_io_cfg; uint16_t chuni_io_get_api_version() { return 0x0102; } @@ -139,12 +140,12 @@ void chuni_io_jvs_poll(uint8_t *opbtn, uint8_t *beams) { if ((chuni_io_file_mapping && chuni_io_file_mapping->testBtn) || GetAsyncKeyState(chuni_io_cfg.vk_test)) { - *opbtn |= CHUNI_IO_OPBTN_TEST; /* Test */ + *opbtn |= CHUNI_IO_OPBTN_TEST; } if ((chuni_io_file_mapping && chuni_io_file_mapping->serviceBtn) || GetAsyncKeyState(chuni_io_cfg.vk_service)) { - *opbtn |= CHUNI_IO_OPBTN_SERVICE; /* Service */ + *opbtn |= CHUNI_IO_OPBTN_SERVICE; } if (GetAsyncKeyState(chuni_io_cfg.vk_ir_emu)) { @@ -179,6 +180,8 @@ void chuni_io_slider_start(void *callback) { return; } + atomic_flag_test_and_set(&chuni_io_slider_run_flag); + chuni_io_slider_thread = (HANDLE)_beginthreadex(NULL, 0, chuni_io_slider_thread_proc, callback, 0, NULL); } @@ -188,12 +191,11 @@ void chuni_io_slider_stop(void) { return; } - chuni_io_slider_stop_flag = true; + atomic_flag_clear(&chuni_io_slider_run_flag); WaitForSingleObject(chuni_io_slider_thread, INFINITE); CloseHandle(chuni_io_slider_thread); chuni_io_slider_thread = NULL; - chuni_io_slider_stop_flag = false; } void chuni_io_slider_set_leds(const uint8_t *rgb) { @@ -209,10 +211,7 @@ void chuni_io_led_set_colors(uint8_t board, uint8_t *rgb) {} static unsigned int __stdcall chuni_io_slider_thread_proc(void *ctx) { const chuni_io_slider_callback_t callback = ctx; -#pragma clang diagnostic push -#pragma ide diagnostic ignored "LoopDoesntUseConditionVariableInspection" - // ReSharper disable once CppDFALoopConditionNotUpdated - while (!chuni_io_slider_stop_flag) { + while (atomic_flag_test_and_set(&chuni_io_slider_run_flag)) { uint8_t pressure[32]; if (chuni_io_file_mapping) { memcpy(pressure, chuni_io_file_mapping->sliderIoStatus, 32); @@ -221,7 +220,6 @@ static unsigned int __stdcall chuni_io_slider_thread_proc(void *ctx) { callback(pressure); Sleep(1); } -#pragma clang diagnostic pop return 0; } diff --git a/chuniio/src/servers/android.c b/chuniio/src/servers/android.c index 3f526c7..a976790 100644 --- a/chuniio/src/servers/android.c +++ b/chuniio/src/servers/android.c @@ -28,8 +28,8 @@ typedef struct { char remote_address[40]; uint16_t remote_port; - atomic_bool exit_flag; - atomic_bool connected; + volatile atomic_flag keep_running_flag; + volatile atomic_bool connected; uint32_t last_input_packet_id; uint8_t last_card_id[10]; @@ -130,7 +130,7 @@ unsigned int __stdcall led_broadcast_thread_proc(void *v) { send_buffer[2] = 'E'; send_buffer[3] = 'D'; - while (!atomic_load(&ctx->exit_flag)) { + while (atomic_flag_test_and_set(&ctx->keep_running_flag)) { uint8_t current_led_status[3 * 32]; if (!atomic_load(&ctx->connected)) { @@ -165,7 +165,7 @@ unsigned int __stdcall led_broadcast_thread_proc(void *v) { print_err("[Android: INFO] Device disconnected!\n"); ctx->connected = false; - ctx->exit_flag = true; + atomic_flag_clear(&ctx->keep_running_flag); break; } } @@ -191,7 +191,7 @@ unsigned int __stdcall input_recv_thread_proc(void *v) { int recv_len, packet_len; uint8_t real_len; - while (!atomic_load(&ctx->exit_flag)) { + while (atomic_flag_test_and_set(&ctx->keep_running_flag)) { char buffer[96]; if (!tcp_mode) { @@ -235,8 +235,10 @@ unsigned int __stdcall input_recv_thread_proc(void *v) { "%d, os error %ld)\n", errno, error); atomic_store(&ctx->connected, false); - atomic_store(&ctx->exit_flag, true); - break; + atomic_flag_clear(&ctx->keep_running_flag); + + free(ctx); + return 1; } recv_len = recv_len + read; @@ -262,8 +264,10 @@ unsigned int __stdcall input_recv_thread_proc(void *v) { "%d, os error %ld)\n", errno, error); atomic_store(&ctx->connected, false); - atomic_store(&ctx->exit_flag, true); - break; + atomic_flag_clear(&ctx->keep_running_flag); + + free(ctx); + return 1; } recv_len = recv_len + read; @@ -319,7 +323,7 @@ unsigned int __stdcall input_recv_thread_proc(void *v) { atomic_store(&ctx->connected, false); if (tcp_mode) { - atomic_store(&ctx->exit_flag, true); + atomic_flag_clear(&ctx->keep_running_flag); print_err("[Android: INFO] Device disconnected (clean disconnect).\n"); break; } @@ -384,8 +388,8 @@ unsigned int __stdcall server_thread_proc(void *v) { print_err("[Android: INFO] Waiting for device on port %d...\n", server_port); android_thread_ctx *ctx = malloc(sizeof(android_thread_ctx)); + atomic_flag_test_and_set(&ctx->keep_running_flag); ctx->sock = sock; - ctx->exit_flag = ATOMIC_VAR_INIT(false); ctx->connected = ATOMIC_VAR_INIT(false); ctx->last_input_packet_id = 0; ctx->memory = memory; @@ -431,8 +435,8 @@ unsigned int __stdcall server_thread_proc(void *v) { } android_thread_ctx *ctx = malloc(sizeof(android_thread_ctx)); + atomic_flag_test_and_set(&ctx->keep_running_flag); ctx->sock = acc_socket; - ctx->exit_flag = ATOMIC_VAR_INIT(false); ctx->connected = ATOMIC_VAR_INIT(true); ctx->last_input_packet_id = 0; ctx->memory = memory; diff --git a/chuniio/src/servers/ios.c b/chuniio/src/servers/ios.c index 4688efd..66f47d5 100644 --- a/chuniio/src/servers/ios.c +++ b/chuniio/src/servers/ios.c @@ -17,7 +17,7 @@ typedef struct { idevice_t device; idevice_connection_t connection; - atomic_bool exit_flag; + volatile atomic_flag keep_running_flag; bool has_previous_led_status; uint8_t previous_led_status[3 * 32]; @@ -35,7 +35,7 @@ unsigned int __stdcall ios_led_broadcast_thread_proc(void *v) { send_buffer[2] = 'E'; send_buffer[3] = 'D'; - while (!atomic_load(&ctx->exit_flag)) { + while (atomic_flag_test_and_set(&ctx->keep_running_flag)) { uint8_t current_led_status[3 * 32]; memcpy(current_led_status, ctx->memory->ledRgbData, 3 * 32); @@ -75,7 +75,7 @@ unsigned int __stdcall ios_input_recv_thread_proc(void *v) { bool air_enabled = true; - while (!atomic_load(&ctx->exit_flag)) { + while (atomic_flag_test_and_set(&ctx->keep_running_flag)) { char buffer[96]; int status; uint32_t read; @@ -87,7 +87,7 @@ unsigned int __stdcall ios_input_recv_thread_proc(void *v) { } print_err("[iOS:ERROR] Could not read data from device: %d\n", status); - atomic_store(&ctx->exit_flag, true); + atomic_flag_clear(&ctx->keep_running_flag); break; } @@ -97,7 +97,7 @@ unsigned int __stdcall ios_input_recv_thread_proc(void *v) { if ((status = idevice_connection_receive_timeout(ctx->connection, buffer + 4, len - 3, &read, 5))) { print_err("[iOS:ERROR] Could not read data from device: %d\n", status); - atomic_store(&ctx->exit_flag, true); + atomic_flag_clear(&ctx->keep_running_flag); break; } @@ -197,7 +197,6 @@ unsigned int __stdcall connect_device(void *v) { } print_err("[iOS: INFO] Connected to device\n"); - atomic_store(&ctx->exit_flag, false); _beginthreadex(NULL, 0, ios_input_recv_thread_proc, ctx, 0, NULL); _beginthreadex(NULL, 0, ios_led_broadcast_thread_proc, ctx, 0, NULL); @@ -213,9 +212,9 @@ void device_event_callback(const idevice_event_t *event, void *user_data) { ios_thread_ctx *args = malloc(sizeof(ios_thread_ctx)); strcpy_s(args->remote_udid, 41, event->udid); + atomic_flag_test_and_set(&args->keep_running_flag); args->device = NULL; args->connection = NULL; - args->exit_flag = ATOMIC_VAR_INIT(false); args->has_previous_led_status = false; args->led_skip_count = 0; args->memory = memory;