From: Michael Wallner Date: Mon, 13 Feb 2012 13:26:22 +0000 (+0000) Subject: persistent handle fixes X-Git-Tag: DEV_2-before-client~46 X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=commitdiff_plain;h=8c69d38f5215bf073ff0a563b65c6ac067937de4 persistent handle fixes --- diff --git a/php_http_env.c b/php_http_env.c index a13e797..9fd8e59 100644 --- a/php_http_env.c +++ b/php_http_env.c @@ -587,9 +587,9 @@ PHP_HTTP_BEGIN_ARGS(negotiate, 2) PHP_HTTP_ARG_VAL(result_array, 1) PHP_HTTP_END_ARGS; -PHP_HTTP_EMPTY_ARGS(persistentHandlesStat); +PHP_HTTP_EMPTY_ARGS(statPersistentHandles); -PHP_HTTP_BEGIN_ARGS(persistentHandlesClean, 0) +PHP_HTTP_BEGIN_ARGS(cleanPersistentHandles, 0) PHP_HTTP_ARG_VAL(name, 0) PHP_HTTP_ARG_VAL(ident, 0) PHP_HTTP_END_ARGS; @@ -611,8 +611,8 @@ zend_function_entry php_http_env_method_entry[] = { PHP_HTTP_ENV_ME(negotiateCharset) PHP_HTTP_ENV_ME(negotiate) - PHP_HTTP_ENV_ME(persistentHandlesStat) - PHP_HTTP_ENV_ME(persistentHandlesClean) + PHP_HTTP_ENV_ME(statPersistentHandles) + PHP_HTTP_ENV_ME(cleanPersistentHandles) EMPTY_FUNCTION_ENTRY }; @@ -810,7 +810,7 @@ PHP_METHOD(HttpEnv, negotiate) } } -PHP_METHOD(HttpEnv, persistentHandlesStat) +PHP_METHOD(HttpEnv, statPersistentHandles) { if (SUCCESS == zend_parse_parameters_none()) { object_init(return_value); @@ -822,7 +822,7 @@ PHP_METHOD(HttpEnv, persistentHandlesStat) RETURN_FALSE; } -PHP_METHOD(HttpEnv, persistentHandlesClean) +PHP_METHOD(HttpEnv, cleanPersistentHandles) { char *name_str = NULL, *ident_str = NULL; int name_len = 0, ident_len = 0; diff --git a/php_http_env.h b/php_http_env.h index 64cc702..493f1e0 100644 --- a/php_http_env.h +++ b/php_http_env.h @@ -84,8 +84,8 @@ PHP_METHOD(HttpEnv, negotiateCharset); PHP_METHOD(HttpEnv, negotiateEncoding); PHP_METHOD(HttpEnv, negotiateContentType); PHP_METHOD(HttpEnv, negotiate); -PHP_METHOD(HttpEnv, persistentHandlesStat); -PHP_METHOD(HttpEnv, persistentHandlesClean); +PHP_METHOD(HttpEnv, statPersistentHandles); +PHP_METHOD(HttpEnv, cleanPersistentHandles); extern zend_class_entry *php_http_env_request_class_entry; extern zend_function_entry php_http_env_request_method_entry[]; diff --git a/php_http_persistent_handle.c b/php_http_persistent_handle.c index 5842035..4210437 100644 --- a/php_http_persistent_handle.c +++ b/php_http_persistent_handle.c @@ -60,21 +60,33 @@ static inline php_http_persistent_handle_list_t *php_http_persistent_handle_list return list; } -static inline void php_http_persistent_handle_list_dtor(php_http_persistent_handle_list_t *list, php_http_persistent_handle_provider_t *provider TSRMLS_DC) +static int php_http_persistent_handle_apply_cleanup_ex(void *pp, void *arg TSRMLS_DC) { - HashPosition pos; - void **handle; - + php_http_resource_factory_t *rf = arg; + void **handle = pp; + #if PHP_HTTP_DEBUG_PHANDLES - fprintf(stderr, "LSTDTOR: %p\n", list); + fprintf(stderr, "DESTROY: %p\n", *handle); #endif - FOREACH_HASH_VAL(pos, &list->free, handle) { + php_http_resource_factory_handle_dtor(rf, *handle TSRMLS_CC); + return ZEND_HASH_APPLY_REMOVE; +} + +static int php_http_persistent_handle_apply_cleanup(void *pp, void *arg TSRMLS_DC) +{ + php_http_resource_factory_t *rf = arg; + php_http_persistent_handle_list_t **listp = pp; + + zend_hash_apply_with_argument(&(*listp)->free, php_http_persistent_handle_apply_cleanup_ex, rf TSRMLS_CC); + return (*listp)->used ? ZEND_HASH_APPLY_KEEP : ZEND_HASH_APPLY_REMOVE; +} + +static inline void php_http_persistent_handle_list_dtor(php_http_persistent_handle_list_t *list, php_http_persistent_handle_provider_t *provider TSRMLS_DC) +{ #if PHP_HTTP_DEBUG_PHANDLES - fprintf(stderr, "DESTROY: %p\n", *handle); + fprintf(stderr, "LSTDTOR: %p\n", list); #endif - - provider->rf.fops.dtor(provider->rf.data, *handle TSRMLS_CC); - } + zend_hash_apply_with_argument(&list->free, php_http_persistent_handle_apply_cleanup_ex, &provider->rf TSRMLS_CC); zend_hash_destroy(&list->free); } @@ -88,6 +100,12 @@ static inline void php_http_persistent_handle_list_free(php_http_persistent_hand *list = NULL; } +static int php_http_persistent_handle_list_apply_dtor(void *listp, void *provider TSRMLS_DC) +{ + php_http_persistent_handle_list_free(listp, provider TSRMLS_CC); + return ZEND_HASH_APPLY_REMOVE; +} + static inline php_http_persistent_handle_list_t *php_http_persistent_handle_list_find(php_http_persistent_handle_provider_t *provider, const char *ident_str, size_t ident_len TSRMLS_DC) { php_http_persistent_handle_list_t **list, *new_list; @@ -124,7 +142,7 @@ static inline STATUS php_http_persistent_handle_do_acquire(php_http_persistent_h *handle = *handle_ptr; zend_hash_index_del(&list->free, index); } else { - *handle = provider->rf.fops.ctor(provider->rf.data TSRMLS_CC); + *handle = php_http_resource_factory_handle_ctor(&provider->rf TSRMLS_CC); } if (*handle) { @@ -145,7 +163,7 @@ static inline STATUS php_http_persistent_handle_do_release(php_http_persistent_h if ((list = php_http_persistent_handle_list_find(provider, ident_str, ident_len TSRMLS_CC))) { if (provider->list.used >= PHP_HTTP_G->persistent_handle.limit) { - provider->rf.fops.dtor(provider->rf.data, *handle TSRMLS_CC); + php_http_resource_factory_handle_dtor(&provider->rf, *handle TSRMLS_CC); } else { if (SUCCESS != zend_hash_next_index_insert(&list->free, (void *) handle, sizeof(void *), NULL)) { return FAILURE; @@ -165,7 +183,7 @@ static inline STATUS php_http_persistent_handle_do_accrete(php_http_persistent_h { php_http_persistent_handle_list_t *list; - if (provider->rf.fops.copy && (*new_handle = provider->rf.fops.copy(provider->rf.data, old_handle TSRMLS_CC))) { + if ((*new_handle = php_http_resource_factory_handle_copy(&provider->rf, old_handle TSRMLS_CC))) { if ((list = php_http_persistent_handle_list_find(provider, ident_str, ident_len TSRMLS_CC))) { ++list->used; } @@ -178,16 +196,9 @@ static inline STATUS php_http_persistent_handle_do_accrete(php_http_persistent_h static void php_http_persistent_handles_hash_dtor(void *p) { php_http_persistent_handle_provider_t *provider = (php_http_persistent_handle_provider_t *) p; - php_http_persistent_handle_list_t **list, *list_tmp; - HashPosition pos; TSRMLS_FETCH(); - FOREACH_HASH_VAL(pos, &provider->list.free, list) { - /* fix shutdown crash in PHP4 */ - list_tmp = *list; - php_http_persistent_handle_list_free(&list_tmp, provider TSRMLS_CC); - } - + zend_hash_apply_with_argument(&provider->list.free, php_http_persistent_handle_list_apply_dtor, provider TSRMLS_CC); zend_hash_destroy(&provider->list.free); php_http_resource_factory_dtor(&provider->rf); } @@ -255,13 +266,13 @@ PHP_HTTP_API php_http_persistent_handle_factory_t *php_http_persistent_handle_co } } else { if (free_a) { - efree(a); + efree(free_a); } a = NULL; } #if PHP_HTTP_DEBUG_PHANDLES - fprintf(stderr, "CONCETE: %p (%s) (%s)\n", a ? a->provider : NULL, name_str, ident_str); + fprintf(stderr, "CONCEDE: %p (%s) (%s)\n", a ? a->provider : NULL, name_str, ident_str); #endif return a; @@ -291,7 +302,7 @@ PHP_HTTP_API void *php_http_persistent_handle_acquire(php_http_persistent_handle PHP_HTTP_API void *php_http_persistent_handle_accrete(php_http_persistent_handle_factory_t *a, void *handle TSRMLS_DC) { - void *new_handle; + void *new_handle = NULL; LOCK(); php_http_persistent_handle_do_accrete(a->provider, a->ident.str, a->ident.len, handle, &new_handle TSRMLS_CC); @@ -307,98 +318,32 @@ PHP_HTTP_API void php_http_persistent_handle_release(php_http_persistent_handle_ UNLOCK(); } -PHP_HTTP_API STATUS php_http_persistent_handle_acquire2(const char *name_str, size_t name_len, const char *ident_str, size_t ident_len, void **handle TSRMLS_DC) -{ - STATUS status = FAILURE; - php_http_persistent_handle_provider_t *provider; - - *handle = NULL; - LOCK(); - if (SUCCESS == zend_symtable_find(&php_http_persistent_handles_hash, name_str, name_len+1, (void *) &provider)) { - status = php_http_persistent_handle_do_acquire(provider, ident_str, ident_len, handle TSRMLS_CC); - } - UNLOCK(); - -#if PHP_HTTP_DEBUG_PHANDLES - fprintf(stderr, "ACQUIRE: %p (%s)\n", *handle, name_str); -#endif - - return status; -} - -PHP_HTTP_API STATUS php_http_persistent_handle_release2(const char *name_str, size_t name_len, const char *ident_str, size_t ident_len, void **handle TSRMLS_DC) -{ - STATUS status = FAILURE; - php_http_persistent_handle_provider_t *provider; -#if PHP_HTTP_DEBUG_PHANDLES - void *handle_tmp = *handle; -#endif - - LOCK(); - if (SUCCESS == zend_symtable_find(&php_http_persistent_handles_hash, name_str, name_len+1, (void *) &provider)) { - status = php_http_persistent_handle_do_release(provider, ident_str, ident_len, handle TSRMLS_CC); - } - UNLOCK(); - -#if PHP_HTTP_DEBUG_PHANDLES - fprintf(stderr, "RELEASE: %p (%s)\n", handle_tmp, name_str); -#endif - - return status; -} - -PHP_HTTP_API STATUS php_http_persistent_handle_accrete2(const char *name_str, size_t name_len, const char *ident_str, size_t ident_len, void *old_handle, void **new_handle TSRMLS_DC) -{ - STATUS status = FAILURE; - php_http_persistent_handle_provider_t *provider; - - *new_handle = NULL; - LOCK(); - if (SUCCESS == zend_symtable_find(&php_http_persistent_handles_hash, name_str, name_len+1, (void *) &provider)) { - status = php_http_persistent_handle_do_accrete(provider, ident_str, ident_len, old_handle, new_handle TSRMLS_CC); - } - UNLOCK(); - -#if PHP_HTTP_DEBUG_PHANDLES - fprintf(stderr, "ACCRETE: %p > %p (%s)\n", old_handle, *new_handle, name_str); -#endif - - return status; -} - PHP_HTTP_API void php_http_persistent_handle_cleanup(const char *name_str, size_t name_len, const char *ident_str, size_t ident_len TSRMLS_DC) { php_http_persistent_handle_provider_t *provider; - php_http_persistent_handle_list_t *list, **listp; - HashPosition pos1, pos2; + php_http_persistent_handle_list_t *list; LOCK(); if (name_str && name_len) { if (SUCCESS == zend_symtable_find(&php_http_persistent_handles_hash, name_str, name_len+1, (void *) &provider)) { if (ident_str && ident_len) { if ((list = php_http_persistent_handle_list_find(provider, ident_str, ident_len TSRMLS_CC))) { - php_http_persistent_handle_list_dtor(list, provider TSRMLS_CC); - php_http_persistent_handle_list_init(list); + zend_hash_apply_with_argument(&list->free, php_http_persistent_handle_apply_cleanup_ex, &provider->rf TSRMLS_CC); } } else { - FOREACH_HASH_VAL(pos1, &provider->list.free, listp) { - php_http_persistent_handle_list_dtor(*listp, provider TSRMLS_CC); - php_http_persistent_handle_list_init(*listp); - } + zend_hash_apply_with_argument(&provider->list.free, php_http_persistent_handle_apply_cleanup, &provider->list.free TSRMLS_CC); } } } else { - FOREACH_HASH_VAL(pos1, &php_http_persistent_handles_hash, provider) { + HashPosition pos; + + FOREACH_HASH_VAL(pos, &php_http_persistent_handles_hash, provider) { if (ident_str && ident_len) { if ((list = php_http_persistent_handle_list_find(provider, ident_str, ident_len TSRMLS_CC))) { - php_http_persistent_handle_list_dtor(list, provider TSRMLS_CC); - php_http_persistent_handle_list_init(list); + zend_hash_apply_with_argument(&list->free, php_http_persistent_handle_apply_cleanup_ex, &provider->rf TSRMLS_CC); } } else { - FOREACH_HASH_VAL(pos2, &provider->list.free, listp) { - php_http_persistent_handle_list_dtor(*listp, provider TSRMLS_CC); - php_http_persistent_handle_list_init(*listp); - } + zend_hash_apply_with_argument(&provider->list.free, php_http_persistent_handle_apply_cleanup, &provider->list.free TSRMLS_CC); } } } @@ -442,6 +387,16 @@ PHP_HTTP_API HashTable *php_http_persistent_handle_statall(HashTable *ht TSRMLS_ return ht; } +static php_http_resource_factory_ops_t php_http_persistent_handle_rf_ops = { + (php_http_resource_factory_handle_ctor_t) php_http_persistent_handle_acquire, + (php_http_resource_factory_handle_copy_t) php_http_persistent_handle_accrete, + (php_http_resource_factory_handle_dtor_t) php_http_persistent_handle_release +}; + +PHP_HTTP_API php_http_resource_factory_ops_t *php_http_persistent_handle_resource_factory_ops(void) +{ + return &php_http_persistent_handle_rf_ops; +} /* * Local variables: diff --git a/php_http_persistent_handle.h b/php_http_persistent_handle.h index 4c43374..b94cafe 100644 --- a/php_http_persistent_handle.h +++ b/php_http_persistent_handle.h @@ -43,15 +43,11 @@ PHP_HTTP_API void *php_http_persistent_handle_acquire(php_http_persistent_handle PHP_HTTP_API void php_http_persistent_handle_release(php_http_persistent_handle_factory_t *a, void *handle TSRMLS_DC); PHP_HTTP_API void *php_http_persistent_handle_accrete(php_http_persistent_handle_factory_t *a, void *handle TSRMLS_DC); -PHP_HTTP_API php_http_resource_factory_ops_t *php_http_persistnet_handle_factory_ops(void); +PHP_HTTP_API php_http_resource_factory_ops_t *php_http_persistent_handle_resource_factory_ops(void); PHP_HTTP_API void php_http_persistent_handle_cleanup(const char *name_str, size_t name_len, const char *ident_str, size_t ident_len TSRMLS_DC); PHP_HTTP_API HashTable *php_http_persistent_handle_statall(HashTable *ht TSRMLS_DC); -PHP_HTTP_API STATUS php_http_persistent_handle_acquire2(const char *name_str, size_t name_len, const char *ident_str, size_t ident_len, void **handle TSRMLS_DC); -PHP_HTTP_API STATUS php_http_persistent_handle_release2(const char *name_str, size_t name_len, const char *ident_str, size_t ident_len, void **handle TSRMLS_DC); -PHP_HTTP_API STATUS php_http_persistent_handle_accrete2(const char *name_str, size_t name_len, const char *ident_str, size_t ident_len, void *old_handle, void **new_handle TSRMLS_DC); - #endif /* PHP_HTTP_PERSISTENT_HANDLE_H */ /* diff --git a/php_http_request_factory.c b/php_http_request_factory.c index 1f5f241..79e348f 100644 --- a/php_http_request_factory.c +++ b/php_http_request_factory.c @@ -147,13 +147,7 @@ PHP_METHOD(HttpRequestFactory, createRequest) name_len = spprintf(&name_str, 0, "http_request.%s", Z_STRVAL_P(zdriver)); if ((pf = php_http_persistent_handle_concede(NULL , name_str, name_len, Z_STRVAL_P(phi), Z_STRLEN_P(phi) TSRMLS_CC))) { - php_http_resource_factory_ops_t ops = { - (php_http_resource_factory_handle_ctor_t) php_http_persistent_handle_acquire, - (php_http_resource_factory_handle_copy_t) php_http_persistent_handle_accrete, - (php_http_resource_factory_handle_dtor_t) php_http_persistent_handle_release - }; - - rf = php_http_resource_factory_init(NULL, &ops, pf, (void (*)(void *)) php_http_persistent_handle_abandon); + rf = php_http_resource_factory_init(NULL, php_http_persistent_handle_resource_factory_ops(), pf, (void (*)(void *)) php_http_persistent_handle_abandon); } efree(name_str); @@ -224,13 +218,7 @@ PHP_METHOD(HttpRequestFactory, createPool) name_len = spprintf(&name_str, 0, "http_request_pool.%s", Z_STRVAL_P(zdriver)); if ((pf = php_http_persistent_handle_concede(NULL , name_str, name_len, Z_STRVAL_P(phi), Z_STRLEN_P(phi) TSRMLS_CC))) { - php_http_resource_factory_ops_t ops = { - (php_http_resource_factory_handle_ctor_t) php_http_persistent_handle_acquire, - (php_http_resource_factory_handle_copy_t) php_http_persistent_handle_accrete, - (php_http_resource_factory_handle_dtor_t) php_http_persistent_handle_release - }; - - rf = php_http_resource_factory_init(NULL, &ops, pf, (void (*)(void *)) php_http_persistent_handle_abandon); + rf = php_http_resource_factory_init(NULL, php_http_persistent_handle_resource_factory_ops(), pf, (void (*)(void *)) php_http_persistent_handle_abandon); } efree(name_str); @@ -291,13 +279,7 @@ PHP_METHOD(HttpRequestFactory, createDataShare) name_len = spprintf(&name_str, 0, "http_request_datashare.%s", Z_STRVAL_P(zdriver)); if ((pf = php_http_persistent_handle_concede(NULL , name_str, name_len, Z_STRVAL_P(phi), Z_STRLEN_P(phi) TSRMLS_CC))) { - php_http_resource_factory_ops_t ops = { - (php_http_resource_factory_handle_ctor_t) php_http_persistent_handle_acquire, - (php_http_resource_factory_handle_copy_t) php_http_persistent_handle_accrete, - (php_http_resource_factory_handle_dtor_t) php_http_persistent_handle_release - }; - - rf = php_http_resource_factory_init(NULL, &ops, pf, (void (*)(void *)) php_http_persistent_handle_abandon); + rf = php_http_resource_factory_init(NULL, php_http_persistent_handle_resource_factory_ops(), pf, (void (*)(void *)) php_http_persistent_handle_abandon); } efree(name_str);