Re: Segfault problem with --enable-maintainer-zts
| From: | Bas van Beek | Date: | Sun, 05 Apr 2009 10:57:52 +0000 |
| Subject: | Re: Segfault problem with --enable-maintainer-zts | ||
| References: | 1 | Groups: | php.pecl.dev |
| Request: | Send a blank email to pecl-dev+get-6294@lists.php.net to get a copy of this message | ||
Hello Cesar,
Op 5 apr 2009, om 09:51 heeft Cesar D. Rodas het volgende geschreven:
Hello, I am writing an open source extension (http://github.com/crodas/phplibtextcat/tree/master). Now I want to add a function that used global variables to set default values, but when I compile it with thread safe enable, it crashed, this is what I do: In php_textcat.h: #ifdef ZTSIn ZTS mode the textcat struct must be allocated for each thread start and freed for each thread end. The ts_allocate_id() function can do this for you. In your PHP_MINIT_FUNCTION() add a line similar to this: #ifdef ZTS ts_allocate_id( &textcat_globals_id, sizeof(textcat_globals), (ts_allocate_ctor)textcat_globals_ctor, (ts_allocate_dtor)textcat_globals_dtor ); #else textcat_globals_ctor(&textcat_globals TSRMLS_CC); #endif In your PHP_MSHUTDOWN_FUNCTION() add a line similar to this: #ifndef ZTS textcat_globals_dtor(&textcat_globals TSRMLS_CC); #endif If you need additional construction and destruction of globals items use functions like these: static void textcat_global_ctor(zend_textcat_globals* textcat_globals TSRMLS_DC) { /* the struct is already allocated... you can setup and/or allocate items in the struct */ textcat_globals->mystringitem = emalloc(MYSTRINGSIZE); textcat_globals->threshold = .... } static void textcat_global_dtor(zend_textcat_globals* textcat_globals TSRMLS_DC) { /* if items within the struct need to be freed, do it here... */ efree(textcat_globals->mystringitem); } The ts_allocate_id() function takes care of allocating and freeing global structs per thread and calling the ctor and dtor functions. In non ZTS mode you need to call the ctor and dtor functions in the MINIT and MSHUTDOWN functions yourself. If you don't need to setup or take down items in the structs you can forget about the ctor and dtor functions and pass NULL to arguments 3 and 4 of ts_allocate_id() kind regards, Bas van Beek#include "TSRM.h"#endif ZEND_BEGIN_MODULE_GLOBALS(textcat)int ngram_limit; int threshold; int max_results;ZEND_END_MODULE_GLOBALS(textcat) ZEND_EXTERN_MODULE_GLOBALS(textcat) #ifdef ZTS#define TCG(v) TSRMG(textcat_globals_id, zend_textcat_globals *, v)#else#define TCG(v) (textcat_globals.v)#endif In the php_texcat.c ZEND_DECLARE_MODULE_GLOBALS(textcat) PHP_FUNCTION(foobar) { /* here it crash */ TCG(ngram_limit) = 1; }