Skip to content

Commit 1670c26

Browse files
committed
remove spinlocks
1 parent 3a40450 commit 1670c26

File tree

6 files changed

+32
-34
lines changed

6 files changed

+32
-34
lines changed

include/palerain.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ typedef unsigned char niarelap_file_t[];
153153

154154
extern unsigned int verbose;
155155

156+
pthread_cond_t spin;
157+
156158
extern char* pongo_path;
157159
#ifdef TUI
158160
extern bool tui_started;
@@ -172,6 +174,7 @@ extern uint64_t palerain_flags;
172174
extern pthread_mutex_t log_mutex;
173175

174176
extern pthread_mutex_t spin_mutex, found_pongo_mutex, ecid_dfu_wait_mutex;
177+
pthread_cond_t spin;
175178

176179
extern int pongo_thr_running, dfuhelper_thr_running;
177180
extern bool device_has_booted;
@@ -213,8 +216,8 @@ int issue_pongo_command(usb_device_handle_t, char*);
213216
int tui(void);
214217
int optparse(int argc, char* argv[]);
215218

216-
bool get_spin(void);
217-
bool set_spin(bool val);
219+
bool palerain_unblock(void);
220+
bool palerain_block(void);
218221
bool get_found_pongo(void);
219222
void* pongo_helper(void* _);
220223
bool set_found_pongo(bool val);
@@ -227,4 +230,5 @@ void io_stop(stuff_t *stuff);
227230

228231
void print_credits(void);
229232

233+
230234
#endif

src/dfuhelper.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ int connected_normal_mode(const usbmuxd_device_info_t *usbmuxd_device) {
100100
printf("DisplayName: %s\n", dev.displayName);
101101

102102
device_has_booted = true;
103-
set_spin(0);
103+
palerain_unblock();
104104
unsubscribe_cmd();
105105
return 0;
106106
}
@@ -115,7 +115,7 @@ int connected_normal_mode(const usbmuxd_device_info_t *usbmuxd_device) {
115115
devinfo_free(&dev);
116116
if ((palerain_flags & palerain_option_enter_recovery)) {
117117
device_has_booted = true;
118-
set_spin(0);
118+
palerain_unblock();
119119
unsubscribe_cmd();
120120
}
121121
return 0;
@@ -226,7 +226,7 @@ void* connected_dfu_mode(struct irecv_device_info* info) {
226226
if (IS_APPLE_HOME) {
227227
step(2, 0, "Put device in upright orientation", NULL, 0);
228228
}
229-
set_spin(0);
229+
palerain_unblock();
230230
unsubscribe_cmd();
231231
pthread_exit(NULL);
232232
return NULL;
@@ -243,7 +243,7 @@ void device_event_cb(const usbmuxd_event_t *event, void* userdata) {
243243
int ret = reboot_cmd(event->device.udid);
244244
if (!ret) {
245245
LOG(LOG_INFO, "Restarted device");
246-
set_spin(0);
246+
palerain_unblock();
247247
unsubscribe_cmd();
248248
}
249249
pthread_exit(NULL);
@@ -274,7 +274,7 @@ void irecv_device_event_cb(const irecv_device_event_t *event, void* userdata) {
274274
if (!ret) {
275275
LOG(LOG_INFO, "Exited recovery mode");
276276
device_has_booted = true;
277-
set_spin(0);
277+
palerain_unblock();
278278
unsubscribe_cmd();
279279
} else {
280280
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) {
295295
printf("DisplayName: %s\n", info.display_name);
296296

297297
device_has_booted = true;
298-
set_spin(0);
298+
palerain_unblock();
299299
unsubscribe_cmd();
300300
}
301301
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) {
321321
printf("DisplayName: %s\n", info.display_name);
322322

323323
device_has_booted = true;
324-
set_spin(0);
324+
palerain_unblock();
325325
unsubscribe_cmd();
326326
}
327327
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) {
346346

347347
void *dfuhelper(void* ptr) {
348348
dfuhelper_thr_running = true;
349-
set_spin(1);
350349
subscribe_cmd(device_event_cb, irecv_device_event_cb);
351-
while (get_spin()) {
352-
sleep(1);
353-
};
350+
palerain_block();
354351
dfuhelper_thr_running = false;
355352
return 0;
356353
}

src/lock_vars.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@
3030
#include <paleinfo.h>
3131

3232
pthread_mutex_t spin_mutex, found_pongo_mutex, ecid_dfu_wait_mutex;
33+
pthread_cond_t spin;
3334

34-
bool spin, found_pongo = 0;
35+
bool found_pongo = 0;
3536
uint64_t ecid_wait_for_dfu = 0;
3637

3738
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) {
5051
return newval;
5152
}
5253

53-
bool get_spin(void) {
54-
return get_locked_bool(&spin, &spin_mutex);
54+
bool palerain_block(void) {
55+
pthread_mutex_lock(&spin_mutex);
56+
pthread_cond_wait(&spin, &spin_mutex);
57+
pthread_mutex_unlock(&spin_mutex);
5558
}
5659

57-
bool set_spin(bool newval) {
58-
return set_locked_bool(&spin, newval, &spin_mutex);
60+
bool palerain_unblock(void) {
61+
pthread_cond_broadcast(&spin);
5962
}
6063

6164
bool get_found_pongo(void) {

src/main.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ int palera1n(int argc, char *argv[], char *envp[]) {
107107
pthread_mutex_init(&spin_mutex, NULL);
108108
pthread_mutex_init(&found_pongo_mutex, NULL);
109109
pthread_mutex_init(&ecid_dfu_wait_mutex, NULL);
110+
pthread_cond_init(&spin, NULL);
110111
if ((ret = build_checks())) return ret;
111112
if ((ret = optparse(argc, argv))) goto cleanup;
112113
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[]) {
140141
pthread_create(&pongo_thread, NULL, pongo_helper, NULL);
141142
pthread_create(&dfuhelper_thread, NULL, dfuhelper, NULL);
142143
pthread_join(dfuhelper_thread, NULL);
143-
set_spin(0);
144+
palerain_unblock();
144145
if ((palerain_flags & (palerain_option_dfuhelper_only |
145146
palerain_option_reboot_device |
146147
palerain_option_exit_recovery |
@@ -154,14 +155,10 @@ int palera1n(int argc, char *argv[], char *envp[]) {
154155

155156
if ((palerain_flags & (palerain_option_pongo_exit | palerain_option_demote)))
156157
goto normal_exit;
157-
set_spin(1);
158158
sleep(2);
159159
pthread_create(&pongo_thread, NULL, pongo_helper, NULL);
160160
pthread_join(pongo_thread, NULL);
161-
while (get_spin())
162-
{
163-
sleep(1);
164-
}
161+
palerain_block();
165162
normal_exit:
166163
cleanup:
167164
if (override_kpf.magic == OVERRIDE_MAGIC) {
@@ -181,6 +178,7 @@ int palera1n(int argc, char *argv[], char *envp[]) {
181178
pthread_mutex_destroy(&spin_mutex);
182179
pthread_mutex_destroy(&found_pongo_mutex);
183180
pthread_mutex_destroy(&ecid_dfu_wait_mutex);
181+
pthread_cond_destroy(&spin);
184182
return ret;
185183
}
186184

src/pongo_helper.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ void* pongo_helper(void* ptr) {
2525
pongo_thr_running = 1;
2626
pthread_cleanup_push(thr_cleanup, &pongo_thr_running);
2727
wait_for_pongo();
28-
while (get_spin()) {
29-
sleep(1);
30-
}
28+
palerain_block();
3129
pthread_cleanup_pop(1);
3230
return NULL;
3331
}
@@ -141,7 +139,7 @@ void *pongo_usb_callback(stuff_t *arg) {
141139
#ifdef USE_LIBUSB
142140
libusb_unref_device(arg->dev);
143141
#endif
144-
set_spin(0);
142+
palerain_unblock();
145143
return NULL;
146144
}
147145

@@ -236,7 +234,7 @@ void io_start(stuff_t *stuff)
236234
if(r != 0)
237235
{
238236
ERR("pthread_create: %s", strerror(r));
239-
set_spin(0);
237+
palerain_unblock();
240238
return;
241239
}
242240
pthread_join(stuff->th, NULL);
@@ -248,14 +246,14 @@ void io_stop(stuff_t *stuff)
248246
if(r != 0)
249247
{
250248
ERR("pthread_cancel: %s", strerror(r));
251-
set_spin(0);
249+
palerain_unblock();
252250
return;
253251
}
254252
r = pthread_join(stuff->th, NULL);
255253
if(r != 0)
256254
{
257255
ERR("pthread_join: %s", strerror(r));
258-
set_spin(0);
256+
palerain_unblock();
259257
return;
260258
}
261259
#ifdef USE_LIBUSB

src/usb_iokit.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,7 @@ int wait_for_pongo(void) {
245245
}
246246
LostDevice(&stuff, lost);
247247
// CFRunLoopRun();
248-
while (get_spin()) {
249-
sleep(1);
250-
}
248+
palerain_block();
251249
CFRelease(cfdict);
252250
return 0;
253251
}

0 commit comments

Comments
 (0)