You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modules-dev@httpd.apache.org by Andrej van der Zee <an...@gmail.com> on 2009/03/17 13:23:32 UTC

child_init for threads?

Hi,

I was wondering if there is an analogue hook like child_init() for
threads that I can use for doing some time-consuming
thread-initialisation that should not slow down an HTTP request
handler. More specific, I want to initialise some per-thread variables
that come from a database. Does the APR offer something like this, or
is there a way to hack this into my module, without changing the APR?

Thank you,
Andrej

Re: child_init for threads?

Posted by Andrej van der Zee <an...@gmail.com>.
Hi,


>
> apr_os_thread_t os_thd = apr_os_thread_current ();
> apr_thread_t *apr_thd;
> apr_status_t apr_os_thread_put(&apr_thd, &os_thd, r->pool);
>

Okay, this clearly doesn't work. I misunderstood the documentation,
but the sources don't lie. Anyway, how can I get the apr_thread_t for
the current thread in a module for MPM worker?

Cheers,
Andrej

Re: child_init for threads?

Posted by Andrej van der Zee <an...@gmail.com>.
Hi,

>
> Note that there is a difference between the apr_thread_data_*() methods and
> the apr_threadkey_private_*() methods. The apr_thread_data_*() methods
> actually associate your data with the thread->pool. Since each new apr
> thread has it's own private thread->pool everything should work fine.
>
> The apr_threadkey_private_*() methods use the underlying thread libraries
> TLS routines.
>
> While apr_thread_data_* may work for you, true TLS is only supplied by
> apr_threadkey_private_* routines.
>

Thanks, that's clear! In my case the apr-thread based suffices, I
hope. But I realize now that it is difficult or impossible to get the
apr_thread_t data from the current thread in an Apache module (for
threaded MPMs). Is this possible to do this somehow? For example, does
this work:

apr_os_thread_t os_thd = apr_os_thread_current ();
apr_thread_t *apr_thd;
apr_status_t apr_os_thread_put(&apr_thd, &os_thd, r->pool);

And what would this do in an MPM prefork?

Or maybe there is some better technique?

Thank you!
Andrej

Re: child_init for threads?

Posted by Saju Pillai <sa...@gmail.com>.
Andrej van der Zee wrote:
> Hi,
> 
> Thanks, that helps!
> 
> Since I am developing my modules in C++, I think I should be using this one:
> 
> apr_status_t apr_thread_data_set  	(  	void *   	 data,
> 		const char *  	key,
> 		apr_status_t(*)(void *)  	cleanup,
> 		apr_thread_t *  	thread	
> 	)


Note that there is a difference between the apr_thread_data_*() methods 
and the apr_threadkey_private_*() methods. The apr_thread_data_*() 
methods actually associate your data with the thread->pool. Since each 
new apr thread has it's own private thread->pool everything should work 
fine.

The apr_threadkey_private_*() methods use the underlying thread 
libraries TLS routines.

While apr_thread_data_* may work for you, true TLS is only supplied by 
apr_threadkey_private_* routines.
  	
> 
> If I understand correctly, I can pass my own cleanup function (calling
> "delete" to free memory) which is called automatically when the thread
> is destroyed by the APR framework. Would that be the way, or am I
> still misunderstanding something?

This is correct. cleanup is a ptr to a function that gets passed a 
pointer to your data. You can choose to dealloc your data within this 
function.


srp
-- 
http://saju.net.in

Re: child_init for threads?

Posted by Andrej van der Zee <an...@gmail.com>.
Hi,

Thanks, that helps!

Since I am developing my modules in C++, I think I should be using this one:

apr_status_t apr_thread_data_set  	(  	void *   	 data,
		const char *  	key,
		apr_status_t(*)(void *)  	cleanup,
		apr_thread_t *  	thread	
	) 	

If I understand correctly, I can pass my own cleanup function (calling
"delete" to free memory) which is called automatically when the thread
is destroyed by the APR framework. Would that be the way, or am I
still misunderstanding something?

Thank you,
Andrej

Re: child_init for threads?

Posted by Saju Pillai <sa...@gmail.com>.
Andrej van der Zee wrote:
> Hi,
> 
> Thanks.
> 
>> Yes. create_connection() is the first hook run as part of request
>> processing, a delay in create_connection() will result in a delay in HTTP
>> processing.
>>
> 
> That's too bad. I guess I have to hack this into the MPM then.
> 
> One more question, what would be the way to store thread-specific data
> that is to be re-used across HTTP requests? Is there an example where
> this is done before?

Look at apr_thread_proc.h
apr_threadkey_private_*()

Can't find usage examples in my (old) version of the httpd src tree.

srp
-- 
http://saju.net.in

Re: child_init for threads?

Posted by Andrej van der Zee <an...@gmail.com>.
Hi,

Thanks.

>
> Yes. create_connection() is the first hook run as part of request
> processing, a delay in create_connection() will result in a delay in HTTP
> processing.
>

That's too bad. I guess I have to hack this into the MPM then.

One more question, what would be the way to store thread-specific data
that is to be re-used across HTTP requests? Is there an example where
this is done before?

Thank you,
Andrej

Re: child_init for threads?

Posted by Saju Pillai <sa...@gmail.com>.
Andrej van der Zee wrote:
> Hi,
> 
> Thanks for your reply.
> 
>> AFAIK there's no support for such thing in apache. Have a look
>> yourself in server/mpm/worker/worker.c.
>>
>> You can hook create_connection (the first hook offered by apache in a
>> thread after its creation). However, create_connection is called
>> several times during the life of a thread, so you'll need some boolean
>> to check if you've already initialised the thread-specific data.
>>
> 
> Do you know if my handler for create_connection() takes (lets say) 3
> seconds to execute, would this slow down an HTTP request for 3 seconds
> too? Or is it executed totally outside the processing of an HTTP
> request?


Yes. create_connection() is the first hook run as part of request 
processing, a delay in create_connection() will result in a delay in 
HTTP processing.

srp
-- 
http://saju.net.in

Re: child_init for threads?

Posted by Andrej van der Zee <an...@gmail.com>.
Hi,

Thanks for your reply.

>
> AFAIK there's no support for such thing in apache. Have a look
> yourself in server/mpm/worker/worker.c.
>
> You can hook create_connection (the first hook offered by apache in a
> thread after its creation). However, create_connection is called
> several times during the life of a thread, so you'll need some boolean
> to check if you've already initialised the thread-specific data.
>

Do you know if my handler for create_connection() takes (lets say) 3
seconds to execute, would this slow down an HTTP request for 3 seconds
too? Or is it executed totally outside the processing of an HTTP
request?

Thank you,
Andrej

Re: child_init for threads?

Posted by Sorin Manolache <so...@gmail.com>.
On Tue, Mar 17, 2009 at 13:23, Andrej van der Zee
<an...@gmail.com> wrote:
> Hi,
>
> I was wondering if there is an analogue hook like child_init() for
> threads that I can use for doing some time-consuming
> thread-initialisation that should not slow down an HTTP request
> handler. More specific, I want to initialise some per-thread variables
> that come from a database. Does the APR offer something like this, or
> is there a way to hack this into my module, without changing the APR?

AFAIK there's no support for such thing in apache. Have a look
yourself in server/mpm/worker/worker.c.

You can hook create_connection (the first hook offered by apache in a
thread after its creation). However, create_connection is called
several times during the life of a thread, so you'll need some boolean
to check if you've already initialised the thread-specific data.

S

-- 
A: Because it reverses the logical flow of conversation.
Q: Why is top-posting frowned upon?
A: Top-posting.
Q: What is the most annoying thing in e-mail?