From 1670c26313ec4ed213e97eba74722783d862503a Mon Sep 17 00:00:00 2001 From: Nick Chan Date: Sat, 21 Sep 2024 20:06:43 +0800 Subject: [PATCH] remove spinlocks --- include/palerain.h | 8 ++++++-- src/dfuhelper.c | 19 ++++++++----------- src/lock_vars.c | 13 ++++++++----- src/main.c | 10 ++++------ src/pongo_helper.c | 12 +++++------- src/usb_iokit.c | 4 +--- 6 files changed, 32 insertions(+), 34 deletions(-) diff --git a/include/palerain.h b/include/palerain.h index 749b6a7..3ede341 100644 --- a/include/palerain.h +++ b/include/palerain.h @@ -153,6 +153,8 @@ typedef unsigned char niarelap_file_t[]; extern unsigned int verbose; +pthread_cond_t spin; + extern char* pongo_path; #ifdef TUI extern bool tui_started; @@ -172,6 +174,7 @@ extern uint64_t palerain_flags; extern pthread_mutex_t log_mutex; extern pthread_mutex_t spin_mutex, found_pongo_mutex, ecid_dfu_wait_mutex; +pthread_cond_t spin; extern int pongo_thr_running, dfuhelper_thr_running; extern bool device_has_booted; @@ -213,8 +216,8 @@ int issue_pongo_command(usb_device_handle_t, char*); int tui(void); int optparse(int argc, char* argv[]); -bool get_spin(void); -bool set_spin(bool val); +bool palerain_unblock(void); +bool palerain_block(void); bool get_found_pongo(void); void* pongo_helper(void* _); bool set_found_pongo(bool val); @@ -227,4 +230,5 @@ void io_stop(stuff_t *stuff); void print_credits(void); + #endif diff --git a/src/dfuhelper.c b/src/dfuhelper.c index 0832fb7..44f263d 100644 --- a/src/dfuhelper.c +++ b/src/dfuhelper.c @@ -100,7 +100,7 @@ int connected_normal_mode(const usbmuxd_device_info_t *usbmuxd_device) { printf("DisplayName: %s\n", dev.displayName); device_has_booted = true; - set_spin(0); + palerain_unblock(); unsubscribe_cmd(); return 0; } @@ -115,7 +115,7 @@ int connected_normal_mode(const usbmuxd_device_info_t *usbmuxd_device) { devinfo_free(&dev); if ((palerain_flags & palerain_option_enter_recovery)) { device_has_booted = true; - set_spin(0); + palerain_unblock(); unsubscribe_cmd(); } return 0; @@ -226,7 +226,7 @@ void* connected_dfu_mode(struct irecv_device_info* info) { if (IS_APPLE_HOME) { step(2, 0, "Put device in upright orientation", NULL, 0); } - set_spin(0); + palerain_unblock(); unsubscribe_cmd(); pthread_exit(NULL); return NULL; @@ -243,7 +243,7 @@ void device_event_cb(const usbmuxd_event_t *event, void* userdata) { int ret = reboot_cmd(event->device.udid); if (!ret) { LOG(LOG_INFO, "Restarted device"); - set_spin(0); + palerain_unblock(); unsubscribe_cmd(); } pthread_exit(NULL); @@ -274,7 +274,7 @@ void irecv_device_event_cb(const irecv_device_event_t *event, void* userdata) { if (!ret) { LOG(LOG_INFO, "Exited recovery mode"); device_has_booted = true; - set_spin(0); + palerain_unblock(); unsubscribe_cmd(); } else { LOG(LOG_WARNING, "Could not exit recovery mode"); @@ -295,7 +295,7 @@ void irecv_device_event_cb(const irecv_device_event_t *event, void* userdata) { printf("DisplayName: %s\n", info.display_name); device_has_booted = true; - set_spin(0); + palerain_unblock(); unsubscribe_cmd(); } if (dfuhelper_thr_running) pthread_cancel(dfuhelper_thread); @@ -321,7 +321,7 @@ void irecv_device_event_cb(const irecv_device_event_t *event, void* userdata) { printf("DisplayName: %s\n", info.display_name); device_has_booted = true; - set_spin(0); + palerain_unblock(); unsubscribe_cmd(); } if (dfuhelper_thr_running) pthread_cancel(dfuhelper_thread); @@ -346,11 +346,8 @@ void irecv_device_event_cb(const irecv_device_event_t *event, void* userdata) { void *dfuhelper(void* ptr) { dfuhelper_thr_running = true; - set_spin(1); subscribe_cmd(device_event_cb, irecv_device_event_cb); - while (get_spin()) { - sleep(1); - }; + palerain_block(); dfuhelper_thr_running = false; return 0; } diff --git a/src/lock_vars.c b/src/lock_vars.c index 7da4219..bd6551f 100644 --- a/src/lock_vars.c +++ b/src/lock_vars.c @@ -30,8 +30,9 @@ #include pthread_mutex_t spin_mutex, found_pongo_mutex, ecid_dfu_wait_mutex; +pthread_cond_t spin; -bool spin, found_pongo = 0; +bool found_pongo = 0; uint64_t ecid_wait_for_dfu = 0; static bool get_locked_bool(bool* val, pthread_mutex_t* mutex) { @@ -50,12 +51,14 @@ static bool set_locked_bool(bool* val, bool newval, pthread_mutex_t* mutex) { return newval; } -bool get_spin(void) { - return get_locked_bool(&spin, &spin_mutex); +bool palerain_block(void) { + pthread_mutex_lock(&spin_mutex); + pthread_cond_wait(&spin, &spin_mutex); + pthread_mutex_unlock(&spin_mutex); } -bool set_spin(bool newval) { - return set_locked_bool(&spin, newval, &spin_mutex); +bool palerain_unblock(void) { + pthread_cond_broadcast(&spin); } bool get_found_pongo(void) { diff --git a/src/main.c b/src/main.c index 6477262..c737bc3 100644 --- a/src/main.c +++ b/src/main.c @@ -107,6 +107,7 @@ int palera1n(int argc, char *argv[], char *envp[]) { pthread_mutex_init(&spin_mutex, NULL); pthread_mutex_init(&found_pongo_mutex, NULL); pthread_mutex_init(&ecid_dfu_wait_mutex, NULL); + pthread_cond_init(&spin, NULL); if ((ret = build_checks())) return ret; if ((ret = optparse(argc, argv))) goto cleanup; if (!(palerain_flags & palerain_option_device_info) && (palerain_flags & palerain_option_palerain_version)) goto normal_exit; @@ -140,7 +141,7 @@ int palera1n(int argc, char *argv[], char *envp[]) { pthread_create(&pongo_thread, NULL, pongo_helper, NULL); pthread_create(&dfuhelper_thread, NULL, dfuhelper, NULL); pthread_join(dfuhelper_thread, NULL); - set_spin(0); + palerain_unblock(); if ((palerain_flags & (palerain_option_dfuhelper_only | palerain_option_reboot_device | palerain_option_exit_recovery | @@ -154,14 +155,10 @@ int palera1n(int argc, char *argv[], char *envp[]) { if ((palerain_flags & (palerain_option_pongo_exit | palerain_option_demote))) goto normal_exit; - set_spin(1); sleep(2); pthread_create(&pongo_thread, NULL, pongo_helper, NULL); pthread_join(pongo_thread, NULL); - while (get_spin()) - { - sleep(1); - } + palerain_block(); normal_exit: cleanup: if (override_kpf.magic == OVERRIDE_MAGIC) { @@ -181,6 +178,7 @@ cleanup: pthread_mutex_destroy(&spin_mutex); pthread_mutex_destroy(&found_pongo_mutex); pthread_mutex_destroy(&ecid_dfu_wait_mutex); + pthread_cond_destroy(&spin); return ret; } diff --git a/src/pongo_helper.c b/src/pongo_helper.c index cda6673..c68e604 100644 --- a/src/pongo_helper.c +++ b/src/pongo_helper.c @@ -25,9 +25,7 @@ void* pongo_helper(void* ptr) { pongo_thr_running = 1; pthread_cleanup_push(thr_cleanup, &pongo_thr_running); wait_for_pongo(); - while (get_spin()) { - sleep(1); - } + palerain_block(); pthread_cleanup_pop(1); return NULL; } @@ -141,7 +139,7 @@ done: #ifdef USE_LIBUSB libusb_unref_device(arg->dev); #endif - set_spin(0); + palerain_unblock(); return NULL; } @@ -236,7 +234,7 @@ void io_start(stuff_t *stuff) if(r != 0) { ERR("pthread_create: %s", strerror(r)); - set_spin(0); + palerain_unblock(); return; } pthread_join(stuff->th, NULL); @@ -248,14 +246,14 @@ void io_stop(stuff_t *stuff) if(r != 0) { ERR("pthread_cancel: %s", strerror(r)); - set_spin(0); + palerain_unblock(); return; } r = pthread_join(stuff->th, NULL); if(r != 0) { ERR("pthread_join: %s", strerror(r)); - set_spin(0); + palerain_unblock(); return; } #ifdef USE_LIBUSB diff --git a/src/usb_iokit.c b/src/usb_iokit.c index 59cf59a..2d5bb86 100644 --- a/src/usb_iokit.c +++ b/src/usb_iokit.c @@ -245,9 +245,7 @@ int wait_for_pongo(void) { } LostDevice(&stuff, lost); // CFRunLoopRun(); - while (get_spin()) { - sleep(1); - } + palerain_block(); CFRelease(cfdict); return 0; }