Skip to content

Commit 61f3c16

Browse files
committed
Check for failures while acquiring read lock
1 parent 78b6c78 commit 61f3c16

File tree

4 files changed

+56
-20
lines changed

4 files changed

+56
-20
lines changed

apc_cache.c

+25-7
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,10 @@ PHP_APCU_API apc_cache_entry_t *apc_cache_find(apc_cache_t* cache, zend_string *
797797
return NULL;
798798
}
799799

800-
APC_RLOCK(cache->header);
800+
if (!APC_RLOCK(cache->header)) {
801+
return NULL;
802+
}
803+
801804
entry = apc_cache_rlocked_find_incref(cache, key, t);
802805
APC_RUNLOCK(cache->header);
803806

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

818-
APC_RLOCK(cache->header);
821+
if (!APC_RLOCK(cache->header)) {
822+
return 0;
823+
}
824+
819825
entry = apc_cache_rlocked_find_incref(cache, key, t);
820826
APC_RUNLOCK(cache->header);
821827

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

844-
APC_RLOCK(cache->header);
850+
if (!APC_RLOCK(cache->header)) {
851+
return 0;
852+
}
853+
845854
entry = apc_cache_rlocked_find_nostat(cache, key, t);
846855
APC_RUNLOCK(cache->header);
847856

@@ -913,7 +922,10 @@ PHP_APCU_API zend_bool apc_cache_atomic_update_long(
913922
}
914923

915924
retry_update:
916-
APC_RLOCK(cache->header);
925+
if (!APC_RLOCK(cache->header)) {
926+
return 0;
927+
}
928+
917929
entry = apc_cache_rlocked_find_nostat(cache, key, t);
918930
if (entry) {
919931
/* Only supports integers */
@@ -1057,12 +1069,15 @@ PHP_APCU_API zend_bool apc_cache_info(zval *info, apc_cache_t *cache, zend_bool
10571069
apc_cache_entry_t *p;
10581070
zend_ulong i, j;
10591071

1072+
ZVAL_NULL(info);
10601073
if (!cache) {
1061-
ZVAL_NULL(info);
10621074
return 0;
10631075
}
10641076

1065-
APC_RLOCK(cache->header);
1077+
if (!APC_RLOCK(cache->header)) {
1078+
return 0;
1079+
}
1080+
10661081
php_apc_try {
10671082
array_init(info);
10681083
add_assoc_long(info, "num_slots", cache->nslots);
@@ -1133,7 +1148,10 @@ PHP_APCU_API void apc_cache_stat(apc_cache_t *cache, zend_string *key, zval *sta
11331148
/* calculate hash and slot */
11341149
apc_cache_hash_slot(cache, key, &h, &s);
11351150

1136-
APC_RLOCK(cache->header);
1151+
if (!APC_RLOCK(cache->header)) {
1152+
return;
1153+
}
1154+
11371155
php_apc_try {
11381156
/* find head */
11391157
apc_cache_entry_t *entry = cache->slots[s];

apc_iterator.c

+12-3
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,10 @@ static int apc_iterator_fetch_active(apc_iterator_t *iterator) {
218218
apc_iterator_item_dtor(apc_stack_pop(iterator->stack));
219219
}
220220

221-
APC_RLOCK(apc_user_cache->header);
221+
if (!APC_RLOCK(apc_user_cache->header)) {
222+
return count;
223+
}
224+
222225
php_apc_try {
223226
while (count <= iterator->chunk_size && iterator->slot_idx < apc_user_cache->nslots) {
224227
apc_cache_entry_t *entry = apc_user_cache->slots[iterator->slot_idx];
@@ -250,7 +253,10 @@ static int apc_iterator_fetch_deleted(apc_iterator_t *iterator) {
250253
int count = 0;
251254
apc_iterator_item_t *item;
252255

253-
APC_RLOCK(apc_user_cache->header);
256+
if (!APC_RLOCK(apc_user_cache->header)) {
257+
return count;
258+
}
259+
254260
php_apc_try {
255261
apc_cache_entry_t *entry = apc_user_cache->header->gc;
256262
while (entry && count <= iterator->slot_idx) {
@@ -283,7 +289,10 @@ static void apc_iterator_totals(apc_iterator_t *iterator) {
283289
time_t t = apc_time();
284290
int i;
285291

286-
APC_RLOCK(apc_user_cache->header);
292+
if (!APC_RLOCK(apc_user_cache->header)) {
293+
return;
294+
}
295+
287296
php_apc_try {
288297
for (i=0; i < apc_user_cache->nslots; i++) {
289298
apc_cache_entry_t *entry = apc_user_cache->slots[i];

apc_lock.c

+18-9
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ PHP_APCU_API zend_bool apc_lock_create(apc_lock_t *lock) {
3737
return NULL != apc_windows_cs_create(lock);
3838
}
3939

40-
PHP_APCU_API zend_bool apc_lock_rlock(apc_lock_t *lock) {
40+
static inline zend_bool apc_lock_rlock_impl(apc_lock_t *lock) {
4141
apc_windows_cs_rdlock(lock);
4242
return 1;
4343
}
@@ -94,9 +94,8 @@ PHP_APCU_API zend_bool apc_lock_create(apc_lock_t *lock) {
9494
return pthread_rwlock_init(lock, &apc_lock_attr) == SUCCESS;
9595
}
9696

97-
PHP_APCU_API zend_bool apc_lock_rlock(apc_lock_t *lock) {
98-
pthread_rwlock_rdlock(lock);
99-
return 1;
97+
static inline zend_bool apc_lock_rlock_impl(apc_lock_t *lock) {
98+
return pthread_rwlock_rdlock(lock) == 0;
10099
}
101100

102101
static inline zend_bool apc_lock_wlock_impl(apc_lock_t *lock) {
@@ -154,9 +153,8 @@ PHP_APCU_API zend_bool apc_lock_create(apc_lock_t *lock) {
154153
return 1;
155154
}
156155

157-
PHP_APCU_API zend_bool apc_lock_rlock(apc_lock_t *lock) {
158-
pthread_mutex_lock(lock);
159-
return 1;
156+
static inline zend_bool apc_lock_rlock_impl(apc_lock_t *lock) {
157+
return pthread_mutex_lock(lock) == 0;
160158
}
161159

162160
static inline zend_bool apc_lock_wlock_impl(apc_lock_t *lock) {
@@ -227,7 +225,7 @@ PHP_APCU_API zend_bool apc_lock_create(apc_lock_t *lock) {
227225
lock->state = 0;
228226
}
229227

230-
PHP_APCU_API zend_bool apc_lock_rlock(apc_lock_t *lock) {
228+
static inline zend_bool apc_lock_rlock_impl(apc_lock_t *lock) {
231229
apc_lock_get(lock);
232230
return 1;
233231
}
@@ -291,7 +289,7 @@ PHP_APCU_API zend_bool apc_lock_create(apc_lock_t *lock) {
291289
}
292290
}
293291

294-
PHP_APCU_API zend_bool apc_lock_rlock(apc_lock_t *lock) {
292+
static inline zend_bool apc_lock_rlock_impl(apc_lock_t *lock) {
295293
apc_fcntl_call((*lock), F_SETLKW, F_RDLCK, 0, SEEK_SET, 0);
296294
return 1;
297295
}
@@ -329,3 +327,14 @@ PHP_APCU_API zend_bool apc_lock_wlock(apc_lock_t *lock) {
329327
apc_warning("Failed to acquire write lock");
330328
return 0;
331329
}
330+
331+
PHP_APCU_API zend_bool apc_lock_rlock(apc_lock_t *lock) {
332+
HANDLE_BLOCK_INTERRUPTIONS();
333+
if (apc_lock_rlock_impl(lock)) {
334+
return 1;
335+
}
336+
337+
HANDLE_UNBLOCK_INTERRUPTIONS();
338+
apc_warning("Failed to acquire read lock");
339+
return 0;
340+
}

apc_lock.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ PHP_APCU_API void apc_lock_destroy(apc_lock_t *lock); /* }}} */
8989
#define DESTROY_LOCK(lock) apc_lock_destroy(lock)
9090
#define WLOCK(lock) apc_lock_wlock(lock)
9191
#define WUNLOCK(lock) { apc_lock_wunlock(lock); HANDLE_UNBLOCK_INTERRUPTIONS(); }
92-
#define RLOCK(lock) { HANDLE_BLOCK_INTERRUPTIONS(); apc_lock_rlock(lock); }
92+
#define RLOCK(lock) apc_lock_rlock(lock)
9393
#define RUNLOCK(lock) { apc_lock_runlock(lock); HANDLE_UNBLOCK_INTERRUPTIONS(); }
9494
/* }}} */
9595

0 commit comments

Comments
 (0)