« third_party/modest/source/myport/posix/mycore/memory.c
↑ Summary
third_party/modest/source/myport/posix/mycore/utils/mcsync.c »
| 1 | /* | ||||||||||
| 2 | Copyright (C) 2015-2017 Alexander Borisov | ||||||||||
| 3 | |||||||||||
| 4 | This library is free software; you can redistribute it and/or | ||||||||||
| 5 | modify it under the terms of the GNU Lesser General Public | ||||||||||
| 6 | License as published by the Free Software Foundation; either | ||||||||||
| 7 | version 2.1 of the License, or (at your option) any later version. | ||||||||||
| 8 | |||||||||||
| 9 | This library is distributed in the hope that it will be useful, | ||||||||||
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||||||||
| 12 | Lesser General Public License for more details. | ||||||||||
| 13 | |||||||||||
| 14 | You should have received a copy of the GNU Lesser General Public | ||||||||||
| 15 | License along with this library; if not, write to the Free Software | ||||||||||
| 16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||||||||||
| 17 | |||||||||||
| 18 | Author: lex.borisov@gmail.com (Alexander Borisov) | ||||||||||
| 19 | */ | ||||||||||
| 20 | |||||||||||
| 21 | #include "mycore/mythread.h" | ||||||||||
| 22 | #include "mycore/utils/mcsync.h" | ||||||||||
| 23 | |||||||||||
| 24 | #ifndef MyCORE_BUILD_WITHOUT_THREADS | ||||||||||
| 25 | #include <pthread.h> | ||||||||||
| 26 | |||||||||||
| 27 | /*********************************************************************************** | ||||||||||
| 28 | * | ||||||||||
| 29 | * For all unix system. POSIX pthread | ||||||||||
| 30 | * | ||||||||||
| 31 | ***********************************************************************************/ | ||||||||||
| 32 | 0 | void * mythread_thread_create(mythread_t *mythread, mythread_process_f process_func, void* ctx) | |||||||||
| 33 | { | ||||||||||
| 34 | 0 | void *thread = mycore_calloc(1, sizeof(pthread_t)); | |||||||||
| 35 | |||||||||||
| 36 | 0 | ▶ | if(thread == NULL) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | 0 | return NULL; | |||||||||
| 38 | |||||||||||
| 39 | 0 | ▶ | if(pthread_create(&(*((pthread_t*)thread)), mythread->attr, process_func, ctx) == 0) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | 0 | return thread; | |||||||||
| 41 | |||||||||||
| 42 | 0 | return NULL; | |||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | 0 | mystatus_t mythread_thread_join(mythread_t *mythread, void* thread) | |||||||||
| 46 | { | ||||||||||
| 47 | 0 | ▶ | if(pthread_join(*((pthread_t*)thread), NULL) == 0) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | 0 | return MyCORE_STATUS_OK; | |||||||||
| 49 | |||||||||||
| 50 | 0 | return MyCORE_STATUS_ERROR; | |||||||||
| 51 | } | ||||||||||
| 52 | |||||||||||
| 53 | 0 | mystatus_t mythread_thread_cancel(mythread_t *mythread, void* thread) | |||||||||
| 54 | { | ||||||||||
| 55 | 0 | ▶ | if(pthread_cancel(*((pthread_t*)thread)) == 0) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | 0 | return MyCORE_STATUS_OK; | |||||||||
| 57 | |||||||||||
| 58 | 0 | return MyCORE_STATUS_ERROR; | |||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | 0 | mystatus_t mythread_thread_destroy(mythread_t *mythread, void* thread) | |||||||||
| 62 | { | ||||||||||
| 63 | 0 | mycore_free(thread); | |||||||||
| 64 | 0 | return MyCORE_STATUS_OK; | |||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | 0 | void * mythread_thread_attr_init(mythread_t *mythread) | |||||||||
| 68 | { | ||||||||||
| 69 | 0 | pthread_attr_t *attr = (pthread_attr_t*)mycore_calloc(1, sizeof(pthread_attr_t)); | |||||||||
| 70 | |||||||||||
| 71 | 0 | ▶ | if(attr == NULL) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 0 | return NULL; | |||||||||
| 73 | |||||||||||
| 74 | 0 | mythread->sys_last_error = pthread_attr_init(attr); | |||||||||
| 75 | 0 | ▶ | if(mythread->sys_last_error) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | 0 | mycore_free(attr); | |||||||||
| 77 | 0 | return NULL; | |||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | 0 | mythread->sys_last_error = pthread_attr_setdetachstate(attr, PTHREAD_CREATE_JOINABLE); | |||||||||
| 81 | 0 | ▶ | if(mythread->sys_last_error) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | 0 | mycore_free(attr); | |||||||||
| 83 | 0 | return NULL; | |||||||||
| 84 | } | ||||||||||
| 85 | |||||||||||
| 86 | 0 | return attr; | |||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | 0 | void mythread_thread_attr_clean(mythread_t *mythread, void* attr) | |||||||||
| 90 | { | ||||||||||
| 91 | /* some code */ | ||||||||||
| 92 | 0 | } | |||||||||
| 93 | |||||||||||
| 94 | 0 | void mythread_thread_attr_destroy(mythread_t *mythread, void* attr) | |||||||||
| 95 | { | ||||||||||
| 96 | 0 | ▶ | if(attr == NULL) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | 0 | return; | |||||||||
| 98 | |||||||||||
| 99 | 0 | mythread->sys_last_error = pthread_attr_destroy(attr); | |||||||||
| 100 | 0 | mycore_free(attr); | |||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | 0 | void * mythread_mutex_create(mythread_t *mythread) | |||||||||
| 104 | { | ||||||||||
| 105 | 0 | void *mutex = mcsync_mutex_create(); | |||||||||
| 106 | 0 | ▶ | if(mutex == NULL) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 107 | 0 | return NULL; | |||||||||
| 108 | |||||||||||
| 109 | 0 | ▶ | if(mcsync_mutex_init(mutex)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 110 | 0 | mycore_free(mutex); | |||||||||
| 111 | 0 | return NULL; | |||||||||
| 112 | } | ||||||||||
| 113 | |||||||||||
| 114 | 0 | return mutex; | |||||||||
| 115 | } | ||||||||||
| 116 | |||||||||||
| 117 | 0 | mystatus_t mythread_mutex_post(mythread_t *mythread, void* mutex) | |||||||||
| 118 | { | ||||||||||
| 119 | 0 | return mcsync_mutex_unlock(mutex); | |||||||||
| 120 | } | ||||||||||
| 121 | |||||||||||
| 122 | 0 | mystatus_t mythread_mutex_wait(mythread_t *mythread, void* mutex) | |||||||||
| 123 | { | ||||||||||
| 124 | 0 | return mcsync_mutex_lock(mutex); | |||||||||
| 125 | } | ||||||||||
| 126 | |||||||||||
| 127 | 0 | mystatus_t mythread_mutex_try_wait(mythread_t *mythread, void* mutex) | |||||||||
| 128 | { | ||||||||||
| 129 | 0 | return mcsync_mutex_try_lock(mutex); | |||||||||
| 130 | } | ||||||||||
| 131 | |||||||||||
| 132 | 0 | void mythread_mutex_close(mythread_t *mythread, void* mutex) | |||||||||
| 133 | { | ||||||||||
| 134 | 0 | mcsync_mutex_destroy(mutex); | |||||||||
| 135 | 0 | } | |||||||||
| 136 | |||||||||||
| 137 | 0 | void * mythread_nanosleep_create(mythread_t* mythread) | |||||||||
| 138 | { | ||||||||||
| 139 | 0 | return mycore_calloc(1, sizeof(struct timespec)); | |||||||||
| 140 | } | ||||||||||
| 141 | |||||||||||
| 142 | 0 | void mythread_nanosleep_clean(void* timespec) | |||||||||
| 143 | { | ||||||||||
| 144 | 0 | memset(timespec, 0, sizeof(struct timespec)); | |||||||||
| 145 | 0 | } | |||||||||
| 146 | |||||||||||
| 147 | 0 | void mythread_nanosleep_destroy(void* timespec) | |||||||||
| 148 | { | ||||||||||
| 149 | 0 | mycore_free(timespec); | |||||||||
| 150 | 0 | } | |||||||||
| 151 | |||||||||||
| 152 | 0 | mystatus_t mythread_nanosleep_sleep(void* timespec) | |||||||||
| 153 | { | ||||||||||
| 154 | 0 | ▶ | if(nanosleep(timespec, NULL) == 0) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 155 | 0 | return MyCORE_STATUS_OK; | |||||||||
| 156 | |||||||||||
| 157 | 0 | return MyCORE_STATUS_ERROR; | |||||||||
| 158 | } | ||||||||||
| 159 | |||||||||||
| 160 | #endif | ||||||||||