[AD] thread safety in primitives addon

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


(moving the discussion from A.cc)
The DirectX part of the primitives addon may allocate some buffers which
are cached for later use.  The pointers to the buffers are currently
global so not thread safe.

Here are two ideas, one complicated, one simple.


TLS solution
============

al_init_primitives_addon() allocates a new key with TlsAlloc().
When a thread needs a buffer it calls TlsGetValue() to get the pointer.

When a thread allocates/reallocates a cached buffer it registers it on a
global list.  al_shutdown_primitives_addon() is responsible for freeing
everything on the global list.  Hence all cache buffers will be freed
eventually, but not immediately when the thread exits.

(al_init_primitives_addon would cause al_shutdown_primitives_addon to be
called implicitly when Allegro is shut down.)


Mutex solution
==============

My guess is that drawing primitives at the same time in multiple threads
is rather unlikely, so we can just use a mutex to prevent more than one
thread using a buffer at any time.

Obviously this solution is preferred if the assumption holds.

Peter




Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/