From d2a920ee6ee060bc2ae0e5e355d61ad1bf1a3092 Mon Sep 17 00:00:00 2001 From: Rob Clark Date: Fri, 5 Mar 2021 15:49:17 -0800 Subject: [PATCH] util: Extract thread-id helpers from u_current Signed-off-by: Rob Clark Reviewed-by: Eric Anholt Part-of: --- src/mapi/u_current.c | 45 +++++--------------------------------------- src/util/u_thread.h | 41 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 40 deletions(-) diff --git a/src/mapi/u_current.c b/src/mapi/u_current.c index 6904167f7d0..7a95e96f509 100644 --- a/src/mapi/u_current.c +++ b/src/mapi/u_current.c @@ -49,6 +49,7 @@ */ #include "c11/threads.h" +#include "util/u_thread.h" #include "u_current.h" #ifndef MAPI_MODE_UTIL @@ -150,42 +151,6 @@ 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 -} - static thread_id knownID; /** @@ -204,10 +169,10 @@ u_current_init(void) if (firstCall) { u_current_init_tsd(); - knownID = get_thread_id(); + knownID = util_get_thread_id(); firstCall = 0; } - else if (!thread_id_equal(knownID, get_thread_id())) { + else if (!util_thread_id_equal(knownID, util_get_thread_id())) { ThreadSafe = 1; u_current_set_table(NULL); u_current_set_context(NULL); @@ -257,7 +222,7 @@ u_current_get_context_internal(void) #else if (ThreadSafe) return tss_get(u_current_context_tsd); - else if (!thread_id_equal(knownID, get_thread_id())) + else if (!util_thread_id_equal(knownID, util_get_thread_id())) return NULL; else return u_current_context; @@ -298,7 +263,7 @@ u_current_get_table_internal(void) #else if (ThreadSafe) return (struct _glapi_table *) tss_get(u_current_table_tsd); - else if (!thread_id_equal(knownID, get_thread_id())) + else if (!util_thread_id_equal(knownID, util_get_thread_id())) return (struct _glapi_table *) table_noop_array; else return (struct _glapi_table *) u_current_table; diff --git a/src/util/u_thread.h b/src/util/u_thread.h index c5f7fb69fd3..8366bfb6d49 100644 --- a/src/util/u_thread.h +++ b/src/util/u_thread.h @@ -319,4 +319,45 @@ static inline void util_barrier_wait(util_barrier *barrier) #endif +/* + * Thread-id's. + * + * thrd_current() is not portable to windows (or at least not in a desirable + * way), so thread_id's provide an alternative mechanism + */ + +#ifdef _WIN32 +typedef DWORD thread_id; +#else +typedef thrd_t thread_id; +#endif + +static inline thread_id +util_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 +util_thread_id_equal(thread_id t1, thread_id t2) +{ +#ifdef _WIN32 + return t1 == t2; +#else + return thrd_equal(t1, t2); +#endif +} + #endif /* U_THREAD_H_ */