even more atomic shit baby

This commit is contained in:
beerpsi 2024-01-01 16:32:55 +07:00
parent 3dffb242c3
commit 1ec4c35ae4
3 changed files with 30 additions and 29 deletions

View File

@ -5,6 +5,7 @@
#include "chuniio.h" #include "chuniio.h"
#include <process.h> #include <process.h>
#include <stdatomic.h>
#include "arch.h" #include "arch.h"
#include "config.h" #include "config.h"
@ -91,7 +92,7 @@ static bool chuni_io_coin;
static uint16_t chuni_io_coins; static uint16_t chuni_io_coins;
static uint8_t chuni_io_hand_pos; static uint8_t chuni_io_hand_pos;
static HANDLE chuni_io_slider_thread; 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; static struct chuni_io_config chuni_io_cfg;
uint16_t chuni_io_get_api_version() { return 0x0102; } 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) || if ((chuni_io_file_mapping && chuni_io_file_mapping->testBtn) ||
GetAsyncKeyState(chuni_io_cfg.vk_test)) { 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) || if ((chuni_io_file_mapping && chuni_io_file_mapping->serviceBtn) ||
GetAsyncKeyState(chuni_io_cfg.vk_service)) { 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)) { if (GetAsyncKeyState(chuni_io_cfg.vk_ir_emu)) {
@ -179,6 +180,8 @@ void chuni_io_slider_start(void *callback) {
return; return;
} }
atomic_flag_test_and_set(&chuni_io_slider_run_flag);
chuni_io_slider_thread = chuni_io_slider_thread =
(HANDLE)_beginthreadex(NULL, 0, chuni_io_slider_thread_proc, callback, 0, NULL); (HANDLE)_beginthreadex(NULL, 0, chuni_io_slider_thread_proc, callback, 0, NULL);
} }
@ -188,12 +191,11 @@ void chuni_io_slider_stop(void) {
return; return;
} }
chuni_io_slider_stop_flag = true; atomic_flag_clear(&chuni_io_slider_run_flag);
WaitForSingleObject(chuni_io_slider_thread, INFINITE); WaitForSingleObject(chuni_io_slider_thread, INFINITE);
CloseHandle(chuni_io_slider_thread); CloseHandle(chuni_io_slider_thread);
chuni_io_slider_thread = NULL; chuni_io_slider_thread = NULL;
chuni_io_slider_stop_flag = false;
} }
void chuni_io_slider_set_leds(const uint8_t *rgb) { 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) { static unsigned int __stdcall chuni_io_slider_thread_proc(void *ctx) {
const chuni_io_slider_callback_t callback = ctx; const chuni_io_slider_callback_t callback = ctx;
#pragma clang diagnostic push while (atomic_flag_test_and_set(&chuni_io_slider_run_flag)) {
#pragma ide diagnostic ignored "LoopDoesntUseConditionVariableInspection"
// ReSharper disable once CppDFALoopConditionNotUpdated
while (!chuni_io_slider_stop_flag) {
uint8_t pressure[32]; uint8_t pressure[32];
if (chuni_io_file_mapping) { if (chuni_io_file_mapping) {
memcpy(pressure, chuni_io_file_mapping->sliderIoStatus, 32); 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); callback(pressure);
Sleep(1); Sleep(1);
} }
#pragma clang diagnostic pop
return 0; return 0;
} }

View File

@ -28,8 +28,8 @@ typedef struct {
char remote_address[40]; char remote_address[40];
uint16_t remote_port; uint16_t remote_port;
atomic_bool exit_flag; volatile atomic_flag keep_running_flag;
atomic_bool connected; volatile atomic_bool connected;
uint32_t last_input_packet_id; uint32_t last_input_packet_id;
uint8_t last_card_id[10]; 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[2] = 'E';
send_buffer[3] = 'D'; 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]; uint8_t current_led_status[3 * 32];
if (!atomic_load(&ctx->connected)) { 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"); print_err("[Android: INFO] Device disconnected!\n");
ctx->connected = false; ctx->connected = false;
ctx->exit_flag = true; atomic_flag_clear(&ctx->keep_running_flag);
break; break;
} }
} }
@ -191,7 +191,7 @@ unsigned int __stdcall input_recv_thread_proc(void *v) {
int recv_len, packet_len; int recv_len, packet_len;
uint8_t real_len; uint8_t real_len;
while (!atomic_load(&ctx->exit_flag)) { while (atomic_flag_test_and_set(&ctx->keep_running_flag)) {
char buffer[96]; char buffer[96];
if (!tcp_mode) { if (!tcp_mode) {
@ -235,8 +235,10 @@ unsigned int __stdcall input_recv_thread_proc(void *v) {
"%d, os error %ld)\n", "%d, os error %ld)\n",
errno, error); errno, error);
atomic_store(&ctx->connected, false); atomic_store(&ctx->connected, false);
atomic_store(&ctx->exit_flag, true); atomic_flag_clear(&ctx->keep_running_flag);
break;
free(ctx);
return 1;
} }
recv_len = recv_len + read; recv_len = recv_len + read;
@ -262,8 +264,10 @@ unsigned int __stdcall input_recv_thread_proc(void *v) {
"%d, os error %ld)\n", "%d, os error %ld)\n",
errno, error); errno, error);
atomic_store(&ctx->connected, false); atomic_store(&ctx->connected, false);
atomic_store(&ctx->exit_flag, true); atomic_flag_clear(&ctx->keep_running_flag);
break;
free(ctx);
return 1;
} }
recv_len = recv_len + read; recv_len = recv_len + read;
@ -319,7 +323,7 @@ unsigned int __stdcall input_recv_thread_proc(void *v) {
atomic_store(&ctx->connected, false); atomic_store(&ctx->connected, false);
if (tcp_mode) { 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"); print_err("[Android: INFO] Device disconnected (clean disconnect).\n");
break; 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); print_err("[Android: INFO] Waiting for device on port %d...\n", server_port);
android_thread_ctx *ctx = malloc(sizeof(android_thread_ctx)); android_thread_ctx *ctx = malloc(sizeof(android_thread_ctx));
atomic_flag_test_and_set(&ctx->keep_running_flag);
ctx->sock = sock; ctx->sock = sock;
ctx->exit_flag = ATOMIC_VAR_INIT(false);
ctx->connected = ATOMIC_VAR_INIT(false); ctx->connected = ATOMIC_VAR_INIT(false);
ctx->last_input_packet_id = 0; ctx->last_input_packet_id = 0;
ctx->memory = memory; ctx->memory = memory;
@ -431,8 +435,8 @@ unsigned int __stdcall server_thread_proc(void *v) {
} }
android_thread_ctx *ctx = malloc(sizeof(android_thread_ctx)); android_thread_ctx *ctx = malloc(sizeof(android_thread_ctx));
atomic_flag_test_and_set(&ctx->keep_running_flag);
ctx->sock = acc_socket; ctx->sock = acc_socket;
ctx->exit_flag = ATOMIC_VAR_INIT(false);
ctx->connected = ATOMIC_VAR_INIT(true); ctx->connected = ATOMIC_VAR_INIT(true);
ctx->last_input_packet_id = 0; ctx->last_input_packet_id = 0;
ctx->memory = memory; ctx->memory = memory;

View File

@ -17,7 +17,7 @@ typedef struct {
idevice_t device; idevice_t device;
idevice_connection_t connection; idevice_connection_t connection;
atomic_bool exit_flag; volatile atomic_flag keep_running_flag;
bool has_previous_led_status; bool has_previous_led_status;
uint8_t previous_led_status[3 * 32]; 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[2] = 'E';
send_buffer[3] = 'D'; 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]; uint8_t current_led_status[3 * 32];
memcpy(current_led_status, ctx->memory->ledRgbData, 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; bool air_enabled = true;
while (!atomic_load(&ctx->exit_flag)) { while (atomic_flag_test_and_set(&ctx->keep_running_flag)) {
char buffer[96]; char buffer[96];
int status; int status;
uint32_t read; 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); 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; 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, if ((status = idevice_connection_receive_timeout(ctx->connection, buffer + 4,
len - 3, &read, 5))) { len - 3, &read, 5))) {
print_err("[iOS:ERROR] Could not read data from device: %d\n", status); 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; break;
} }
@ -197,7 +197,6 @@ unsigned int __stdcall connect_device(void *v) {
} }
print_err("[iOS: INFO] Connected to device\n"); 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_input_recv_thread_proc, ctx, 0, NULL);
_beginthreadex(NULL, 0, ios_led_broadcast_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)); ios_thread_ctx *args = malloc(sizeof(ios_thread_ctx));
strcpy_s(args->remote_udid, 41, event->udid); strcpy_s(args->remote_udid, 41, event->udid);
atomic_flag_test_and_set(&args->keep_running_flag);
args->device = NULL; args->device = NULL;
args->connection = NULL; args->connection = NULL;
args->exit_flag = ATOMIC_VAR_INIT(false);
args->has_previous_led_status = false; args->has_previous_led_status = false;
args->led_skip_count = 0; args->led_skip_count = 0;
args->memory = memory; args->memory = memory;