Ticket #346: signal.diff

File signal.diff, 8.3 KB (added by arnecls, 5 years ago)

PID based unlocking on signal

  • mod_cacher/xc_cacher.c

    From a938874898568bedadf40161ab3c22600d2b96c4 Mon Sep 17 00:00:00 2001
    From: Arne Claus <arne.claus@trivago.com>
    Date: Mon, 1 Sep 2014 09:20:37 +0200
    Subject: [PATCH] Return a lock on signal
    
    ---
     mod_cacher/xc_cacher.c |  8 ++++++++
     xcache/xc_spinlock.c   | 18 ++++++++++++++++--
     xcache/xc_spinlock.h   |  1 +
     3 files changed, 25 insertions(+), 2 deletions(-)
    
    diff --git a/mod_cacher/xc_cacher.c b/mod_cacher/xc_cacher.c
    index 09c6f43..e7d7779 100644
    a b  
    2020#include "util/xc_vector.h"
    2121#include "util/xc_align.h"
    2222
     23#ifdef HAVE_XCACHE_SPINLOCK
     24#include "xcache/xc_spinlock.h"
     25#endif
     26
    2327#include "php.h"
    2428#include "ext/standard/info.h"
    2529#include "ext/standard/md5.h"
     
    4448#define MUTEX_SIZE() xc_spinlock_size()
    4549#define LOCK(x) xc_spinlock_lock((x)->mutex)
    4650#define UNLOCK(x) xc_spinlock_unlock((x)->mutex)
     51#define SIGNAL_UNLOCK(x) xc_spinlock_signal_unlock((x)->mutex)
    4752#else
    4853#define MUTEX_TYPE xc_mutex_t
    4954#define MUTEX_INIT(x,p,i) xc_mutex_init(x,p,i)
     
    5156#define MUTEX_SIZE() xc_mutex_size()
    5257#define LOCK(x) xc_mutex_lock((x)->mutex)
    5358#define UNLOCK(x) xc_mutex_unlock((x)->mutex)
     59#define SIGNAL_UNLOCK(x)
    5460#endif
    5561
    5662#define ENTER_LOCK_EX(x) \
    void xc_cacher_disable() /* {{{ */ 
    38483854            if (xc_php_caches[i].cached) {
    38493855                xc_php_caches[i].cached->disabled = now;
    38503856            }
     3857            SIGNAL_UNLOCK(xc_php_caches + i);
    38513858        }
    38523859    }
    38533860
    void xc_cacher_disable() /* {{{ */ 
    38563863            if (xc_var_caches[i].cached) {
    38573864                xc_var_caches[i].cached->disabled = now;
    38583865            }
     3866            SIGNAL_UNLOCK(xc_var_caches + i);
    38593867        }
    38603868    }
    38613869}
  • xcache/xc_spinlock.c

    diff --git a/xcache/xc_spinlock.c b/xcache/xc_spinlock.c
    index 7d5e598..35cbe45 100644
    a b  
    33#include <assert.h>
    44
    55#ifdef _MSC_VER
    6 #define XC_THREAD_YIELD() SwitchToThread();
     6#define XC_THREAD_YIELD() SwitchToThread()
     7#define XC_GET_PID() GetCurrentProcessId()
     8#define XC_PID_TYPE DWORD
    79#else
    810#include <sched.h>
    9 #define XC_THREAD_YIELD() sched_yield();
     11#include <unistd.h>
     12#define XC_THREAD_YIELD() sched_yield()
     13#define XC_GET_PID() getpid()
     14#define XC_PID_TYPE pid_t
    1015#endif
    1116
    1217struct _xc_spinlock_t {
    1318    long state;
    1419};
    1520
     21static XC_PID_TYPE xc_spinlock_pid = 1;
     22
    1623static __inline void xc_spinlock_yield(long spincount) /* {{{ */
    1724{
    1825    if ((spincount & 0x3F) == 0) {
    size_t xc_spinlock_size(void) /* {{{ */ 
    3138xc_spinlock_t *xc_spinlock_init(xc_spinlock_t *shared_spinlock) /* {{{ */
    3239{
    3340    assert(shared_spinlock);
     41    xc_spinlock_pid = XC_GET_PID();
    3442    shared_spinlock->state = 0;
    3543    return shared_spinlock;
    3644}
    void xc_spinlock_unlock(xc_spinlock_t *spinlock) /* {{{ */ 
    5260    spinlock->state = 0;
    5361}
    5462/* }}} */
     63void xc_spinlock_signal_unlock(xc_spinlock_t *spinlock) /* {{{ */
     64{
     65    if (spinlock->state == xc_spinlock_pid)
     66        spinlock->state = 0;
     67}
     68/* }}} */
  • xcache/xc_spinlock.h

    diff --git a/xcache/xc_spinlock.h b/xcache/xc_spinlock.h
    index 4f98d1e..908dc82 100644
    a b xc_spinlock_t *xc_spinlock_init(xc_spinlock_t *shared_spinlock); 
    1414void xc_spinlock_destroy(xc_spinlock_t *spinlock);
    1515void xc_spinlock_lock(xc_spinlock_t *spinlock);
    1616void xc_spinlock_unlock(xc_spinlock_t *spinlock);
     17void xc_spinlock_signal_unlock(xc_spinlock_t *spinlock);
    1718
    1819#endif /* XC_SPINLOCK_H_ */
  • xcache/xc_spinlock.c

    --
    1.8.4.2
    
    From 2fd1dba4b9c4cb9c1cee2925b30fc4ff87bc1bd2 Mon Sep 17 00:00:00 2001
    From: Arne Claus <arne.claus@trivago.com>
    Date: Mon, 1 Sep 2014 10:22:31 +0200
    Subject: [PATCH] Safer check for PID
    
    ---
     xcache/xc_spinlock.c | 4 ++--
     1 file changed, 2 insertions(+), 2 deletions(-)
    
    diff --git a/xcache/xc_spinlock.c b/xcache/xc_spinlock.c
    index 35cbe45..6ab2987 100644
    a b struct _xc_spinlock_t { 
    1818    long state;
    1919};
    2020
    21 static XC_PID_TYPE xc_spinlock_pid = 1;
     21static XC_PID_TYPE xc_spinlock_pid;
    2222
    2323static __inline void xc_spinlock_yield(long spincount) /* {{{ */
    2424{
    void xc_spinlock_unlock(xc_spinlock_t *spinlock) /* {{{ */ 
    6262/* }}} */
    6363void xc_spinlock_signal_unlock(xc_spinlock_t *spinlock) /* {{{ */
    6464{
    65     if (spinlock->state == xc_spinlock_pid)
     65    if (spinlock->state == XC_GET_PID())
    6666        spinlock->state = 0;
    6767}
    6868/* }}} */
  • mod_cacher/xc_cacher.c

    --
    1.8.4.2
    
    From 195c49f4bdb3d2ebb5bff124bf807bff7de4935f Mon Sep 17 00:00:00 2001
    From: Arne Claus <arne.claus@trivago.com>
    Date: Mon, 1 Sep 2014 10:55:16 +0200
    Subject: [PATCH] Moved unlock-on-signal to separate function
    
    ---
     mod_cacher/xc_cacher.c | 22 ++++++++++++++++++----
     mod_cacher/xc_cacher.h |  1 +
     xcache.c               |  7 +++++--
     3 files changed, 24 insertions(+), 6 deletions(-)
    
    diff --git a/mod_cacher/xc_cacher.c b/mod_cacher/xc_cacher.c
    index e7d7779..0cefa81 100644
    a b  
    4848#define MUTEX_SIZE() xc_spinlock_size()
    4949#define LOCK(x) xc_spinlock_lock((x)->mutex)
    5050#define UNLOCK(x) xc_spinlock_unlock((x)->mutex)
    51 #define SIGNAL_UNLOCK(x) xc_spinlock_signal_unlock((x)->mutex)
    5251#else
    5352#define MUTEX_TYPE xc_mutex_t
    5453#define MUTEX_INIT(x,p,i) xc_mutex_init(x,p,i)
     
    5655#define MUTEX_SIZE() xc_mutex_size()
    5756#define LOCK(x) xc_mutex_lock((x)->mutex)
    5857#define UNLOCK(x) xc_mutex_unlock((x)->mutex)
    59 #define SIGNAL_UNLOCK(x)
    6058#endif
    6159
    6260#define ENTER_LOCK_EX(x) \
    void xc_cacher_disable() /* {{{ */ 
    38543852            if (xc_php_caches[i].cached) {
    38553853                xc_php_caches[i].cached->disabled = now;
    38563854            }
    3857             SIGNAL_UNLOCK(xc_php_caches + i);
    38583855        }
    38593856    }
    38603857
    void xc_cacher_disable() /* {{{ */ 
    38633860            if (xc_var_caches[i].cached) {
    38643861                xc_var_caches[i].cached->disabled = now;
    38653862            }
    3866             SIGNAL_UNLOCK(xc_var_caches + i);
    38673863        }
    38683864    }
    38693865}
    38703866/* }}} */
     3867void xc_cacher_unlock() /* {{{ */
     3868{
     3869#ifdef HAVE_XCACHE_SPINLOCK
     3870    size_t i;
     3871    if (xc_php_caches) {
     3872        for (i = 0; i < xc_php_hcache.size; i ++) {
     3873            xc_spinlock_signal_unlock(xc_php_caches[i].mutex);
     3874        }
     3875    }
     3876
     3877    if (xc_var_caches) {
     3878        for (i = 0; i < xc_var_hcache.size; i ++) {
     3879            xc_spinlock_signal_unlock(xc_var_caches[i].mutex);
     3880        }
     3881    }
     3882#endif
     3883}
     3884/* }}} */
  • mod_cacher/xc_cacher.h

    diff --git a/mod_cacher/xc_cacher.h b/mod_cacher/xc_cacher.h
    index 70957d2..9656c76 100644
    a b  
    77
    88int xc_cacher_startup_module();
    99void xc_cacher_disable();
     10void xc_cacher_unlock();
    1011
    1112#endif /* XC_CACHER_H_1CADCD7E46ABC70014D0766CE97B9741 */
  • xcache.c

    diff --git a/xcache.c b/xcache.c
    index 93468e0..a5387e4 100644
    a b static void xc_call_getter(xc_name_getter_t getter, int count, INTERNAL_FUNCTION 
    272272            int len = (int) strlen(name);
    273273            return_value->value.str.len = len;
    274274            return_value->value.str.val = estrndup(name, len);
    275             return_value->type = IS_STRING;
     275            return_value->type = IS_STRING;
    276276            return;
    277277        }
    278278    }
    static LONG WINAPI miniDumperFilter(struct _EXCEPTION_POINTERS *pExceptionInfo) 
    486486        xc_cacher_disable();
    487487    }
    488488
     489    xc_cacher_unlock();
     490
    489491    return ret;
    490492}
    491493/* }}} */
    static void xcache_signal_handler(int sig) /* {{{ */ 
    582584        xc_disable_on_crash = 0;
    583585        xc_cacher_disable();
    584586    }
     587    xc_cacher_unlock();
    585588    raise(sig);
    586589}
    587590/* }}} */
    static zend_module_dep xcache_module_deps[] = { 
    873876    ZEND_MOD_END
    874877};
    875878#endif
    876 /* }}} */
     879/* }}} */
    877880/* {{{ module definition structure */
    878881zend_module_entry xcache_module_entry = {
    879882#ifdef STANDARD_MODULE_HEADER_EX
  • xcache/xc_spinlock.c

    --
    1.8.4.2
    
    From 942071e7165a5d061164db88f1c9a142db9dd7c7 Mon Sep 17 00:00:00 2001
    From: Arne Claus <arne.claus@trivago.com>
    Date: Mon, 1 Sep 2014 11:28:17 +0200
    Subject: [PATCH] Fixed faulty locking after switching from
     __sync_bool_compare_and_swap to __sync_val_compare_and_swap
    
    ---
     xcache/xc_spinlock.c | 4 ++--
     1 file changed, 2 insertions(+), 2 deletions(-)
    
    diff --git a/xcache/xc_spinlock.c b/xcache/xc_spinlock.c
    index 6ab2987..c7b4449 100644
    a b struct _xc_spinlock_t { 
    1818    long state;
    1919};
    2020
    21 static XC_PID_TYPE xc_spinlock_pid;
     21static XC_PID_TYPE xc_spinlock_pid=1;
    2222
    2323static __inline void xc_spinlock_yield(long spincount) /* {{{ */
    2424{
    void xc_spinlock_destroy(xc_spinlock_t *spinlock) /* {{{ */ 
    5050void xc_spinlock_lock(xc_spinlock_t *spinlock) /* {{{ */
    5151{
    5252    long spincount = 0;
    53     while (!XC_ATOMIC_CAS32(spinlock->state, 0, 1)) {
     53    while (XC_ATOMIC_CAS32(spinlock->state, 0, xc_spinlock_pid) != 0) {
    5454        xc_spinlock_yield(++spincount);
    5555    }
    5656}