Skip to content

Instantly share code, notes, and snippets.

@userid
Created January 22, 2017 11:01
Show Gist options
  • Select an option

  • Save userid/0158b34ba6a57cf59f6ae3b250a0e246 to your computer and use it in GitHub Desktop.

Select an option

Save userid/0158b34ba6a57cf59f6ae3b250a0e246 to your computer and use it in GitHub Desktop.
APCu 中定义的lock相关的宏
PHP_APCU_API zend_bool apc_lock_init();
PHP_APCU_API void apc_lock_cleanup();
/*
The following functions should be self explanitory:
*/
PHP_APCU_API zend_bool apc_lock_create(apc_lock_t *lock);
PHP_APCU_API zend_bool apc_lock_rlock(apc_lock_t *lock);
PHP_APCU_API zend_bool apc_lock_wlock(apc_lock_t *lock);
PHP_APCU_API zend_bool apc_lock_runlock(apc_lock_t *lock);
PHP_APCU_API zend_bool apc_lock_wunlock(apc_lock_t *lock);
PHP_APCU_API void apc_lock_destroy(apc_lock_t *lock); /* }}} */
/* {{{ generic locking macros */
#define CREATE_LOCK(lock) apc_lock_create(lock)
#define DESTROY_LOCK(lock) apc_lock_destroy(lock)
#define WLOCK(lock) { HANDLE_BLOCK_INTERRUPTIONS(); apc_lock_wlock(lock); }
#define WUNLOCK(lock) { apc_lock_wunlock(lock); HANDLE_UNBLOCK_INTERRUPTIONS(); }
#define RLOCK(lock) { HANDLE_BLOCK_INTERRUPTIONS(); apc_lock_rlock(lock); }
#define RUNLOCK(lock) { apc_lock_runlock(lock); HANDLE_UNBLOCK_INTERRUPTIONS(); }
#define LOCK WLOCK
#define UNLOCK WUNLOCK
/* }}} */
/* {{{ object locking macros */
#define APC_WLOCK(o) WLOCK(&(o)->lock)
#define APC_LOCK APC_WLOCK
#define APC_WUNLOCK(o) WUNLOCK(&(o)->lock)
#define APC_UNLOCK APC_WUNLOCK
#define APC_RLOCK(o) RLOCK(&(o)->lock)
#define APC_RUNLOCK(o) RUNLOCK(&(o)->lock) /* }}} */
/* atomic operations */
#if defined(APC_LOCK_SHARED)
# ifdef PHP_WIN32
# ifdef _WIN64
# define ATOMIC_INC(c, a) InterlockedIncrement64(&a)
# define ATOMIC_DEC(c, a) InterlockedDecrement64(&a)
# else
# define ATOMIC_INC(c, a) InterlockedIncrement(&a)
# define ATOMIC_DEC(c, a) InterlockedDecrement(&a)
# endif
# else
# define ATOMIC_INC(c, a) __sync_add_and_fetch(&a, 1)
# define ATOMIC_DEC(c, a) __sync_sub_and_fetch(&a, 1)
# endif
#elif defined(APC_LOCK_RECURSIVE)
# define ATOMIC_INC(c, a) do { \
if (apc_lock_wlock(&(c)->header->lock)) { \
(a)++; \
apc_lock_wunlock(&(c)->header->lock); \
} \
} while(0)
# define ATOMIC_DEC(c, a) do { \
if (apc_lock_wlock(&(c)->header->lock)) { \
(a)--; \
apc_lock_wunlock(&(c)->header->lock); \
} \
} while(0)
#else
# define ATOMIC_INC(c, a) (a)++
# define ATOMIC_DEC(c, a) (a)--
#endif
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment