diff options
Diffstat (limited to 'src/thread.c')
| -rw-r--r-- | src/thread.c | 215 |
1 files changed, 0 insertions, 215 deletions
diff --git a/src/thread.c b/src/thread.c deleted file mode 100644 index 796ea0b..0000000 --- a/src/thread.c +++ /dev/null | |||
| @@ -1,215 +0,0 @@ | |||
| 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 | if (!thread) | ||
| 71 | return 0; | ||
| 72 | #ifdef WIN32 | ||
| 73 | return WaitForSingleObject(thread, 0) == WAIT_TIMEOUT; | ||
| 74 | #else | ||
| 75 | return pthread_kill(thread, 0) == 0; | ||
| 76 | #endif | ||
| 77 | } | ||
| 78 | |||
| 79 | int thread_cancel(THREAD_T thread) | ||
| 80 | { | ||
| 81 | #ifdef WIN32 | ||
| 82 | return -1; | ||
| 83 | #else | ||
| 84 | #ifdef HAVE_PTHREAD_CANCEL | ||
| 85 | return pthread_cancel(thread); | ||
| 86 | #else | ||
| 87 | return -1; | ||
| 88 | #endif | ||
| 89 | #endif | ||
| 90 | } | ||
| 91 | |||
| 92 | void mutex_init(mutex_t* mutex) | ||
| 93 | { | ||
| 94 | #ifdef WIN32 | ||
| 95 | InitializeCriticalSection(mutex); | ||
| 96 | #else | ||
| 97 | pthread_mutex_init(mutex, NULL); | ||
| 98 | #endif | ||
| 99 | } | ||
| 100 | |||
| 101 | void mutex_destroy(mutex_t* mutex) | ||
| 102 | { | ||
| 103 | #ifdef WIN32 | ||
| 104 | DeleteCriticalSection(mutex); | ||
| 105 | #else | ||
| 106 | pthread_mutex_destroy(mutex); | ||
| 107 | #endif | ||
| 108 | } | ||
| 109 | |||
| 110 | void mutex_lock(mutex_t* mutex) | ||
| 111 | { | ||
| 112 | #ifdef WIN32 | ||
| 113 | EnterCriticalSection(mutex); | ||
| 114 | #else | ||
| 115 | pthread_mutex_lock(mutex); | ||
| 116 | #endif | ||
| 117 | } | ||
| 118 | |||
| 119 | void mutex_unlock(mutex_t* mutex) | ||
| 120 | { | ||
| 121 | #ifdef WIN32 | ||
| 122 | LeaveCriticalSection(mutex); | ||
| 123 | #else | ||
| 124 | pthread_mutex_unlock(mutex); | ||
| 125 | #endif | ||
| 126 | } | ||
| 127 | |||
| 128 | void thread_once(thread_once_t *once_control, void (*init_routine)(void)) | ||
| 129 | { | ||
| 130 | #ifdef WIN32 | ||
| 131 | while (InterlockedExchange(&(once_control->lock), 1) != 0) { | ||
| 132 | Sleep(1); | ||
| 133 | } | ||
| 134 | if (!once_control->state) { | ||
| 135 | once_control->state = 1; | ||
| 136 | init_routine(); | ||
| 137 | } | ||
| 138 | InterlockedExchange(&(once_control->lock), 0); | ||
| 139 | #else | ||
| 140 | pthread_once(once_control, init_routine); | ||
| 141 | #endif | ||
| 142 | } | ||
| 143 | |||
| 144 | void cond_init(cond_t* cond) | ||
| 145 | { | ||
| 146 | #ifdef WIN32 | ||
| 147 | cond->sem = CreateSemaphore(NULL, 0, 32767, NULL); | ||
| 148 | #else | ||
| 149 | pthread_cond_init(cond, NULL); | ||
| 150 | #endif | ||
| 151 | } | ||
| 152 | |||
| 153 | void cond_destroy(cond_t* cond) | ||
| 154 | { | ||
| 155 | #ifdef WIN32 | ||
| 156 | CloseHandle(cond->sem); | ||
| 157 | #else | ||
| 158 | pthread_cond_destroy(cond); | ||
| 159 | #endif | ||
| 160 | } | ||
| 161 | |||
| 162 | int cond_signal(cond_t* cond) | ||
| 163 | { | ||
| 164 | #ifdef WIN32 | ||
| 165 | int result = 0; | ||
| 166 | if (!ReleaseSemaphore(cond->sem, 1, NULL)) { | ||
| 167 | result = -1; | ||
| 168 | } | ||
| 169 | return result; | ||
| 170 | #else | ||
| 171 | return pthread_cond_signal(cond); | ||
| 172 | #endif | ||
| 173 | } | ||
| 174 | |||
| 175 | int cond_wait(cond_t* cond, mutex_t* mutex) | ||
| 176 | { | ||
| 177 | #ifdef WIN32 | ||
| 178 | mutex_unlock(mutex); | ||
| 179 | DWORD res = WaitForSingleObject(cond->sem, INFINITE); | ||
| 180 | switch (res) { | ||
| 181 | case WAIT_OBJECT_0: | ||
| 182 | return 0; | ||
| 183 | default: | ||
| 184 | return -1; | ||
| 185 | } | ||
| 186 | #else | ||
| 187 | return pthread_cond_wait(cond, mutex); | ||
| 188 | #endif | ||
| 189 | } | ||
| 190 | |||
| 191 | int cond_wait_timeout(cond_t* cond, mutex_t* mutex, unsigned int timeout_ms) | ||
| 192 | { | ||
| 193 | #ifdef WIN32 | ||
| 194 | mutex_unlock(mutex); | ||
| 195 | DWORD res = WaitForSingleObject(cond->sem, timeout_ms); | ||
| 196 | switch (res) { | ||
| 197 | case WAIT_OBJECT_0: | ||
| 198 | case WAIT_TIMEOUT: | ||
| 199 | return 0; | ||
| 200 | default: | ||
| 201 | return -1; | ||
| 202 | } | ||
| 203 | #else | ||
| 204 | struct timespec ts; | ||
| 205 | struct timeval now; | ||
| 206 | gettimeofday(&now, NULL); | ||
| 207 | |||
| 208 | ts.tv_sec = now.tv_sec + timeout_ms / 1000; | ||
| 209 | ts.tv_nsec = now.tv_usec * 1000 + 1000 * 1000 * (timeout_ms % 1000); | ||
| 210 | ts.tv_sec += ts.tv_nsec / (1000 * 1000 * 1000); | ||
| 211 | ts.tv_nsec %= (1000 * 1000 * 1000); | ||
| 212 | |||
| 213 | return pthread_cond_timedwait(cond, mutex, &ts); | ||
| 214 | #endif | ||
| 215 | } | ||
