summaryrefslogtreecommitdiffstats
path: root/src/thread.c
diff options
context:
space:
mode:
authorGravatar Nikias Bassen2019-10-11 23:08:48 +0200
committerGravatar Nikias Bassen2019-10-11 23:10:07 +0200
commit767fcceaeb456f5e281a6bd1b3f51713e4989293 (patch)
treed9a01fbea3c1a85f41ca0f033037737739a14629 /src/thread.c
parent75a61508bd3c2013492744fb5aea755e19b01086 (diff)
downloadidevicerestore-767fcceaeb456f5e281a6bd1b3f51713e4989293.tar.gz
idevicerestore-767fcceaeb456f5e281a6bd1b3f51713e4989293.tar.bz2
Use condition variable instead of active waiting for device event handling
With some devices and USB hardware the reconnect of a device might actually be faster than the check interval of the active waiting loop. With mutexes and a condition variable we will not miss the moment of reconnect anymore, even if it is really quick (like 7ms, right DanyL?)
Diffstat (limited to 'src/thread.c')
-rw-r--r--src/thread.c60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/thread.c b/src/thread.c
index b84ee66..8ccdd7f 100644
--- a/src/thread.c
+++ b/src/thread.c
@@ -115,3 +115,63 @@ void thread_once(thread_once_t *once_control, void (*init_routine)(void))
pthread_once(once_control, init_routine);
#endif
}
+
+void cond_init(cond_t* cond)
+{
+#ifdef WIN32
+ cond->sem = CreateSemaphore(NULL, 0, 32767, NULL);
+#else
+ pthread_cond_init(cond, NULL);
+#endif
+}
+
+void cond_destroy(cond_t* cond)
+{
+#ifdef WIN32
+ CloseHandle(cond->sem);
+#else
+ pthread_cond_destroy(cond);
+#endif
+}
+
+int cond_signal(cond_t* cond)
+{
+#ifdef WIN32
+ int result = 0;
+ if (!ReleaseSemaphore(cond->sem, 1, NULL)) {
+ result = -1;
+ }
+ return result;
+#else
+ return pthread_cond_signal(cond);
+#endif
+}
+
+int cond_wait(cond_t* cond, mutex_t* mutex)
+{
+#ifdef WIN32
+ mutex_unlock(mutex);
+ WaitForSingleObject(cond->sem, INFINITE);
+#else
+ return pthread_cond_wait(cond, mutex);
+#endif
+}
+
+int cond_wait_timeout(cond_t* cond, mutex_t* mutex, unsigned int timeout_ms)
+{
+#ifdef WIN32
+ mutex_unlock(mutex);
+ WaitForSingleObject(cond->sem, timeout_ms);
+#else
+ struct timespec ts;
+ struct timeval now;
+ gettimeofday(&now, NULL);
+
+ ts.tv_sec = now.tv_sec + timeout_ms / 1000;
+ ts.tv_nsec = now.tv_usec * 1000 + 1000 * 1000 * (timeout_ms % 1000);
+ ts.tv_sec += ts.tv_nsec / (1000 * 1000 * 1000);
+ ts.tv_nsec %= (1000 * 1000 * 1000);
+
+ return pthread_cond_timedwait(cond, mutex, &ts);
+#endif
+}