Merge branch 'mesa_7_5_branch' into mesa_7_6_branch
[mesa.git] / src / mesa / glapi / glthread.h
index 283d549df0749d69a7dadd9f4e052ad8c427897d..8ec933a85148fef41efcac5e10db9c263ff771fb 100644 (file)
@@ -1,9 +1,8 @@
-
 /*
  * Mesa 3-D graphics library
- * Version:  3.5
+ * Version:  6.5.2
  *
- * Copyright (C) 1999-2001  Brian Paul   All Rights Reserved.
+ * Copyright (C) 1999-2006  Brian Paul   All Rights Reserved.
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
 #define GLTHREAD_H
 
 
-#if defined(PTHREADS) || defined(SOLARIS_THREADS) || defined(WIN32_THREADS) || defined(XTHREADS)
-#define THREADS
+#if defined(USE_MGL_NAMESPACE)
+#define _glapi_Dispatch _mglapi_Dispatch
+#endif
+
+
+
+#if (defined(PTHREADS) || defined(SOLARIS_THREADS) ||\
+     defined(WIN32_THREADS) || defined(BEOS_THREADS)) \
+    && !defined(THREADS)
+# define THREADS
 #endif
 
 #ifdef VMS
@@ -109,23 +116,49 @@ typedef pthread_mutex_t _glthread_Mutex;
 #define _glthread_UNLOCK_MUTEX(name) \
    (void) pthread_mutex_unlock(&(name))
 
-/* This is temporarilly removed because driver binaries cannot count on
- * the existance of _gl_DispatchTSD in libGL.  It only exists in "new"
- * libGL.  We may be able to ressurect this optimization at some point
- * for DRI driver or for software Mesa.
- */
-#if 0
-extern struct _glapi_table * _glapi_DispatchTSD;
-extern _glthread_TSD _gl_DispatchTSD;
+typedef pthread_cond_t _glthread_Cond;
 
-#define GL_CALL(name) \
-   (((__builtin_expect( _glapi_DispatchTSD != NULL, 1 )) \
-       ? _glapi_DispatchTSD : (struct _glapi_table *) pthread_getspecific(_gl_DispatchTSD.key))-> name)
-#endif
+#define _glthread_DECLARE_STATIC_COND(name) \
+   static _glthread_Cond name = PTHREAD_COND_INITIALIZER
+
+#define _glthread_INIT_COND(cond)                      \
+   pthread_cond_init(&(cond), NULL)
+
+#define _glthread_DESTROY_COND(name) \
+   pthread_cond_destroy(&(name))
+
+#define _glthread_COND_WAIT(cond, mutex) \
+  pthread_cond_wait(&(cond), &(mutex))
+
+#define _glthread_COND_SIGNAL(cond) \
+  pthread_cond_signal(&(cond))
+
+#define _glthread_COND_BROADCAST(cond) \
+  pthread_cond_broadcast(&(cond))
+
+
+#else /* PTHREADS */
+
+typedef unsigned int _glthread_Cond;
+#define _glthread_DECLARE_STATIC_COND(name) \
+//  #warning Condition variables not implemented.
 
-#endif /* PTHREADS */
+#define _glthread_INIT_COND(cond)          \
+  ASSERT(0);
 
+#define _glthread_DESTROY_COND(name) \
+  ASSERT(0);
 
+#define _glthread_COND_WAIT(cond, mutex) \
+  ASSERT(0);
+
+#define _glthread_COND_SIGNAL(cond) \
+  ASSERT(0);
+
+#define _glthread_COND_BROADCAST(cond) \
+  ASSERT(0);
+
+#endif
 
 
 /*
@@ -176,64 +209,34 @@ typedef HANDLE _glthread_Thread;
 
 typedef CRITICAL_SECTION _glthread_Mutex;
 
-/* XXX need to really implement mutex-related macros */
-#define _glthread_DECLARE_STATIC_MUTEX(name)  static _glthread_Mutex name = 0
-#define _glthread_INIT_MUTEX(name)  (void) name
-#define _glthread_DESTROY_MUTEX(name)  (void) name
-#define _glthread_LOCK_MUTEX(name)  (void) name
-#define _glthread_UNLOCK_MUTEX(name)  (void) name
+#define _glthread_DECLARE_STATIC_MUTEX(name)  /*static*/ _glthread_Mutex name = {0,0,0,0,0,0}
+#define _glthread_INIT_MUTEX(name)  InitializeCriticalSection(&name)
+#define _glthread_DESTROY_MUTEX(name)  DeleteCriticalSection(&name)
+#define _glthread_LOCK_MUTEX(name)  EnterCriticalSection(&name)
+#define _glthread_UNLOCK_MUTEX(name)  LeaveCriticalSection(&name)
 
 #endif /* WIN32_THREADS */
 
 
-
-
 /*
- * XFree86 has its own thread wrapper, Xthreads.h
- * We wrap it again for GL.
+ * BeOS threads. R5.x required.
  */
-#ifdef XTHREADS
-#include "Xthreads.h"
-
-typedef struct {
-   xthread_key_t key;
-   int initMagic;
-} _glthread_TSD;
-
-typedef xthread_t _glthread_Thread;
-
-typedef xmutex_rec _glthread_Mutex;
+#ifdef BEOS_THREADS
 
-#ifdef XMUTEX_INITIALIZER
-#define _glthread_DECLARE_STATIC_MUTEX(name) \
-   static _glthread_Mutex name = XMUTEX_INITIALIZER
-#else
-#define _glthread_DECLARE_STATIC_MUTEX(name) \
-   static _glthread_Mutex name
+/* Problem with OS.h and this file on haiku */
+#ifndef __HAIKU__
+#include <kernel/OS.h>
 #endif
 
-#define _glthread_INIT_MUTEX(name) \
-   xmutex_init(&(name))
-
-#define _glthread_DESTROY_MUTEX(name) \
-   xmutex_clear(&(name))
-
-#define _glthread_LOCK_MUTEX(name) \
-   (void) xmutex_lock(&(name))
-
-#define _glthread_UNLOCK_MUTEX(name) \
-   (void) xmutex_unlock(&(name))
-
-#endif /* XTHREADS */
-
-
+#include <support/TLS.h>
 
-/*
- * BeOS threads. R5.x required.
+/* The only two typedefs required here
+ * this is cause of the OS.h problem
  */
-#ifdef BEOS_THREADS
-#include <kernel/OS.h>
-#include <support/TLS.h>
+#ifdef __HAIKU__
+typedef int32 thread_id;
+typedef int32 sem_id;
+#endif
 
 typedef struct {
    int32        key;
@@ -249,11 +252,12 @@ typedef struct {
 } benaphore;
 typedef benaphore _glthread_Mutex;
 
-#define _glthread_DECLARE_STATIC_MUTEX(name)  static _glthread_Mutex name = { 0,
-create_sem(0, #name"_benaphore") }
-#define _glthread_INIT_MUTEX(name)    name.sem = create_sem(0, #name"_benaphore"), name.lock = 0
-#define _glthread_LOCK_MUTEX(name)    if((atomic_add(&(name.lock), 1)) >= 1) acquire_sem(name.sem)
-#define _glthread_UNLOCK_MUTEX(name)  if((atomic_add(&(name.lock), -1)) > 1) release_sem(name.sem)
+#define _glthread_DECLARE_STATIC_MUTEX(name)  static _glthread_Mutex name = { 0, 0 }
+#define _glthread_INIT_MUTEX(name)     name.sem = create_sem(0, #name"_benaphore"), name.lock = 0
+#define _glthread_DESTROY_MUTEX(name)  delete_sem(name.sem), name.lock = 0
+#define _glthread_LOCK_MUTEX(name)     if (name.sem == 0) _glthread_INIT_MUTEX(name); \
+                                                                               if (atomic_add(&(name.lock), 1) >= 1) acquire_sem(name.sem)
+#define _glthread_UNLOCK_MUTEX(name)   if (atomic_add(&(name.lock), -1) > 1) release_sem(name.sem)
 
 #endif /* BEOS_THREADS */
 
@@ -265,11 +269,11 @@ create_sem(0, #name"_benaphore") }
  * THREADS not defined
  */
 
-typedef GLuint _glthread_TSD;
+typedef unsigned _glthread_TSD;
 
-typedef GLuint _glthread_Thread;
+typedef unsigned _glthread_Thread;
 
-typedef GLuint _glthread_Mutex;
+typedef unsigned _glthread_Mutex;
 
 #define _glthread_DECLARE_STATIC_MUTEX(name)  static _glthread_Mutex name = 0
 
@@ -304,14 +308,24 @@ _glthread_GetTSD(_glthread_TSD *);
 extern void
 _glthread_SetTSD(_glthread_TSD *, void *);
 
-#ifndef GL_CALL
+#if !defined __GNUC__ || __GNUC__ < 3
+#  define __builtin_expect(x, y) x
+#endif
+
+#if defined(GLX_USE_TLS)
+
+extern __thread struct _glapi_table * _glapi_tls_Dispatch
+    __attribute__((tls_model("initial-exec")));
+
+#define GET_DISPATCH() _glapi_tls_Dispatch
+
+#elif !defined(GL_CALL)
 # if defined(THREADS)
-extern struct _glapi_table * _glapi_DispatchTSD;
-#  define GL_CALL(name) \
-   (((__builtin_expect( _glapi_DispatchTSD != NULL, 1 )) \
-       ? _glapi_DispatchTSD : _glapi_get_dispatch())-> name)
+#  define GET_DISPATCH() \
+   ((__builtin_expect( _glapi_Dispatch != NULL, 1 )) \
+       ? _glapi_Dispatch : _glapi_get_dispatch())
 # else
-#  define GL_CALL(name) (*(_glapi_Dispatch-> name))
+#  define GET_DISPATCH() _glapi_Dispatch
 # endif /* defined(THREADS) */
 #endif  /* ndef GL_CALL */