| line | stmt | bran | cond | sub | pod | time | code | 
| 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 | 799 |  |  |  |  |  | mcsync_t * mcsync_create(void) | 
| 24 |  |  |  |  |  |  | { | 
| 25 | 799 |  |  |  |  |  | return mycore_calloc(1, sizeof(mcsync_t)); | 
| 26 |  |  |  |  |  |  | } | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  |  | 
| 30 | 799 |  |  |  |  |  | mcsync_status_t mcsync_init(mcsync_t* mcsync) | 
| 31 |  |  |  |  |  |  | { | 
| 32 |  |  |  |  |  |  | #ifndef MyCORE_BUILD_WITHOUT_THREADS | 
| 33 |  |  |  |  |  |  | /* spinlock */ | 
| 34 | 799 | 50 |  |  |  |  | if((mcsync->spinlock = mcsync_spin_create()) == NULL) | 
| 35 | 0 |  |  |  |  |  | return MCSYNC_STATUS_NOT_OK; | 
| 36 |  |  |  |  |  |  |  | 
| 37 | 799 |  |  |  |  |  | mcsync_status_t status = mcsync_spin_init(mcsync->spinlock); | 
| 38 | 799 | 50 |  |  |  |  | if(status) { | 
| 39 | 0 |  |  |  |  |  | mcsync_spin_destroy(mcsync->spinlock); | 
| 40 | 0 |  |  |  |  |  | return status; | 
| 41 |  |  |  |  |  |  | } | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | /* mutex */ | 
| 44 | 799 | 50 |  |  |  |  | if((mcsync->mutex = mcsync_mutex_create()) == NULL) | 
| 45 | 0 |  |  |  |  |  | return MCSYNC_STATUS_NOT_OK; | 
| 46 |  |  |  |  |  |  |  | 
| 47 | 799 | 50 |  |  |  |  | if((status = mcsync_mutex_init(mcsync->mutex))) { | 
| 48 | 0 |  |  |  |  |  | mcsync_spin_destroy(mcsync->spinlock); | 
| 49 | 0 |  |  |  |  |  | mcsync_mutex_destroy(mcsync->mutex); | 
| 50 |  |  |  |  |  |  |  | 
| 51 | 0 |  |  |  |  |  | return status; | 
| 52 |  |  |  |  |  |  | } | 
| 53 |  |  |  |  |  |  | #endif | 
| 54 |  |  |  |  |  |  |  | 
| 55 | 799 |  |  |  |  |  | return MCSYNC_STATUS_OK; | 
| 56 |  |  |  |  |  |  | } | 
| 57 |  |  |  |  |  |  |  | 
| 58 | 0 |  |  |  |  |  | void mcsync_clean(mcsync_t* mcsync) | 
| 59 |  |  |  |  |  |  | { | 
| 60 |  |  |  |  |  |  | #ifndef MyCORE_BUILD_WITHOUT_THREADS | 
| 61 | 0 |  |  |  |  |  | mcsync_spin_clean(mcsync->spinlock); | 
| 62 | 0 |  |  |  |  |  | mcsync_mutex_clean(mcsync->mutex); | 
| 63 |  |  |  |  |  |  | #endif | 
| 64 | 0 |  |  |  |  |  | } | 
| 65 |  |  |  |  |  |  |  | 
| 66 | 794 |  |  |  |  |  | mcsync_t * mcsync_destroy(mcsync_t* mcsync, int destroy_self) | 
| 67 |  |  |  |  |  |  | { | 
| 68 |  |  |  |  |  |  | #ifndef MyCORE_BUILD_WITHOUT_THREADS | 
| 69 | 794 | 50 |  |  |  |  | if(mcsync == NULL) | 
| 70 | 0 |  |  |  |  |  | return NULL; | 
| 71 |  |  |  |  |  |  |  | 
| 72 | 794 |  |  |  |  |  | mcsync_spin_destroy(mcsync->spinlock); | 
| 73 | 794 |  |  |  |  |  | mcsync_mutex_destroy(mcsync->mutex); | 
| 74 |  |  |  |  |  |  | #endif | 
| 75 | 794 | 50 |  |  |  |  | if(destroy_self) { | 
| 76 | 794 |  |  |  |  |  | mycore_free(mcsync); | 
| 77 | 794 |  |  |  |  |  | return NULL; | 
| 78 |  |  |  |  |  |  | } | 
| 79 |  |  |  |  |  |  |  | 
| 80 | 0 |  |  |  |  |  | return mcsync; | 
| 81 |  |  |  |  |  |  | } | 
| 82 |  |  |  |  |  |  |  | 
| 83 | 1314 |  |  |  |  |  | mcsync_status_t mcsync_lock(mcsync_t* mcsync) | 
| 84 |  |  |  |  |  |  | { | 
| 85 |  |  |  |  |  |  | #ifndef MyCORE_BUILD_WITHOUT_THREADS | 
| 86 | 1314 |  |  |  |  |  | return mcsync_spin_lock(mcsync->spinlock); | 
| 87 |  |  |  |  |  |  | #else | 
| 88 |  |  |  |  |  |  | return MCSYNC_STATUS_OK; | 
| 89 |  |  |  |  |  |  | #endif | 
| 90 |  |  |  |  |  |  | } | 
| 91 |  |  |  |  |  |  |  | 
| 92 | 1314 |  |  |  |  |  | mcsync_status_t mcsync_unlock(mcsync_t* mcsync) | 
| 93 |  |  |  |  |  |  | { | 
| 94 |  |  |  |  |  |  | #ifndef MyCORE_BUILD_WITHOUT_THREADS | 
| 95 | 1314 |  |  |  |  |  | return mcsync_spin_unlock(mcsync->spinlock); | 
| 96 |  |  |  |  |  |  | #else | 
| 97 |  |  |  |  |  |  | return MCSYNC_STATUS_OK; | 
| 98 |  |  |  |  |  |  | #endif | 
| 99 |  |  |  |  |  |  | } |