« third_party/modest/source/myport/posix/mycore/thread.c
↑ Summary
third_party/modest/source/myurl/host.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/utils/mcsync.h" | ||||||||||
| 22 | |||||||||||
| 23 | #ifndef MyCORE_BUILD_WITHOUT_THREADS | ||||||||||
| 24 | #include <pthread.h> | ||||||||||
| 25 | |||||||||||
| 26 | #if ((defined(__GNUC__) && __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) || defined(__ATOMIC_SEQ_CST)) | ||||||||||
| 27 | #define MyCORE_MCSYNC_SPINLOCK_PRESENT | ||||||||||
| 28 | #endif | ||||||||||
| 29 | |||||||||||
| 30 | #ifdef MyCORE_MCSYNC_SPINLOCK_PRESENT | ||||||||||
| 31 | 0 | static mcsync_status_t mcsync_static_atomic_lock(void* spinlock) | |||||||||
| 32 | { | ||||||||||
| 33 | 0 | int compare = 0; | |||||||||
| 34 | 0 | ▶ | while (!__atomic_compare_exchange_n((int*)spinlock, &compare, 1, 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) {} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | |||||||||||
| 36 | 0 | return MCSYNC_STATUS_OK; | |||||||||
| 37 | } | ||||||||||
| 38 | |||||||||||
| 39 | 0 | static mcsync_status_t mcsync_static_atomic_unlock(void* spinlock) | |||||||||
| 40 | { | ||||||||||
| 41 | 0 | __atomic_store_n((int*)spinlock, 0, __ATOMIC_SEQ_CST); | |||||||||
| 42 | |||||||||||
| 43 | 0 | return MCSYNC_STATUS_OK; | |||||||||
| 44 | } | ||||||||||
| 45 | #endif /* MyCORE_MCSYNC_SPINLOCK_PRESENT */ | ||||||||||
| 46 | |||||||||||
| 47 | /* spinlock */ | ||||||||||
| 48 | 0 | void * mcsync_spin_create(void) | |||||||||
| 49 | { | ||||||||||
| 50 | #ifndef MyCORE_MCSYNC_SPINLOCK_PRESENT | ||||||||||
| 51 | return mcsync_mutex_create(); | ||||||||||
| 52 | #else | ||||||||||
| 53 | 0 | return mycore_calloc(1, sizeof(int)); | |||||||||
| 54 | #endif | ||||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | 0 | mcsync_status_t mcsync_spin_init(void* spinlock) | |||||||||
| 58 | { | ||||||||||
| 59 | #ifndef MyCORE_MCSYNC_SPINLOCK_PRESENT | ||||||||||
| 60 | return mcsync_mutex_init(spinlock); | ||||||||||
| 61 | #else | ||||||||||
| 62 | 0 | return MCSYNC_STATUS_OK; | |||||||||
| 63 | #endif | ||||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | 0 | void mcsync_spin_clean(void* spinlock) | |||||||||
| 67 | { | ||||||||||
| 68 | #ifdef MyCORE_MCSYNC_SPINLOCK_PRESENT | ||||||||||
| 69 | 0 | *((int*)spinlock) = 0; | |||||||||
| 70 | #endif | ||||||||||
| 71 | 0 | } | |||||||||
| 72 | |||||||||||
| 73 | 0 | void mcsync_spin_destroy(void* spinlock) | |||||||||
| 74 | { | ||||||||||
| 75 | #ifdef MyCORE_MCSYNC_SPINLOCK_PRESENT | ||||||||||
| 76 | 0 | mycore_free(spinlock); | |||||||||
| 77 | #else | ||||||||||
| 78 | mcsync_mutex_destroy(spinlock); | ||||||||||
| 79 | #endif | ||||||||||
| 80 | 0 | } | |||||||||
| 81 | |||||||||||
| 82 | 0 | mcsync_status_t mcsync_spin_lock(void* spinlock) | |||||||||
| 83 | { | ||||||||||
| 84 | #ifdef MyCORE_MCSYNC_SPINLOCK_PRESENT | ||||||||||
| 85 | 0 | return mcsync_static_atomic_lock(spinlock); | |||||||||
| 86 | #else | ||||||||||
| 87 | return mcsync_mutex_lock(spinlock); | ||||||||||
| 88 | #endif | ||||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | 0 | mcsync_status_t mcsync_spin_unlock(void* spinlock) | |||||||||
| 92 | { | ||||||||||
| 93 | #ifdef MyCORE_MCSYNC_SPINLOCK_PRESENT | ||||||||||
| 94 | 0 | return mcsync_static_atomic_unlock(spinlock); | |||||||||
| 95 | #else | ||||||||||
| 96 | return mcsync_mutex_unlock(spinlock); | ||||||||||
| 97 | #endif | ||||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | /* mutex */ | ||||||||||
| 101 | 0 | void * mcsync_mutex_create(void) | |||||||||
| 102 | { | ||||||||||
| 103 | 0 | void *mutex = mycore_calloc(1, sizeof(pthread_mutex_t)); | |||||||||
| 104 | 0 | ▶ | if(mutex == NULL) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 105 | 0 | return NULL; | |||||||||
| 106 | |||||||||||
| 107 | 0 | return mutex; | |||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | 0 | mcsync_status_t mcsync_mutex_init(void* mutex) | |||||||||
| 111 | { | ||||||||||
| 112 | 0 | ▶ | if(pthread_mutex_init(mutex, NULL)) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 113 | 0 | return MCSYNC_STATUS_NOT_OK; | |||||||||
| 114 | |||||||||||
| 115 | 0 | return MCSYNC_STATUS_OK; | |||||||||
| 116 | } | ||||||||||
| 117 | |||||||||||
| 118 | 0 | void mcsync_mutex_clean(void* mutex) | |||||||||
| 119 | { | ||||||||||
| 120 | /* clean function */ | ||||||||||
| 121 | 0 | } | |||||||||
| 122 | |||||||||||
| 123 | 0 | void mcsync_mutex_destroy(void* mutex) | |||||||||
| 124 | { | ||||||||||
| 125 | 0 | pthread_mutex_destroy(mutex); | |||||||||
| 126 | 0 | mycore_free(mutex); | |||||||||
| 127 | 0 | } | |||||||||
| 128 | |||||||||||
| 129 | 0 | mcsync_status_t mcsync_mutex_lock(void* mutex) | |||||||||
| 130 | { | ||||||||||
| 131 | 0 | ▶ | if(pthread_mutex_lock(mutex) == 0) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 132 | 0 | return MCSYNC_STATUS_OK; | |||||||||
| 133 | |||||||||||
| 134 | 0 | return MCSYNC_STATUS_NOT_OK; | |||||||||
| 135 | } | ||||||||||
| 136 | |||||||||||
| 137 | 0 | mcsync_status_t mcsync_mutex_try_lock(void* mutex) | |||||||||
| 138 | { | ||||||||||
| 139 | 0 | ▶ | if(pthread_mutex_trylock(mutex) == 0) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 140 | 0 | return MCSYNC_STATUS_OK; | |||||||||
| 141 | |||||||||||
| 142 | 0 | return MCSYNC_STATUS_NOT_OK; | |||||||||
| 143 | } | ||||||||||
| 144 | |||||||||||
| 145 | 0 | mcsync_status_t mcsync_mutex_unlock(void* mutex) | |||||||||
| 146 | { | ||||||||||
| 147 | 0 | ▶ | if(pthread_mutex_unlock(mutex) == 0) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 148 | 0 | return MCSYNC_STATUS_OK; | |||||||||
| 149 | |||||||||||
| 150 | 0 | return MCSYNC_STATUS_NOT_OK; | |||||||||
| 151 | } | ||||||||||
| 152 | |||||||||||
| 153 | #endif | ||||||||||