mesa: put gl_thread_state inside gl_context to remove pointer indirection
[mesa.git] / src / mesa / main / glthread.c
index 76eb0cf7dac818193858b856e429a156acb46fb0..5d50e138150dd6fe87d2475f2a0247d046c7dbe8 100644 (file)
 
 #include "main/mtypes.h"
 #include "main/glthread.h"
-#include "main/marshal.h"
-#include "main/marshal_generated.h"
+#include "main/glthread_marshal.h"
+#include "main/hash.h"
+#include "util/u_atomic.h"
+#include "util/u_thread.h"
 
-#ifdef HAVE_PTHREAD
 
 static void
-glthread_allocate_batch(struct gl_context *ctx)
+glthread_unmarshal_batch(void *job, int thread_index)
 {
-   struct glthread_state *glthread = ctx->GLThread;
+   struct glthread_batch *batch = (struct glthread_batch*)job;
+   struct gl_context *ctx = batch->ctx;
+   int pos = 0;
+   int used = batch->used;
+   uint8_t *buffer = batch->buffer;
 
-   /* TODO: handle memory allocation failure. */
-   glthread->batch = calloc(1, sizeof(*glthread->batch));
-   if (!glthread->batch)
-      return;
-   glthread->batch->buffer = malloc(MARSHAL_MAX_CMD_SIZE);
-}
+   _glapi_set_dispatch(ctx->CurrentServerDispatch);
 
-static void
-glthread_unmarshal_batch(struct gl_context *ctx, struct glthread_batch *batch)
-{
-   free(batch->buffer);
-   free(batch);
+   while (pos < used) {
+      const struct marshal_cmd_base *cmd =
+         (const struct marshal_cmd_base *)&buffer[pos];
+
+      _mesa_unmarshal_dispatch[cmd->cmd_id](ctx, cmd);
+      pos += cmd->cmd_size;
+   }
+
+   assert(pos == used);
+   batch->used = 0;
 }
 
-static void *
-glthread_worker(void *data)
+static void
+glthread_thread_initialization(void *job, int thread_index)
 {
-   struct gl_context *ctx = data;
-   struct glthread_state *glthread = ctx->GLThread;
+   struct gl_context *ctx = (struct gl_context*)job;
 
-   ctx->Driver.SetBackgroundContext(ctx);
+   ctx->Driver.SetBackgroundContext(ctx, &ctx->GLThread.stats);
    _glapi_set_context(ctx);
-
-   pthread_mutex_lock(&glthread->mutex);
-
-   while (true) {
-      struct glthread_batch *batch;
-
-      /* Block (dropping the lock) until new work arrives for us. */
-      while (!glthread->batch_queue && !glthread->shutdown) {
-         pthread_cond_broadcast(&glthread->work_done);
-         pthread_cond_wait(&glthread->new_work, &glthread->mutex);
-      }
-
-      batch = glthread->batch_queue;
-
-      if (glthread->shutdown && !batch) {
-         pthread_cond_broadcast(&glthread->work_done);
-         pthread_mutex_unlock(&glthread->mutex);
-         return NULL;
-      }
-      glthread->batch_queue = batch->next;
-      if (glthread->batch_queue_tail == &batch->next)
-         glthread->batch_queue_tail = &glthread->batch_queue;
-
-      glthread->busy = true;
-      pthread_mutex_unlock(&glthread->mutex);
-
-      glthread_unmarshal_batch(ctx, batch);
-
-      pthread_mutex_lock(&glthread->mutex);
-      glthread->busy = false;
-   }
-
-   /* UNREACHED */
-   return NULL;
 }
 
 void
 _mesa_glthread_init(struct gl_context *ctx)
 {
-   struct glthread_state *glthread = calloc(1, sizeof(*glthread));
+   struct glthread_state *glthread = &ctx->GLThread;
 
-   if (!glthread)
+   assert(!glthread->enabled);
+
+   if (!util_queue_init(&glthread->queue, "gl", MARSHAL_MAX_BATCHES - 2,
+                        1, 0)) {
       return;
+   }
 
-   pthread_mutex_init(&glthread->mutex, NULL);
-   pthread_cond_init(&glthread->new_work, NULL);
-   pthread_cond_init(&glthread->work_done, NULL);
+   glthread->VAOs = _mesa_NewHashTable();
+   if (!glthread->VAOs) {
+      util_queue_destroy(&glthread->queue);
+      return;
+   }
+   glthread->CurrentVAO = &glthread->DefaultVAO;
+
+   ctx->MarshalExec = _mesa_create_marshal_table(ctx);
+   if (!ctx->MarshalExec) {
+      _mesa_DeleteHashTable(glthread->VAOs);
+      util_queue_destroy(&glthread->queue);
+      return;
+   }
 
-   glthread->batch_queue_tail = &glthread->batch_queue;
-   ctx->GLThread = glthread;
+   for (unsigned i = 0; i < MARSHAL_MAX_BATCHES; i++) {
+      glthread->batches[i].ctx = ctx;
+      util_queue_fence_init(&glthread->batches[i].fence);
+   }
 
-   glthread_allocate_batch(ctx);
+   glthread->enabled = true;
+   glthread->stats.queue = &glthread->queue;
+   ctx->CurrentClientDispatch = ctx->MarshalExec;
+
+   /* Execute the thread initialization function in the thread. */
+   struct util_queue_fence fence;
+   util_queue_fence_init(&fence);
+   util_queue_add_job(&glthread->queue, ctx, &fence,
+                      glthread_thread_initialization, NULL, 0);
+   util_queue_fence_wait(&fence);
+   util_queue_fence_destroy(&fence);
+}
 
-   pthread_create(&glthread->thread, NULL, glthread_worker, ctx);
+static void
+free_vao(GLuint key, void *data, void *userData)
+{
+   free(data);
 }
 
 void
 _mesa_glthread_destroy(struct gl_context *ctx)
 {
-   struct glthread_state *glthread = ctx->GLThread;
+   struct glthread_state *glthread = &ctx->GLThread;
 
-   if (!glthread)
+   if (!glthread->enabled)
       return;
 
-   _mesa_glthread_flush_batch(ctx);
+   _mesa_glthread_finish(ctx);
+   util_queue_destroy(&glthread->queue);
 
-   pthread_mutex_lock(&glthread->mutex);
-   glthread->shutdown = true;
-   pthread_cond_broadcast(&glthread->new_work);
-   pthread_mutex_unlock(&glthread->mutex);
+   for (unsigned i = 0; i < MARSHAL_MAX_BATCHES; i++)
+      util_queue_fence_destroy(&glthread->batches[i].fence);
 
-   /* Since this waits for the thread to exit, it means that all queued work
-    * will have been completed.
-    */
-   pthread_join(glthread->thread, NULL);
+   _mesa_HashDeleteAll(glthread->VAOs, free_vao, NULL);
+   _mesa_DeleteHashTable(glthread->VAOs);
 
-   pthread_cond_destroy(&glthread->new_work);
-   pthread_cond_destroy(&glthread->work_done);
-   pthread_mutex_destroy(&glthread->mutex);
+   ctx->GLThread.enabled = false;
 
-   /* Due to the join above, there should be one empty batch allocated at this
-    * point, and no batches queued.
+   _mesa_glthread_restore_dispatch(ctx, "destroy");
+}
+
+void
+_mesa_glthread_restore_dispatch(struct gl_context *ctx, const char *func)
+{
+   /* Remove ourselves from the dispatch table except if another ctx/thread
+    * already installed a new dispatch table.
+    *
+    * Typically glxMakeCurrent will bind a new context (install new table) then
+    * old context might be deleted.
     */
-   assert(!glthread->batch->used);
-   assert(!glthread->batch->next);
-   free(glthread->batch);
-   assert(!glthread->batch_queue);
+   if (_glapi_get_dispatch() == ctx->MarshalExec) {
+       ctx->CurrentClientDispatch = ctx->CurrentServerDispatch;
+       _glapi_set_dispatch(ctx->CurrentClientDispatch);
+#if 0
+       printf("glthread disabled: %s\n", func);
+#endif
+   }
+}
 
-   free(glthread);
-   ctx->GLThread = NULL;
+void
+_mesa_glthread_disable(struct gl_context *ctx, const char *func)
+{
+   _mesa_glthread_finish_before(ctx, func);
+   _mesa_glthread_restore_dispatch(ctx, func);
 }
 
 void
 _mesa_glthread_flush_batch(struct gl_context *ctx)
 {
-   struct glthread_state *glthread = ctx->GLThread;
-   struct glthread_batch *batch;
-
-   if (!glthread)
+   struct glthread_state *glthread = &ctx->GLThread;
+   if (!glthread->enabled)
       return;
 
-   batch = glthread->batch;
-   if (!batch->used)
+   struct glthread_batch *next = &glthread->batches[glthread->next];
+   if (!next->used)
       return;
 
-   /* Immediately reallocate a new batch, since the next marshalled call would
-    * just do it.
-    */
-   glthread_allocate_batch(ctx);
-
    /* Debug: execute the batch immediately from this thread.
     *
     * Note that glthread_unmarshal_batch() changes the dispatch table so we'll
     * need to restore it when it returns.
     */
    if (false) {
-      glthread_unmarshal_batch(ctx, batch);
+      glthread_unmarshal_batch(next, 0);
+      _glapi_set_dispatch(ctx->CurrentClientDispatch);
       return;
    }
 
-   pthread_mutex_lock(&glthread->mutex);
-   *glthread->batch_queue_tail = batch;
-   glthread->batch_queue_tail = &batch->next;
-   pthread_cond_broadcast(&glthread->new_work);
-   pthread_mutex_unlock(&glthread->mutex);
+   p_atomic_add(&glthread->stats.num_offloaded_items, next->used);
+
+   util_queue_add_job(&glthread->queue, next, &next->fence,
+                      glthread_unmarshal_batch, NULL, 0);
+   glthread->last = glthread->next;
+   glthread->next = (glthread->next + 1) % MARSHAL_MAX_BATCHES;
 }
 
 /**
@@ -202,9 +208,8 @@ _mesa_glthread_flush_batch(struct gl_context *ctx)
 void
 _mesa_glthread_finish(struct gl_context *ctx)
 {
-   struct glthread_state *glthread = ctx->GLThread;
-
-   if (!glthread)
+   struct glthread_state *glthread = &ctx->GLThread;
+   if (!glthread->enabled)
       return;
 
    /* If this is called from the worker thread, then we've hit a path that
@@ -212,17 +217,43 @@ _mesa_glthread_finish(struct gl_context *ctx)
     * dri interface entrypoints), in which case we don't need to actually
     * synchronize against ourself.
     */
-   if (pthread_self() == glthread->thread)
+   if (u_thread_is_self(glthread->queue.threads[0]))
       return;
 
-   _mesa_glthread_flush_batch(ctx);
+   struct glthread_batch *last = &glthread->batches[glthread->last];
+   struct glthread_batch *next = &glthread->batches[glthread->next];
+   bool synced = false;
 
-   pthread_mutex_lock(&glthread->mutex);
+   if (!util_queue_fence_is_signalled(&last->fence)) {
+      util_queue_fence_wait(&last->fence);
+      synced = true;
+   }
+
+   if (next->used) {
+      p_atomic_add(&glthread->stats.num_direct_items, next->used);
 
-   while (glthread->batch_queue || glthread->busy)
-      pthread_cond_wait(&glthread->work_done, &glthread->mutex);
+      /* Since glthread_unmarshal_batch changes the dispatch to direct,
+       * restore it after it's done.
+       */
+      struct _glapi_table *dispatch = _glapi_get_dispatch();
+      glthread_unmarshal_batch(next, 0);
+      _glapi_set_dispatch(dispatch);
 
-   pthread_mutex_unlock(&glthread->mutex);
+      /* It's not a sync because we don't enqueue partial batches, but
+       * it would be a sync if we did. So count it anyway.
+       */
+      synced = true;
+   }
+
+   if (synced)
+      p_atomic_inc(&glthread->stats.num_syncs);
 }
 
-#endif
+void
+_mesa_glthread_finish_before(struct gl_context *ctx, const char *func)
+{
+   _mesa_glthread_finish(ctx);
+
+   /* Uncomment this if you want to know where glthread syncs. */
+   /*printf("fallback to sync: %s\n", func);*/
+}