[fpc-devel] TAVLTree(avl_tree.pp) thread safety, fcl-xml(DOM) is also concerned.

Burkhard Carstens fpc at bcsoft.de
Thu Aug 7 11:15:16 CEST 2008


Am Mittwoch, 6. August 2008 20:41 schrieb Inoussa OUEDRAOGO:
> Hi,
>
> TAVLTree in avl_tree.pp is not thread safe due to the node
> allocation and de-allocation done through the global
> declared "NodeMemManager" variable. TAVLTreeNodeMemManager
> implementation is cleary not thread safe, which btw IMHO
> is a good thing ( for performance reason).
>
> Proposition :
>
> (a) TAVLTree should allow, at construct time, to
>     specify a Node memory manager which will be kept and used.
>     If not specified the global one will be used.
>
> (b) "NodeMemManager" should be declared as "ThreadVar".
>
> This changes does not break the API while making the
> implementation thread safe.
>
> By the way note that the XML DOM's implementation (
> TDOMNode_WithChildren ) uses TAVLTree, so every code that uses the
> fcl-xml package mainly the DOM unit, is not thread safe. If this
> proposition is accepted it will be nice to have it introduced in the
> soon to be release fpc 2.2.2, in the case that is still possible,
> mainly for server programming.
>
> Attached is a patch that demonstrate the above proposition.

What about this:
* let TAVLTree access the node manager using a local reference
  (TAVLTree.FNodeMemManager)
* in TAVLTree.create:
  if IsMultiThread then 
    FNodeMemManager:=TAVLTreeNodeMemManager.create
  else
    FNodeMemManager:=NodeMemManager;
* in TAVLTree.destroy:
  if IsMultiThread then 
    FNodeMemManager.free;

This way, nothing would change for single threaded apps but it would be 
fully threadsafe for multithreaded (AFAIKS). This is probably not a 
perfect solution, but the least intrusive one ..

regards
 Burkhard




More information about the fpc-devel mailing list