Skip to content

Commit

Permalink
Check for failures while acquiring read lock
Browse files Browse the repository at this point in the history
  • Loading branch information
nikic committed Feb 7, 2021
1 parent 78b6c78 commit 61f3c16
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 20 deletions.
32 changes: 25 additions & 7 deletions apc_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,10 @@ PHP_APCU_API apc_cache_entry_t *apc_cache_find(apc_cache_t* cache, zend_string *
return NULL;
}

APC_RLOCK(cache->header);
if (!APC_RLOCK(cache->header)) {
return NULL;
}

entry = apc_cache_rlocked_find_incref(cache, key, t);
APC_RUNLOCK(cache->header);

Expand All @@ -815,7 +818,10 @@ PHP_APCU_API zend_bool apc_cache_fetch(apc_cache_t* cache, zend_string *key, tim
return 0;
}

APC_RLOCK(cache->header);
if (!APC_RLOCK(cache->header)) {
return 0;
}

entry = apc_cache_rlocked_find_incref(cache, key, t);
APC_RUNLOCK(cache->header);

Expand All @@ -841,7 +847,10 @@ PHP_APCU_API zend_bool apc_cache_exists(apc_cache_t* cache, zend_string *key, ti
return 0;
}

APC_RLOCK(cache->header);
if (!APC_RLOCK(cache->header)) {
return 0;
}

entry = apc_cache_rlocked_find_nostat(cache, key, t);
APC_RUNLOCK(cache->header);

Expand Down Expand Up @@ -913,7 +922,10 @@ PHP_APCU_API zend_bool apc_cache_atomic_update_long(
}

retry_update:
APC_RLOCK(cache->header);
if (!APC_RLOCK(cache->header)) {
return 0;
}

entry = apc_cache_rlocked_find_nostat(cache, key, t);
if (entry) {
/* Only supports integers */
Expand Down Expand Up @@ -1057,12 +1069,15 @@ PHP_APCU_API zend_bool apc_cache_info(zval *info, apc_cache_t *cache, zend_bool
apc_cache_entry_t *p;
zend_ulong i, j;

ZVAL_NULL(info);
if (!cache) {
ZVAL_NULL(info);
return 0;
}

APC_RLOCK(cache->header);
if (!APC_RLOCK(cache->header)) {
return 0;
}

php_apc_try {
array_init(info);
add_assoc_long(info, "num_slots", cache->nslots);
Expand Down Expand Up @@ -1133,7 +1148,10 @@ PHP_APCU_API void apc_cache_stat(apc_cache_t *cache, zend_string *key, zval *sta
/* calculate hash and slot */
apc_cache_hash_slot(cache, key, &h, &s);

APC_RLOCK(cache->header);
if (!APC_RLOCK(cache->header)) {
return;
}

php_apc_try {
/* find head */
apc_cache_entry_t *entry = cache->slots[s];
Expand Down
15 changes: 12 additions & 3 deletions apc_iterator.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,10 @@ static int apc_iterator_fetch_active(apc_iterator_t *iterator) {
apc_iterator_item_dtor(apc_stack_pop(iterator->stack));
}

APC_RLOCK(apc_user_cache->header);
if (!APC_RLOCK(apc_user_cache->header)) {
return count;
}

php_apc_try {
while (count <= iterator->chunk_size && iterator->slot_idx < apc_user_cache->nslots) {
apc_cache_entry_t *entry = apc_user_cache->slots[iterator->slot_idx];
Expand Down Expand Up @@ -250,7 +253,10 @@ static int apc_iterator_fetch_deleted(apc_iterator_t *iterator) {
int count = 0;
apc_iterator_item_t *item;

APC_RLOCK(apc_user_cache->header);
if (!APC_RLOCK(apc_user_cache->header)) {
return count;
}

php_apc_try {
apc_cache_entry_t *entry = apc_user_cache->header->gc;
while (entry && count <= iterator->slot_idx) {
Expand Down Expand Up @@ -283,7 +289,10 @@ static void apc_iterator_totals(apc_iterator_t *iterator) {
time_t t = apc_time();
int i;

APC_RLOCK(apc_user_cache->header);
if (!APC_RLOCK(apc_user_cache->header)) {
return;
}

php_apc_try {
for (i=0; i < apc_user_cache->nslots; i++) {
apc_cache_entry_t *entry = apc_user_cache->slots[i];
Expand Down
27 changes: 18 additions & 9 deletions apc_lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ PHP_APCU_API zend_bool apc_lock_create(apc_lock_t *lock) {
return NULL != apc_windows_cs_create(lock);
}

PHP_APCU_API zend_bool apc_lock_rlock(apc_lock_t *lock) {
static inline zend_bool apc_lock_rlock_impl(apc_lock_t *lock) {
apc_windows_cs_rdlock(lock);
return 1;
}
Expand Down Expand Up @@ -94,9 +94,8 @@ PHP_APCU_API zend_bool apc_lock_create(apc_lock_t *lock) {
return pthread_rwlock_init(lock, &apc_lock_attr) == SUCCESS;
}

PHP_APCU_API zend_bool apc_lock_rlock(apc_lock_t *lock) {
pthread_rwlock_rdlock(lock);
return 1;
static inline zend_bool apc_lock_rlock_impl(apc_lock_t *lock) {
return pthread_rwlock_rdlock(lock) == 0;
}

static inline zend_bool apc_lock_wlock_impl(apc_lock_t *lock) {
Expand Down Expand Up @@ -154,9 +153,8 @@ PHP_APCU_API zend_bool apc_lock_create(apc_lock_t *lock) {
return 1;
}

PHP_APCU_API zend_bool apc_lock_rlock(apc_lock_t *lock) {
pthread_mutex_lock(lock);
return 1;
static inline zend_bool apc_lock_rlock_impl(apc_lock_t *lock) {
return pthread_mutex_lock(lock) == 0;
}

static inline zend_bool apc_lock_wlock_impl(apc_lock_t *lock) {
Expand Down Expand Up @@ -227,7 +225,7 @@ PHP_APCU_API zend_bool apc_lock_create(apc_lock_t *lock) {
lock->state = 0;
}

PHP_APCU_API zend_bool apc_lock_rlock(apc_lock_t *lock) {
static inline zend_bool apc_lock_rlock_impl(apc_lock_t *lock) {
apc_lock_get(lock);
return 1;
}
Expand Down Expand Up @@ -291,7 +289,7 @@ PHP_APCU_API zend_bool apc_lock_create(apc_lock_t *lock) {
}
}

PHP_APCU_API zend_bool apc_lock_rlock(apc_lock_t *lock) {
static inline zend_bool apc_lock_rlock_impl(apc_lock_t *lock) {
apc_fcntl_call((*lock), F_SETLKW, F_RDLCK, 0, SEEK_SET, 0);
return 1;
}
Expand Down Expand Up @@ -329,3 +327,14 @@ PHP_APCU_API zend_bool apc_lock_wlock(apc_lock_t *lock) {
apc_warning("Failed to acquire write lock");
return 0;
}

PHP_APCU_API zend_bool apc_lock_rlock(apc_lock_t *lock) {
HANDLE_BLOCK_INTERRUPTIONS();
if (apc_lock_rlock_impl(lock)) {
return 1;
}

HANDLE_UNBLOCK_INTERRUPTIONS();
apc_warning("Failed to acquire read lock");
return 0;
}
2 changes: 1 addition & 1 deletion apc_lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ PHP_APCU_API void apc_lock_destroy(apc_lock_t *lock); /* }}} */
#define DESTROY_LOCK(lock) apc_lock_destroy(lock)
#define WLOCK(lock) 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 RLOCK(lock) apc_lock_rlock(lock)
#define RUNLOCK(lock) { apc_lock_runlock(lock); HANDLE_UNBLOCK_INTERRUPTIONS(); }
/* }}} */

Expand Down

0 comments on commit 61f3c16

Please sign in to comment.