Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:38082 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 74790 invoked from network); 2 Jun 2008 16:49:08 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 2 Jun 2008 16:49:08 -0000 X-Host-Fingerprint: 89.79.39.63 chello089079039063.chello.pl Received: from [89.79.39.63] ([89.79.39.63:1401] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 6E/AD-26717-18424484 for ; Mon, 02 Jun 2008 12:49:06 -0400 Message-ID: <6E.AD.26717.18424484@pb1.pair.com> To: internals@lists.php.net Date: Mon, 02 Jun 2008 18:49:01 +0200 User-Agent: Thunderbird 2.0.0.14 (X11/20080502) MIME-Version: 1.0 References: <4843CE26.40902@daylessday.org> In-Reply-To: <4843CE26.40902@daylessday.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Posted-By: 89.79.39.63 Subject: Re: [PHP-DEV] Global structures in extension From: adam@klobukowski.pl (Adam Klobukowski) Antony Dovgal pisze: > On 02.06.2008 13:35, Adam Klobukowski wrote: >> Hello >> >> I'm writing a PHP extension where I'm using a custom library. This >> library uses a structure that needs to be initialized once per server >> instance. First I initialized it in each of my functions and >> deinitialize it ant the end. It works ok, but unfortunetly it turns to >> be to slow. So, I'm trying to do it globally. Everything in that >> library is internally synchronized, so I don not have to worry about >> thread safety. I declare my structure globally (in fact a pointer to a >> structure), initialize it in PHP_MINIT (it works), then deinitialize >> in PHP_MSHUTDOWN (works too). > > That's exactly what lots of extensions do. > >> Unfortunetly, when I'm trying to use this structure in my >> PHP_FUNCTIONs, it does not work. > > You've managed to describe everything except for the problem itself =) > What does this "does not work" mean? > How exactly do you initialize and address these variables? My code uses Yami message passing library (http://www.msobczak.com/prog/yami/) Ok, here is the code, first the "working" (one standalone function) version: PHP_FUNCTION(myFunc) { HPARAMSET params, returnparamset; HMESSAGE hm; enum msgStatus status; char *id = NULL; int id_len; int i, count, size; char *element = NULL; char *server_port = NULL; char *server_ip = NULL; char *name = NULL; char *value = NULL; double d; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &id, &id_len) == FAILURE) return; yamiNetInitialize(); yamiCreateAgent(&agent, 0, NULL); yamiAgentDomainRegister(agent, SERVER_NAME, SERVER_ADDRES, SERVER_PORT, 2); yamiCreateParamSet(¶ms, 1); yamiSetString(params, 0, id); yamiAgentMsgSend(agent, SERVER_NAME, id, "get_info", params, &hm); yamiAgentMsgWait(hm); yamiAgentMsgGetStatus(hm, &status); if (status == eReplied) { yamiAgentMsgGetResponse(hm, &returnparamset); if (!returnparamset) { return; } yamiGetParamCount(returnparamset, &count); array_init(return_value); for (i = 0; i < count; i++) { yamiGetStringLength(returnparamset, i, &size); name = emalloc (sizeof (char) * (size +1)); yamiGetStringValue(returnparamset, i, name); i++; yamiGetStringLength(returnparamset, i, &size); value = emalloc (sizeof (char) * (size +1)); yamiGetStringValue(returnparamset, i, value); add_assoc_string(return_value, name , value, 0); } yamiDestroyParamSet(returnparamset); yamiDestroyParamSet(params); yamiDestroyAgent(agent); yamiNetCleanup(); } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Wrong answer from server."); } } And the non working (with PHP_MINIT and PHP_SHUTDOWN) version: HYAMIAGENT agent; PHP_MINIT_FUNCTION(gra) { yamiNetInitialize(); yamiCreateAgent(&agent, 0, NULL); yamiAgentDomainRegister(agent, SERVER_NAME, SERVER_ADDRES, SERVER_PORT, 2); return SUCCESS; } /* }}} */ /* {{{ PHP_MSHUTDOWN_FUNCTION */ PHP_MSHUTDOWN_FUNCTION(gra) { yamiDestroyAgent(agent); yamiNetCleanup(); return SUCCESS; } PHP_FUNCTION(myFunc) { HPARAMSET params, returnparamset; HMESSAGE hm; enum msgStatus status; char *id = NULL; int id_len; int i, count, size; char *element = NULL; char *server_port = NULL; char *server_ip = NULL; char *name = NULL; char *value = NULL; double d; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &id, &id_len) == FAILURE) return; yamiCreateParamSet(¶ms, 1); yamiSetString(params, 0, id); yamiAgentMsgSend(agent, SERVER_NAME, id, "get_info", params, &hm); yamiAgentMsgWait(hm); yamiAgentMsgGetStatus(hm, &status); if (status == eReplied) { yamiAgentMsgGetResponse(hm, &returnparamset); if (!returnparamset) { return; } yamiGetParamCount(returnparamset, &count); array_init(return_value); for (i = 0; i < count; i++) { yamiGetStringLength(returnparamset, i, &size); name = emalloc (sizeof (char) * (size +1)); yamiGetStringValue(returnparamset, i, name); i++; yamiGetStringLength(returnparamset, i, &size); value = emalloc (sizeof (char) * (size +1)); yamiGetStringValue(returnparamset, i, value); add_assoc_string(return_value, name , value, 0); } yamiDestroyParamSet(returnparamset); yamiDestroyParamSet(params); } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Wrong answer from server."); } } And by "non working" I mean that it hangs in yamiAgentMsgWait forever :( Adam Klobukowski