krakjoe/pthreads

How to get unique php-extension instance for each php thread?

mvnaz opened this issue · 6 comments

mvnaz commented

Hello, this is not an issue or bug. This is the question related to pthreads extension. So I hope I may ask here.

I have a difficult task, creating php extension (I will call it custom-extension) with libssh2. Extension must work with pthreads together. Let's say I have 100 php threads. As I see all threads has the same custom-extension instance. And, as I think, this is why I get a lot problems when I use more then 1 thread. For example global variables in custom-extension C code will be owerwritten by pthreads. Sure, I may use __thread, but it doesn't solve the problem. Libssh2 usage in custom-extension makes things more difficult. I will not provide any code, because I think it doesn't matter.

So the question is: is it possible to make each php pthread use own new custom-extension instance ? I think it will be better for my case, because then I will encapsulate custom-extension for each thread. Also libssh2, used in custom-extension also should be new instance for each thread. This way has to save me for variables overwritting and other segfaults errors.

So maybe I should just compile custom-extension with some flag to get the goal ? I think I have to make own custom-extension instance for each thread to satisfy shared-nothing rule.

Environment

  • Ubuntu 18.04 64bit*
  • PHP CLI 7.2.2 ZTS*

The custom extension has to be thread safe (that means it needs to use PHP's ZTS capabilities, such as the TSRM). https://php.net/manual/en/internals2.memory.tsrm.php

mvnaz commented

The custom extension has to be thread safe (that means it needs to use PHP's ZTS capabilities, such as the TSRM). https://php.net/manual/en/internals2.memory.tsrm.php

Hm, thank you for very important info for me. If I compile custom extension properly, will I have own custom extension instance for each php thread ?

Can you please clarify it a bit for me ? I have added the following code an recompiled but it has not affected:

#ifdef ZTS
#define COUNTER_G(v) TSRMG(counter_globals_id, zend_counter_globals *, v)
#else
#define COUNTER_G(v) (counter_globals.v)
#endif

No, just compiling is not necessary. The php extension code has to make use of it. Copypasting some example also will not simply make it work. The complete code needs to be aware of ZTS.

mvnaz commented

No, just compiling is not necessary. The php extension code has to make use of it. Copypasting some example also will not simply make it work. The complete code needs to be aware of ZTS.

So as I understand anyway custom-extension will not be an unique instance for each thread ? It still will be the same instance but extension code must be written in thread safety to avoid potential problems. So it means that it is impossible to make custom-extension instance to be own for each thread, right ?

mvnaz commented

https://github.com/krakjoe/pthreads#developers

But I don't ask about threads creation on my extension. I ask about is it possible to make pthreads use own instance of custom-extension ? Because now custom-extension is shared-object