diff options
| author | 2019-09-05 08:41:42 +0200 | |
|---|---|---|
| committer | 2019-09-05 08:41:42 +0200 | |
| commit | 43003cbc26315aaaee342158939c3b3504f367d0 (patch) | |
| tree | 49586b12981f955388cc7a765c557d687c5bfc9e /src | |
| 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')
| -rw-r--r-- | src/thread.c | 140 | ||||
| -rw-r--r-- | src/thread.h | 76 | ||||
| -rw-r--r-- | src/utils.c | 92 | ||||
| -rw-r--r-- | src/utils.h | 52 |
4 files changed, 360 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 | } | ||
diff --git a/src/thread.h b/src/thread.h new file mode 100644 index 0000000..23e4510 --- /dev/null +++ b/src/thread.h | |||
| @@ -0,0 +1,76 @@ | |||
| 1 | /* | ||
| 2 | * thread.h | ||
| 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 | #ifndef __THREAD_H | ||
| 23 | #define __THREAD_H | ||
| 24 | |||
| 25 | #include <stddef.h> | ||
| 26 | |||
| 27 | #ifdef WIN32 | ||
| 28 | #include <windows.h> | ||
| 29 | typedef HANDLE THREAD_T; | ||
| 30 | typedef CRITICAL_SECTION mutex_t; | ||
| 31 | typedef volatile struct { | ||
| 32 | LONG lock; | ||
| 33 | int state; | ||
| 34 | } thread_once_t; | ||
| 35 | #define THREAD_ONCE_INIT {0, 0} | ||
| 36 | #define THREAD_ID GetCurrentThreadId() | ||
| 37 | #define THREAD_T_NULL (THREAD_T)NULL | ||
| 38 | #else | ||
| 39 | #include <pthread.h> | ||
| 40 | #include <signal.h> | ||
| 41 | typedef pthread_t THREAD_T; | ||
| 42 | typedef pthread_mutex_t mutex_t; | ||
| 43 | typedef pthread_once_t thread_once_t; | ||
| 44 | #define THREAD_ONCE_INIT PTHREAD_ONCE_INIT | ||
| 45 | #define THREAD_ID pthread_self() | ||
| 46 | #define THREAD_T_NULL (THREAD_T)NULL | ||
| 47 | #endif | ||
| 48 | |||
| 49 | typedef void* (*thread_func_t)(void* data); | ||
| 50 | |||
| 51 | int thread_new(THREAD_T* thread, thread_func_t thread_func, void* data); | ||
| 52 | void thread_detach(THREAD_T thread); | ||
| 53 | void thread_free(THREAD_T thread); | ||
| 54 | int thread_join(THREAD_T thread); | ||
| 55 | int thread_alive(THREAD_T thread); | ||
| 56 | |||
| 57 | int 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 | ||
| 68 | |||
| 69 | void mutex_init(mutex_t* mutex); | ||
| 70 | void mutex_destroy(mutex_t* mutex); | ||
| 71 | void mutex_lock(mutex_t* mutex); | ||
| 72 | void mutex_unlock(mutex_t* mutex); | ||
| 73 | |||
| 74 | void thread_once(thread_once_t *once_control, void (*init_routine)(void)); | ||
| 75 | |||
| 76 | #endif | ||
diff --git a/src/utils.c b/src/utils.c new file mode 100644 index 0000000..826bfbe --- /dev/null +++ b/src/utils.c | |||
| @@ -0,0 +1,92 @@ | |||
| 1 | /* | ||
| 2 | * utils.c | ||
| 3 | * | ||
| 4 | * Copyright (C) 2009 Hector Martin <hector@marcansoft.com> | ||
| 5 | * Copyright (C) 2009 Nikias Bassen <nikias@gmx.li> | ||
| 6 | * Copyright (c) 2013 Federico Mena Quintero | ||
| 7 | * | ||
| 8 | * This library is free software; you can redistribute it and/or modify | ||
| 9 | * it under the terms of the GNU Lesser General Public License as | ||
| 10 | * published by the Free Software Foundation, either version 2.1 of the | ||
| 11 | * License, or (at your option) any later version. | ||
| 12 | * | ||
| 13 | * This library is distributed in the hope that it will be useful, | ||
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 16 | * GNU General Public License for more details. | ||
| 17 | * | ||
| 18 | * You should have received a copy of the GNU Lesser General Public | ||
| 19 | * License along with this program; if not, write to the Free Software | ||
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 21 | */ | ||
| 22 | |||
| 23 | #ifdef HAVE_CONFIG_H | ||
| 24 | #include <config.h> | ||
| 25 | #endif | ||
| 26 | |||
| 27 | #include <stdlib.h> | ||
| 28 | #include <string.h> | ||
| 29 | #include <stdio.h> | ||
| 30 | #include <errno.h> | ||
| 31 | |||
| 32 | #include "utils.h" | ||
| 33 | |||
| 34 | #define CAPACITY_STEP 8 | ||
| 35 | |||
| 36 | void collection_init(struct collection *col) | ||
| 37 | { | ||
| 38 | col->list = malloc(sizeof(void *) * CAPACITY_STEP); | ||
| 39 | memset(col->list, 0, sizeof(void *) * CAPACITY_STEP); | ||
| 40 | col->capacity = CAPACITY_STEP; | ||
| 41 | } | ||
| 42 | |||
| 43 | void collection_free(struct collection *col) | ||
| 44 | { | ||
| 45 | free(col->list); | ||
| 46 | col->list = NULL; | ||
| 47 | col->capacity = 0; | ||
| 48 | } | ||
| 49 | |||
| 50 | void collection_add(struct collection *col, void *element) | ||
| 51 | { | ||
| 52 | int i; | ||
| 53 | for(i=0; i<col->capacity; i++) { | ||
| 54 | if(!col->list[i]) { | ||
| 55 | col->list[i] = element; | ||
| 56 | return; | ||
| 57 | } | ||
| 58 | } | ||
| 59 | col->list = realloc(col->list, sizeof(void*) * (col->capacity + CAPACITY_STEP)); | ||
| 60 | memset(&col->list[col->capacity], 0, sizeof(void *) * CAPACITY_STEP); | ||
| 61 | col->list[col->capacity] = element; | ||
| 62 | col->capacity += CAPACITY_STEP; | ||
| 63 | } | ||
| 64 | |||
| 65 | void collection_remove(struct collection *col, void *element) | ||
| 66 | { | ||
| 67 | int i; | ||
| 68 | for(i=0; i<col->capacity; i++) { | ||
| 69 | if(col->list[i] == element) { | ||
| 70 | col->list[i] = NULL; | ||
| 71 | return; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | int collection_count(struct collection *col) | ||
| 77 | { | ||
| 78 | int i, cnt = 0; | ||
| 79 | for(i=0; i<col->capacity; i++) { | ||
| 80 | if(col->list[i]) | ||
| 81 | cnt++; | ||
| 82 | } | ||
| 83 | return cnt; | ||
| 84 | } | ||
| 85 | |||
| 86 | void collection_copy(struct collection *dest, struct collection *src) | ||
| 87 | { | ||
| 88 | if (!dest || !src) return; | ||
| 89 | dest->capacity = src->capacity; | ||
| 90 | dest->list = malloc(sizeof(void*) * src->capacity); | ||
| 91 | memcpy(dest->list, src->list, sizeof(void*) * src->capacity); | ||
| 92 | } | ||
diff --git a/src/utils.h b/src/utils.h new file mode 100644 index 0000000..ccb3a09 --- /dev/null +++ b/src/utils.h | |||
| @@ -0,0 +1,52 @@ | |||
| 1 | /* | ||
| 2 | * utils.h | ||
| 3 | * | ||
| 4 | * Copyright (C) 2009 Hector Martin <hector@marcansoft.com> | ||
| 5 | * Copyright (C) 2009 Nikias Bassen <nikias@gmx.li> | ||
| 6 | * | ||
| 7 | * This library is free software; you can redistribute it and/or modify | ||
| 8 | * it under the terms of the GNU Lesser General Public License as | ||
| 9 | * published by the Free Software Foundation, either version 2.1 of the | ||
| 10 | * 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 | ||
| 15 | * GNU 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 program; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 20 | */ | ||
| 21 | |||
| 22 | #ifndef UTILS_H | ||
| 23 | #define UTILS_H | ||
| 24 | |||
| 25 | struct collection { | ||
| 26 | void **list; | ||
| 27 | int capacity; | ||
| 28 | }; | ||
| 29 | |||
| 30 | void collection_init(struct collection *col); | ||
| 31 | void collection_add(struct collection *col, void *element); | ||
| 32 | void collection_remove(struct collection *col, void *element); | ||
| 33 | int collection_count(struct collection *col); | ||
| 34 | void collection_free(struct collection *col); | ||
| 35 | void collection_copy(struct collection *dest, struct collection *src); | ||
| 36 | |||
| 37 | #define MERGE_(a,b) a ## _ ## b | ||
| 38 | #define LABEL_(a,b) MERGE_(a, b) | ||
| 39 | #define UNIQUE_VAR(a) LABEL_(a, __LINE__) | ||
| 40 | |||
| 41 | #define FOREACH(var, col) \ | ||
| 42 | do { \ | ||
| 43 | int UNIQUE_VAR(_iter); \ | ||
| 44 | for(UNIQUE_VAR(_iter)=0; UNIQUE_VAR(_iter)<(col)->capacity; UNIQUE_VAR(_iter)++) { \ | ||
| 45 | if(!(col)->list[UNIQUE_VAR(_iter)]) continue; \ | ||
| 46 | var = (col)->list[UNIQUE_VAR(_iter)]; | ||
| 47 | |||
| 48 | #define ENDFOREACH \ | ||
| 49 | } \ | ||
| 50 | } while(0); | ||
| 51 | |||
| 52 | #endif | ||
