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
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php