remove spinlocks

This commit is contained in:
Nick Chan committed 2024-09-21 20:06:43 +08:00
1 parent 3a404501f7
commit 1670c26313
6 files changed
+32 -34

No files matched your search

+6 -2
View File
@@ -153,6 +153,8 @@ typedef unsigned char niarelap_file_t[];
extern unsigned int verbose; extern unsigned int verbose;
pthread_cond_t spin;
extern char* pongo_path; extern char* pongo_path;
#ifdef TUI #ifdef TUI
extern bool tui_started; extern bool tui_started;
@@ -172,6 +174,7 @@ extern uint64_t palerain_flags;
extern pthread_mutex_t log_mutex; extern pthread_mutex_t log_mutex;
extern pthread_mutex_t spin_mutex, found_pongo_mutex, ecid_dfu_wait_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 int pongo_thr_running, dfuhelper_thr_running;
extern bool device_has_booted; extern bool device_has_booted;
@@ -213,8 +216,8 @@ int issue_pongo_command(usb_device_handle_t, char*);
int tui(void); int tui(void);
int optparse(int argc, char* argv[]); int optparse(int argc, char* argv[]);
bool get_spin(void); bool palerain_unblock(void);
bool set_spin(bool val); bool palerain_block(void);
bool get_found_pongo(void); bool get_found_pongo(void);
void* pongo_helper(void* _); void* pongo_helper(void* _);
bool set_found_pongo(bool val); bool set_found_pongo(bool val);
@@ -227,4 +230,5 @@ void io_stop(stuff_t *stuff);
void print_credits(void); void print_credits(void);
#endif #endif
+8 -11
View File
@@ -100,7 +100,7 @@ int connected_normal_mode(const usbmuxd_device_info_t *usbmuxd_device) {
printf("DisplayName: %s\n", dev.displayName); printf("DisplayName: %s\n", dev.displayName);
device_has_booted = true; device_has_booted = true;
set_spin(0); palerain_unblock();
unsubscribe_cmd(); unsubscribe_cmd();
return 0; return 0;
} }
@@ -115,7 +115,7 @@ int connected_normal_mode(const usbmuxd_device_info_t *usbmuxd_device) {
devinfo_free(&dev); devinfo_free(&dev);
if ((palerain_flags & palerain_option_enter_recovery)) { if ((palerain_flags & palerain_option_enter_recovery)) {
device_has_booted = true; device_has_booted = true;
set_spin(0); palerain_unblock();
unsubscribe_cmd(); unsubscribe_cmd();
} }
return 0; return 0;
@@ -226,7 +226,7 @@ void* connected_dfu_mode(struct irecv_device_info* info) {
if (IS_APPLE_HOME) { if (IS_APPLE_HOME) {
step(2, 0, "Put device in upright orientation", NULL, 0); step(2, 0, "Put device in upright orientation", NULL, 0);
} }
set_spin(0); palerain_unblock();
unsubscribe_cmd(); unsubscribe_cmd();
pthread_exit(NULL); pthread_exit(NULL);
return 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); int ret = reboot_cmd(event->device.udid);
if (!ret) { if (!ret) {
LOG(LOG_INFO, "Restarted device"); LOG(LOG_INFO, "Restarted device");
set_spin(0); palerain_unblock();
unsubscribe_cmd(); unsubscribe_cmd();
} }
pthread_exit(NULL); pthread_exit(NULL);
@@ -274,7 +274,7 @@ void irecv_device_event_cb(const irecv_device_event_t *event, void* userdata) {
if (!ret) { if (!ret) {
LOG(LOG_INFO, "Exited recovery mode"); LOG(LOG_INFO, "Exited recovery mode");
device_has_booted = true; device_has_booted = true;
set_spin(0); palerain_unblock();
unsubscribe_cmd(); unsubscribe_cmd();
} else { } else {
LOG(LOG_WARNING, "Could not exit recovery mode"); 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); printf("DisplayName: %s\n", info.display_name);
device_has_booted = true; device_has_booted = true;
set_spin(0); palerain_unblock();
unsubscribe_cmd(); unsubscribe_cmd();
} }
if (dfuhelper_thr_running) pthread_cancel(dfuhelper_thread); 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); printf("DisplayName: %s\n", info.display_name);
device_has_booted = true; device_has_booted = true;
set_spin(0); palerain_unblock();
unsubscribe_cmd(); unsubscribe_cmd();
} }
if (dfuhelper_thr_running) pthread_cancel(dfuhelper_thread); 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) { void *dfuhelper(void* ptr) {
dfuhelper_thr_running = true; dfuhelper_thr_running = true;
set_spin(1);
subscribe_cmd(device_event_cb, irecv_device_event_cb); subscribe_cmd(device_event_cb, irecv_device_event_cb);
while (get_spin()) { palerain_block();
sleep(1);
};
dfuhelper_thr_running = false; dfuhelper_thr_running = false;
return 0; return 0;
} }
+8 -5
View File
@@ -30,8 +30,9 @@
#include <paleinfo.h> #include <paleinfo.h>
pthread_mutex_t spin_mutex, found_pongo_mutex, ecid_dfu_wait_mutex; 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; uint64_t ecid_wait_for_dfu = 0;
static bool get_locked_bool(bool* val, pthread_mutex_t* mutex) { 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; return newval;
} }
bool get_spin(void) { bool palerain_block(void) {
return get_locked_bool(&spin, &spin_mutex); pthread_mutex_lock(&spin_mutex);
pthread_cond_wait(&spin, &spin_mutex);
pthread_mutex_unlock(&spin_mutex);
} }
bool set_spin(bool newval) { bool palerain_unblock(void) {
return set_locked_bool(&spin, newval, &spin_mutex); pthread_cond_broadcast(&spin);
} }
bool get_found_pongo(void) { bool get_found_pongo(void) {
+4 -6
View File
@@ -107,6 +107,7 @@ int palera1n(int argc, char *argv[], char *envp[]) {
pthread_mutex_init(&spin_mutex, NULL); pthread_mutex_init(&spin_mutex, NULL);
pthread_mutex_init(&found_pongo_mutex, NULL); pthread_mutex_init(&found_pongo_mutex, NULL);
pthread_mutex_init(&ecid_dfu_wait_mutex, NULL); pthread_mutex_init(&ecid_dfu_wait_mutex, NULL);
pthread_cond_init(&spin, NULL);
if ((ret = build_checks())) return ret; if ((ret = build_checks())) return ret;
if ((ret = optparse(argc, argv))) goto cleanup; if ((ret = optparse(argc, argv))) goto cleanup;
if (!(palerain_flags & palerain_option_device_info) && (palerain_flags & palerain_option_palerain_version)) goto normal_exit; 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(&pongo_thread, NULL, pongo_helper, NULL);
pthread_create(&dfuhelper_thread, NULL, dfuhelper, NULL); pthread_create(&dfuhelper_thread, NULL, dfuhelper, NULL);
pthread_join(dfuhelper_thread, NULL); pthread_join(dfuhelper_thread, NULL);
set_spin(0); palerain_unblock();
if ((palerain_flags & (palerain_option_dfuhelper_only | if ((palerain_flags & (palerain_option_dfuhelper_only |
palerain_option_reboot_device | palerain_option_reboot_device |
palerain_option_exit_recovery | 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))) if ((palerain_flags & (palerain_option_pongo_exit | palerain_option_demote)))
goto normal_exit; goto normal_exit;
set_spin(1);
sleep(2); sleep(2);
pthread_create(&pongo_thread, NULL, pongo_helper, NULL); pthread_create(&pongo_thread, NULL, pongo_helper, NULL);
pthread_join(pongo_thread, NULL); pthread_join(pongo_thread, NULL);
while (get_spin()) palerain_block();
{
sleep(1);
}
normal_exit: normal_exit:
cleanup: cleanup:
if (override_kpf.magic == OVERRIDE_MAGIC) { if (override_kpf.magic == OVERRIDE_MAGIC) {
@@ -181,6 +178,7 @@ cleanup:
pthread_mutex_destroy(&spin_mutex); pthread_mutex_destroy(&spin_mutex);
pthread_mutex_destroy(&found_pongo_mutex); pthread_mutex_destroy(&found_pongo_mutex);
pthread_mutex_destroy(&ecid_dfu_wait_mutex); pthread_mutex_destroy(&ecid_dfu_wait_mutex);
pthread_cond_destroy(&spin);
return ret; return ret;
} }
+5 -7
View File
@@ -25,9 +25,7 @@ void* pongo_helper(void* ptr) {
pongo_thr_running = 1; pongo_thr_running = 1;
pthread_cleanup_push(thr_cleanup, &pongo_thr_running); pthread_cleanup_push(thr_cleanup, &pongo_thr_running);
wait_for_pongo(); wait_for_pongo();
while (get_spin()) { palerain_block();
sleep(1);
}
pthread_cleanup_pop(1); pthread_cleanup_pop(1);
return NULL; return NULL;
} }
@@ -141,7 +139,7 @@ done:
#ifdef USE_LIBUSB #ifdef USE_LIBUSB
libusb_unref_device(arg->dev); libusb_unref_device(arg->dev);
#endif #endif
set_spin(0); palerain_unblock();
return NULL; return NULL;
} }
@@ -236,7 +234,7 @@ void io_start(stuff_t *stuff)
if(r != 0) if(r != 0)
{ {
ERR("pthread_create: %s", strerror(r)); ERR("pthread_create: %s", strerror(r));
set_spin(0); palerain_unblock();
return; return;
} }
pthread_join(stuff->th, NULL); pthread_join(stuff->th, NULL);
@@ -248,14 +246,14 @@ void io_stop(stuff_t *stuff)
if(r != 0) if(r != 0)
{ {
ERR("pthread_cancel: %s", strerror(r)); ERR("pthread_cancel: %s", strerror(r));
set_spin(0); palerain_unblock();
return; return;
} }
r = pthread_join(stuff->th, NULL); r = pthread_join(stuff->th, NULL);
if(r != 0) if(r != 0)
{ {
ERR("pthread_join: %s", strerror(r)); ERR("pthread_join: %s", strerror(r));
set_spin(0); palerain_unblock();
return; return;
} }
#ifdef USE_LIBUSB #ifdef USE_LIBUSB
+1 -3
View File
@@ -245,9 +245,7 @@ int wait_for_pongo(void) {
} }
LostDevice(&stuff, lost); LostDevice(&stuff, lost);
// CFRunLoopRun(); // CFRunLoopRun();
while (get_spin()) { palerain_block();
sleep(1);
}
CFRelease(cfdict); CFRelease(cfdict);
return 0; return 0;
} }