source/myhtml/utils/mcsync.c

filter: Statementstmt 82.7% 24 / 29 Branchbran 50.0% 3 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 77.1% 27 / 35 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« source/myhtml/utils/mcsimple.c ↑ Summary blib/lib/HTML/MyHTML.pm »
1 /*
2 Copyright 2015 Alexander Borisov
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15
16 Author: lex.borisov@gmail.com (Alexander Borisov)
17 */
18  
19 #include "myhtml/utils/mcsync.h"
20  
21 #if !defined(MyHTML_WITHOUT_THREADS) && ((defined(__GNUC__) && __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) || \
22 defined(__ATOMIC_SEQ_CST))
23 #define MyHTML_FORCE_SPINLOCK
24 #endif
25  
26 #if defined(MyHTML_FORCE_SPINLOCK)
27 13 static int mcsync_atomic_compare_exchange(int* ptr, int compare, int exchange)
28 {
29 13 return __atomic_compare_exchange_n(ptr, &compare, exchange, 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
30 }
31  
32 13 static void mcsync_atomic_store(int* ptr, int value)
33 {
34 13 __atomic_store_n(ptr, 0, __ATOMIC_SEQ_CST);
35 13 }
36 #endif
37  
38 #if !defined(MyHTML_WITHOUT_THREADS) && defined(IS_OS_WINDOWS)
39 static int pthread_mutex_lock(pthread_mutex_t *mutex)
40 {
41 EnterCriticalSection(mutex);
42 return 0;
43 }
44  
45 static int pthread_mutex_unlock(pthread_mutex_t *mutex)
46 {
47 LeaveCriticalSection(mutex);
48 return 0;
49 }
50  
51 static int pthread_mutex_init(pthread_mutex_t *mutex, pthread_mutexattr_t *attr)
52 {
53 (void)attr;
54 InitializeCriticalSection(mutex);
55 return 0;
56 }
57  
58 static int pthread_mutex_destroy(pthread_mutex_t *mutex)
59 {
60 DeleteCriticalSection(mutex);
61 return 0;
62 }
63 #endif
64  
65 8 mcsync_t * mcsync_create(void)
66 {
67 8 return calloc(1, sizeof(mcsync_t));
68 }
69  
70 1 mcsync_status_t mcsync_init(mcsync_t* mcsync)
71 {
72 1 mcsync_clean(mcsync);
73 1 return MCSYNC_STATUS_OK;
74 }
75  
76 8 mcsync_t * mcsync_destroy(mcsync_t* mcsync, int destroy_self)
77 {
78 8 ▶ if(mcsync == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(mcsync == NULL) 0 8 8
79 0 return NULL;
80
81 #if !defined(MyHTML_FORCE_SPINLOCK)
82 if(mcsync->mutex) {
83 pthread_mutex_destroy(mcsync->mutex);
84 free(mcsync->mutex);
85 }
86 #endif
87
88 8 ▶ if(destroy_self)
Branch: 1 branch
BranchTrueFalseTotal
if(destroy_self) 8 0 8
89 8 free(mcsync);
90
91 8 return NULL;
92 }
93  
94 1 void mcsync_clean(mcsync_t* mcsync)
95 {
96 1 mcsync->spinlock = 0;
97 1 }
98  
99 13 mcsync_status_t mcsync_lock(mcsync_t* mcsync)
100 {
101 #if defined(MyHTML_FORCE_SPINLOCK)
102 13 ▶ while (!mcsync_atomic_compare_exchange(&mcsync->spinlock, 0, 1)) {}
Branch: 1 branch
BranchTrueFalseTotal
while (!mcsync_atomic_compare_exchange(&mcsync->spinlock, 0, 1)) {} 0 13 13
103 #else
104 mcsync_mutex_lock(mcsync);
105 #endif
106
107 13 return MCSYNC_STATUS_OK;
108 }
109  
110 13 mcsync_status_t mcsync_unlock(mcsync_t* mcsync)
111 {
112 #if defined(MyHTML_FORCE_SPINLOCK)
113 13 mcsync_atomic_store(&mcsync->spinlock, 0);
114 #else
115 mcsync_mutex_unlock(mcsync);
116 #endif
117
118 13 return MCSYNC_STATUS_OK;
119 }
120  
121 0 mcsync_status_t mcsync_mutex_lock(mcsync_t* mcsync)
122 {
123 #if !defined(MyHTML_FORCE_SPINLOCK)
124 if(mcsync->mutex == NULL) {
125 mcsync->mutex = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t));
126
127 if(mcsync->mutex == NULL)
128 return MCSYNC_STATUS_ERROR_MEM_ALLOCATE;
129
130 pthread_mutex_init(mcsync->mutex, NULL);
131 }
132
133 if(pthread_mutex_lock(mcsync->mutex) == 0)
134 return MCSYNC_STATUS_OK;
135 else
136 return MCSYNC_STATUS_NOT_OK;
137 #else
138 0 return MCSYNC_STATUS_NOT_OK;
139 #endif
140 }
141  
142 0 mcsync_status_t mcsync_mutex_unlock(mcsync_t* mcsync)
143 {
144 #if !defined(MyHTML_FORCE_SPINLOCK)
145 if(pthread_mutex_unlock(mcsync->mutex) == 0)
146 return MCSYNC_STATUS_OK;
147 else
148 return MCSYNC_STATUS_NOT_OK;
149 #else
150 0 return MCSYNC_STATUS_NOT_OK;
151 #endif
152 }
153  
« source/myhtml/utils/mcsimple.c ↑ Summary blib/lib/HTML/MyHTML.pm »