add prepare api

This commit is contained in:
Shuhei Tanuma 2012-06-17 22:50:18 +09:00
parent 1f7cdb555f
commit b98e8f05b6
4 changed files with 175 additions and 5 deletions

View File

@ -28,10 +28,6 @@
* UV_EXTERN int uv_tty_get_winsize(uv_tty_t*, int* width, int* height);
* UV_EXTERN uv_handle_type uv_guess_handle(uv_file file);
* UV_EXTERN int uv_prepare_init(uv_loop_t*, uv_prepare_t* prepare);
* UV_EXTERN int uv_prepare_start(uv_prepare_t* prepare, uv_prepare_cb cb);
* UV_EXTERN int uv_prepare_stop(uv_prepare_t* prepare);
* UV_EXTERN int uv_check_init(uv_loop_t*, uv_check_t* check);
* UV_EXTERN int uv_check_start(uv_check_t* check, uv_check_cb cb);
* UV_EXTERN int uv_check_stop(uv_check_t* check);

10
examples/prepare.php Normal file
View File

@ -0,0 +1,10 @@
<?php
$loop = uv_default_loop();
$prepare = uv_prepare_init($loop);
uv_prepare_start($prepare, function($status){
echo "Hello";
uv_unref(uv_default_loop());
});
uv_run();

161
php_uv.c
View File

@ -52,6 +52,7 @@ typedef struct {
uv->udp_send_cb = NULL; \
uv->pipe_connect_cb = NULL; \
uv->proc_close_cb = NULL; \
uv->prepare_cb = NULL; \
}
/* static variables */
@ -251,6 +252,11 @@ void static destruct_uv(zend_rsrc_list_entry *rsrc TSRMLS_DC)
zval_ptr_dtor(&obj->proc_close_cb);
obj->proc_close_cb = NULL;
}
if (obj->prepare_cb) {
//fprintf(stderr, "uv_prepare: %d\n", Z_REFCOUNT_P(obj->prepare_cb));
zval_ptr_dtor(&obj->prepare_cb);
obj->prepare_cb = NULL;
}
if (obj->resource_id) {
base_id = obj->resource_id;
@ -543,6 +549,41 @@ static void php_uv_read_cb(uv_stream_t* handle, ssize_t nread, uv_buf_t buf)
}
static void php_uv_prepare_cb(uv_stream_t* handle, int status)
{
zval *retval_ptr = NULL;
zval **params[1];
#if PHP_UV_DEBUG>=1
fprintf(stderr,"prepare_cb");
#endif
php_uv_t *uv = (php_uv_t*)handle->data;
TSRMLS_FETCH_FROM_CTX(uv->thread_ctx);
zval *zstat;
MAKE_STD_ZVAL(zstat);
ZVAL_LONG(zstat, status);
params[0] = &zstat;
php_uv_do_callback(&retval_ptr, uv->prepare_cb, params, 1 TSRMLS_CC);
zval_ptr_dtor(&zstat);
zval_ptr_dtor(&retval_ptr);
#if PHP_UV_DEBUG>=1
{
zend_rsrc_list_entry *le;
if (zend_hash_index_find(&EG(regular_list), uv->resource_id, (void **) &le)==SUCCESS) {
printf("# uv_prepare_cb del(%d): %d->%d\n", uv->resource_id, le->refcount, le->refcount-1);
} else {
printf("# can't find (prepare_cb)");
}
}
#endif
}
static void php_uv_udp_recv_cb(uv_udp_t* handle, ssize_t nread, uv_buf_t buf, struct sockaddr* addr, unsigned flags)
{
/* TODO: is this implment correct? */
@ -713,6 +754,9 @@ static inline uv_stream_t* php_uv_get_current_stream(php_uv_t *uv)
case IS_UV_PROCESS:
stream = (uv_stream_t*)&uv->uv.process;
break;
case IS_UV_PREPARE:
stream = (uv_stream_t*)&uv->uv.prepare;
break;
default: {
TSRMLS_FETCH();
php_error_docref(NULL TSRMLS_CC, E_ERROR, "unexpected type found");
@ -2887,6 +2931,119 @@ PHP_FUNCTION(uv_mutex_unlock)
}
/* }}} */
/* {{{ */
PHP_FUNCTION(uv_prepare_init)
{
int r;
zval *zloop = NULL;
uv_loop_t *loop;
php_uv_t *uv;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"|z",&zloop) == FAILURE) {
return;
}
uv = (php_uv_t *)emalloc(sizeof(php_uv_t));
if (!uv) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "uv_prepare_init emalloc failed");
return;
}
if (zloop != NULL) {
ZEND_FETCH_RESOURCE(loop, uv_loop_t*, &zloop, -1, PHP_UV_LOOP_RESOURCE_NAME, uv_loop_handle);
} else {
loop = uv_default_loop();
}
uv->type = IS_UV_PREPARE;
r = uv_prepare_init(loop, &uv->uv.prepare);
if (r) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "uv_prepare_init failed");
return;
}
uv->uv.prepare.data = uv;
PHP_UV_INIT_ZVALS(uv)
TSRMLS_SET_CTX(uv->thread_ctx);
ZEND_REGISTER_RESOURCE(return_value, uv, uv_resource_handle);
uv->resource_id = Z_LVAL_P(return_value);
}
/* }}} */
/* {{{ */
PHP_FUNCTION(uv_prepare_start)
{
#if PHP_UV_DEBUG>=1
fprintf(stderr,"uv_prepare_start");
#endif
zval *handle, *callback;
php_uv_t *uv;
int r;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"rz",&handle, &callback) == FAILURE) {
return;
}
ZEND_FETCH_RESOURCE(uv, php_uv_t *, &handle, -1, PHP_UV_RESOURCE_NAME, uv_resource_handle);
Z_ADDREF_P(callback);
zend_list_addref(uv->resource_id);
uv->prepare_cb = callback;
if(uv->type == IS_UV_PREPARE) {
uv->uv.tcp.data = uv;
} else {
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "this type does not support yet");
}
r = uv_prepare_start((uv_stream_t*)php_uv_get_current_stream(uv), php_uv_prepare_cb);
if (r) {
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "read failed");
}
#if PHP_UV_DEBUG>=1
{
zend_rsrc_list_entry *le;
if (zend_hash_index_find(&EG(regular_list), uv->resource_id, (void **) &le)==SUCCESS) {
printf("# uv_prepare_start del(%d): %d->%d\n", uv->resource_id, le->refcount, le->refcount-1);
} else {
printf("# can't find(uv_prepare_start)");
}
}
#endif
}
/* }}} */
/* {{{ */
PHP_FUNCTION(uv_prepare_stop)
{
zval *handle;
php_uv_t *uv;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"r", &handle) == FAILURE) {
return;
}
ZEND_FETCH_RESOURCE(uv, php_uv_t *, &handle, -1, PHP_UV_RESOURCE_NAME, uv_resource_handle);
uv_prepare_stop((uv_stream_t*)php_uv_get_current_stream(uv));
#if PHP_UV_DEBUG>=1
{
zend_rsrc_list_entry *le;
if (zend_hash_index_find(&EG(regular_list), uv->resource_id, (void **) &le)==SUCCESS) {
printf("# uv_prepare_stop del(%d): %d->%d\n", uv->resource_id, le->refcount, le->refcount-1);
} else {
printf("# can't find(uv_prepare_stop)");
}
}
#endif
}
/* }}} */
static zend_function_entry uv_functions[] = {
/* general */
PHP_FE(uv_update_time, arginfo_uv_update_time)
@ -2965,6 +3122,10 @@ static zend_function_entry uv_functions[] = {
PHP_FE(uv_mutex_lock, NULL)
PHP_FE(uv_mutex_trylock, NULL)
PHP_FE(uv_mutex_unlock, NULL)
/* prepare */
PHP_FE(uv_prepare_init, NULL)
PHP_FE(uv_prepare_start, NULL)
PHP_FE(uv_prepare_stop, NULL)
/* info */
PHP_FE(uv_loadavg, NULL)
PHP_FE(uv_uptime, NULL)

View File

@ -35,7 +35,8 @@ enum php_uv_resource_type{
IS_UV_STREAM = 8,
IS_UV_ADDRINFO = 9,
IS_UV_PROCESS = 10,
IS_UV_MAX = 11
IS_UV_PREPARE = 11,
IS_UV_MAX = 12
};
typedef struct {
@ -56,6 +57,7 @@ typedef struct {
uv_handle_t handle;
uv_stream_t stream;
uv_getaddrinfo_t addrinfo;
uv_prepare_t prepare;
uv_process_t process;
} uv;
zval *address;
@ -71,6 +73,7 @@ typedef struct {
zval *udp_send_cb;
zval *pipe_connect_cb;
zval *proc_close_cb;
zval *prepare_cb;
} php_uv_t;
typedef struct {