Re: Module globals in a C++ extension

From: Date: Tue, 24 Jan 2012 21:52:52 +0000
Subject: Re: Module globals in a C++ extension
References: 1 2 3  Groups: php.pecl.dev 
Request: Send a blank email to pecl-dev+get-9069@lists.php.net to get a copy of this message
On 24/01/12 21:33, Kārlis Repsons wrpte:
On 01/23/2012 09:25 PM, Antony Dovgal wrote:
On 01/23/2012 11:10 PM, Kārlis Repsons wrote:
Hi again, could someone post some info about writing extensions in C++ with object as a module global? (that is, regardless of the used MPM (or something else), make some object instance common for all running instances of the same extension)
Why don't you just pass it when needed? Or create the object, increase zval's refcount in order to keep it and return it when needed? Sound weird to me, but it's not C++ specific at all.
Here's what's wrong (or part of it): by writing "module global" I meant a variable, which is common for all processes/threads, in which PHP extension code is being run. Think of some common resource, which must be initialized just once and accessed through a single class instance, no matter how the extension code is executed! (and that class instance may contain no zend engine structures) How should it be done? Any examples? Just store that class in a global variable.
What you need to take into account is that you may need to serialize the access to that variable. The easiest would be a counter variable, where you could simply use atomic increments, or a read-only table, which could be initialised in MINIT. On most cases, however, you will need to use tsrm_mutex_lock / tsrm_mutex_unlock around its accesses.

« previous php.pecl.dev (#9069) next »