talloc null context thread safe ?

tridge at samba.org tridge at samba.org
Tue Nov 7 00:15:08 GMT 2006


Rémi,

 > I understand that talloc does not deal with threads, and in fact, it is thread 
 > safe, as long as each thread uses different memory contexts. If two threads 
 > uses the same context then they need to synchronise in order to be safe.
 > Am I correct ?

yes. It would be possible to make it thread safe for sharing of
contexts between threads, but I think it would have a pretty large
performance cost. If you can find a way to do it at low cost please
let me know :-)

 > If so, my concern is with the allocation of "top-level" contexts i.e. when 
 > using a NULL parent. When using the "talloc_enable_leak_report", I understand 
 > that the NULL context is in fact a hidden "null context" global variable, so 
 > allocating anything from the NULL context (e.g. tmp_ctx = talloc_new (NULL)) 
 > is no longer thread safe.

that's right. It doesn't affect Samba, but it is something that should
concern threaded programs. 

 > Am I missing something, or may be this is obvious ?
 > So in multi-thread environement, I should never use a NULL context as a parent 
 > (may be instead define my own per-thread null-context). 

yes, we could use thread local storage for the null context, or even
mutex just that one pointer and that shouldn't be a large cost. 

I'd certainly consider a patch that did that, as long as it was clean.

Cheers, Tridge


More information about the samba-technical mailing list