Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:50739 Return-Path: <128bitencrypted@gmail.com> Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 42617 invoked from network); 30 Nov 2010 22:15:29 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 30 Nov 2010 22:15:29 -0000 Received: from [127.0.0.1] ([127.0.0.1:4474]) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ECSTREAM id B5/50-39751-08775FC4 for ; Tue, 30 Nov 2010 17:15:29 -0500 X-Host-Fingerprint: 93.210.147.139 p5DD2938B.dip0.t-ipconnect.de Received: from [93.210.147.139] ([93.210.147.139:6865] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 3C/52-13326-CC965FC4 for ; Tue, 30 Nov 2010 16:17:01 -0500 To: internals@lists.php.net Content-Type: text/plain; charset=iso-8859-15; format=flowed; delsp=yes Date: Tue, 30 Nov 2010 22:16:57 +0100 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Message-ID: User-Agent: Opera Mail/11.00 (Win32) X-Posted-By: 93.210.147.139 Subject: ts_free_thread() frees only temporary in multiple parallel threads From: 128bitencrypted@gmail.com ("128bitencrypted at gmail dot com") 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 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(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 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: ~50.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 couldn't really locate the problem yet.