# request for comments on threadsafe / multi-thread enabled Embed2 SAPI

- Thread: https://externals.io/message/43499
- Original archive: https://news-web.php.net/php.internals/43499
- Started by Bas van Beek on 2009-03-26T19:35:44+00:00
- Messages: 3
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #43499 — Bas van Beek — 2009-03-26T19:35:44+00:00

URL: https://externals.io/message/43499#43499

Hi Guys,

The Embed2 SAPI for embedded applications needing multi-thread enabled  
PHP scripting support can be inspected from the following repository:

	http://svn.tobin.nl/public/php/embed2/trunk/

The original Embedded SAPI does not allow concurrent script runs and  
as such is less suited for multi-threaded applications . Also single  
threaded applications
can benefit from the Embed2 SAPI as for each PHP script run, it is not  
necessary to initialize and shutdown the entire PHP environment.

Examples for embedding PHP using the Embed2 SAPI can be found in the  
examples directory.

The C example uses pthreads to showcase a typical posix implementation.
The C++ example uses the boost::thread library to showcase a cross  
platform implementation. Build instructions / make files are also  
added for Linux, OS X and Windows

If good enough I would like to donate the Embed2 SAPI to the PHP  
community using the standard PHP License and request a CVS account for  
adding it to the source tree.

The code has been tested to run on PHP-5.2.9 and PHP-5.3.0RC1 and was  
tested using Mac OS X, Debian Linux and Windows XP.

kind regards,

Bas van Beek

---

## #43507 — Moriyoshi Koizumi — 2009-03-27T02:38:44+00:00

URL: https://externals.io/message/43499#43507 · In reply to: #43499

Isn't it better to avoid any behaviour-changing #define's in a header
file? I mean the following series of lines in php_embed2.h:

/* we control char* lifetime of smart_str as we allow it to cross
request boundaries */
#define SMART_STR_USE_REALLOC 1
/* we use bigger numbers than default as script output will most
likely be bigger than anticipated for smart_str usage */
#define SMART_STR_PREALLOC 2048
#define SMART_STR_START_SIZE 2000
#include <ext/standard/php_smart_str.h>

TSRM needs to be initialized just once on startup. It's not the thing
that has to be initialized per thread

my 2c,
Moriyoshi


> Hi Guys,
>
> The Embed2 SAPI for embedded applications needing multi-thread enabled PHP
> scripting support can be inspected from the following repository:
>
>        http://svn.tobin.nl/public/php/embed2/trunk/
>
> The original Embedded SAPI does not allow concurrent script runs and as such
> is less suited for multi-threaded applications . Also single threaded
> applications
> can benefit from the Embed2 SAPI as for each PHP script run, it is not
> necessary to initialize and shutdown the entire PHP environment.
>
> Examples for embedding PHP using the Embed2 SAPI can be found in the
> examples directory.
>
> The C example uses pthreads to showcase a typical posix implementation.
> The C++ example uses the boost::thread library to showcase a cross platform
> implementation. Build instructions / make files are also added for Linux, OS
> X and Windows
>
> If good enough I would like to donate the Embed2 SAPI to the PHP community
> using the standard PHP License and request a CVS account for adding it to
> the source tree.
>
> The code has been tested to run on PHP-5.2.9 and PHP-5.3.0RC1 and was tested
> using Mac OS X, Debian Linux and Windows XP.
>
> kind regards,
>
> Bas van Beek

---

## #43510 — Bas van Beek — 2009-03-27T07:34:15+00:00

URL: https://externals.io/message/43499#43510 · In reply to: #43507

Hello Moriyoshi,

Op 27 mrt 2009, om 03:38 heeft Moriyoshi Koizumi het volgende  
geschreven:

> Isn't it better to avoid any behaviour-changing #define's in a header
> file? I mean the following series of lines in php_embed2.h:

I agree... I have moved the defines and inclusion of php_smart_str.h  
to the php_embed2.c file so it's behavior
is internal to this source file only.

> TSRM needs to be initialized just once on startup. It's not the thing
> that has to be initialized per thread

TSRM is initialized only once and on startup when using the  
php_embed2_init call.
In threads one simply has to call php_embed2_req_init() to startup the  
request environment when one needs
to run a PHP script.

> my 2c,
> Moriyoshi

kind regards,

Bas van Beek

> 
> > […]
