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


Ignore:
Timestamp:
2012-12-17T10:18:22Z (3 years ago)
Author:
Xuefer
Message:

fix for ZTS

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/xcache/xc_lock.c

    r1198 r1199  
    66#ifdef ZEND_WIN32
    77#   include <process.h>
     8#else
     9#   include <unistd.h>
     10#   include <fcntl.h>
     11#   include <errno.h>
    812#endif
    913
     
    2327        NULL)
    2428#endif
    25 #include "xc_lock.h"
    26 
    27 struct _xc_lock_t {
     29
     30typedef struct {
    2831    HANDLE fd;
    2932    char *pathname;
    30 };
    31 
     33} xc_fcntl_lock_t;
     34
     35/* {{{ fcntl lock impl */
    3236#ifndef ZEND_WIN32
    33 #   include <unistd.h>
    34 #   include <fcntl.h>
    35 #   include <errno.h>
    3637#   define LCK_WR F_WRLCK
    3738#   define LCK_RD F_RDLCK
    3839#   define LCK_UN F_UNLCK
    3940#   define LCK_NB 0
    40 static inline int dolock(xc_lock_t *lck, int type) /* {{{ */
     41static inline int dolock(xc_fcntl_lock_t *lck, int type)
    4142{
    4243    int ret;
     
    5455    return ret;
    5556}
    56 /* }}} */
    5757#else
    5858
     
    6969#   define LCK_UN 0
    7070#   define LCK_NB LOCKFILE_FAIL_IMMEDIATELY
    71 static inline int dolock(xc_lock_t *lck, int type) /* {{{ */
     71static inline int dolock(xc_fcntl_lock_t *lck, int type)
    7272{
    7373    static OVERLAPPED offset = {0, 0, 0, 0, NULL};
     
    8080    }
    8181}
    82 /* }}} */
    83 #endif
    84 
    85 xc_lock_t *xc_fcntl_init(const char *pathname) /* {{{ */
     82#endif
     83/* }}} */
     84
     85static zend_bool xc_fcntl_init(xc_fcntl_lock_t *lck, const char *pathname) /* {{{ */
    8686{
    8787    HANDLE fd;
    88     xc_lock_t *lck;
    8988    int size;
    9089    char *myname;
     
    114113
    115114    if (fd != INVALID_HANDLE_VALUE) {
    116         lck = malloc(sizeof(lck[0]));
    117115
    118116#ifndef __CYGWIN__
     
    133131    }
    134132
    135     return lck;
    136 }
    137 /* }}} */
    138 void xc_fcntl_destroy(xc_lock_t *lck) /* {{{ */
     133    return lck ? 1 : 0;
     134}
     135/* }}} */
     136static void xc_fcntl_destroy(xc_fcntl_lock_t *lck) /* {{{ */
    139137{
    140138    close(lck->fd);
     
    143141#endif
    144142    free(lck->pathname);
    145     free(lck);
    146 }
    147 /* }}} */
    148 void xc_fcntl_lock(xc_lock_t *lck) /* {{{ */
     143}
     144/* }}} */
     145static void xc_fcntl_lock(xc_fcntl_lock_t *lck) /* {{{ */
    149146{
    150147    if (dolock(lck, LCK_WR) < 0) {
     
    153150}
    154151/* }}} */
    155 void xc_fcntl_rdlock(xc_lock_t *lck) /* {{{ */
     152static void xc_fcntl_rdlock(xc_fcntl_lock_t *lck) /* {{{ */
    156153{
    157154    if (dolock(lck, LCK_RD) < 0) {
     
    160157}
    161158/* }}} */
    162 void xc_fcntl_unlock(xc_lock_t *lck) /* {{{ */
     159static void xc_fcntl_unlock(xc_fcntl_lock_t *lck) /* {{{ */
    163160{
    164161    if (dolock(lck, LCK_UN) < 0) {
     
    167164}
    168165/* }}} */
     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
     180
     181struct _xc_lock_t {
     182#ifdef XC_LOCK_UNSUED
     183    int dummy;
     184#else
     185#   ifdef ZTS
     186    MUTEX_T tsrm_mutex;
     187#   endif
     188
     189#   ifndef XC_INTERPROCESS_LOCK_IMPLEMENTED
     190#       ifdef ZTS
     191    zend_bool use_fcntl;
     192#       endif
     193    xc_fcntl_lock_t fcntl_lock;
     194#   endif
     195#endif
     196};
     197
     198xc_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#   ifdef ZTS
     204    xc_lock_t *lck = malloc(sizeof(xc_lock_t));
     205#       if defined(PTHREADS)
     206    pthread_mutexattr_t psharedm;
     207    pthread_mutexattr_init(&psharedm);
     208    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
     212    lck->tsrm_mutex = tsrm_mutex_alloc();
     213#       endif
     214#   endif
     215#   ifndef XC_INTERPROCESS_LOCK_IMPLEMENTED
     216#       ifdef ZTS
     217    lck->use_fcntl = interprocess;
     218    if (lck->use_fcntl)
     219#       endif
     220        xc_fcntl_init(&lck->fcntl_lock, pathname);
     221#   endif
     222    return lck;
     223#endif
     224}
     225/* }}} */
     226void xc_lock_destroy(xc_lock_t *lck) /* {{{ */
     227{
     228#ifdef XC_LOCK_UNSUED
     229    /* do nothing */
     230#else
     231#   ifdef ZTS
     232    tsrm_mutex_free(lck->tsrm_mutex);
     233#   endif
     234#   ifndef XC_INTERPROCESS_LOCK_IMPLEMENTED
     235#       ifdef ZTS
     236    if (lck->use_fcntl)
     237#       endif
     238        xc_fcntl_destroy(&lck->fcntl_lock);
     239#   endif
     240    free(lck);
     241#endif
     242}
     243/* }}} */
     244void xc_lock(xc_lock_t *lck) /* {{{ */
     245{
     246#ifdef XC_LOCK_UNSUED
     247#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
     253#   ifndef XC_INTERPROCESS_LOCK_IMPLEMENTED
     254#       ifdef ZTS
     255    if (lck->use_fcntl)
     256#       endif
     257        xc_fcntl_lock(&lck->fcntl_lock);
     258#   endif
     259#endif
     260}
     261/* }}} */
     262void 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/* }}} */
     280void 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
     288        xc_fcntl_unlock(&lck->fcntl_lock);
     289#   endif
     290#endif
     291#   ifdef ZTS
     292    if (tsrm_mutex_unlock(lck->tsrm_mutex) < 0) {
     293        zend_error(E_ERROR, "xc_unlock failed errno:%d", errno);
     294    }
     295#   endif
     296}
     297/* }}} */
Note: See TracChangeset for help on using the changeset viewer.