Changeset 1206


Ignore:
Timestamp:
2012-12-18T05:31:22+01:00 (22 months ago)
Author:
moo
Message:

mutex code clean up

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/xcache/xc_lock.c

    r1204 r1206  
    1212#endif 
    1313 
    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 
    2239#   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 
    3351#ifndef ZEND_WIN32 
    3452typedef int HANDLE; 
     
    5270} xc_fcntl_lock_t; 
    5371 
    54 /* {{{ fcntl lock impl */ 
    5572#ifndef ZEND_WIN32 
    5673#   define LCK_WR F_WRLCK 
     
    183200} 
    184201/* }}} */ 
    185 #endif /* XC_INTERPROCESS_LOCK_IMPLEMENTED */ 
     202#endif /* XC_LOCK_USE_FCNTL */ 
    186203 
    187204struct _xc_lock_t { 
    188 #ifdef XC_LOCK_UNSUED 
     205#ifdef XC_LOCK_USE_NOOP 
    189206    int dummy; 
    190 #else 
    191 #   ifdef ZTS 
     207#endif 
     208 
     209#ifdef XC_LOCK_USE_TSRM 
    192210    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 
    196218    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 
    202222    xc_fcntl_lock_t fcntl_lock; 
    203 #   endif 
    204223#endif 
    205224 
     
    209228}; 
    210229 
     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 
    211236size_t xc_lock_size(void) /* {{{ */ 
    212237{ 
     
    216241xc_lock_t *xc_lock_init(xc_lock_t *lck, const char *pathname, unsigned char interprocess) /* {{{ */ 
    217242{ 
    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 
    226257    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) { 
    235262        xc_fcntl_init(&lck->fcntl_lock, pathname); 
     263    } 
    236264#endif 
    237265 
     
    245273void xc_lock_destroy(xc_lock_t *lck) /* {{{ */ 
    246274{ 
    247 #ifdef ZTS 
    248 #   ifdef _POSIX_THREAD_PROCESS_SHARED 
     275#ifdef XC_LOCK_USE_PTHREAD 
    249276    pthread_mutex_destroy(&lck->pthread_mutex); 
    250 #   else 
     277#endif 
     278 
     279#ifdef XC_LOCK_USE_TSRM 
    251280    tsrm_mutex_free(lck->tsrm_mutex); 
    252 #   endif 
    253281    lck->tsrm_mutex = NULL; 
    254282#endif 
    255283 
    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) { 
    260286        xc_fcntl_destroy(&lck->fcntl_lock); 
     287    } 
    261288#endif 
    262289} 
     
    264291void xc_lock(xc_lock_t *lck) /* {{{ */ 
    265292{ 
    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 
    269300    if (tsrm_mutex_lock(lck->tsrm_mutex) < 0) { 
    270301        zend_error(E_ERROR, "xc_lock failed errno:%d", errno); 
    271302    } 
    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) { 
    277307        xc_fcntl_lock(&lck->fcntl_lock); 
    278 #   endif 
     308    } 
    279309#endif 
    280310 
     
    288318void xc_rdlock(xc_lock_t *lck) /* {{{ */ 
    289319{ 
    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 
    293327    if (tsrm_mutex_lock(lck->tsrm_mutex) < 0) { 
    294328        zend_error(E_ERROR, "xc_rdlock failed errno:%d", errno); 
    295329    } 
    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) { 
    301334        xc_fcntl_lock(&lck->fcntl_lock); 
    302 #   endif 
     335    } 
    303336#endif 
    304337 
     
    318351#endif 
    319352 
    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) { 
    326355        xc_fcntl_unlock(&lck->fcntl_lock); 
    327 #   endif 
    328 #endif 
    329 #   ifdef ZTS 
     356    } 
     357#endif 
     358 
     359#ifdef XC_LOCK_USE_TSRM 
    330360    if (tsrm_mutex_unlock(lck->tsrm_mutex) < 0) { 
    331361        zend_error(E_ERROR, "xc_unlock failed errno:%d", errno); 
    332362    } 
    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/* }}} */ 
Note: See TracChangeset for help on using the changeset viewer.