diff options
| author | 2019-09-05 08:41:42 +0200 | |
|---|---|---|
| committer | 2019-09-05 08:41:42 +0200 | |
| commit | 43003cbc26315aaaee342158939c3b3504f367d0 (patch) | |
| tree | 49586b12981f955388cc7a765c557d687c5bfc9e /src/thread.c | |
| parent | 174123a943cd26a9c003745d0226eb5bfd1a5d6f (diff) | |
| download | libirecovery-43003cbc26315aaaee342158939c3b3504f367d0.tar.gz libirecovery-43003cbc26315aaaee342158939c3b3504f367d0.tar.bz2 | |
Add missing files for previous commit
I shouldn't do late night commits without checking that I added all files
Diffstat (limited to 'src/thread.c')
| -rw-r--r-- | src/thread.c | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/src/thread.c b/src/thread.c new file mode 100644 index 0000000..eb535ab --- /dev/null +++ b/src/thread.c | |||
| @@ -0,0 +1,140 @@ | |||
| 1 | /* | ||
| 2 | * thread.c | ||
| 3 | * | ||
| 4 | * Copyright (c) 2012-2019 Nikias Bassen, All Rights Reserved. | ||
| 5 | * Copyright (c) 2012 Martin Szulecki, All Rights Reserved. | ||
| 6 | * | ||
| 7 | * This library is free software; you can redistribute it and/or | ||
| 8 | * modify it under the terms of the GNU Lesser General Public | ||
| 9 | * License as published by the Free Software Foundation; either | ||
| 10 | * version 2.1 of the License, or (at your option) any later version. | ||
| 11 | * | ||
| 12 | * This library is distributed in the hope that it will be useful, | ||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 15 | * Lesser General Public License for more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU Lesser General Public | ||
| 18 | * License along with this library; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 20 | */ | ||
| 21 | |||
| 22 | #ifdef HAVE_CONFIG_H | ||
| 23 | #include <config.h> | ||
| 24 | #endif | ||
| 25 | #include "thread.h" | ||
| 26 | |||
| 27 | int thread_new(THREAD_T *thread, thread_func_t thread_func, void* data) | ||
| 28 | { | ||
| 29 | #ifdef WIN32 | ||
| 30 | HANDLE th = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)thread_func, data, 0, NULL); | ||
| 31 | if (th == NULL) { | ||
| 32 | return -1; | ||
| 33 | } | ||
| 34 | *thread = th; | ||
| 35 | return 0; | ||
| 36 | #else | ||
| 37 | int res = pthread_create(thread, NULL, thread_func, data); | ||
| 38 | return res; | ||
| 39 | #endif | ||
| 40 | } | ||
| 41 | |||
| 42 | void thread_detach(THREAD_T thread) | ||
| 43 | { | ||
| 44 | #ifdef WIN32 | ||
| 45 | CloseHandle(thread); | ||
| 46 | #else | ||
| 47 | pthread_detach(thread); | ||
| 48 | #endif | ||
| 49 | } | ||
| 50 | |||
| 51 | void thread_free(THREAD_T thread) | ||
| 52 | { | ||
| 53 | #ifdef WIN32 | ||
| 54 | CloseHandle(thread); | ||
| 55 | #endif | ||
| 56 | } | ||
| 57 | |||
| 58 | int thread_join(THREAD_T thread) | ||
| 59 | { | ||
| 60 | /* wait for thread to complete */ | ||
| 61 | #ifdef WIN32 | ||
| 62 | return (int)WaitForSingleObject(thread, INFINITE); | ||
| 63 | #else | ||
| 64 | return pthread_join(thread, NULL); | ||
| 65 | #endif | ||
| 66 | } | ||
| 67 | |||
| 68 | int thread_alive(THREAD_T thread) | ||
| 69 | { | ||
| 70 | #ifdef WIN32 | ||
| 71 | return WaitForSingleObject(thread, 0) == WAIT_TIMEOUT; | ||
| 72 | #else | ||
| 73 | return pthread_kill(thread, 0) == 0; | ||
| 74 | #endif | ||
| 75 | } | ||
| 76 | |||
| 77 | int 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 | ||
| 87 | #endif | ||
| 88 | } | ||
| 89 | |||
| 90 | void mutex_init(mutex_t* mutex) | ||
| 91 | { | ||
| 92 | #ifdef WIN32 | ||
| 93 | InitializeCriticalSection(mutex); | ||
| 94 | #else | ||
| 95 | pthread_mutex_init(mutex, NULL); | ||
| 96 | #endif | ||
| 97 | } | ||
| 98 | |||
| 99 | void mutex_destroy(mutex_t* mutex) | ||
| 100 | { | ||
| 101 | #ifdef WIN32 | ||
| 102 | DeleteCriticalSection(mutex); | ||
| 103 | #else | ||
| 104 | pthread_mutex_destroy(mutex); | ||
| 105 | #endif | ||
| 106 | } | ||
| 107 | |||
| 108 | void mutex_lock(mutex_t* mutex) | ||
| 109 | { | ||
| 110 | #ifdef WIN32 | ||
| 111 | EnterCriticalSection(mutex); | ||
| 112 | #else | ||
| 113 | pthread_mutex_lock(mutex); | ||
| 114 | #endif | ||
| 115 | } | ||
| 116 | |||
| 117 | void mutex_unlock(mutex_t* mutex) | ||
| 118 | { | ||
| 119 | #ifdef WIN32 | ||
| 120 | LeaveCriticalSection(mutex); | ||
| 121 | #else | ||
| 122 | pthread_mutex_unlock(mutex); | ||
| 123 | #endif | ||
| 124 | } | ||
| 125 | |||
| 126 | void thread_once(thread_once_t *once_control, void (*init_routine)(void)) | ||
| 127 | { | ||
| 128 | #ifdef WIN32 | ||
| 129 | while (InterlockedExchange(&(once_control->lock), 1) != 0) { | ||
| 130 | Sleep(1); | ||
| 131 | } | ||
| 132 | if (!once_control->state) { | ||
| 133 | once_control->state = 1; | ||
| 134 | init_routine(); | ||
| 135 | } | ||
| 136 | InterlockedExchange(&(once_control->lock), 0); | ||
| 137 | #else | ||
| 138 | pthread_once(once_control, init_routine); | ||
| 139 | #endif | ||
| 140 | } | ||
