From b2924a1549d874fb86deaad5fd2438e567c65bf4 Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Wed, 20 Mar 2013 05:38:06 +0100 Subject: use new internal common code for thread, mutex, and socket operations --- dev/afccheck.c | 10 +++++----- src/Makefile.am | 2 +- src/afc.c | 24 ++++------------------- src/afc.h | 12 ++---------- src/installation_proxy.c | 46 ++++++------------------------------------- src/installation_proxy.h | 16 +++------------ src/mobile_image_mounter.c | 24 ++++------------------- src/mobile_image_mounter.h | 13 ++---------- src/notification_proxy.c | 49 +++++++--------------------------------------- src/notification_proxy.h | 16 +++------------ src/sbservices.c | 24 ++++------------------- src/sbservices.h | 13 ++---------- tools/Makefile.am | 2 +- tools/idevicesyslog.c | 29 ++++++--------------------- 14 files changed, 50 insertions(+), 230 deletions(-) diff --git a/dev/afccheck.c b/dev/afccheck.c index 665aa47..3eb53c8 100644 --- a/dev/afccheck.c +++ b/dev/afccheck.c @@ -22,11 +22,11 @@ #include #include #include -#include #include #include #include +#include "common/thread.h" #define BUFFER_SIZE 20000 #define NB_THREADS 10 @@ -85,7 +85,7 @@ static void* check_afc(void *data) //cleanup afc_remove_path(((param *) data)->afc, path); - pthread_exit(0); + return NULL; } int main(int argc, char *argv[]) @@ -125,18 +125,18 @@ int main(int argc, char *argv[]) service = NULL; } - pthread_t threads[NB_THREADS]; + thread_t threads[NB_THREADS]; param data[NB_THREADS]; int i = 0; for (i = 0; i < NB_THREADS; i++) { data[i].afc = afc; data[i].id = i + 1; - pthread_create(&threads[i], NULL, check_afc, data + i); + thread_create(&threads[i], check_afc, data + i); } for (i = 0; i < NB_THREADS; i++) { - pthread_join(threads[i], NULL); + thread_join(threads[i]); } lockdownd_client_free(client); diff --git a/src/Makefile.am b/src/Makefile.am index 574075e..66be29f 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -4,7 +4,7 @@ AM_CFLAGS = $(GLOBAL_CFLAGS) $(libusbmuxd_CFLAGS) $(libgnutls_CFLAGS) $(libtasn1 AM_LDFLAGS = $(libgnutls_LIBS) $(libtasn1_LIBS) $(libplist_LIBS) $(libusbmuxd_LIBS) $(libgcrypt_LIBS) ${libpthread_LIBS} $(openssl_LIBS) lib_LTLIBRARIES = libimobiledevice.la -libimobiledevice_la_LIBADD = +libimobiledevice_la_LIBADD = $(top_srcdir)/common/libinternalcommon.la libimobiledevice_la_LDFLAGS = $(AM_LDFLAGS) -version-info $(LIBIMOBILEDEVICE_SO_VERSION) -no-undefined libimobiledevice_la_SOURCES = idevice.c idevice.h \ debug.c debug.h\ diff --git a/src/afc.c b/src/afc.c index c281e2c..f39562d 100644 --- a/src/afc.c +++ b/src/afc.c @@ -43,11 +43,7 @@ static const int MAXIMUM_PACKET_SIZE = (2 << 15); static void afc_lock(afc_client_t client) { debug_info("Locked"); -#ifdef WIN32 - EnterCriticalSection(&client->mutex); -#else - pthread_mutex_lock(&client->mutex); -#endif + mutex_lock(&client->mutex); } /** @@ -58,11 +54,7 @@ static void afc_lock(afc_client_t client) static void afc_unlock(afc_client_t client) { debug_info("Unlocked"); -#ifdef WIN32 - LeaveCriticalSection(&client->mutex); -#else - pthread_mutex_unlock(&client->mutex); -#endif + mutex_unlock(&client->mutex); } /** @@ -99,11 +91,7 @@ afc_error_t afc_client_new_with_service_client(service_client_t service_client, memcpy(client_loc->afc_packet->magic, AFC_MAGIC, AFC_MAGIC_LEN); client_loc->file_handle = 0; client_loc->lock = 0; -#ifdef WIN32 - InitializeCriticalSection(&client_loc->mutex); -#else - pthread_mutex_init(&client_loc->mutex, NULL); -#endif + mutex_init(&client_loc->mutex); *client = client_loc; return AFC_E_SUCCESS; @@ -156,11 +144,7 @@ afc_error_t afc_client_free(afc_client_t client) client->parent = NULL; } free(client->afc_packet); -#ifdef WIN32 - DeleteCriticalSection(&client->mutex); -#else - pthread_mutex_destroy(&client->mutex); -#endif + mutex_destroy(&client->mutex); free(client); return AFC_E_SUCCESS; } diff --git a/src/afc.h b/src/afc.h index 33662f2..ae122e2 100644 --- a/src/afc.h +++ b/src/afc.h @@ -23,15 +23,11 @@ #define __AFC_H #include -#ifdef WIN32 -#include -#else -#include -#endif #include "libimobiledevice/afc.h" #include "service.h" #include "endianness.h" +#include "common/thread.h" #define AFC_MAGIC "CFA6LPAA" #define AFC_MAGIC_LEN (8) @@ -62,11 +58,7 @@ struct afc_client_private { AFCPacket *afc_packet; int file_handle; int lock; -#ifdef WIN32 - CRITICAL_SECTION mutex; -#else - pthread_mutex_t mutex; -#endif + mutex_t mutex; int free_parent; }; diff --git a/src/installation_proxy.c b/src/installation_proxy.c index f8da91c..c49014d 100644 --- a/src/installation_proxy.c +++ b/src/installation_proxy.c @@ -43,11 +43,7 @@ struct instproxy_status_data { static void instproxy_lock(instproxy_client_t client) { debug_info("InstallationProxy: Locked"); -#ifdef WIN32 - EnterCriticalSection(&client->mutex); -#else - pthread_mutex_lock(&client->mutex); -#endif + mutex_lock(&client->mutex); } /** @@ -58,11 +54,7 @@ static void instproxy_lock(instproxy_client_t client) static void instproxy_unlock(instproxy_client_t client) { debug_info("InstallationProxy: Unlocked"); -#ifdef WIN32 - LeaveCriticalSection(&client->mutex); -#else - pthread_mutex_unlock(&client->mutex); -#endif + mutex_unlock(&client->mutex); } /** @@ -112,13 +104,8 @@ instproxy_error_t instproxy_client_new(idevice_t device, lockdownd_service_descr instproxy_client_t client_loc = (instproxy_client_t) malloc(sizeof(struct instproxy_client_private)); client_loc->parent = plistclient; -#ifdef WIN32 - InitializeCriticalSection(&client_loc->mutex); + mutex_init(&client_loc->mutex); client_loc->status_updater = NULL; -#else - pthread_mutex_init(&client_loc->mutex, NULL); - client_loc->status_updater = (pthread_t)NULL; -#endif *client = client_loc; return INSTPROXY_E_SUCCESS; @@ -142,17 +129,9 @@ instproxy_error_t instproxy_client_free(instproxy_client_t client) client->parent = NULL; if (client->status_updater) { debug_info("joining status_updater"); -#ifdef WIN32 - WaitForSingleObject(client->status_updater, INFINITE); -#else - pthread_join(client->status_updater, NULL); -#endif + thread_join(client->status_updater); } -#ifdef WIN32 - DeleteCriticalSection(&client->mutex); -#else - pthread_mutex_destroy(&client->mutex); -#endif + mutex_destroy(&client->mutex); free(client); return INSTPROXY_E_SUCCESS; @@ -375,11 +354,7 @@ static void* instproxy_status_updater(void* arg) if (data->operation) { free(data->operation); } -#ifdef WIN32 data->client->status_updater = NULL; -#else - data->client->status_updater = (pthread_t)NULL; -#endif instproxy_unlock(data->client); free(data); @@ -414,18 +389,9 @@ static instproxy_error_t instproxy_create_status_updater(instproxy_client_t clie data->operation = strdup(operation); data->user_data = user_data; -#ifdef WIN32 - client->status_updater = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)instproxy_status_updater, data, 0, NULL); - if (client->status_updater != INVALID_HANDLE_VALUE) { - res = INSTPROXY_E_SUCCESS; - } else { - client->status_updater = NULL; - } -#else - if (pthread_create(&client->status_updater, NULL, instproxy_status_updater, data) == 0) { + if (thread_create(&client->status_updater, instproxy_status_updater, data) == 0) { res = INSTPROXY_E_SUCCESS; } -#endif } } else { /* sync mode */ diff --git a/src/installation_proxy.h b/src/installation_proxy.h index 40175a0..4f79c12 100644 --- a/src/installation_proxy.h +++ b/src/installation_proxy.h @@ -22,24 +22,14 @@ #ifndef __INSTALLATION_PROXY_H #define __INSTALLATION_PROXY_H -#ifdef WIN32 -#include -#else -#include -#endif - #include "libimobiledevice/installation_proxy.h" #include "property_list_service.h" +#include "common/thread.h" struct instproxy_client_private { property_list_service_client_t parent; -#ifdef WIN32 - CRITICAL_SECTION mutex; - HANDLE status_updater; -#else - pthread_mutex_t mutex; - pthread_t status_updater; -#endif + mutex_t mutex; + thread_t status_updater; }; #endif diff --git a/src/mobile_image_mounter.c b/src/mobile_image_mounter.c index 1d608db..209e367 100644 --- a/src/mobile_image_mounter.c +++ b/src/mobile_image_mounter.c @@ -35,11 +35,7 @@ */ static void mobile_image_mounter_lock(mobile_image_mounter_client_t client) { -#ifdef WIN32 - EnterCriticalSection(&client->mutex); -#else - pthread_mutex_lock(&client->mutex); -#endif + mutex_lock(&client->mutex); } /** @@ -49,11 +45,7 @@ static void mobile_image_mounter_lock(mobile_image_mounter_client_t client) */ static void mobile_image_mounter_unlock(mobile_image_mounter_client_t client) { -#ifdef WIN32 - LeaveCriticalSection(&client->mutex); -#else - pthread_mutex_unlock(&client->mutex); -#endif + mutex_unlock(&client->mutex); } /** @@ -107,11 +99,7 @@ mobile_image_mounter_error_t mobile_image_mounter_new(idevice_t device, lockdown mobile_image_mounter_client_t client_loc = (mobile_image_mounter_client_t) malloc(sizeof(struct mobile_image_mounter_client_private)); client_loc->parent = plistclient; -#ifdef WIN32 - InitializeCriticalSection(&client_loc->mutex); -#else - pthread_mutex_init(&client_loc->mutex, NULL); -#endif + mutex_init(&client_loc->mutex); *client = client_loc; return MOBILE_IMAGE_MOUNTER_E_SUCCESS; @@ -133,11 +121,7 @@ mobile_image_mounter_error_t mobile_image_mounter_free(mobile_image_mounter_clie property_list_service_client_free(client->parent); client->parent = NULL; -#ifdef WIN32 - DeleteCriticalSection(&client->mutex); -#else - pthread_mutex_destroy(&client->mutex); -#endif + mutex_destroy(&client->mutex); free(client); return MOBILE_IMAGE_MOUNTER_E_SUCCESS; diff --git a/src/mobile_image_mounter.h b/src/mobile_image_mounter.h index 4056dc8..67cb589 100644 --- a/src/mobile_image_mounter.h +++ b/src/mobile_image_mounter.h @@ -22,22 +22,13 @@ #ifndef __MOBILE_IMAGE_MOUNTER_H #define __MOBILE_IMAGE_MOUNTER_H -#ifdef WIN32 -#include -#else -#include -#endif - #include "libimobiledevice/mobile_image_mounter.h" #include "property_list_service.h" +#include "common/thread.h" struct mobile_image_mounter_client_private { property_list_service_client_t parent; -#ifdef WIN32 - CRITICAL_SECTION mutex; -#else - pthread_mutex_t mutex; -#endif + mutex_t mutex; }; #endif diff --git a/src/notification_proxy.c b/src/notification_proxy.c index 5b293f8..952e5f2 100644 --- a/src/notification_proxy.c +++ b/src/notification_proxy.c @@ -46,11 +46,7 @@ struct np_thread { static void np_lock(np_client_t client) { debug_info("NP: Locked"); -#ifdef WIN32 - EnterCriticalSection(&client->mutex); -#else - pthread_mutex_lock(&client->mutex); -#endif + mutex_lock(&client->mutex); } /** @@ -61,11 +57,7 @@ static void np_lock(np_client_t client) static void np_unlock(np_client_t client) { debug_info("NP: Unlocked"); -#ifdef WIN32 - LeaveCriticalSection(&client->mutex); -#else - pthread_mutex_unlock(&client->mutex); -#endif + mutex_unlock(&client->mutex); } /** @@ -117,13 +109,8 @@ np_error_t np_client_new(idevice_t device, lockdownd_service_descriptor_t servic np_client_t client_loc = (np_client_t) malloc(sizeof(struct np_client_private)); client_loc->parent = plistclient; -#ifdef WIN32 - InitializeCriticalSection(&client_loc->mutex); + mutex_init(&client_loc->mutex); client_loc->notifier = NULL; -#else - pthread_mutex_init(&client_loc->mutex, NULL); - client_loc->notifier = (pthread_t)NULL; -#endif *client = client_loc; return NP_E_SUCCESS; @@ -146,17 +133,9 @@ np_error_t np_client_free(np_client_t client) client->parent = NULL; if (client->notifier) { debug_info("joining np callback"); -#ifdef WIN32 - WaitForSingleObject(client->notifier, INFINITE); -#else - pthread_join(client->notifier, NULL); -#endif + thread_join(client->notifier); } -#ifdef WIN32 - DeleteCriticalSection(&client->mutex); -#else - pthread_mutex_destroy(&client->mutex); -#endif + mutex_destroy(&client->mutex); free(client); return NP_E_SUCCESS; @@ -415,13 +394,8 @@ np_error_t np_set_notify_callback( np_client_t client, np_notify_cb_t notify_cb, debug_info("callback already set, removing\n"); property_list_service_client_t parent = client->parent; client->parent = NULL; -#ifdef WIN32 - WaitForSingleObject(client->notifier, INFINITE); + thread_join(client->notifier); client->notifier = NULL; -#else - pthread_join(client->notifier, NULL); - client->notifier = (pthread_t)NULL; -#endif client->parent = parent; } @@ -432,18 +406,9 @@ np_error_t np_set_notify_callback( np_client_t client, np_notify_cb_t notify_cb, npt->cbfunc = notify_cb; npt->user_data = user_data; -#ifdef WIN32 - client->notifier = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)np_notifier, npt, 0, NULL); - if (client->notifier != INVALID_HANDLE_VALUE) { - res = NP_E_SUCCESS; - } else { - client->notifier = NULL; - } -#else - if (pthread_create(&client->notifier, NULL, np_notifier, npt) == 0) { + if (thread_create(&client->notifier, np_notifier, npt) == 0) { res = NP_E_SUCCESS; } -#endif } } else { debug_info("no callback set"); diff --git a/src/notification_proxy.h b/src/notification_proxy.h index d8e9915..c2ded6a 100644 --- a/src/notification_proxy.h +++ b/src/notification_proxy.h @@ -22,24 +22,14 @@ #ifndef __NOTIFICATION_PROXY_H #define __NOTIFICATION_PROXY_H -#ifdef WIN32 -#include -#else -#include -#endif - #include "libimobiledevice/notification_proxy.h" #include "property_list_service.h" +#include "common/thread.h" struct np_client_private { property_list_service_client_t parent; -#ifdef WIN32 - CRITICAL_SECTION mutex; - HANDLE notifier; -#else - pthread_mutex_t mutex; - pthread_t notifier; -#endif + mutex_t mutex; + thread_t notifier; }; void* np_notifier(void* arg); diff --git a/src/sbservices.c b/src/sbservices.c index 00f2862..1cd17db 100644 --- a/src/sbservices.c +++ b/src/sbservices.c @@ -36,11 +36,7 @@ static void sbs_lock(sbservices_client_t client) { debug_info("SBServices: Locked"); -#ifdef WIN32 - EnterCriticalSection(&client->mutex); -#else - pthread_mutex_lock(&client->mutex); -#endif + mutex_lock(&client->mutex); } /** @@ -51,11 +47,7 @@ static void sbs_lock(sbservices_client_t client) static void sbs_unlock(sbservices_client_t client) { debug_info("SBServices: Unlocked"); -#ifdef WIN32 - LeaveCriticalSection(&client->mutex); -#else - pthread_mutex_unlock(&client->mutex); -#endif + mutex_unlock(&client->mutex); } /** @@ -105,11 +97,7 @@ sbservices_error_t sbservices_client_new(idevice_t device, lockdownd_service_des sbservices_client_t client_loc = (sbservices_client_t) malloc(sizeof(struct sbservices_client_private)); client_loc->parent = plistclient; -#ifdef WIN32 - InitializeCriticalSection(&client_loc->mutex); -#else - pthread_mutex_init(&client_loc->mutex, NULL); -#endif + mutex_init(&client_loc->mutex); *client = client_loc; return SBSERVICES_E_SUCCESS; @@ -131,11 +119,7 @@ sbservices_error_t sbservices_client_free(sbservices_client_t client) sbservices_error_t err = sbservices_error(property_list_service_client_free(client->parent)); client->parent = NULL; -#ifdef WIN32 - DeleteCriticalSection(&client->mutex); -#else - pthread_mutex_destroy(&client->mutex); -#endif + mutex_destroy(&client->mutex); free(client); return err; diff --git a/src/sbservices.h b/src/sbservices.h index 598b794..ba64d67 100644 --- a/src/sbservices.h +++ b/src/sbservices.h @@ -22,22 +22,13 @@ #ifndef __SBSERVICES_H #define __SBSERVICES_H -#ifdef WIN32 -#include -#else -#include -#endif - #include "libimobiledevice/sbservices.h" #include "property_list_service.h" +#include "common/thread.h" struct sbservices_client_private { property_list_service_client_t parent; -#ifdef WIN32 - CRITICAL_SECTION mutex; -#else - pthread_mutex_t mutex; -#endif + mutex_t mutex; }; #endif diff --git a/tools/Makefile.am b/tools/Makefile.am index 57445d8..e668592 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -18,7 +18,7 @@ idevicepair_LDADD = ../src/libimobiledevice.la idevicesyslog_SOURCES = idevicesyslog.c idevicesyslog_CFLAGS = $(AM_CFLAGS) idevicesyslog_LDFLAGS = $(AM_LDFLAGS) -idevicesyslog_LDADD = ../src/libimobiledevice.la +idevicesyslog_LDADD = $(top_srcdir)/common/libinternalcommon.la ../src/libimobiledevice.la idevice_id_SOURCES = idevice_id.c idevice_id_CFLAGS = $(AM_CFLAGS) diff --git a/tools/idevicesyslog.c b/tools/idevicesyslog.c index 32163e6..3dd2257 100644 --- a/tools/idevicesyslog.c +++ b/tools/idevicesyslog.c @@ -29,13 +29,13 @@ #ifdef WIN32 #include #define sleep(x) Sleep(x*1000) -#else -#include #endif #include #include -#include "../src/service.h" + +#include "src/service.h" +#include "common/thread.h" static int quit_flag = 0; @@ -46,11 +46,7 @@ static char* udid = NULL; static idevice_t device = NULL; static service_client_t syslog = NULL; -#ifdef WIN32 -HANDLE worker = NULL; -#else -pthread_t worker; -#endif +thread_t worker = NULL; static int logging = 0; @@ -100,19 +96,10 @@ static int start_logging() /* start worker thread */ logging = 1; -#ifdef WIN32 - worker = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)syslog_worker, NULL, 0, NULL); - if (worker == INVALID_HANDLE_VALUE) { + if (thread_create(&worker, syslog_worker, NULL) != 0) { logging = 0; return -1; } -#else - if (pthread_create(&worker, NULL, syslog_worker, NULL) != 0) { - logging = 0; - return -1; - } -#endif - return 0; } @@ -127,11 +114,7 @@ static void stop_logging() } /* wait for thread to complete */ -#ifdef WIN32 - WaitForSingleObject(worker, INFINITE); -#else - pthread_join(worker, NULL); -#endif + thread_join(worker); } if (device) { -- cgit v1.1-32-gdbae