diff --git a/posix_semaphores.c b/posix_semaphores.c index 3edab09..d9c9a5b 100644 --- a/posix_semaphores.c +++ b/posix_semaphores.c @@ -14,6 +14,9 @@ static zend_function_entry posix_semaphores_functions[] = { PHP_FE(posix_sem_open, NULL) PHP_FE(posix_sem_close, NULL) PHP_FE(posix_sem_unlink, NULL) + PHP_FE(posix_sem_getvalue, NULL) + PHP_FE(posix_sem_post, NULL) + PHP_FE(posix_sem_wait, NULL) {NULL, NULL, NULL} }; @@ -44,6 +47,7 @@ typedef struct _php_posix_semaphore { sem_t *sem; } php_posix_semaphore; + static void php_posix_semaphore_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) { php_posix_semaphore *s = (php_posix_semaphore*)rsrc->ptr; @@ -134,4 +138,87 @@ PHP_FUNCTION(posix_sem_unlink) RETURN_FALSE; } RETURN_TRUE; -} \ No newline at end of file +} + +PHP_FUNCTION(posix_sem_getvalue) +{ + php_posix_semaphore *semaphore; + zval *zsemaphore; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zsemaphore) == FAILURE) + { + RETURN_NULL(); + } + + ZEND_FETCH_RESOURCE(semaphore, php_posix_semaphore*, &zsemaphore, -1, PHP_POSIX_SEMAPHORES_RES_NAME, le_posix_semaphore); + if (semaphore->sem) + { + int value; + if (sem_getvalue(semaphore->sem, &value) == -1) + { + RETURN_NULL(); + } + else + { + RETURN_LONG(value); + } + } + else + { + RETURN_NULL(); + } +} + +PHP_FUNCTION(posix_sem_post) +{ + php_posix_semaphore *semaphore; + zval *zsemaphore; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zsemaphore) == FAILURE) + { + RETURN_FALSE; + } + + ZEND_FETCH_RESOURCE(semaphore, php_posix_semaphore*, &zsemaphore, -1, PHP_POSIX_SEMAPHORES_RES_NAME, le_posix_semaphore); + if (semaphore->sem) + { + if (sem_post(semaphore->sem) == -1) + { + RETURN_FALSE; + } + else + { + RETURN_TRUE; + } + } + else + { + RETURN_FALSE; + } +} + +PHP_FUNCTION(posix_sem_wait) +{ + php_posix_semaphore *semaphore; + zval *zsemaphore; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zsemaphore) == FAILURE) + { + RETURN_FALSE; + } + + ZEND_FETCH_RESOURCE(semaphore, php_posix_semaphore*, &zsemaphore, -1, PHP_POSIX_SEMAPHORES_RES_NAME, le_posix_semaphore); + if (semaphore->sem) + { + if (sem_wait(semaphore->sem) == -1) + { + RETURN_FALSE; + } + else + { + RETURN_TRUE; + } + } + else + { + RETURN_FALSE; + } +} + diff --git a/posix_semaphores.h b/posix_semaphores.h index 721b1d3..6685d12 100644 --- a/posix_semaphores.h +++ b/posix_semaphores.h @@ -10,6 +10,9 @@ PHP_MSHUTDOWN_FUNCTION(posix_semaphores); PHP_FUNCTION(posix_sem_open); PHP_FUNCTION(posix_sem_close); PHP_FUNCTION(posix_sem_unlink); +PHP_FUNCTION(posix_sem_getvalue); +PHP_FUNCTION(posix_sem_post); +PHP_FUNCTION(posix_sem_wait); extern zend_module_entry posix_semaphores_module_entry; #define phpext_posix_semaphores_ptr &posix_semaphores_module_entry;