compiler: Add a mutex for shader creation
authorCody Northrop <cody@lunarg.com>
Fri, 30 Jan 2015 17:42:23 +0000 (10:42 -0700)
committerCody Northrop <cody@lunarg.com>
Sat, 21 Feb 2015 00:05:01 +0000 (17:05 -0700)
Wrap the contents of shader_create_ir with a mutex to allow
multiple threads to issue xglCreateShader calls.

Most of this path is thread safe, we just need to rethink
how some initialization is happening, calls that used to live
on a single thread.

icd/intel/compiler/shader/compiler_interface.cpp

index 2e00b4a..212f884 100644 (file)
@@ -345,13 +345,19 @@ extern "C" {
 struct intel_ir *shader_create_ir(const struct intel_gpu *gpu,
                                   const void *code, size_t size)
 {
+    // Wrap this path in a mutex until we can clean up initialization
+    static mtx_t mutex = _MTX_INITIALIZER_NP;
+    mtx_lock(&mutex);
+
     struct icd_bil_header header;
     struct gl_context local_ctx;
     struct gl_context *ctx = &local_ctx;
 
     memcpy(&header, code, sizeof(header));
-    if (header.magic != ICD_BIL_MAGIC)
+    if (header.magic != ICD_BIL_MAGIC) {
+        mtx_unlock(&mutex);
         return NULL;
+    }
 
     _mesa_create_shader_compiler();
     initialize_mesa_context_to_defaults(ctx);
@@ -437,6 +443,7 @@ struct intel_ir *shader_create_ir(const struct intel_gpu *gpu,
 
     if (!shader->CompileStatus) {
         _mesa_destroy_shader_compiler();
+        mtx_unlock(&mutex);
         return NULL;
     }
 
@@ -455,11 +462,13 @@ struct intel_ir *shader_create_ir(const struct intel_gpu *gpu,
 
     if (!shader_program->LinkStatus) {
         _mesa_destroy_shader_compiler();
+        mtx_unlock(&mutex);
         return NULL;
     }
 
     _mesa_destroy_shader_compiler();
 
+    mtx_unlock(&mutex);
     return (struct intel_ir *) shader_program;
 }