Changeset 1206 for trunk/xcache/xc_lock.c
- Timestamp:
- 12/18/2012 05:31:22 AM (5 months ago)
- Files:
-
- 1 modified
-
trunk/xcache/xc_lock.c (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/xcache/xc_lock.c
r1204 r1206 12 12 #endif 13 13 14 /* {{{ detect what lock to use */ 15 #undef XC_INTERPROCESS_LOCK_IMPLEMENTED 16 #undef XC_LOCK_UNSUED 17 18 #ifdef ZEND_WIN32 19 # define XC_INTERPROCESS_LOCK_IMPLEMENTED 20 # ifndef ZTS 21 # define XC_LOCK_UNSUED 14 /* {{{ detect what type of lock is needed */ 15 #ifdef ZTS 16 # define XC_LOCK_NEED_TS 17 #endif 18 19 #ifndef ZEND_WIN32 20 # define XC_LOCK_NEED_INTERPROCESS 21 #endif 22 23 #if defined(XC_LOCK_NEED_TS) && defined(XC_LOCK_NEED_INTERPROCESS) 24 /* allow switching off interprocess support */ 25 # define XC_LOCK_HAVE_INTERPROCESS_SWITCH 26 #endif 27 /* }}} */ 28 29 /* {{{ detect which lock is needed */ 30 #if defined(XC_LOCK_NEED_TS) && defined(XC_LOCK_NEED_INTERPROCESS) 31 # ifdef PTHREAD 32 # define XC_LOCK_USE_PTHREAD 33 # ifndef _POSIX_THREAD_PROCESS_SHARED 34 # define XC_LOCK_USE_FCNTL 35 # endif 36 # else 37 # define XC_LOCK_USE_TSRM 38 # define XC_LOCK_USE_FCNTL 22 39 # endif 23 #endif 24 25 #ifdef _POSIX_THREAD_PROCESS_SHARED 26 # include "../mod_cacher/xc_cache.h" 27 # define XC_INTERPROCESS_LOCK_IMPLEMENTED 28 #endif 29 /* }}} */ 30 31 #ifndef XC_INTERPROCESS_LOCK_IMPLEMENTED 32 40 #elif defined(XC_LOCK_NEED_TS) 41 # define XC_LOCK_USE_TSRM 42 #elif defined(XC_LOCK_NEED_INTERPROCESS) 43 # define XC_LOCK_USE_FCNTL 44 #else 45 # define XC_LOCK_USE_NOOP 46 #endif 47 /* }}} */ 48 49 /* {{{ fcntl lock impl */ 50 #ifdef XC_LOCK_USE_FCNTL 33 51 #ifndef ZEND_WIN32 34 52 typedef int HANDLE; … … 52 70 } xc_fcntl_lock_t; 53 71 54 /* {{{ fcntl lock impl */55 72 #ifndef ZEND_WIN32 56 73 # define LCK_WR F_WRLCK … … 183 200 } 184 201 /* }}} */ 185 #endif /* XC_ INTERPROCESS_LOCK_IMPLEMENTED*/202 #endif /* XC_LOCK_USE_FCNTL */ 186 203 187 204 struct _xc_lock_t { 188 #ifdef XC_LOCK_U NSUED205 #ifdef XC_LOCK_USE_NOOP 189 206 int dummy; 190 #else 191 # ifdef ZTS 207 #endif 208 209 #ifdef XC_LOCK_USE_TSRM 192 210 MUTEX_T tsrm_mutex; 193 # endif 194 195 # ifdef _POSIX_THREAD_PROCESS_SHARED 211 #endif 212 213 #ifdef XC_LOCK_HAVE_INTERPROCESS_SWITCH 214 zend_bool interprocess; 215 #endif 216 217 #ifdef XC_LOCK_USE_PTHREAD 196 218 pthread_mutex_t pthread_mutex; 197 # endif 198 # ifndef XC_INTERPROCESS_LOCK_IMPLEMENTED 199 # ifdef ZTS 200 zend_bool use_fcntl; 201 # endif 219 #endif 220 221 #ifdef XC_LOCK_USE_FCNTL 202 222 xc_fcntl_lock_t fcntl_lock; 203 # endif204 223 #endif 205 224 … … 209 228 }; 210 229 230 #ifdef XC_LOCK_HAVE_INTERPROCESS_SWITCH 231 # define XC_LOCK_INTERPROCESS (lck->interprocess) 232 #else 233 # define XC_LOCK_INTERPROCESS 1 234 #endif 235 211 236 size_t xc_lock_size(void) /* {{{ */ 212 237 { … … 216 241 xc_lock_t *xc_lock_init(xc_lock_t *lck, const char *pathname, unsigned char interprocess) /* {{{ */ 217 242 { 218 #ifdef ZTS 219 # ifdef _POSIX_THREAD_PROCESS_SHARED 220 pthread_mutexattr_t psharedm; 221 pthread_mutexattr_init(&psharedm); 222 pthread_mutexattr_setpshared(&psharedm, PTHREAD_PROCESS_SHARED); 223 pthread_mutex_init(&lck->pthread_mutex, &psharedm); 224 lck->tsrm_mutex = &lck->pthread_mutex; 225 # else 243 #ifdef XC_LOCK_HAVE_INTERPROCESS_SWITCH 244 lck->interprocess = interprocess; 245 #endif 246 247 #ifdef XC_LOCK_USE_PTHREAD 248 { 249 pthread_mutexattr_t psharedm; 250 pthread_mutexattr_init(&psharedm); 251 pthread_mutexattr_setpshared(&psharedm, XC_LOCK_INTERPROCESS ? PTHREAD_PROCESS_PRIVATE : PTHREAD_PROCESS_SHARED); 252 pthread_mutex_init(&lck->pthread_mutex, &psharedm); 253 } 254 #endif 255 256 #ifdef XC_LOCK_USE_TSRM 226 257 lck->tsrm_mutex = tsrm_mutex_alloc(); 227 # endif 228 #endif 229 230 #ifndef XC_INTERPROCESS_LOCK_IMPLEMENTED 231 # ifdef ZTS 232 lck->use_fcntl = interprocess; 233 if (lck->use_fcntl) 234 # endif 258 #endif 259 260 #ifdef XC_LOCK_USE_FCNTL 261 if (XC_LOCK_INTERPROCESS) { 235 262 xc_fcntl_init(&lck->fcntl_lock, pathname); 263 } 236 264 #endif 237 265 … … 245 273 void xc_lock_destroy(xc_lock_t *lck) /* {{{ */ 246 274 { 247 #ifdef ZTS 248 # ifdef _POSIX_THREAD_PROCESS_SHARED 275 #ifdef XC_LOCK_USE_PTHREAD 249 276 pthread_mutex_destroy(&lck->pthread_mutex); 250 # else 277 #endif 278 279 #ifdef XC_LOCK_USE_TSRM 251 280 tsrm_mutex_free(lck->tsrm_mutex); 252 # endif253 281 lck->tsrm_mutex = NULL; 254 282 #endif 255 283 256 #ifndef XC_INTERPROCESS_LOCK_IMPLEMENTED 257 # ifdef ZTS 258 if (lck->use_fcntl) 259 # endif 284 #ifdef XC_LOCK_USE_FCNTL 285 if (XC_LOCK_INTERPROCESS) { 260 286 xc_fcntl_destroy(&lck->fcntl_lock); 287 } 261 288 #endif 262 289 } … … 264 291 void xc_lock(xc_lock_t *lck) /* {{{ */ 265 292 { 266 #ifdef XC_LOCK_UNSUED 267 #else 268 # ifdef ZTS 293 #ifdef XC_LOCK_USE_PTHREAD 294 if (pthread_mutex_lock(&lck->pthread_mutex) < 0) { 295 zend_error(E_ERROR, "xc_lock failed errno:%d", errno); 296 } 297 #endif 298 299 #ifdef XC_LOCK_USE_TSRM 269 300 if (tsrm_mutex_lock(lck->tsrm_mutex) < 0) { 270 301 zend_error(E_ERROR, "xc_lock failed errno:%d", errno); 271 302 } 272 # endif 273 # ifndef XC_INTERPROCESS_LOCK_IMPLEMENTED 274 # ifdef ZTS 275 if (lck->use_fcntl) 276 # endif 303 #endif 304 305 #ifdef XC_LOCK_USE_FCNTL 306 if (XC_LOCK_INTERPROCESS) { 277 307 xc_fcntl_lock(&lck->fcntl_lock); 278 # endif 308 } 279 309 #endif 280 310 … … 288 318 void xc_rdlock(xc_lock_t *lck) /* {{{ */ 289 319 { 290 #ifdef XC_LOCK_UNSUED 291 #else 292 # ifdef ZTS 320 #ifdef XC_LOCK_USE_PTHREAD 321 if (pthread_mutex_lock(&lck->pthread_mutex) < 0) { 322 zend_error(E_ERROR, "xc_rdlock failed errno:%d", errno); 323 } 324 #endif 325 326 #ifdef XC_LOCK_USE_TSRM 293 327 if (tsrm_mutex_lock(lck->tsrm_mutex) < 0) { 294 328 zend_error(E_ERROR, "xc_rdlock failed errno:%d", errno); 295 329 } 296 # endif 297 # ifndef XC_INTERPROCESS_LOCK_IMPLEMENTED 298 # ifdef ZTS 299 if (lck->use_fcntl) 300 # endif 330 #endif 331 332 #ifdef XC_LOCK_USE_FCNTL 333 if (XC_LOCK_INTERPROCESS) { 301 334 xc_fcntl_lock(&lck->fcntl_lock); 302 # endif 335 } 303 336 #endif 304 337 … … 318 351 #endif 319 352 320 #ifdef XC_LOCK_UNSUED 321 #else 322 # ifndef XC_INTERPROCESS_LOCK_IMPLEMENTED 323 # ifdef ZTS 324 if (lck->use_fcntl) 325 # endif 353 #ifdef XC_LOCK_USE_FCNTL 354 if (XC_LOCK_INTERPROCESS) { 326 355 xc_fcntl_unlock(&lck->fcntl_lock); 327 # endif 328 #endif 329 # ifdef ZTS 356 } 357 #endif 358 359 #ifdef XC_LOCK_USE_TSRM 330 360 if (tsrm_mutex_unlock(lck->tsrm_mutex) < 0) { 331 361 zend_error(E_ERROR, "xc_unlock failed errno:%d", errno); 332 362 } 333 # endif 334 } 335 /* }}} */ 363 #endif 364 365 #ifdef XC_LOCK_USE_PTHREAD 366 if (pthread_mutex_unlock(&lck->pthread_mutex) < 0) { 367 zend_error(E_ERROR, "xc_unlock failed errno:%d", errno); 368 } 369 #endif 370 } 371 /* }}} */

