Changeset 1204 in svn for trunk/xcache/xc_lock.c


Ignore:
Timestamp:
2012-12-17T18:11:58+01:00 (2 years ago)
Author:
Xuefer
Message:

finished improve stability on threaded env

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/xcache/xc_lock.c

    r1200 r1204  
    1111#   include <errno.h>
    1212#endif
     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
     22#   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
    1332
    1433#ifndef ZEND_WIN32
     
    7493
    7594    if (type == LCK_UN) {
    76         return UnlockFileEx((HANDLE)lck->fd, 0, 1, 0, &offset);
     95        return UnlockFileEx(lck->fd, 0, 1, 0, &offset);
    7796    }
    7897    else {
    79         return LockFileEx((HANDLE)lck->fd, type, 0, 1, 0, &offset);
     98        return LockFileEx(lck->fd, type, 0, 1, 0, &offset);
    8099    }
    81100}
     
    164183}
    165184/* }}} */
    166 
    167 #undef XC_INTERPROCESS_LOCK_IMPLEMENTED
    168 #undef XC_LOCK_UNSUED
    169 
    170 #ifdef ZEND_WIN32
    171 #   define XC_INTERPROCESS_LOCK_IMPLEMENTED
    172 #   ifndef ZTS
    173 #       define XC_LOCK_UNSUED
    174 #   endif
    175 #endif
    176 
    177 #if defined(PTHREADS)
    178 #   define XC_INTERPROCESS_LOCK_IMPLEMENTED
    179 #endif
     185#endif /* XC_INTERPROCESS_LOCK_IMPLEMENTED */
    180186
    181187struct _xc_lock_t {
     
    187193#   endif
    188194
     195#   ifdef _POSIX_THREAD_PROCESS_SHARED
     196    pthread_mutex_t pthread_mutex;
     197#   endif
    189198#   ifndef XC_INTERPROCESS_LOCK_IMPLEMENTED
    190199#       ifdef ZTS
     
    194203#   endif
    195204#endif
     205
     206#ifndef NDEBUG
     207    zend_bool locked;
     208#endif
    196209};
    197210
    198 xc_lock_t *xc_lock_init(const char *pathname, int interprocess) /* {{{ */
    199 {
    200 #ifdef XC_LOCK_UNSUED
    201     return (xc_lock_t *) 1;
    202 #else
    203     xc_lock_t *lck = malloc(sizeof(xc_lock_t));
    204 #   ifdef ZTS
    205 #       if defined(PTHREADS)
     211size_t xc_lock_size(void) /* {{{ */
     212{
     213    return sizeof(xc_lock_t);
     214}
     215/* }}} */
     216xc_lock_t *xc_lock_init(xc_lock_t *lck, const char *pathname, unsigned char interprocess) /* {{{ */
     217{
     218#ifdef ZTS
     219#   ifdef _POSIX_THREAD_PROCESS_SHARED
    206220    pthread_mutexattr_t psharedm;
    207221    pthread_mutexattr_init(&psharedm);
    208222    pthread_mutexattr_setpshared(&psharedm, PTHREAD_PROCESS_SHARED);
    209     lck->tsrm_mutex = (pthread_mutex_t *) malloc(sizeof(pthread_mutex_t));
    210     pthread_mutex_init(lck->tsrm_mutex, &psharedm);
    211 #       else
     223    pthread_mutex_init(&lck->pthread_mutex, &psharedm);
     224    lck->tsrm_mutex = &lck->pthread_mutex;
     225#   else
    212226    lck->tsrm_mutex = tsrm_mutex_alloc();
    213 #       endif
     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
     235        xc_fcntl_init(&lck->fcntl_lock, pathname);
     236#endif
     237
     238#ifndef NDEBUG
     239    lck->locked = 0;
     240#endif
     241
     242    return lck;
     243}
     244/* }}} */
     245void xc_lock_destroy(xc_lock_t *lck) /* {{{ */
     246{
     247#ifdef ZTS
     248#   ifdef _POSIX_THREAD_PROCESS_SHARED
     249    pthread_mutex_destroy(&lck->pthread_mutex);
     250#   else
     251    tsrm_mutex_free(lck->tsrm_mutex);
     252#   endif
     253    lck->tsrm_mutex = NULL;
     254#endif
     255
     256#ifndef XC_INTERPROCESS_LOCK_IMPLEMENTED
     257#   ifdef ZTS
     258    if (lck->use_fcntl)
     259#   endif
     260        xc_fcntl_destroy(&lck->fcntl_lock);
     261#endif
     262}
     263/* }}} */
     264void xc_lock(xc_lock_t *lck) /* {{{ */
     265{
     266#ifdef XC_LOCK_UNSUED
     267#else
     268#   ifdef ZTS
     269    if (tsrm_mutex_lock(lck->tsrm_mutex) < 0) {
     270        zend_error(E_ERROR, "xc_lock failed errno:%d", errno);
     271    }
    214272#   endif
    215273#   ifndef XC_INTERPROCESS_LOCK_IMPLEMENTED
    216274#       ifdef ZTS
    217     lck->use_fcntl = interprocess;
    218275    if (lck->use_fcntl)
    219276#       endif
    220         xc_fcntl_init(&lck->fcntl_lock, pathname);
    221 #   endif
    222     return lck;
    223 #endif
    224 }
    225 /* }}} */
    226 void xc_lock_destroy(xc_lock_t *lck) /* {{{ */
     277        xc_fcntl_lock(&lck->fcntl_lock);
     278#   endif
     279#endif
     280
     281#ifndef NDEBUG
     282    assert(!lck->locked);
     283    lck->locked = 1;
     284    assert(lck->locked);
     285#endif
     286}
     287/* }}} */
     288void xc_rdlock(xc_lock_t *lck) /* {{{ */
    227289{
    228290#ifdef XC_LOCK_UNSUED
    229     /* do nothing */
    230 #else
    231 #   ifdef ZTS
    232     tsrm_mutex_free(lck->tsrm_mutex);
     291#else
     292#   ifdef ZTS
     293    if (tsrm_mutex_lock(lck->tsrm_mutex) < 0) {
     294        zend_error(E_ERROR, "xc_rdlock failed errno:%d", errno);
     295    }
    233296#   endif
    234297#   ifndef XC_INTERPROCESS_LOCK_IMPLEMENTED
     
    236299    if (lck->use_fcntl)
    237300#       endif
    238         xc_fcntl_destroy(&lck->fcntl_lock);
    239 #   endif
    240     free(lck);
    241 #endif
    242 }
    243 /* }}} */
    244 void xc_lock(xc_lock_t *lck) /* {{{ */
    245 {
     301        xc_fcntl_lock(&lck->fcntl_lock);
     302#   endif
     303#endif
     304
     305#ifndef NDEBUG
     306    assert(!lck->locked);
     307    lck->locked = 1;
     308    assert(lck->locked);
     309#endif
     310}
     311/* }}} */
     312void xc_unlock(xc_lock_t *lck) /* {{{ */
     313{
     314#ifndef NDEBUG
     315    assert(lck->locked);
     316    lck->locked = 0;
     317    assert(!lck->locked);
     318#endif
     319
    246320#ifdef XC_LOCK_UNSUED
    247321#else
    248 #   ifdef ZTS
    249     if (tsrm_mutex_lock(lck->tsrm_mutex) < 0) {
    250         zend_error(E_ERROR, "xc_lock failed errno:%d", errno);
    251     }
    252 #   endif
    253322#   ifndef XC_INTERPROCESS_LOCK_IMPLEMENTED
    254323#       ifdef ZTS
    255324    if (lck->use_fcntl)
    256325#       endif
    257         xc_fcntl_lock(&lck->fcntl_lock);
    258 #   endif
    259 #endif
    260 }
    261 /* }}} */
    262 void xc_rdlock(xc_lock_t *lck) /* {{{ */
    263 {
    264 #ifdef XC_LOCK_UNSUED
    265 #else
    266 #   ifdef ZTS
    267     if (tsrm_mutex_lock(lck->tsrm_mutex) < 0) {
    268         zend_error(E_ERROR, "xc_rdlock failed errno:%d", errno);
    269     }
    270 #   endif
    271 #   ifndef XC_INTERPROCESS_LOCK_IMPLEMENTED
    272 #       ifdef ZTS
    273     if (lck->use_fcntl)
    274 #       endif
    275         xc_fcntl_lock(&lck->fcntl_lock);
    276 #   endif
    277 #endif
    278 }
    279 /* }}} */
    280 void xc_unlock(xc_lock_t *lck) /* {{{ */
    281 {
    282 #ifdef XC_LOCK_UNSUED
    283 #else
    284 #   ifndef XC_INTERPROCESS_LOCK_IMPLEMENTED
    285 #       ifdef ZTS
    286     if (lck->use_fcntl)
    287 #       endif
    288326        xc_fcntl_unlock(&lck->fcntl_lock);
    289327#   endif
Note: See TracChangeset for help on using the changeset viewer.