Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » PHP
  • » ts_free_thread() frees only temporary in multiple parallel threads [RSS Feed]

#1 Nov. 30, 2010 22:16:18

128bitencrypted a.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

ts_free_thread() frees only temporary in multiple parallel threads


OS: Windows (XP and 7 tested)
PHP: 5.2.9 and 5.3.3

I created my own php sapi 1 year ago and it was only single threaded.
Now I tried to make it multi-threaded by using TSRM.I got 1 thread starting up and shutting down the TSRM and my module andmany other threads executing requests. You can take a look at thisabstract example here:DWORD WINAPI test_thread(void*)
{
TSRMLS_FETCH() // a lot of memory is allocated here

// execute the request (just core functions written here)
php_request_startup(TSRMLS_C);
php_execute_script(fh, TSRMLS_C);
php_request_shutdown(TSRMLS_C);ts_free_thread(); // I tried to free the memory allocated throughTSRMLS_FETCH() here}

int main()
{
tsrm_startup(25, 1, 0, NULL);
sapi_startup(&my_php_sapi);
php_module_startup(&my_php_sapi, NULL, 0);
sapi_activate(TSRMLS_C);

// start 25 threads running "test_thread()" here using CreateThread()

// call module/sapi shutdown functions
tsrm_shutdown(); // all memory gets freed here
}The request threads work fine but I noticed that all the copies allocatedthrough TSRMLS_FETCH() last in the memory until tsrm_shutdown() getsexecuted. So I browsed the web and found that ts_free_thread() shouldsolve it. But now here's the problem:When I'm executing only 1 request thread at a time, everything works fineand ts_free_thread() frees all resources. But when I run multiple threadsat the same time the memory gets up and down until all threads finished.But after all threads exited the memory grows up enormous.This is what happens:
- start 25 threads -- memory: ~25.000 k
- threads finish one by one -- memory: ~25.000 k --> ~4.000 k
- all threads finished -- memory: ~50.000 kI was using php 5.2.9 and now tried the same with the latest stable 5.3.3release but the problem still resists.I couldn't really locate the problem yet.

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit:http://www.php.net/unsub.php

Offline

#2 Dec. 3, 2010 10:17:57

Gary Z.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

ts_free_thread() frees only temporary in multiple parallel threads


I created my own php sapi 1 year ago and it was only single threaded.
Now I tried to make it multi-threaded by using TSRM.

I got 1 thread starting up and shutting down the TSRM and my module and
many other threads executing requests. You can take a look at this
abstract example here:

DWORD WINAPI test_thread(void*)
{
TSRMLS_FETCH() // a lot of memory is allocated here

// execute the request (just core functions written here)
php_request_startup(TSRMLS_C);
php_execute_script(fh, TSRMLS_C);
php_request_shutdown(TSRMLS_C);

ts_free_thread(); // I tried to free the memory allocated through
TSRMLS_FETCH() here
}

int main()
{
tsrm_startup(1, 1, 0, NULL);
sapi_startup(&my_php_sapi);
php_module_startup(&my_php_sapi, NULL, 0);
sapi_activate(TSRMLS_C);

// start 25 threads running "test_thread()" here

// call module/sapi shutdown functions
tsrm_shutdown(); // all memory gets freed here
}

The request threads work fine but I noticed that all the copies
allocated through TSRMLS_FETCH() last in the memory until
tsrm_shutdown() gets executed. So I browsed the web and found that
ts_free_thread() should solve it. But now here's the problem:
When I'm executing only 1 request thread at a time, everything works
fine and ts_free_thread() frees all resources. But when I run multiple
threads at the same time the memory gets up and down until all threads
finished. But after all threads exited the memory grows up enormous.

This is what happens:
- start 25 threads -- memory: ~25.000 k
- threads finish one by one -- memory: ~25.000 k --> ~4.000 k
- all threads finished -- memory: ~40.000 k

I was using php 5.2.9 and now tried the same with the latest stable
5.3.3 release but the problem still resists.

I found out that the timeout thread seems to reallocate my destroyed
resources because it occours short after the thread finishes and gets
destroyed.

I'm currently not really sure if this is a bug or if I'm doing something
wrong?!

Offline

#3 Dec. 12, 2010 15:48:21

Gary Z.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

ts_free_thread() frees only temporary in multiple parallel threads


(Referring to my posts: #50838 and #50916)

Well although I got the feeling no one cares, I finally found a solution
to my problem!!!

Combining my patch (from post #50916) with the php_embed2 module makes
everything working great in multi-threaded usage. The normal php_embed
module does not seem to be designed for that.

So my final conclusion for the multi-threaded SAPI is the following:
- my patch is needed (to avoid false resource allocation)
- php_embed2 module is needed (instead of php_embed)
- TSRMLS_FETCH() needs to be called at the start of every request
- ts_free_thread() should be called at the end of every request

But I still would like to get any comment about my patch...

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit:http://www.php.net/unsub.php

Offline

  • Root
  • » PHP
  • » ts_free_thread() frees only temporary in multiple parallel threads [RSS Feed]

Board footer

Moderator control

Enjoy the 24th of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support