Re: Re: global variables
| From: | Justin Hannus | Date: | Tue, 07 Dec 2004 15:54:20 +0000 |
| Subject: | Re: Re: global variables | ||
| References: | 1 2 3 | Groups: | php.pecl.dev |
| Request: | Send a blank email to pecl-dev+get-1701@lists.php.net to get a copy of this message | ||
Thank you for clearing that up :)
-Justin
----- Original Message ----- From: "Wez Furlong" <kingwez@gmail.com>
To: "Justin Hannus" <jhannus@visualconceptsinc.com>
Cc: <pecl-dev@lists.php.net>
Sent: Tuesday, December 07, 2004 9:26 AM
Subject: Re: [PECL-DEV] Re: global variables
ZEND_MODULE_GLOBALS is intended to create per-request globals, so if you hope to maintain state between requests, this approach won't work. To maintain variables between requests, simply declare them as globals as you would normally in a C program, but note the following: - in threaded web servers you should mutex access to the globals or risk races and possible crashes - in forking web servers (eg: Apache) the globals are "local" to the current apache child. A given client is not guaranteed to hit the same process for consecutive requests, and the Apache child can be killed and recycled out from under your PHP script between requests. In other words, only per-request globals make sense under Apache. You should also be aware that PHP has the concept of isolated requests; that is, after a request is run, all state should be reset to clean, default values. There are a few special cases where this doesn't happen (persistent resource management and opcode caches being the only two things that spring to mind right now). For your particular problem, I would create the tree as a true global during MINIT (and free it in MSHUTDOWN). Provided that you don't modify the tree contents after MINIT, it should work out just fine on all SAPI. Note that loading it in MINIT will delay the initialization of PHP by however long that process takes, for each Apache/CGI child process. If it takes "too long", you might want to rethink the structure of your application. --Wez. On Mon, 6 Dec 2004 13:15:14 -0500, Justin Hannus <jhannus@visualconceptsinc.com> wrote:Please use "Reply all" so the post goes to the list. Your code seems fine. The only thing I can think of is that maybe your getting a segfault somewhere which causes php_first_test_init_globals() to be called again?? Though I'm not sure that under a webserver sapi that php module startup process would start all over again after a segfault. Try compiling your extension with --enable-debug and watch for output, check your webservers logs or see if you have any core dumps laying around from it. Maybe some-one else can comment. -Justin ----- Original Message ----- From: "Siewn" <ml@croiset.com> To: "Justin Hannus" <jhannus@visualconceptsinc.com> Sent: Monday, December 06, 2004 12:45 PM Subject: Re: [PECL-DEV] Re: global variablesI'm sorry to disturb you with this problem, I know it's probably simple but I really tried many ways and I can't find the good one. I tried to do as you suggest but it doesn't work. Your can see ( http://ns3288.ovh.net/~dweis/test_ext.php ), if you refresh slowly the variable is well incremented but if you do it fast, it doesn't work any more and you can guess the variable is initialized in each apache process. If you can just look at my short source code, I would be really thankful. http://ns3288.ovh.net/~dweis/first_test.c.txt http://ns3288.ovh.net/~dweis/php_first_test.h.txt http://ns3288.ovh.net/~dweis/test_ext.php.txt Thanks a lot. TristanYou can do this with a combo of your extension globals struct, which you should have defined in your php_ext.h file generated by PECL_Gen or ext_skel, and your PHP_MINIT_FUNCTION which is called at module startup. Its counterpart is the PHP_MSHUTDOWN_FUNCTION. On module startup you can build your tree in the PHP_MINIT_FUNCTION and store it in your extension globals struct, which can be accessed by other functions in your extension. Just remember to free any memory that you emalloc in your PHP_MINIT_FUNCTION with your PHP_MSHUTDOWN_FUNCTION. You can find examples in just about all extension source code. -Justin "Siewn" <ml@croiset.com> wrote in message news:41B233D6.9000401@croiset.com...Hi, I writing an extension for text search tools and I have a little problem with global variables. In each search, I use a binary tree which is always the same. So it's a waste of time to build this tree every time I call my search function. Then I would prefer create this tree just one time, when the extension is loaded (when apache & php start) and always access to this same data. But I can't find how to do this. I try to create a variable just under /* True global resources - no need for thread safety here */ but I assume it's not good because this variable seems to be dependant on each apache process... I'm sure there's a solution because turckmmcache store data in such global variable but I can't really understand how it did it. Thanks a lot -- tristan (who must improve his english)-- PECL development discussion Mailing List (http://pecl.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php