From b9d57156352853d46ce698b9cfa2f32d34660948 Mon Sep 17 00:00:00 2001 From: Shuhei Tanuma Date: Mon, 9 Jul 2012 00:18:26 +0900 Subject: [PATCH] add uv_sem_init --- php_uv.c | 43 ++++++++++++++++++++++++++++++++++++++++--- php_uv.h | 4 +++- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/php_uv.c b/php_uv.c index 6b6a56c..e483c11 100644 --- a/php_uv.c +++ b/php_uv.c @@ -294,6 +294,13 @@ void static destruct_uv_lock(zend_rsrc_list_entry *rsrc TSRMLS_DC) lock->locked = 0x00; } uv_mutex_destroy(&lock->lock.mutex); + } else if (lock->type == IS_UV_SEMAPHORE) { + if (lock->locked == 0x01) { + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "uv_sem: unlocked resoruce detected. force unlock resource."); + uv_sem_post(&lock->lock.semaphore); + lock->locked = 0x00; + } + uv_sem_destroy(&lock->lock.semaphore); } efree(lock); @@ -2101,6 +2108,10 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_mutex_unlock, 0, 0, 1) ZEND_ARG_INFO(0, handle) ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_sem_init, 0, 0, 1) + ZEND_ARG_INFO(0, val) +ZEND_END_ARG_INFO() + ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_prepare_init, 0, 0, 1) ZEND_ARG_INFO(0, loop) ZEND_END_ARG_INFO() @@ -4055,15 +4066,13 @@ PHP_FUNCTION(uv_rwlock_wrunlock) } /* }}} */ - - /* {{{ proto uv_lock uv_mutex_init(void) */ PHP_FUNCTION(uv_mutex_init) { php_uv_lock_t *mutex; int error; - mutex = emalloc(sizeof(php_uv_t)); + mutex = emalloc(sizeof(php_uv_lock_t)); error = uv_mutex_init(&mutex->lock.mutex); if (error == 0) { ZEND_REGISTER_RESOURCE(return_value, mutex, uv_lock_handle); @@ -4134,6 +4143,32 @@ PHP_FUNCTION(uv_mutex_unlock) } /* }}} */ +/* {{{ proto uv_lock uv_sem_init(void) */ +PHP_FUNCTION(uv_sem_init) +{ + php_uv_lock_t *semaphore; + int error = 0; + unsigned long val = 0; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, + "l", &val) == FAILURE) { + return; + } + + semaphore = emalloc(sizeof(php_uv_lock_t)); + error = uv_sem_init(&semaphore->lock.semaphore, val); + + if (error == 0) { + ZEND_REGISTER_RESOURCE(return_value, semaphore, uv_lock_handle); + semaphore->type = IS_UV_SEMAPHORE; + } else { + efree(semaphore); + RETURN_FALSE; + } +} +/* }}} */ + + /* {{{ */ PHP_FUNCTION(uv_prepare_init) { @@ -5556,6 +5591,8 @@ static zend_function_entry uv_functions[] = { PHP_FE(uv_mutex_lock, arginfo_uv_mutex_lock) PHP_FE(uv_mutex_trylock, arginfo_uv_mutex_trylock) PHP_FE(uv_mutex_unlock, arginfo_uv_mutex_unlock) + /* semaphore */ + PHP_FE(uv_sem_init, NULL) /* prepare (before poll hook) */ PHP_FE(uv_prepare_init, NULL) PHP_FE(uv_prepare_start, arginfo_uv_prepare_start) diff --git a/php_uv.h b/php_uv.h index 299ebf2..fc84688 100644 --- a/php_uv.h +++ b/php_uv.h @@ -32,7 +32,8 @@ extern zend_class_entry *uv_class_entry; enum php_uv_lock_type{ IS_UV_RWLOCK = 1, - IS_UV_MUTEX = 2 + IS_UV_MUTEX = 2, + IS_UV_SEMAPHORE = 3, }; enum php_uv_resource_type{ @@ -131,6 +132,7 @@ typedef struct { union { uv_rwlock_t rwlock; uv_mutex_t mutex; + uv_sem_t semaphore; } lock; } php_uv_lock_t;