diff options
-rw-r--r-- | common/thread.c | 48 | ||||
-rw-r--r-- | common/thread.h | 33 | ||||
-rw-r--r-- | src/installation_proxy.c | 11 | ||||
-rw-r--r-- | src/installation_proxy.h | 2 | ||||
-rw-r--r-- | src/notification_proxy.c | 6 | ||||
-rw-r--r-- | src/notification_proxy.h | 2 | ||||
-rw-r--r-- | src/syslog_relay.c | 8 | ||||
-rw-r--r-- | src/syslog_relay.h | 2 | ||||
-rw-r--r-- | tools/idevicedebugserverproxy.c | 6 |
9 files changed, 87 insertions, 31 deletions
diff --git a/common/thread.c b/common/thread.c index fdc8112..eb535ab 100644 --- a/common/thread.c +++ b/common/thread.c @@ -1,8 +1,8 @@ /* * thread.c * - * Copyright (c) 2012 Martin Szulecki All Rights Reserved. - * Copyright (c) 2012 Nikias Bassen All Rights Reserved. + * Copyright (c) 2012-2019 Nikias Bassen, All Rights Reserved. + * Copyright (c) 2012 Martin Szulecki, All Rights Reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -19,9 +19,12 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif #include "thread.h" -int thread_new(thread_t *thread, thread_func_t thread_func, void* data) +int thread_new(THREAD_T *thread, thread_func_t thread_func, void* data) { #ifdef WIN32 HANDLE th = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)thread_func, data, 0, NULL); @@ -36,20 +39,51 @@ int thread_new(thread_t *thread, thread_func_t thread_func, void* data) #endif } -void thread_free(thread_t thread) +void thread_detach(THREAD_T thread) { #ifdef WIN32 CloseHandle(thread); +#else + pthread_detach(thread); #endif } -void thread_join(thread_t thread) +void thread_free(THREAD_T thread) +{ +#ifdef WIN32 + CloseHandle(thread); +#endif +} + +int thread_join(THREAD_T thread) { /* wait for thread to complete */ #ifdef WIN32 - WaitForSingleObject(thread, INFINITE); + return (int)WaitForSingleObject(thread, INFINITE); +#else + return pthread_join(thread, NULL); +#endif +} + +int thread_alive(THREAD_T thread) +{ +#ifdef WIN32 + return WaitForSingleObject(thread, 0) == WAIT_TIMEOUT; #else - pthread_join(thread, NULL); + return pthread_kill(thread, 0) == 0; +#endif +} + +int thread_cancel(THREAD_T thread) +{ +#ifdef WIN32 + return -1; +#else +#ifdef HAVE_PTHREAD_CANCEL + return pthread_cancel(thread); +#else + return -1; +#endif #endif } diff --git a/common/thread.h b/common/thread.h index bd53c5b..23e4510 100644 --- a/common/thread.h +++ b/common/thread.h @@ -1,8 +1,8 @@ /* * thread.h * - * Copyright (c) 2012 Martin Szulecki All Rights Reserved. - * Copyright (c) 2012 Nikias Bassen All Rights Reserved. + * Copyright (c) 2012-2019 Nikias Bassen, All Rights Reserved. + * Copyright (c) 2012 Martin Szulecki, All Rights Reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -22,9 +22,11 @@ #ifndef __THREAD_H #define __THREAD_H +#include <stddef.h> + #ifdef WIN32 #include <windows.h> -typedef HANDLE thread_t; +typedef HANDLE THREAD_T; typedef CRITICAL_SECTION mutex_t; typedef volatile struct { LONG lock; @@ -32,20 +34,37 @@ typedef volatile struct { } thread_once_t; #define THREAD_ONCE_INIT {0, 0} #define THREAD_ID GetCurrentThreadId() +#define THREAD_T_NULL (THREAD_T)NULL #else #include <pthread.h> -typedef pthread_t thread_t; +#include <signal.h> +typedef pthread_t THREAD_T; typedef pthread_mutex_t mutex_t; typedef pthread_once_t thread_once_t; #define THREAD_ONCE_INIT PTHREAD_ONCE_INIT #define THREAD_ID pthread_self() +#define THREAD_T_NULL (THREAD_T)NULL #endif typedef void* (*thread_func_t)(void* data); -int thread_new(thread_t* thread, thread_func_t thread_func, void* data); -void thread_free(thread_t thread); -void thread_join(thread_t thread); +int thread_new(THREAD_T* thread, thread_func_t thread_func, void* data); +void thread_detach(THREAD_T thread); +void thread_free(THREAD_T thread); +int thread_join(THREAD_T thread); +int thread_alive(THREAD_T thread); + +int thread_cancel(THREAD_T thread); + +#ifdef WIN32 +#undef HAVE_THREAD_CLEANUP +#else +#ifdef HAVE_PTHREAD_CANCEL +#define HAVE_THREAD_CLEANUP 1 +#define thread_cleanup_push(routine, arg) pthread_cleanup_push(routine, arg) +#define thread_cleanup_pop(execute) pthread_cleanup_pop(execute) +#endif +#endif void mutex_init(mutex_t* mutex); void mutex_destroy(mutex_t* mutex); diff --git a/src/installation_proxy.c b/src/installation_proxy.c index f82eecc..24044aa 100644 --- a/src/installation_proxy.c +++ b/src/installation_proxy.c @@ -20,6 +20,9 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif #include <string.h> #include <stdlib.h> #include <inttypes.h> @@ -240,7 +243,7 @@ LIBIMOBILEDEVICE_API instproxy_error_t instproxy_client_new(idevice_t device, lo instproxy_client_t client_loc = (instproxy_client_t) malloc(sizeof(struct instproxy_client_private)); client_loc->parent = plistclient; mutex_init(&client_loc->mutex); - client_loc->receive_status_thread = (thread_t)NULL; + client_loc->receive_status_thread = THREAD_T_NULL; *client = client_loc; return INSTPROXY_E_SUCCESS; @@ -264,7 +267,7 @@ LIBIMOBILEDEVICE_API instproxy_error_t instproxy_client_free(instproxy_client_t debug_info("joining receive_status_thread"); thread_join(client->receive_status_thread); thread_free(client->receive_status_thread); - client->receive_status_thread = (thread_t)NULL; + client->receive_status_thread = THREAD_T_NULL; } mutex_destroy(&client->mutex); free(client); @@ -343,7 +346,7 @@ static instproxy_error_t instproxy_receive_status_loop(instproxy_client_t client /* parse status response */ if (node) { - /* check status for possible errorĀ to allow reporting it and aborting it gracefully */ + /* check status for possible error to allow reporting it and aborting it gracefully */ res = instproxy_status_get_error(node, &error_name, &error_description, &error_code); if (res != INSTPROXY_E_SUCCESS) { debug_info("command: %s, error %d, code 0x%08"PRIx64", name: %s, description: \"%s\"", command_name, res, error_code, error_name, error_description ? error_description: "N/A"); @@ -431,7 +434,7 @@ static void* instproxy_receive_status_loop_thread(void* arg) if (data->client->receive_status_thread) { thread_free(data->client->receive_status_thread); - data->client->receive_status_thread = (thread_t)NULL; + data->client->receive_status_thread = THREAD_T_NULL; } instproxy_unlock(data->client); diff --git a/src/installation_proxy.h b/src/installation_proxy.h index bbc14ce..66dd5d0 100644 --- a/src/installation_proxy.h +++ b/src/installation_proxy.h @@ -30,7 +30,7 @@ struct instproxy_client_private { property_list_service_client_t parent; mutex_t mutex; - thread_t receive_status_thread; + THREAD_T receive_status_thread; }; #endif diff --git a/src/notification_proxy.c b/src/notification_proxy.c index c0b216e..3015ed9 100644 --- a/src/notification_proxy.c +++ b/src/notification_proxy.c @@ -98,7 +98,7 @@ LIBIMOBILEDEVICE_API np_error_t np_client_new(idevice_t device, lockdownd_servic client_loc->parent = plistclient; mutex_init(&client_loc->mutex); - client_loc->notifier = (thread_t)NULL; + client_loc->notifier = THREAD_T_NULL; *client = client_loc; return NP_E_SUCCESS; @@ -132,7 +132,7 @@ LIBIMOBILEDEVICE_API np_error_t np_client_free(np_client_t client) debug_info("joining np callback"); thread_join(client->notifier); thread_free(client->notifier); - client->notifier = (thread_t)NULL; + client->notifier = THREAD_T_NULL; } else { dict = NULL; property_list_service_receive_plist(parent, &dict); @@ -350,7 +350,7 @@ LIBIMOBILEDEVICE_API np_error_t np_set_notify_callback( np_client_t client, np_n client->parent = NULL; thread_join(client->notifier); thread_free(client->notifier); - client->notifier = (thread_t)NULL; + client->notifier = THREAD_T_NULL; client->parent = parent; } diff --git a/src/notification_proxy.h b/src/notification_proxy.h index cc25a95..f641e25 100644 --- a/src/notification_proxy.h +++ b/src/notification_proxy.h @@ -29,7 +29,7 @@ struct np_client_private { property_list_service_client_t parent; mutex_t mutex; - thread_t notifier; + THREAD_T notifier; }; void* np_notifier(void* arg); diff --git a/src/syslog_relay.c b/src/syslog_relay.c index 29f4de5..3be84e0 100644 --- a/src/syslog_relay.c +++ b/src/syslog_relay.c @@ -81,7 +81,7 @@ LIBIMOBILEDEVICE_API syslog_relay_error_t syslog_relay_client_new(idevice_t devi syslog_relay_client_t client_loc = (syslog_relay_client_t) malloc(sizeof(struct syslog_relay_client_private)); client_loc->parent = parent; - client_loc->worker = (thread_t)NULL; + client_loc->worker = THREAD_T_NULL; *client = client_loc; @@ -107,7 +107,7 @@ LIBIMOBILEDEVICE_API syslog_relay_error_t syslog_relay_client_free(syslog_relay_ debug_info("Joining syslog capture callback worker thread"); thread_join(client->worker); thread_free(client->worker); - client->worker = (thread_t)NULL; + client->worker = THREAD_T_NULL; } free(client); @@ -209,9 +209,9 @@ LIBIMOBILEDEVICE_API syslog_relay_error_t syslog_relay_stop_capture(syslog_relay /* join thread to make it exit */ thread_join(client->worker); thread_free(client->worker); - client->worker = (thread_t)NULL; + client->worker = THREAD_T_NULL; client->parent = parent; } return SYSLOG_RELAY_E_SUCCESS; -}
\ No newline at end of file +} diff --git a/src/syslog_relay.h b/src/syslog_relay.h index cd45775..3e48fa4 100644 --- a/src/syslog_relay.h +++ b/src/syslog_relay.h @@ -28,7 +28,7 @@ struct syslog_relay_client_private { service_client_t parent; - thread_t worker; + THREAD_T worker; }; void *syslog_relay_worker(void *arg); diff --git a/tools/idevicedebugserverproxy.c b/tools/idevicedebugserverproxy.c index 0b0419b..e99d0bf 100644 --- a/tools/idevicedebugserverproxy.c +++ b/tools/idevicedebugserverproxy.c @@ -50,7 +50,7 @@ typedef struct { } socket_info_t; struct thread_info { - thread_t th; + THREAD_T th; struct thread_info *next; }; @@ -140,7 +140,7 @@ static void *thread_client_to_device(void *data) int recv_len; int sent; char buffer[131072]; - thread_t dtoc; + THREAD_T dtoc; debug("%s: started thread...\n", __func__); @@ -205,7 +205,7 @@ static void* connection_handler(void* data) { debugserver_error_t derr = DEBUGSERVER_E_SUCCESS; socket_info_t* socket_info = (socket_info_t*)data; - thread_t ctod; + THREAD_T ctod; debug("%s: client_fd = %d\n", __func__, socket_info->client_fd); |