diff --git a/adb/commandline.cpp b/adb/commandline.cpp index 3477246d5..ed09028e2 100644 --- a/adb/commandline.cpp +++ b/adb/commandline.cpp @@ -403,7 +403,6 @@ static void *stdin_read_thread(void *x) } static int interactive_shell() { - adb_thread_t thr; int fdi; std::string error; @@ -424,7 +423,8 @@ static int interactive_shell() { fds[1] = fdi; stdin_raw_init(fdi); - adb_thread_create(&thr, stdin_read_thread, fds); + + adb_thread_create(stdin_read_thread, fds); read_and_dump(fd); stdin_raw_restore(fdi); return 0; diff --git a/adb/services.cpp b/adb/services.cpp index 7e477291d..e53a28c2d 100644 --- a/adb/services.cpp +++ b/adb/services.cpp @@ -210,8 +210,7 @@ static int create_service_thread(void (*func)(int, void *), void *cookie) sti->cookie = cookie; sti->fd = s[1]; - adb_thread_t t; - if (adb_thread_create(&t, service_bootstrap_func, sti)) { + if (!adb_thread_create(service_bootstrap_func, sti)) { free(sti); adb_close(s[0]); adb_close(s[1]); @@ -401,8 +400,7 @@ static int create_subproc_thread(const char *name, bool pty = false) { sti->cookie = (void*) (uintptr_t) pid; sti->fd = ret_fd; - adb_thread_t t; - if (adb_thread_create(&t, service_bootstrap_func, sti)) { + if (!adb_thread_create(service_bootstrap_func, sti)) { free(sti); adb_close(ret_fd); fprintf(stderr, "cannot create service thread\n"); diff --git a/adb/sysdeps.h b/adb/sysdeps.h index 59e5b0b3d..95d0e2015 100644 --- a/adb/sysdeps.h +++ b/adb/sysdeps.h @@ -79,19 +79,13 @@ static __inline__ void adb_mutex_unlock( adb_mutex_t* lock ) LeaveCriticalSection( lock ); } -typedef struct { unsigned tid; } adb_thread_t; - typedef void* (*adb_thread_func_t)(void* arg); typedef void (*win_thread_func_t)(void* arg); -static __inline__ int adb_thread_create( adb_thread_t *thread, adb_thread_func_t func, void* arg) -{ - thread->tid = _beginthread( (win_thread_func_t)func, 0, arg ); - if (thread->tid == (unsigned)-1L) { - return -1; - } - return 0; +static __inline__ bool adb_thread_create(adb_thread_func_t func, void* arg) { + unsigned tid = _beginthread( (win_thread_func_t)func, 0, arg ); + return (tid != (unsigned)-1L); } static __inline__ unsigned long adb_thread_id() @@ -429,18 +423,16 @@ static __inline__ int adb_socket_accept(int serverfd, struct sockaddr* addr, #define unix_write adb_write #define unix_close adb_close -typedef pthread_t adb_thread_t; - typedef void* (*adb_thread_func_t)( void* arg ); -static __inline__ int adb_thread_create( adb_thread_t *pthread, adb_thread_func_t start, void* arg ) -{ - pthread_attr_t attr; +static __inline__ bool adb_thread_create(adb_thread_func_t start, void* arg) { + pthread_attr_t attr; + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); - pthread_attr_init (&attr); - pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); - - return pthread_create( pthread, &attr, start, arg ); + pthread_t thread; + errno = pthread_create(&thread, &attr, start, arg); + return (errno == 0); } static __inline__ int adb_socket_setbufsize( int fd, int bufsize ) diff --git a/adb/transport.cpp b/adb/transport.cpp index e168b8621..bb5be6b42 100644 --- a/adb/transport.cpp +++ b/adb/transport.cpp @@ -530,8 +530,6 @@ transport_write_action(int fd, struct tmsg* m) static void transport_registration_func(int _fd, unsigned ev, void *data) { tmsg m; - adb_thread_t output_thread_ptr; - adb_thread_t input_thread_ptr; int s[2]; atransport *t; @@ -600,11 +598,11 @@ static void transport_registration_func(int _fd, unsigned ev, void *data) fdevent_set(&(t->transport_fde), FDE_READ); - if(adb_thread_create(&input_thread_ptr, input_thread, t)){ + if (!adb_thread_create(input_thread, t)) { fatal_errno("cannot create input thread"); } - if(adb_thread_create(&output_thread_ptr, output_thread, t)){ + if (!adb_thread_create(output_thread, t)) { fatal_errno("cannot create output thread"); } } diff --git a/adb/transport_local.cpp b/adb/transport_local.cpp index b1deffd0c..5f7449d22 100644 --- a/adb/transport_local.cpp +++ b/adb/transport_local.cpp @@ -234,9 +234,8 @@ static const char _ok_resp[] = "ok"; if (fd < 0) { /* This could be an older version of the emulator, that doesn't * implement adb QEMUD service. Fall back to the old TCP way. */ - adb_thread_t thr; D("adb service is not available. Falling back to TCP socket.\n"); - adb_thread_create(&thr, server_socket_thread, arg); + adb_thread_create(server_socket_thread, arg); return 0; } @@ -279,7 +278,6 @@ static const char _ok_resp[] = "ok"; void local_init(int port) { - adb_thread_t thr; void* (*func)(void *); if(HOST) { @@ -304,9 +302,8 @@ void local_init(int port) D("transport: local %s init\n", HOST ? "client" : "server"); - if(adb_thread_create(&thr, func, (void *) (uintptr_t) port)) { - fatal_errno("cannot create local socket %s thread", - HOST ? "client" : "server"); + if (!adb_thread_create(func, (void *) (uintptr_t) port)) { + fatal_errno("cannot create local socket %s thread", HOST ? "client" : "server"); } } diff --git a/adb/usb_linux.cpp b/adb/usb_linux.cpp index 999eb118b..b61bf711b 100644 --- a/adb/usb_linux.cpp +++ b/adb/usb_linux.cpp @@ -668,7 +668,6 @@ static void sigalrm_handler(int signo) void usb_init() { - adb_thread_t tid; struct sigaction actions; memset(&actions, 0, sizeof(actions)); @@ -677,7 +676,7 @@ void usb_init() actions.sa_handler = sigalrm_handler; sigaction(SIGALRM,& actions, NULL); - if(adb_thread_create(&tid, device_poll_thread, NULL)){ + if (!adb_thread_create(device_poll_thread, nullptr)) { fatal_errno("cannot create input thread"); } } diff --git a/adb/usb_linux_client.cpp b/adb/usb_linux_client.cpp index 18289e2ff..f337ccd17 100644 --- a/adb/usb_linux_client.cpp +++ b/adb/usb_linux_client.cpp @@ -264,8 +264,7 @@ static void usb_adb_init() } D("[ usb_init - starting thread ]\n"); - adb_thread_t tid; - if(adb_thread_create(&tid, usb_adb_open_thread, h)){ + if (!adb_thread_create(usb_adb_open_thread, h)) { fatal_errno("cannot create usb thread"); } } @@ -483,8 +482,7 @@ static void usb_ffs_init() adb_mutex_init(&h->lock, 0); D("[ usb_init - starting thread ]\n"); - adb_thread_t tid; - if (adb_thread_create(&tid, usb_ffs_open_thread, h)){ + if (!adb_thread_create(usb_ffs_open_thread, h)) { fatal_errno("[ cannot create usb thread ]\n"); } } diff --git a/adb/usb_osx.cpp b/adb/usb_osx.cpp index a795ce38e..0d0b3ad63 100644 --- a/adb/usb_osx.cpp +++ b/adb/usb_osx.cpp @@ -398,22 +398,20 @@ void* RunLoopThread(void* unused) IONotificationPortDestroy(notificationPort); DBG("RunLoopThread done\n"); - return NULL; + return NULL; } static int initialized = 0; -void usb_init() -{ +void usb_init() { if (!initialized) { - adb_thread_t tid; - adb_mutex_init(&start_lock, NULL); adb_cond_init(&start_cond, NULL); - if(adb_thread_create(&tid, RunLoopThread, NULL)) + if (!adb_thread_create(RunLoopThread, nullptr)) { fatal_errno("cannot create input thread"); + } // Wait for initialization to finish adb_mutex_lock(&start_lock); diff --git a/adb/usb_windows.cpp b/adb/usb_windows.cpp index d2bd58cdf..1d6ec8cd8 100644 --- a/adb/usb_windows.cpp +++ b/adb/usb_windows.cpp @@ -181,9 +181,7 @@ void* device_poll_thread(void* unused) { } void usb_init() { - adb_thread_t tid; - - if(adb_thread_create(&tid, device_poll_thread, NULL)) { + if (!adb_thread_create(device_poll_thread, nullptr)) { fatal_errno("cannot create input thread"); } }