summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Nikias Bassen2019-06-11 01:12:49 +0200
committerGravatar Nikias Bassen2019-06-11 01:12:49 +0200
commit59adbacef6d400d4c6458f26daddda24bcdfd635 (patch)
treefab41dc53822655f4be988017be4031be64cac33
parent34444782e22ebfd2abc9e5e9c27e170a839ff66b (diff)
downloadlibimobiledevice-59adbacef6d400d4c6458f26daddda24bcdfd635.tar.gz
libimobiledevice-59adbacef6d400d4c6458f26daddda24bcdfd635.tar.bz2
common: Update thread.c/.h to match the one from libusbmuxd
-rw-r--r--common/thread.c48
-rw-r--r--common/thread.h33
-rw-r--r--src/installation_proxy.c11
-rw-r--r--src/installation_proxy.h2
-rw-r--r--src/notification_proxy.c6
-rw-r--r--src/notification_proxy.h2
-rw-r--r--src/syslog_relay.c8
-rw-r--r--src/syslog_relay.h2
-rw-r--r--tools/idevicedebugserverproxy.c6
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 @@
1/* 1/*
2 * thread.c 2 * thread.c
3 * 3 *
4 * Copyright (c) 2012 Martin Szulecki All Rights Reserved. 4 * Copyright (c) 2012-2019 Nikias Bassen, All Rights Reserved.
5 * Copyright (c) 2012 Nikias Bassen All Rights Reserved. 5 * Copyright (c) 2012 Martin Szulecki, All Rights Reserved.
6 * 6 *
7 * This library is free software; you can redistribute it and/or 7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public 8 * modify it under the terms of the GNU Lesser General Public
@@ -19,9 +19,12 @@
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */ 20 */
21 21
22#ifdef HAVE_CONFIG_H
23#include <config.h>
24#endif
22#include "thread.h" 25#include "thread.h"
23 26
24int thread_new(thread_t *thread, thread_func_t thread_func, void* data) 27int thread_new(THREAD_T *thread, thread_func_t thread_func, void* data)
25{ 28{
26#ifdef WIN32 29#ifdef WIN32
27 HANDLE th = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)thread_func, data, 0, NULL); 30 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)
36#endif 39#endif
37} 40}
38 41
39void thread_free(thread_t thread) 42void thread_detach(THREAD_T thread)
40{ 43{
41#ifdef WIN32 44#ifdef WIN32
42 CloseHandle(thread); 45 CloseHandle(thread);
46#else
47 pthread_detach(thread);
43#endif 48#endif
44} 49}
45 50
46void thread_join(thread_t thread) 51void thread_free(THREAD_T thread)
52{
53#ifdef WIN32
54 CloseHandle(thread);
55#endif
56}
57
58int thread_join(THREAD_T thread)
47{ 59{
48 /* wait for thread to complete */ 60 /* wait for thread to complete */
49#ifdef WIN32 61#ifdef WIN32
50 WaitForSingleObject(thread, INFINITE); 62 return (int)WaitForSingleObject(thread, INFINITE);
63#else
64 return pthread_join(thread, NULL);
65#endif
66}
67
68int thread_alive(THREAD_T thread)
69{
70#ifdef WIN32
71 return WaitForSingleObject(thread, 0) == WAIT_TIMEOUT;
51#else 72#else
52 pthread_join(thread, NULL); 73 return pthread_kill(thread, 0) == 0;
74#endif
75}
76
77int thread_cancel(THREAD_T thread)
78{
79#ifdef WIN32
80 return -1;
81#else
82#ifdef HAVE_PTHREAD_CANCEL
83 return pthread_cancel(thread);
84#else
85 return -1;
86#endif
53#endif 87#endif
54} 88}
55 89
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 @@
1/* 1/*
2 * thread.h 2 * thread.h
3 * 3 *
4 * Copyright (c) 2012 Martin Szulecki All Rights Reserved. 4 * Copyright (c) 2012-2019 Nikias Bassen, All Rights Reserved.
5 * Copyright (c) 2012 Nikias Bassen All Rights Reserved. 5 * Copyright (c) 2012 Martin Szulecki, All Rights Reserved.
6 * 6 *
7 * This library is free software; you can redistribute it and/or 7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public 8 * modify it under the terms of the GNU Lesser General Public
@@ -22,9 +22,11 @@
22#ifndef __THREAD_H 22#ifndef __THREAD_H
23#define __THREAD_H 23#define __THREAD_H
24 24
25#include <stddef.h>
26
25#ifdef WIN32 27#ifdef WIN32
26#include <windows.h> 28#include <windows.h>
27typedef HANDLE thread_t; 29typedef HANDLE THREAD_T;
28typedef CRITICAL_SECTION mutex_t; 30typedef CRITICAL_SECTION mutex_t;
29typedef volatile struct { 31typedef volatile struct {
30 LONG lock; 32 LONG lock;
@@ -32,20 +34,37 @@ typedef volatile struct {
32} thread_once_t; 34} thread_once_t;
33#define THREAD_ONCE_INIT {0, 0} 35#define THREAD_ONCE_INIT {0, 0}
34#define THREAD_ID GetCurrentThreadId() 36#define THREAD_ID GetCurrentThreadId()
37#define THREAD_T_NULL (THREAD_T)NULL
35#else 38#else
36#include <pthread.h> 39#include <pthread.h>
37typedef pthread_t thread_t; 40#include <signal.h>
41typedef pthread_t THREAD_T;
38typedef pthread_mutex_t mutex_t; 42typedef pthread_mutex_t mutex_t;
39typedef pthread_once_t thread_once_t; 43typedef pthread_once_t thread_once_t;
40#define THREAD_ONCE_INIT PTHREAD_ONCE_INIT 44#define THREAD_ONCE_INIT PTHREAD_ONCE_INIT
41#define THREAD_ID pthread_self() 45#define THREAD_ID pthread_self()
46#define THREAD_T_NULL (THREAD_T)NULL
42#endif 47#endif
43 48
44typedef void* (*thread_func_t)(void* data); 49typedef void* (*thread_func_t)(void* data);
45 50
46int thread_new(thread_t* thread, thread_func_t thread_func, void* data); 51int thread_new(THREAD_T* thread, thread_func_t thread_func, void* data);
47void thread_free(thread_t thread); 52void thread_detach(THREAD_T thread);
48void thread_join(thread_t thread); 53void thread_free(THREAD_T thread);
54int thread_join(THREAD_T thread);
55int thread_alive(THREAD_T thread);
56
57int thread_cancel(THREAD_T thread);
58
59#ifdef WIN32
60#undef HAVE_THREAD_CLEANUP
61#else
62#ifdef HAVE_PTHREAD_CANCEL
63#define HAVE_THREAD_CLEANUP 1
64#define thread_cleanup_push(routine, arg) pthread_cleanup_push(routine, arg)
65#define thread_cleanup_pop(execute) pthread_cleanup_pop(execute)
66#endif
67#endif
49 68
50void mutex_init(mutex_t* mutex); 69void mutex_init(mutex_t* mutex);
51void mutex_destroy(mutex_t* mutex); 70void 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 @@
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */ 21 */
22 22
23#ifdef HAVE_CONFIG_H
24#include <config.h>
25#endif
23#include <string.h> 26#include <string.h>
24#include <stdlib.h> 27#include <stdlib.h>
25#include <inttypes.h> 28#include <inttypes.h>
@@ -240,7 +243,7 @@ LIBIMOBILEDEVICE_API instproxy_error_t instproxy_client_new(idevice_t device, lo
240 instproxy_client_t client_loc = (instproxy_client_t) malloc(sizeof(struct instproxy_client_private)); 243 instproxy_client_t client_loc = (instproxy_client_t) malloc(sizeof(struct instproxy_client_private));
241 client_loc->parent = plistclient; 244 client_loc->parent = plistclient;
242 mutex_init(&client_loc->mutex); 245 mutex_init(&client_loc->mutex);
243 client_loc->receive_status_thread = (thread_t)NULL; 246 client_loc->receive_status_thread = THREAD_T_NULL;
244 247
245 *client = client_loc; 248 *client = client_loc;
246 return INSTPROXY_E_SUCCESS; 249 return INSTPROXY_E_SUCCESS;
@@ -264,7 +267,7 @@ LIBIMOBILEDEVICE_API instproxy_error_t instproxy_client_free(instproxy_client_t
264 debug_info("joining receive_status_thread"); 267 debug_info("joining receive_status_thread");
265 thread_join(client->receive_status_thread); 268 thread_join(client->receive_status_thread);
266 thread_free(client->receive_status_thread); 269 thread_free(client->receive_status_thread);
267 client->receive_status_thread = (thread_t)NULL; 270 client->receive_status_thread = THREAD_T_NULL;
268 } 271 }
269 mutex_destroy(&client->mutex); 272 mutex_destroy(&client->mutex);
270 free(client); 273 free(client);
@@ -343,7 +346,7 @@ static instproxy_error_t instproxy_receive_status_loop(instproxy_client_t client
343 346
344 /* parse status response */ 347 /* parse status response */
345 if (node) { 348 if (node) {
346 /* check status for possible error to allow reporting it and aborting it gracefully */ 349 /* check status for possible error to allow reporting it and aborting it gracefully */
347 res = instproxy_status_get_error(node, &error_name, &error_description, &error_code); 350 res = instproxy_status_get_error(node, &error_name, &error_description, &error_code);
348 if (res != INSTPROXY_E_SUCCESS) { 351 if (res != INSTPROXY_E_SUCCESS) {
349 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"); 352 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)
431 434
432 if (data->client->receive_status_thread) { 435 if (data->client->receive_status_thread) {
433 thread_free(data->client->receive_status_thread); 436 thread_free(data->client->receive_status_thread);
434 data->client->receive_status_thread = (thread_t)NULL; 437 data->client->receive_status_thread = THREAD_T_NULL;
435 } 438 }
436 439
437 instproxy_unlock(data->client); 440 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 @@
30struct instproxy_client_private { 30struct instproxy_client_private {
31 property_list_service_client_t parent; 31 property_list_service_client_t parent;
32 mutex_t mutex; 32 mutex_t mutex;
33 thread_t receive_status_thread; 33 THREAD_T receive_status_thread;
34}; 34};
35 35
36#endif 36#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
98 client_loc->parent = plistclient; 98 client_loc->parent = plistclient;
99 99
100 mutex_init(&client_loc->mutex); 100 mutex_init(&client_loc->mutex);
101 client_loc->notifier = (thread_t)NULL; 101 client_loc->notifier = THREAD_T_NULL;
102 102
103 *client = client_loc; 103 *client = client_loc;
104 return NP_E_SUCCESS; 104 return NP_E_SUCCESS;
@@ -132,7 +132,7 @@ LIBIMOBILEDEVICE_API np_error_t np_client_free(np_client_t client)
132 debug_info("joining np callback"); 132 debug_info("joining np callback");
133 thread_join(client->notifier); 133 thread_join(client->notifier);
134 thread_free(client->notifier); 134 thread_free(client->notifier);
135 client->notifier = (thread_t)NULL; 135 client->notifier = THREAD_T_NULL;
136 } else { 136 } else {
137 dict = NULL; 137 dict = NULL;
138 property_list_service_receive_plist(parent, &dict); 138 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
350 client->parent = NULL; 350 client->parent = NULL;
351 thread_join(client->notifier); 351 thread_join(client->notifier);
352 thread_free(client->notifier); 352 thread_free(client->notifier);
353 client->notifier = (thread_t)NULL; 353 client->notifier = THREAD_T_NULL;
354 client->parent = parent; 354 client->parent = parent;
355 } 355 }
356 356
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 @@
29struct np_client_private { 29struct np_client_private {
30 property_list_service_client_t parent; 30 property_list_service_client_t parent;
31 mutex_t mutex; 31 mutex_t mutex;
32 thread_t notifier; 32 THREAD_T notifier;
33}; 33};
34 34
35void* np_notifier(void* arg); 35void* 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
81 81
82 syslog_relay_client_t client_loc = (syslog_relay_client_t) malloc(sizeof(struct syslog_relay_client_private)); 82 syslog_relay_client_t client_loc = (syslog_relay_client_t) malloc(sizeof(struct syslog_relay_client_private));
83 client_loc->parent = parent; 83 client_loc->parent = parent;
84 client_loc->worker = (thread_t)NULL; 84 client_loc->worker = THREAD_T_NULL;
85 85
86 *client = client_loc; 86 *client = client_loc;
87 87
@@ -107,7 +107,7 @@ LIBIMOBILEDEVICE_API syslog_relay_error_t syslog_relay_client_free(syslog_relay_
107 debug_info("Joining syslog capture callback worker thread"); 107 debug_info("Joining syslog capture callback worker thread");
108 thread_join(client->worker); 108 thread_join(client->worker);
109 thread_free(client->worker); 109 thread_free(client->worker);
110 client->worker = (thread_t)NULL; 110 client->worker = THREAD_T_NULL;
111 } 111 }
112 free(client); 112 free(client);
113 113
@@ -209,9 +209,9 @@ LIBIMOBILEDEVICE_API syslog_relay_error_t syslog_relay_stop_capture(syslog_relay
209 /* join thread to make it exit */ 209 /* join thread to make it exit */
210 thread_join(client->worker); 210 thread_join(client->worker);
211 thread_free(client->worker); 211 thread_free(client->worker);
212 client->worker = (thread_t)NULL; 212 client->worker = THREAD_T_NULL;
213 client->parent = parent; 213 client->parent = parent;
214 } 214 }
215 215
216 return SYSLOG_RELAY_E_SUCCESS; 216 return SYSLOG_RELAY_E_SUCCESS;
217} \ No newline at end of file 217}
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 @@
28 28
29struct syslog_relay_client_private { 29struct syslog_relay_client_private {
30 service_client_t parent; 30 service_client_t parent;
31 thread_t worker; 31 THREAD_T worker;
32}; 32};
33 33
34void *syslog_relay_worker(void *arg); 34void *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 {
50} socket_info_t; 50} socket_info_t;
51 51
52struct thread_info { 52struct thread_info {
53 thread_t th; 53 THREAD_T th;
54 struct thread_info *next; 54 struct thread_info *next;
55}; 55};
56 56
@@ -140,7 +140,7 @@ static void *thread_client_to_device(void *data)
140 int recv_len; 140 int recv_len;
141 int sent; 141 int sent;
142 char buffer[131072]; 142 char buffer[131072];
143 thread_t dtoc; 143 THREAD_T dtoc;
144 144
145 debug("%s: started thread...\n", __func__); 145 debug("%s: started thread...\n", __func__);
146 146
@@ -205,7 +205,7 @@ static void* connection_handler(void* data)
205{ 205{
206 debugserver_error_t derr = DEBUGSERVER_E_SUCCESS; 206 debugserver_error_t derr = DEBUGSERVER_E_SUCCESS;
207 socket_info_t* socket_info = (socket_info_t*)data; 207 socket_info_t* socket_info = (socket_info_t*)data;
208 thread_t ctod; 208 THREAD_T ctod;
209 209
210 debug("%s: client_fd = %d\n", __func__, socket_info->client_fd); 210 debug("%s: client_fd = %d\n", __func__, socket_info->client_fd);
211 211