add uv_sem_init

This commit is contained in:
Shuhei Tanuma 2012-07-09 00:18:26 +09:00
parent 89aba91e94
commit b9d5715635
2 changed files with 43 additions and 4 deletions

View File

@ -294,6 +294,13 @@ void static destruct_uv_lock(zend_rsrc_list_entry *rsrc TSRMLS_DC)
lock->locked = 0x00; lock->locked = 0x00;
} }
uv_mutex_destroy(&lock->lock.mutex); 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); efree(lock);
@ -2101,6 +2108,10 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_mutex_unlock, 0, 0, 1)
ZEND_ARG_INFO(0, handle) ZEND_ARG_INFO(0, handle)
ZEND_END_ARG_INFO() 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_BEGIN_ARG_INFO_EX(arginfo_uv_prepare_init, 0, 0, 1)
ZEND_ARG_INFO(0, loop) ZEND_ARG_INFO(0, loop)
ZEND_END_ARG_INFO() ZEND_END_ARG_INFO()
@ -4055,15 +4066,13 @@ PHP_FUNCTION(uv_rwlock_wrunlock)
} }
/* }}} */ /* }}} */
/* {{{ proto uv_lock uv_mutex_init(void) */ /* {{{ proto uv_lock uv_mutex_init(void) */
PHP_FUNCTION(uv_mutex_init) PHP_FUNCTION(uv_mutex_init)
{ {
php_uv_lock_t *mutex; php_uv_lock_t *mutex;
int error; int error;
mutex = emalloc(sizeof(php_uv_t)); mutex = emalloc(sizeof(php_uv_lock_t));
error = uv_mutex_init(&mutex->lock.mutex); error = uv_mutex_init(&mutex->lock.mutex);
if (error == 0) { if (error == 0) {
ZEND_REGISTER_RESOURCE(return_value, mutex, uv_lock_handle); 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) 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_lock, arginfo_uv_mutex_lock)
PHP_FE(uv_mutex_trylock, arginfo_uv_mutex_trylock) PHP_FE(uv_mutex_trylock, arginfo_uv_mutex_trylock)
PHP_FE(uv_mutex_unlock, arginfo_uv_mutex_unlock) PHP_FE(uv_mutex_unlock, arginfo_uv_mutex_unlock)
/* semaphore */
PHP_FE(uv_sem_init, NULL)
/* prepare (before poll hook) */ /* prepare (before poll hook) */
PHP_FE(uv_prepare_init, NULL) PHP_FE(uv_prepare_init, NULL)
PHP_FE(uv_prepare_start, arginfo_uv_prepare_start) PHP_FE(uv_prepare_start, arginfo_uv_prepare_start)

View File

@ -32,7 +32,8 @@ extern zend_class_entry *uv_class_entry;
enum php_uv_lock_type{ enum php_uv_lock_type{
IS_UV_RWLOCK = 1, IS_UV_RWLOCK = 1,
IS_UV_MUTEX = 2 IS_UV_MUTEX = 2,
IS_UV_SEMAPHORE = 3,
}; };
enum php_uv_resource_type{ enum php_uv_resource_type{
@ -131,6 +132,7 @@ typedef struct {
union { union {
uv_rwlock_t rwlock; uv_rwlock_t rwlock;
uv_mutex_t mutex; uv_mutex_t mutex;
uv_sem_t semaphore;
} lock; } lock;
} php_uv_lock_t; } php_uv_lock_t;