Re: [PHP-DEV] thread safety for binding issues

2002-03-31 Thread Markus Fischer

Bless You! It still segfaults all over the place (which is
another story), but that particular call_user_function() now
works, thanks :) and happy easter egg searching ;)

- Markus

On Sun, Mar 31, 2002 at 01:11:27PM +0300, Zeev Suraski wrote : 
 At 20:59 30/03/2002, Markus Fischer wrote:
 Yes, exactly.
 
 Ok, in that case, by default this won't work 'very nicely' with PHP 
 (because it's not very standard), but you should be able to get it to work 
 relatively easily.
 
 The problem:
 
 All of PHP's resources (globals, memory management, resource lists, 
 basically everything) are locked to a specific thread.  If you use all of 
 the standard macros (TSRMLS_FETCH(), TSRMLS_DC, etc.) then your code lives 
 entirely within the thread in which it's running, and cannot access 
 anything from other threads.
 
 
 The solution:
 
 If you need to manipulate resources of another thread, you can do so, but 
 you have to do a bit of hacking on your own.
 
 1.  When you register the callback, you have to remember which thread you'd 
 want to interface with when the callback is called.  You can obtain the 
 thread id by using tsrm_thread_id():
 THREAD_T thread_id = tsrm_thread_id();
 
 You have to store this information in a place which would be later 
 accessible from the callback.
 
 2.  inside the callback, don't use TSRMLS_FETCH().  Instead, use the 
 following line of code:
 
 void ***tsrm_ls = (void ***) ts_resource_ex(0, thread_id);
 
 3.  It's important to remember that there are some things that you must not 
 do inside your callback, due to mutual exclusion problems.  For instance, 
 doing any 'writes' to the resource lists, or even the memory manager - is 
 likely to cause a crash due to race conditions with the thread that owns 
 these resources.  As long as you limit yourself to read-only data, it 
 should be fine (even though I don't think we ever made sure that the data 
 structures are safe for reading while another thread writes to them).
 
 That's about all I can tell you, other than good luck, and don't do it 
 unless you really have to :)
 
 Zeev
 
 
 -- 
 PHP Development Mailing List http://www.php.net/
 To unsubscribe, visit: http://www.php.net/unsub.php

-- 
Please always Cc to me when replying to me on the lists.
GnuPG Key: http://guru.josefine.at/~mfischer/C2272BD0.asc
Mind if I MFH ? What QA did you do on it? the usual? ah... none :)

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




Re: [PHP-DEV] thread safety for binding issues

2002-03-30 Thread Markus Fischer

Yes, thanks for the answer.

I think the problem is just that I mixed up two different
things (thread-safety vs. multi-threaded). Err .. any idea
if/when PHP itself will get MTed ? :)

- Markus

On Sat, Mar 30, 2002 at 09:14:56AM +0200, Zeev Suraski wrote : 
 It should work, it's using exactly the same thread safe code as the one 
 under Windows...
 
 Zeev
 
 At 14:14 29/03/2002, Markus Fischer wrote:
 Hi,
 
 how much thread-safe is PHP on linux when compiled with
 --enable-experimental-zts ? When I a set up a callback
 handler for a 3rd library which uses threads and in this (C)
 callback I call call_user_function() (to provide custom
 callbacks) - bang , I get a segfault. Should it work and I'm
 doing something stupid or is this some limitation? Hints?
 
 - Markus
 
 --
 Please always Cc to me when replying to me on the lists.
 GnuPG Key: http://guru.josefine.at/~mfischer/C2272BD0.asc
 Mind if I MFH ? What QA did you do on it? the usual? ah... none :)
 
 --
 PHP Development Mailing List http://www.php.net/
 To unsubscribe, visit: http://www.php.net/unsub.php
 
 
 -- 
 PHP Development Mailing List http://www.php.net/
 To unsubscribe, visit: http://www.php.net/unsub.php

-- 
Please always Cc to me when replying to me on the lists.
GnuPG Key: http://guru.josefine.at/~mfischer/C2272BD0.asc
Mind if I MFH ? What QA did you do on it? the usual? ah... none :)

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




Re: [PHP-DEV] thread safety for binding issues

2002-03-30 Thread Zeev Suraski

Get MT'd?  What do you mean by that?  We have no plans to make PHP a 
multithreaded application at any time in the future, but I'm not exactly 
sure why it matters to you..?

Zeev

At 17:16 30/03/2002, Markus Fischer wrote:
 Yes, thanks for the answer.

 I think the problem is just that I mixed up two different
 things (thread-safety vs. multi-threaded). Err .. any idea
 if/when PHP itself will get MTed ? :)

 - Markus

On Sat, Mar 30, 2002 at 09:14:56AM +0200, Zeev Suraski wrote :
  It should work, it's using exactly the same thread safe code as the one
  under Windows...
 
  Zeev
 
  At 14:14 29/03/2002, Markus Fischer wrote:
  Hi,
  
  how much thread-safe is PHP on linux when compiled with
  --enable-experimental-zts ? When I a set up a callback
  handler for a 3rd library which uses threads and in this (C)
  callback I call call_user_function() (to provide custom
  callbacks) - bang , I get a segfault. Should it work and I'm
  doing something stupid or is this some limitation? Hints?
  
  - Markus
  
  --
  Please always Cc to me when replying to me on the lists.
  GnuPG Key: http://guru.josefine.at/~mfischer/C2272BD0.asc
  Mind if I MFH ? What QA did you do on it? the usual? ah... none :)
  
  --
  PHP Development Mailing List http://www.php.net/
  To unsubscribe, visit: http://www.php.net/unsub.php
 
 
  --
  PHP Development Mailing List http://www.php.net/
  To unsubscribe, visit: http://www.php.net/unsub.php

--
Please always Cc to me when replying to me on the lists.
GnuPG Key: http://guru.josefine.at/~mfischer/C2272BD0.asc
Mind if I MFH ? What QA did you do on it? the usual? ah... none :)


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




Re: [PHP-DEV] thread safety for binding issues

2002-03-30 Thread Shane Caraveo

My understanding from Markus' original email was that the library he is 
using calls back into php on a different thread than that which he 
called it with.

Shane

Zeev Suraski wrote:
 Get MT'd?  What do you mean by that?  We have no plans to make PHP a 
 multithreaded application at any time in the future, but I'm not exactly 
 sure why it matters to you..?
 
 Zeev
 
 At 17:16 30/03/2002, Markus Fischer wrote:
 
 Yes, thanks for the answer.

 I think the problem is just that I mixed up two different
 things (thread-safety vs. multi-threaded). Err .. any idea
 if/when PHP itself will get MTed ? :)

 - Markus

 On Sat, Mar 30, 2002 at 09:14:56AM +0200, Zeev Suraski wrote :
  It should work, it's using exactly the same thread safe code as the one
  under Windows...
 
  Zeev
 
  At 14:14 29/03/2002, Markus Fischer wrote:
  Hi,
  
  how much thread-safe is PHP on linux when compiled with
  --enable-experimental-zts ? When I a set up a callback
  handler for a 3rd library which uses threads and in this (C)
  callback I call call_user_function() (to provide custom
  callbacks) - bang , I get a segfault. Should it work and I'm
  doing something stupid or is this some limitation? Hints?
  
  - Markus
  
  --
  Please always Cc to me when replying to me on the lists.
  GnuPG Key: http://guru.josefine.at/~mfischer/C2272BD0.asc
  Mind if I MFH ? What QA did you do on it? the usual? ah... 
 none :)
  
  --
  PHP Development Mailing List http://www.php.net/
  To unsubscribe, visit: http://www.php.net/unsub.php
 
 
  --
  PHP Development Mailing List http://www.php.net/
  To unsubscribe, visit: http://www.php.net/unsub.php

 -- 
 Please always Cc to me when replying to me on the lists.
 GnuPG Key: http://guru.josefine.at/~mfischer/C2272BD0.asc
 Mind if I MFH ? What QA did you do on it? the usual? ah... none 
 :)
 
 
 




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




Re: [PHP-DEV] thread safety for binding issues

2002-03-30 Thread Zeev Suraski

Markus - is that what you're talking about..?

Zeev

At 20:37 30/03/2002, Shane Caraveo wrote:
My understanding from Markus' original email was that the library he is 
using calls back into php on a different thread than that which he called 
it with.

Shane


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




Re: [PHP-DEV] thread safety for binding issues

2002-03-30 Thread Markus Fischer

Yes, exactly.

On Sun, Mar 31, 2002 at 12:48:04AM +0300, Zeev Suraski wrote : 
 Markus - is that what you're talking about..?
 
 Zeev
 
 At 20:37 30/03/2002, Shane Caraveo wrote:
 My understanding from Markus' original email was that the library he is 
 using calls back into php on a different thread than that which he called 
 it with.
 
 Shane

-- 
Please always Cc to me when replying to me on the lists.
GnuPG Key: http://guru.josefine.at/~mfischer/C2272BD0.asc
Mind if I MFH ? What QA did you do on it? the usual? ah... none :)

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




Re: [PHP-DEV] thread safety for binding issues

2002-03-30 Thread Zeev Suraski

At 20:59 30/03/2002, Markus Fischer wrote:
 Yes, exactly.

Ok, in that case, by default this won't work 'very nicely' with PHP 
(because it's not very standard), but you should be able to get it to work 
relatively easily.

The problem:

All of PHP's resources (globals, memory management, resource lists, 
basically everything) are locked to a specific thread.  If you use all of 
the standard macros (TSRMLS_FETCH(), TSRMLS_DC, etc.) then your code lives 
entirely within the thread in which it's running, and cannot access 
anything from other threads.


The solution:

If you need to manipulate resources of another thread, you can do so, but 
you have to do a bit of hacking on your own.

1.  When you register the callback, you have to remember which thread you'd 
want to interface with when the callback is called.  You can obtain the 
thread id by using tsrm_thread_id():
THREAD_T thread_id = tsrm_thread_id();

You have to store this information in a place which would be later 
accessible from the callback.

2.  inside the callback, don't use TSRMLS_FETCH().  Instead, use the 
following line of code:

void ***tsrm_ls = (void ***) ts_resource_ex(0, thread_id);

3.  It's important to remember that there are some things that you must not 
do inside your callback, due to mutual exclusion problems.  For instance, 
doing any 'writes' to the resource lists, or even the memory manager - is 
likely to cause a crash due to race conditions with the thread that owns 
these resources.  As long as you limit yourself to read-only data, it 
should be fine (even though I don't think we ever made sure that the data 
structures are safe for reading while another thread writes to them).

That's about all I can tell you, other than good luck, and don't do it 
unless you really have to :)

Zeev


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




Re: [PHP-DEV] thread safety for binding issues

2002-03-29 Thread Shane Caraveo

Markus Fischer wrote:
 Hi,
 
 how much thread-safe is PHP on linux when compiled with
 --enable-experimental-zts ? When I a set up a callback
 handler for a 3rd library which uses threads and in this (C)
 callback I call call_user_function() (to provide custom
 callbacks) - bang , I get a segfault. Should it work and I'm
 doing something stupid or is this some limitation? Hints?
 
 - Markus
 

PHP is thread safe, not multi-threaded.  ie. scripts cannot be 
multi-threaded.  You will have to somehow proxy the data back to the 
original calling thread for your library to work.

Shane


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




Re: [PHP-DEV] thread safety for binding issues

2002-03-29 Thread Markus Fischer

On Fri, Mar 29, 2002 at 09:14:46AM -0800, Shane Caraveo wrote : 
 Markus Fischer wrote:
 Hi,
 
 how much thread-safe is PHP on linux when compiled with
 --enable-experimental-zts ? When I a set up a callback
 handler for a 3rd library which uses threads and in this (C)
 callback I call call_user_function() (to provide custom
 callbacks) - bang , I get a segfault. Should it work and I'm
 doing something stupid or is this some limitation? Hints?
 
 - Markus
 
 
 PHP is thread safe, not multi-threaded.  ie. scripts cannot be 
 multi-threaded.  You will have to somehow proxy the data back to the 
 original calling thread for your library to work.

Ah, understood. So, the short answer would be it was by own
stupidity ;-)

About proxying the data .. you don't happen to have some more
description or resource how to accomplish this before I try
to reinvent the wheel ? thanks.

- Markus

-- 
Please always Cc to me when replying to me on the lists.
GnuPG Key: http://guru.josefine.at/~mfischer/C2272BD0.asc
Mind if I MFH ? What QA did you do on it? the usual? ah... none :)

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




Re: [PHP-DEV] thread safety for binding issues

2002-03-29 Thread Shane Caraveo

Markus Fischer wrote:
 On Fri, Mar 29, 2002 at 09:14:46AM -0800, Shane Caraveo wrote : 
 
Markus Fischer wrote:

   Hi,

   how much thread-safe is PHP on linux when compiled with
   --enable-experimental-zts ? When I a set up a callback
   handler for a 3rd library which uses threads and in this (C)
   callback I call call_user_function() (to provide custom
   callbacks) - bang , I get a segfault. Should it work and I'm
   doing something stupid or is this some limitation? Hints?

   - Markus


PHP is thread safe, not multi-threaded.  ie. scripts cannot be 
multi-threaded.  You will have to somehow proxy the data back to the 
original calling thread for your library to work.
 
 
 Ah, understood. So, the short answer would be it was by own
 stupidity ;-)
 
 About proxying the data .. you don't happen to have some more
 description or resource how to accomplish this before I try
 to reinvent the wheel ? thanks.
 
 - Markus
 


You'll probably be able to get away with using condition variables, but 
it's too much to describe, and I haven't done much pthreads stuff.  You 
could get the o'reilly pthreads book for reference, or dig around for 
exampls on the net.

Shane



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




Re: [PHP-DEV] thread safety for binding issues

2002-03-29 Thread Zeev Suraski

It should work, it's using exactly the same thread safe code as the one 
under Windows...

Zeev

At 14:14 29/03/2002, Markus Fischer wrote:
 Hi,

 how much thread-safe is PHP on linux when compiled with
 --enable-experimental-zts ? When I a set up a callback
 handler for a 3rd library which uses threads and in this (C)
 callback I call call_user_function() (to provide custom
 callbacks) - bang , I get a segfault. Should it work and I'm
 doing something stupid or is this some limitation? Hints?

 - Markus

--
Please always Cc to me when replying to me on the lists.
GnuPG Key: http://guru.josefine.at/~mfischer/C2272BD0.asc
Mind if I MFH ? What QA did you do on it? the usual? ah... none :)

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


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