summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrian Paul <[email protected]>2015-03-04 19:17:57 -0700
committerBrian Paul <[email protected]>2015-03-05 06:59:43 -0700
commit458c7490c29ef2960a33a089f65490e044da5d27 (patch)
treeed6a48f2a292815dc2fa17e2925aa5149f23ab1c
parent6b5eb7bce6ac5351b0c96252a1c8738f51834e1d (diff)
mapi: rewrite u_current_init() function without u_thread_self()
Remove u_thread_self() since u_thread.h is going away soon. Create a simple thread ID abstraction which wraps WIN32 or c11 threads. This also gets rid of the questionable casting of thrd_t to an unsigned long. Reviewed-by: Matt Turner <[email protected]> Reviewed-by: José Fonseca <[email protected]>
-rw-r--r--src/mapi/u_current.c43
-rw-r--r--src/mapi/u_thread.h24
2 files changed, 40 insertions, 27 deletions
diff --git a/src/mapi/u_current.c b/src/mapi/u_current.c
index eb77cc7772f..c1a486bd0e1 100644
--- a/src/mapi/u_current.c
+++ b/src/mapi/u_current.c
@@ -146,6 +146,43 @@ u_current_init_tsd(void)
*/
static mtx_t ThreadCheckMutex = _MTX_INITIALIZER_NP;
+
+#ifdef _WIN32
+typedef DWORD thread_id;
+#else
+typedef thrd_t thread_id;
+#endif
+
+
+static inline thread_id
+get_thread_id(void)
+{
+ /*
+ * XXX: Callers of of this function assume it is a lightweight function.
+ * But unfortunately C11's thrd_current() gives no such guarantees. In
+ * fact, it's pretty hard to have a compliant implementation of
+ * thrd_current() on Windows with such characteristics. So for now, we
+ * side-step this mess and use Windows thread primitives directly here.
+ */
+#ifdef _WIN32
+ return GetCurrentThreadId();
+#else
+ return thrd_current();
+#endif
+}
+
+
+static inline int
+thread_id_equal(thread_id t1, thread_id t2)
+{
+#ifdef _WIN32
+ return t1 == t2;
+#else
+ return thrd_equal(t1, t2);
+#endif
+}
+
+
/**
* We should call this periodically from a function such as glXMakeCurrent
* in order to test if multiple threads are being used.
@@ -153,7 +190,7 @@ static mtx_t ThreadCheckMutex = _MTX_INITIALIZER_NP;
void
u_current_init(void)
{
- static unsigned long knownID;
+ static thread_id knownID;
static int firstCall = 1;
if (ThreadSafe)
@@ -163,10 +200,10 @@ u_current_init(void)
if (firstCall) {
u_current_init_tsd();
- knownID = u_thread_self();
+ knownID = get_thread_id();
firstCall = 0;
}
- else if (knownID != u_thread_self()) {
+ else if (!thread_id_equal(knownID, get_thread_id())) {
ThreadSafe = 1;
u_current_set_table(NULL);
u_current_set_context(NULL);
diff --git a/src/mapi/u_thread.h b/src/mapi/u_thread.h
index 4dd95150d3f..e57c69a2430 100644
--- a/src/mapi/u_thread.h
+++ b/src/mapi/u_thread.h
@@ -80,30 +80,6 @@ struct u_tsd {
};
-static inline unsigned long
-u_thread_self(void)
-{
- /*
- * XXX: Callers of u_thread_self assume it is a lightweight function,
- * returning a numeric value. But unfortunately C11's thrd_current() gives
- * no such guarantees. In fact, it's pretty hard to have a compliant
- * implementation of thrd_current() on Windows with such characteristics.
- * So for now, we side-step this mess and use Windows thread primitives
- * directly here.
- *
- * FIXME: On the other hand, u_thread_self() is a bad
- * abstraction. Even with pthreads, there is no guarantee that
- * pthread_self() will return numeric IDs -- we should be using
- * pthread_equal() instead of assuming we can compare thread ids...
- */
-#ifdef _WIN32
- return GetCurrentThreadId();
-#else
- return (unsigned long) (uintptr_t) thrd_current();
-#endif
-}
-
-
static inline void
u_tsd_init(struct u_tsd *tsd)
{