summaryrefslogtreecommitdiffstats
path: root/libusbmuxd/libusbmuxd.c
diff options
context:
space:
mode:
Diffstat (limited to 'libusbmuxd/libusbmuxd.c')
-rw-r--r--libusbmuxd/libusbmuxd.c98
1 files changed, 44 insertions, 54 deletions
diff --git a/libusbmuxd/libusbmuxd.c b/libusbmuxd/libusbmuxd.c
index 952e680..904f1ed 100644
--- a/libusbmuxd/libusbmuxd.c
+++ b/libusbmuxd/libusbmuxd.c
@@ -35,7 +35,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#else
#include <sys/socket.h>
#include <arpa/inet.h>
-#include <pthread.h>
#endif
#ifdef HAVE_INOTIFY
@@ -62,6 +61,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#include "usbmuxd-proto.h"
// socket utility functions
#include "sock_stuff.h"
+// threads and mutexes
+#include "thread.h"
// misc utility functions
#include "utils.h"
@@ -75,23 +76,25 @@ static int handle_events = 0;
static usbmuxd_event_cb_t event_cb = NULL;
static void* event_user_data = NULL;
-#ifdef WIN32
-HANDLE devmon = NULL;
-CRITICAL_SECTION mutex;
-static int mutex_initialized = 0;
-#define LOCK if (!mutex_initialized) { InitializeCriticalSection(&mutex); mutex_initialized = 1; } EnterCriticalSection(&mutex);
-#define UNLOCK LeaveCriticalSection(&mutex);
-#else
-pthread_t devmon = 0;
-pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
-#define LOCK pthread_mutex_lock(&mutex)
-#define UNLOCK pthread_mutex_unlock(&mutex)
-#endif
+
+static thread_t devmon = NULL;
+static mutex_t mutex;
+
static int listenfd = -1;
static int use_tag = 0;
static int proto_version = 0;
+static __attribute__((constructor)) void libusbmuxd_library_init()
+{
+ mutex_init(&mutex);
+}
+
+static __attribute__((destructor)) void libusbmuxd_library_deinit()
+{
+ mutex_destroy(&mutex);
+}
+
/**
* Finds a device info record by its handle.
* if the record is not found, NULL is returned.
@@ -510,15 +513,15 @@ retry:
}
use_tag++;
- LOCK;
+ mutex_lock(&mutex);
if (send_listen_packet(sfd, use_tag) <= 0) {
- UNLOCK;
+ mutex_unlock(&mutex);
DEBUG(1, "%s: ERROR: could not send listen packet\n", __func__);
close_socket(sfd);
return -1;
}
if (usbmuxd_get_result(sfd, use_tag, &res) && (res != 0)) {
- UNLOCK;
+ mutex_unlock(&mutex);
close_socket(sfd);
#ifdef HAVE_PLIST
if ((res == RESULT_BADVERSION) && (proto_version != 1)) {
@@ -529,7 +532,7 @@ retry:
DEBUG(1, "%s: ERROR: did not get OK but %d\n", __func__, res);
return -1;
}
- UNLOCK;
+ mutex_unlock(&mutex);
return sfd;
}
@@ -621,8 +624,8 @@ static void *device_monitor(void *data)
{
collection_init(&devices);
-#ifndef WIN32
- pthread_cleanup_push(device_monitor_cleanup, NULL);
+#ifdef THREAD_CLEANUP_SUPPORTED
+ thread_cleanup_push(device_monitor_cleanup, NULL);
#endif
while (handle_events) {
@@ -639,8 +642,8 @@ static void *device_monitor(void *data)
}
}
-#ifndef WIN32
- pthread_cleanup_pop(1);
+#ifdef THREAD_CLEANUP_SUPPORTED
+ thread_cleanup_pop(1);
#else
device_monitor_cleanup(NULL);
#endif
@@ -650,48 +653,35 @@ static void *device_monitor(void *data)
int usbmuxd_init()
{
int res = 0;
- LOCK;
+ mutex_lock(&mutex);
handle_events = 1;
-#ifdef WIN32
- devmon = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)device_monitor, NULL, 0, NULL);
- if (devmon == NULL) {
- res = GetLastError();
- }
-#else
- res = pthread_create(&devmon, NULL, device_monitor, NULL);
-#endif
+ res = thread_create(&devmon, device_monitor, NULL);
if (res != 0) {
DEBUG(1, "%s: ERROR: Could not start device watcher thread!\n", __func__);
handle_events = 0;
- UNLOCK;
+ mutex_unlock(&mutex);
return res;
}
struct timespec ts = {0, 250000000};
nanosleep(&ts, NULL);
libusbmuxd_initialized = 1;
- UNLOCK;
+ mutex_unlock(&mutex);
return 0;
}
int usbmuxd_deinit()
{
- LOCK;
+ mutex_lock(&mutex);
handle_events = 0;
shutdown_socket(listenfd, SHUT_RDWR);
-#ifdef WIN32
- if (devmon != NULL) {
- WaitForSingleObject(devmon, INFINITE);
- }
-#else
- if (pthread_kill(devmon, 0) == 0) {
- pthread_cancel(devmon);
- pthread_join(devmon, NULL);
+ if (thread_valid(devmon)) {
+ thread_cancel(devmon);
+ thread_join(devmon);
}
-#endif
libusbmuxd_initialized = 0;
- UNLOCK;
+ mutex_unlock(&mutex);
return 0;
}
@@ -704,9 +694,9 @@ int usbmuxd_subscribe(usbmuxd_event_cb_t callback, void *user_data)
event_user_data = user_data;
int is_initialized = 0;
- LOCK;
+ mutex_lock(&mutex);
is_initialized = libusbmuxd_initialized;
- UNLOCK;
+ mutex_unlock(&mutex);
if (!is_initialized) {
usbmuxd_init();
@@ -751,14 +741,14 @@ retry:
}
use_tag++;
- //LOCK;
+ //mutex_lock(&mutex);
if (send_listen_packet(sfd, use_tag) > 0) {
res = -1;
// get response
if (usbmuxd_get_result(sfd, use_tag, &res) && (res == 0)) {
listen_success = 1;
} else {
- //UNLOCK;
+ //mutex_unlock(&mutex);
close_socket(sfd);
#ifdef HAVE_PLIST
if ((res == RESULT_BADVERSION) && (proto_version != 1)) {
@@ -766,13 +756,13 @@ retry:
goto retry;
}
#endif
- DEBUG(1, "%s: Did not get response to scan request (with result=0)...\n", __func__);
- return res;
+ DEBUG(1, "%s: Did not get response to scan request with result 0, instead got result %d ...\n", __func__, res);
+ return -1;
}
}
if (!listen_success) {
- //UNLOCK;
+ //mutex_unlock(&mutex);
DEBUG(1, "%s: Could not send listen request!\n", __func__);
return -1;
}
@@ -786,7 +776,7 @@ retry:
dev = payload;
usbmuxd_device_info_t *devinfo = (usbmuxd_device_info_t*)malloc(sizeof(usbmuxd_device_info_t));
if (!devinfo) {
- //UNLOCK;
+ //mutex_unlock(&mutex);
DEBUG(1, "%s: Out of memory!\n", __func__);
free(payload);
return -1;
@@ -830,7 +820,7 @@ retry:
break;
}
}
- //UNLOCK;
+ //mutex_unlock(&mutex);
// explicitly close connection
close_socket(sfd);
@@ -871,9 +861,9 @@ int usbmuxd_get_device_by_udid(const char *udid, usbmuxd_device_info_t *device)
}
int is_initialized = 0;
- LOCK;
+ mutex_lock(&mutex);
is_initialized = libusbmuxd_initialized;
- UNLOCK;
+ mutex_unlock(&mutex);
if (!is_initialized) {
usbmuxd_init();