mapi: do not return thread-specific data for wrong thread

If the current thread asks for either the current context or the current
dispatch table for a thread that has not yet set any context current, we
currently risk returning the wrong data if there was only a single
thread that had called u_current_init() yet.

So let's first check if the only expected thread-id is the one getting
these, and return NULL and/or __glapi_noop_table instead if not.

Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
Reviewed-by: Louis-Francis Ratté-Boulianne <lfrb@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/7280>
diff --git a/src/mapi/u_current.c b/src/mapi/u_current.c
index 554c974..0e133e0 100644
--- a/src/mapi/u_current.c
+++ b/src/mapi/u_current.c
@@ -180,6 +180,7 @@
 #endif
 }
 
+static thread_id knownID;
 
 /**
  * We should call this periodically from a function such as glXMakeCurrent
@@ -188,7 +189,6 @@
 void
 u_current_init(void)
 {
-   static thread_id knownID;
    static int firstCall = 1;
 
    if (ThreadSafe)
@@ -249,7 +249,12 @@
 #if defined(USE_ELF_TLS)
    return u_current_context;
 #else
-   return ThreadSafe ? tss_get(u_current_context_tsd) : u_current_context;
+   if (ThreadSafe)
+      return tss_get(u_current_context_tsd);
+   else if (!thread_id_equal(knownID, get_thread_id()))
+      return NULL;
+   else
+      return u_current_context;
 #endif
 }
 
@@ -287,6 +292,8 @@
 #else
    if (ThreadSafe)
       return (struct _glapi_table *) tss_get(u_current_table_tsd);
+   else if (!thread_id_equal(knownID, get_thread_id()))
+      return (struct _glapi_table *) table_noop_array;
    else
       return (struct _glapi_table *) u_current_table;
 #endif