1 /*
2     OW -- One-Wire filesystem
3     version 0.4 7/2/2003
4 
5 
6     Written 2003 Paul H Alfille
7         Fuse code based on "fusexmp" {GPL} by Miklos Szeredi, mszeredi@inf.bme.hu
8         Serial code based on "xt" {GPL} by David Querbach, www.realtime.bc.ca
9         in turn based on "miniterm" by Sven Goldt, goldt@math.tu.berlin.de
10     GPL license
11     This program is free software; you can redistribute it and/or
12     modify it under the terms of the GNU General Public License
13     as published by the Free Software Foundation; either version 2
14     of the License, or (at your option) any later version.
15 
16     This program is distributed in the hope that it will be useful,
17     but WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19     GNU General Public License for more details.
20 
21     Other portions based on Dallas Semiconductor Public Domain Kit,
22     ---------------------------------------------------------------------------
23     Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved.
24         Permission is hereby granted, free of charge, to any person obtaining a
25         copy of this software and associated documentation files (the "Software"),
26         to deal in the Software without restriction, including without limitation
27         the rights to use, copy, modify, merge, publish, distribute, sublicense,
28         and/or sell copies of the Software, and to permit persons to whom the
29         Software is furnished to do so, subject to the following conditions:
30         The above copyright notice and this permission notice shall be included
31         in all copies or substantial portions of the Software.
32     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
33     OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
34     MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
35     IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES
36     OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
37     ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
38     OTHER DEALINGS IN THE SOFTWARE.
39         Except as contained in this notice, the name of Dallas Semiconductor
40         shall not be used except as stated in the Dallas Semiconductor
41         Branding Policy.
42     ---------------------------------------------------------------------------
43     Implementation:
44     25-05-2003 iButtonLink device
45 */
46 
47 
48 /* Cannot stand alone -- part of ow.h and only separated out for clarity */
49 
50 #ifndef OW_MUTEX_H			/* tedious wrapper */
51 #define OW_MUTEX_H
52 
53 extern const char mutex_init_failed[];
54 extern const char mutex_destroy_failed[];
55 extern const char mutex_lock_failed[];
56 extern const char mutex_unlock_failed[];
57 extern const char mutexattr_init_failed[];
58 extern const char mutexattr_destroy_failed[];
59 extern const char mutexattr_settype_failed[];
60 extern const char rwlock_init_failed[];
61 extern const char rwlock_read_lock_failed[];
62 extern const char rwlock_read_unlock_failed[];
63 extern const char cond_timedwait_failed[];
64 extern const char cond_signal_failed[];
65 extern const char cond_broadcast_failed[];
66 extern const char cond_wait_failed[];
67 extern const char cond_init_failed[];
68 extern const char cond_destroy_failed[];
69 extern const char sem_init_failed[];
70 extern const char sem_post_failed[];
71 extern const char sem_wait_failed[];
72 extern const char sem_trywait_failed[];
73 extern const char sem_timedwait_failed[];
74 extern const char sem_destroy_failed[];
75 
76 #define LOCK_DEBUG(...)  if ( Globals.locks != 0 ) { LEVEL_DEFAULT(__VA_ARGS__) ; }
77 
78 /* FreeBSD might fail on sem_init() since they are limited per machine */
79 #define my_sem_init(sem, shared, value)                                 \
80 	do {																\
81 		int mrc = sem_init(sem, shared, value);							\
82 		if(mrc != 0) {													\
83 			FATAL_ERROR(sem_init_failed, mrc, strerror(mrc));			\
84 		}																\
85 		LOCK_DEBUG("sem_init %lX, %d, %d\n", (unsigned long)sem, shared, value); \
86 	} while(0)
87 
88 #define my_sem_post(sem)                                                \
89 	do {																\
90 		int mrc = sem_post(sem);										\
91 		if(mrc != 0) {													\
92 			FATAL_ERROR(sem_post_failed, mrc, strerror(mrc));			\
93 		}																\
94 		LOCK_DEBUG("sem_post %lX done", (unsigned long)sem);		\
95 	} while(0)
96 
97 #define my_sem_wait(sem)                                                \
98 	do {																\
99 		int mrc = sem_wait(sem);										\
100 		if(mrc != 0) {													\
101 			FATAL_ERROR(sem_wait_failed, mrc, strerror(mrc));			\
102 		}																\
103 		LOCK_DEBUG("sem_wait %lX done", (unsigned long)sem);		\
104 	} while(0)
105 
106 #define my_sem_trywait(sem, res)                                        \
107 	do {																\
108 		int mrc = sem_trywait(sem);										\
109 		if((mrc < 0) && (errno != EAGAIN)) {							\
110 			FATAL_ERROR(sem_trywait_failed, mrc, strerror(mrc));		\
111 		}																\
112 		*(res) = mrc;													\
113 		LOCK_DEBUG("sem_trywait %lX done", (unsigned long)sem);	\
114 	} while(0)
115 
116 #define my_sem_timedwait(sem, ts, res)                                  \
117 	do {																\
118 		int mrc;														\
119 		while(((mrc = sem_timedwait(sem, ts)) == -1) && (errno == EINTR)) { \
120 			continue; /* Restart if interrupted by handler */			\
121 		}																\
122 		if((mrc < 0) && (errno != ETIMEDOUT)) {							\
123 			FATAL_ERROR(sem_timedwait_failed, mrc, strerror(mrc));		\
124 		}																\
125 		*(res) = mrc; /* res=-1 timeout, res=0 succeed */				\
126 		LOCK_DEBUG("sem_timedwait %lX done", (unsigned long)sem); \
127 	} while(0)
128 
129 #define my_sem_destroy(sem)                                             \
130 	do {																\
131 		int mrc = sem_destroy(sem);										\
132 		if(mrc != 0) {													\
133 			FATAL_ERROR(sem_destroy_failed, mrc, strerror(mrc));		\
134 		}																\
135 		LOCK_DEBUG("sem_destroy %lX", (unsigned long)sem);		\
136 	} while(0)
137 
138 
139 #define my_pthread_mutex_init(mutex, attr)                              \
140 	do {																\
141 		int mrc;														\
142 		LOCK_DEBUG("pthread_mutex_init %lX begin", (unsigned long)mutex); \
143 		mrc = pthread_mutex_init(mutex, attr);							\
144 		if(mrc != 0) {													\
145 			FATAL_ERROR(mutex_init_failed, mrc, strerror(mrc));			\
146 		}																\
147 		LOCK_DEBUG("pthread_mutex_init %lX done", (unsigned long)mutex); \
148 	} while(0)
149 
150 #define my_pthread_mutex_destroy(mutex)                                 \
151 	do {																\
152 		int mrc = pthread_mutex_destroy(mutex);							\
153 		LOCK_DEBUG("pthread_mutex_destroy %lX begin", (unsigned long)mutex); \
154 		if(mrc != 0) {													\
155 			LEVEL_DEFAULT(mutex_destroy_failed, mrc, strerror(mrc));	\
156 		}																\
157 		LOCK_DEBUG("pthread_mutex_destroy %lX done", (unsigned long)mutex); \
158 	} while(0)
159 
160 #define my_pthread_mutex_lock(mutex)                                    \
161 	do {																\
162 		int mrc;														\
163 		LOCK_DEBUG("pthread_mutex_lock %lX begin", (unsigned long)mutex); \
164 		mrc = pthread_mutex_lock(mutex);								\
165 		if(mrc != 0) {													\
166 			FATAL_ERROR(mutex_lock_failed, mrc, strerror(mrc));			\
167 		}																\
168 		LOCK_DEBUG("pthread_mutex_lock %lX done", (unsigned long)mutex); \
169 	} while(0)
170 
171 #define my_pthread_mutex_unlock(mutex)                                  \
172 	do {																\
173 		int mrc;														\
174 		LOCK_DEBUG("pthread_mutex_unlock %lX begin", (unsigned long)mutex); \
175 		mrc = pthread_mutex_unlock(mutex);								\
176 		if(mrc != 0) {													\
177 			FATAL_ERROR(mutex_unlock_failed, mrc, strerror(mrc));		\
178 		}																\
179 		LOCK_DEBUG("pthread_mutex_unlock %lX done", (unsigned long)mutex); \
180 	} while(0)
181 
182 #define my_pthread_mutexattr_init(attr)                                 \
183 	do {																\
184 		int mrc = pthread_mutexattr_init(attr);							\
185 		if(mrc != 0) {													\
186 			FATAL_ERROR(mutexattr_init_failed, mrc, strerror(mrc));		\
187 		}																\
188 	} while(0)
189 
190 #define my_pthread_mutexattr_destroy(attr)							   \
191 	do {                                                               \
192 		int mrc = pthread_mutexattr_destroy(attr);					   \
193 		if(mrc != 0) {												   \
194 			FATAL_ERROR(mutexattr_destroy_failed, mrc, strerror(mrc)); \
195 		}															   \
196 	} while(0)
197 
198 #define my_pthread_mutexattr_settype(attr, typ)                         \
199 	do {																\
200 		int mrc = pthread_mutexattr_settype(attr, typ);					\
201 		if(mrc != 0) {													\
202 			FATAL_ERROR(mutexattr_settype_failed, mrc, strerror(mrc));	\
203 		}																\
204 	} while(0)
205 
206 #define my_pthread_cond_timedwait(cond, mutex, abstime)				   \
207 	do {                                                               \
208 		int mrc = pthread_cond_timedwait(cond, mutex, abstime);		   \
209 		if(mrc != 0) {												   \
210 			FATAL_ERROR(cond_timedwait_failed, mrc, strerror(mrc));	   \
211 		}															   \
212 	} while(0)
213 
214 #define my_pthread_cond_wait(cond, mutex)							 \
215 	do {                                                             \
216 		int mrc = pthread_cond_wait(cond, mutex);					 \
217 		if(mrc != 0) {												 \
218 			FATAL_ERROR(cond_wait_failed, mrc, strerror(mrc));		 \
219 		}															 \
220 	} while(0)
221 
222 #define my_pthread_cond_signal(cond)								\
223 	do {                                                            \
224 		int mrc = pthread_cond_signal(cond);						\
225 		if(mrc != 0) {												\
226 			FATAL_ERROR(cond_signal_failed, mrc, strerror(mrc));	\
227 		}															\
228 	} while(0)
229 
230 #define my_pthread_cond_broadcast(cond)								   \
231 	do {                                                               \
232 		int mrc = pthread_cond_broadcast(cond);						   \
233 		if(mrc != 0) {												   \
234 			FATAL_ERROR(cond_broadcast_failed, mrc, strerror(mrc));	   \
235 		}															   \
236 	} while(0)
237 
238 #define my_pthread_cond_init(cond, attr)                                \
239 	do {																\
240 		int mrc = pthread_cond_init(cond, attr);						\
241 		if(mrc != 0) {													\
242 			FATAL_ERROR(cond_init_failed, mrc, strerror(mrc));			\
243 		}																\
244 	} while(0)
245 
246 #define my_pthread_cond_destroy(cond)								 \
247 	do {                                                             \
248 		int mrc = pthread_cond_destroy(cond);						 \
249 		if(mrc != 0) {												 \
250 			FATAL_ERROR(cond_destroy_failed, mrc, strerror(mrc));	 \
251 		}															 \
252 	} while(0)
253 
254 #endif
255