[AD] tls.c

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


Please find below a patch to add thread-local storage support for OSX.
Someone might be able to rearrange things to make it a bit neater
overall.

Peter

Index: src/tls.c
===================================================================
--- src/tls.c   (revision 9781)
+++ src/tls.c   (working copy)
@@ -134,13 +134,44 @@

 #if defined ALLEGRO_MSVC
 #define THREAD_LOCAL __declspec(thread)
+#define HAVE_NATIVE_TLS
 #elif defined ALLEGRO_MACOSX
 #define THREAD_LOCAL
-#else
+
+static pthread_key_t tls_key = 0;
+
+void osx_threads_init(void)
+{
+  pthread_key_create(&tls_key, NULL);
+}
+
+static thread_local_state _tls;
+
+static thread_local_state* osx_thread_init(void)
+{
+       /* Allocate and copy the 'template' object */
+    thread_local_state* ptr = (thread_local_state*)
_AL_MALLOC(sizeof(thread_local_state));
+    memcpy(ptr, &_tls, sizeof(thread_local_state));
+    pthread_setspecific(tls_key, ptr);
+       return ptr;
+}
+
+/* This function is short so it can hopefully be inlined. */
+static thread_local_state* tls_get(void)
+{
+       thread_local_state* ptr = (thread_local_state*)
pthread_getspecific(tls_key);
+       if (ptr == NULL)
+    {
+               /* Must create object */
+               ptr = osx_thread_init();
+    }
+       return ptr;
+}
+#else /* Not OSX */
 #define THREAD_LOCAL __thread
+#define HAVE_NATIVE_TLS
 #endif

-
 static THREAD_LOCAL thread_local_state _tls = {
    0,
    0,
@@ -159,13 +190,13 @@
    _al_blender_alpha_inverse_alpha
 };

-
+#ifdef HAVE_NATIVE_TLS
 static thread_local_state *tls_get(void)
 {
        return &_tls;
 }
+#endif

-
 #endif




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