Added few more stubs so that control reaches to DestroyDevice().
[mesa.git] / src / mesa / main / syncobj.c
index 64f923ff91f4ceb2e12e4a8989c81c9b8030e13e..23b49c981ae079023d4d98b4dc8c2f70b30e0353 100644 (file)
  * \author Ian Romanick <ian.d.romanick@intel.com>
  */
 
+#include <inttypes.h>
 #include "glheader.h"
-#include "imports.h"
+
 #include "context.h"
 #include "macros.h"
+#include "get.h"
+#include "mtypes.h"
+#include "util/hash_table.h"
+#include "util/set.h"
+#include "util/u_memory.h"
 
-#if FEATURE_ARB_sync
 #include "syncobj.h"
 
 static struct gl_sync_object *
-_mesa_new_sync_object(GLcontext *ctx, GLenum type)
+_mesa_new_sync_object(struct gl_context *ctx)
 {
-   struct gl_sync_object *s = MALLOC_STRUCT(gl_sync_object);
+   struct gl_sync_object *s = CALLOC_STRUCT(gl_sync_object);
    (void) ctx;
-   (void) type;
 
    return s;
 }
 
 
 static void
-_mesa_delete_sync_object(GLcontext *ctx, struct gl_sync_object *syncObj)
+_mesa_delete_sync_object(struct gl_context *ctx, struct gl_sync_object *syncObj)
 {
    (void) ctx;
-   _mesa_free(syncObj);
+   free(syncObj->Label);
+   free(syncObj);
 }
 
 
 static void
-_mesa_fence_sync(GLcontext *ctx, struct gl_sync_object *syncObj,
-                GLenum condition, GLbitfield flags)
+_mesa_fence_sync(struct gl_context *ctx, struct gl_sync_object *syncObj,
+                 GLenum condition, GLbitfield flags)
 {
    (void) ctx;
    (void) condition;
@@ -95,7 +100,7 @@ _mesa_fence_sync(GLcontext *ctx, struct gl_sync_object *syncObj,
 
 
 static void
-_mesa_check_sync(GLcontext *ctx, struct gl_sync_object *syncObj)
+_mesa_check_sync(struct gl_context *ctx, struct gl_sync_object *syncObj)
 {
    (void) ctx;
    (void) syncObj;
@@ -107,8 +112,8 @@ _mesa_check_sync(GLcontext *ctx, struct gl_sync_object *syncObj)
 
 
 static void
-_mesa_wait_sync(GLcontext *ctx, struct gl_sync_object *syncObj,
-               GLbitfield flags, GLuint64 timeout)
+_mesa_wait_sync(struct gl_context *ctx, struct gl_sync_object *syncObj,
+                GLbitfield flags, GLuint64 timeout)
 {
    (void) ctx;
    (void) syncObj;
@@ -135,12 +140,11 @@ _mesa_init_sync_object_functions(struct dd_function_table *driver)
    driver->ServerWaitSync = _mesa_wait_sync;
 }
 
-
 /**
  * Allocate/init the context state related to sync objects.
  */
 void
-_mesa_init_sync(GLcontext *ctx)
+_mesa_init_sync(struct gl_context *ctx)
 {
    (void) ctx;
 }
@@ -150,63 +154,79 @@ _mesa_init_sync(GLcontext *ctx)
  * Free the context state related to sync objects.
  */
 void
-_mesa_free_sync_data(GLcontext *ctx)
+_mesa_free_sync_data(struct gl_context *ctx)
 {
    (void) ctx;
 }
 
 
-static int
-_mesa_validate_sync(struct gl_sync_object *syncObj)
+/**
+ * Check if the given sync object is:
+ *  - non-null
+ *  - not in sync objects hash table
+ *  - not marked as deleted
+ *
+ * Returns the internal gl_sync_object pointer if the sync object is valid
+ * or NULL if it isn't.
+ *
+ * If "incRefCount" is true, the reference count is incremented, which is
+ * normally what you want; otherwise, a glDeleteSync from another thread
+ * could delete the sync object while you are still working on it.
+ */
+struct gl_sync_object *
+_mesa_get_and_ref_sync(struct gl_context *ctx, GLsync sync, bool incRefCount)
 {
-   return (syncObj != NULL)
-      && (syncObj->Type == GL_SYNC_FENCE)
-      && !syncObj->DeletePending;
+   struct gl_sync_object *syncObj = (struct gl_sync_object *) sync;
+   simple_mtx_lock(&ctx->Shared->Mutex);
+   if (syncObj != NULL
+      && _mesa_set_search(ctx->Shared->SyncObjects, syncObj) != NULL
+      && !syncObj->DeletePending) {
+     if (incRefCount) {
+       syncObj->RefCount++;
+     }
+   } else {
+     syncObj = NULL;
+   }
+   simple_mtx_unlock(&ctx->Shared->Mutex);
+   return syncObj;
 }
 
 
 void
-_mesa_ref_sync_object(GLcontext *ctx, struct gl_sync_object *syncObj)
+_mesa_unref_sync_object(struct gl_context *ctx, struct gl_sync_object *syncObj,
+                        int amount)
 {
-   _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
-   syncObj->RefCount++;
-   _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
-}
+   struct set_entry *entry;
 
-
-void
-_mesa_unref_sync_object(GLcontext *ctx, struct gl_sync_object *syncObj)
-{
-   _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
-   syncObj->RefCount--;
+   simple_mtx_lock(&ctx->Shared->Mutex);
+   syncObj->RefCount -= amount;
    if (syncObj->RefCount == 0) {
-      remove_from_list(& syncObj->link);
-      _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
+      entry = _mesa_set_search(ctx->Shared->SyncObjects, syncObj);
+      assert (entry != NULL);
+      _mesa_set_remove(ctx->Shared->SyncObjects, entry);
+      simple_mtx_unlock(&ctx->Shared->Mutex);
 
       ctx->Driver.DeleteSyncObject(ctx, syncObj);
    } else {
-      _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
+      simple_mtx_unlock(&ctx->Shared->Mutex);
    }
 }
 
 
-GLboolean
+GLboolean GLAPIENTRY
 _mesa_IsSync(GLsync sync)
 {
    GET_CURRENT_CONTEXT(ctx);
-   struct gl_sync_object *const syncObj = (struct gl_sync_object *) sync;
    ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
 
-   return _mesa_validate_sync(syncObj) ? GL_TRUE : GL_FALSE;
+   return _mesa_get_and_ref_sync(ctx, sync, false) ? GL_TRUE : GL_FALSE;
 }
 
 
-void
-_mesa_DeleteSync(GLsync sync)
+static ALWAYS_INLINE void
+delete_sync(struct gl_context *ctx, GLsync sync, bool no_error)
 {
-   GET_CURRENT_CONTEXT(ctx);
-   struct gl_sync_object *const syncObj = (struct gl_sync_object *) sync;
-   ASSERT_OUTSIDE_BEGIN_END(ctx);
+   struct gl_sync_object *syncObj;
 
    /* From the GL_ARB_sync spec:
     *
@@ -218,41 +238,46 @@ _mesa_DeleteSync(GLsync sync)
       return;
    }
 
-   if (!_mesa_validate_sync(syncObj)) {
-      _mesa_error(ctx, GL_INVALID_OPERATION, "glDeleteSync");
+   syncObj = _mesa_get_and_ref_sync(ctx, sync, true);
+   if (!no_error && !syncObj) {
+      _mesa_error(ctx, GL_INVALID_VALUE,
+                  "glDeleteSync (not a valid sync object)");
       return;
    }
 
    /* If there are no client-waits or server-waits pending on this sync, delete
-    * the underlying object.
+    * the underlying object. Note that we double-unref the object, as
+    * _mesa_get_and_ref_sync above took an extra refcount to make sure the
+    * pointer is valid for us to manipulate.
     */
    syncObj->DeletePending = GL_TRUE;
-   _mesa_unref_sync_object(ctx, syncObj);
+   _mesa_unref_sync_object(ctx, syncObj, 2);
 }
 
 
-GLsync
-_mesa_FenceSync(GLenum condition, GLbitfield flags)
+void GLAPIENTRY
+_mesa_DeleteSync_no_error(GLsync sync)
 {
    GET_CURRENT_CONTEXT(ctx);
-   struct gl_sync_object *syncObj;
-   ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
+   delete_sync(ctx, sync, true);
+}
 
-   if (condition != GL_SYNC_GPU_COMMANDS_COMPLETE) {
-      _mesa_error(ctx, GL_INVALID_ENUM, "glFenceSync(condition=0x%x)",
-                 condition);
-      return 0;
-   }
 
-   if (flags != 0) {
-      _mesa_error(ctx, GL_INVALID_VALUE, "glFenceSync(flags=0x%x)",
-                 condition);
-      return 0;
-   }
+void GLAPIENTRY
+_mesa_DeleteSync(GLsync sync)
+{
+   GET_CURRENT_CONTEXT(ctx);
+   delete_sync(ctx, sync, false);
+}
+
+
+static GLsync
+fence_sync(struct gl_context *ctx, GLenum condition, GLbitfield flags)
+{
+   struct gl_sync_object *syncObj;
 
-   syncObj = ctx->Driver.NewSyncObject(ctx, GL_SYNC_FENCE);
+   syncObj = ctx->Driver.NewSyncObject(ctx);
    if (syncObj != NULL) {
-      syncObj->Type = GL_SYNC_FENCE;
       /* The name is not currently used, and it is never visible to
        * applications.  If sync support is extended to provide support for
        * NV_fence, this field will be used.  We'll also need to add an object
@@ -267,36 +292,51 @@ _mesa_FenceSync(GLenum condition, GLbitfield flags)
 
       ctx->Driver.FenceSync(ctx, syncObj, condition, flags);
 
-      _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
-      insert_at_tail(& ctx->Shared->SyncObjects, & syncObj->link);
-      _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
+      simple_mtx_lock(&ctx->Shared->Mutex);
+      _mesa_set_add(ctx->Shared->SyncObjects, syncObj);
+      simple_mtx_unlock(&ctx->Shared->Mutex);
 
-      return (GLsync) syncObj;
+      return (GLsync)syncObj;
    }
 
    return NULL;
 }
 
 
-GLenum
-_mesa_ClientWaitSync(GLsync sync, GLbitfield flags, GLuint64 timeout)
+GLsync GLAPIENTRY
+_mesa_FenceSync_no_error(GLenum condition, GLbitfield flags)
 {
    GET_CURRENT_CONTEXT(ctx);
-   struct gl_sync_object *const syncObj = (struct gl_sync_object *) sync;
-   GLenum ret;
-   ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_WAIT_FAILED);
+   return fence_sync(ctx, condition, flags);
+}
 
-   if (!_mesa_validate_sync(syncObj)) {
-      _mesa_error(ctx, GL_INVALID_OPERATION, "glClientWaitSync");
-      return GL_WAIT_FAILED;
+
+GLsync GLAPIENTRY
+_mesa_FenceSync(GLenum condition, GLbitfield flags)
+{
+   GET_CURRENT_CONTEXT(ctx);
+   ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
+
+   if (condition != GL_SYNC_GPU_COMMANDS_COMPLETE) {
+      _mesa_error(ctx, GL_INVALID_ENUM, "glFenceSync(condition=0x%x)",
+                  condition);
+      return 0;
    }
 
-   if ((flags & ~GL_SYNC_FLUSH_COMMANDS_BIT) != 0) {
-      _mesa_error(ctx, GL_INVALID_ENUM, "glClientWaitSync(flags=0x%x)", flags);
-      return GL_WAIT_FAILED;
+   if (flags != 0) {
+      _mesa_error(ctx, GL_INVALID_VALUE, "glFenceSync(flags=0x%x)", condition);
+      return 0;
    }
 
-   _mesa_ref_sync_object(ctx, syncObj);
+   return fence_sync(ctx, condition, flags);
+}
+
+
+static GLenum
+client_wait_sync(struct gl_context *ctx, struct gl_sync_object *syncObj,
+                 GLbitfield flags, GLuint64 timeout)
+{
+   GLenum ret;
 
    /* From the GL_ARB_sync spec:
     *
@@ -309,63 +349,121 @@ _mesa_ClientWaitSync(GLsync sync, GLbitfield flags, GLuint64 timeout)
    if (syncObj->StatusFlag) {
       ret = GL_ALREADY_SIGNALED;
    } else {
-      ctx->Driver.ClientWaitSync(ctx, syncObj, flags, timeout);
-
-      ret = syncObj->StatusFlag ? GL_CONDITION_SATISFIED : GL_TIMEOUT_EXPIRED;
+      if (timeout == 0) {
+         ret = GL_TIMEOUT_EXPIRED;
+      } else {
+         ctx->Driver.ClientWaitSync(ctx, syncObj, flags, timeout);
+
+         ret = syncObj->StatusFlag
+            ? GL_CONDITION_SATISFIED : GL_TIMEOUT_EXPIRED;
+      }
    }
 
-   _mesa_unref_sync_object(ctx, syncObj);
+   _mesa_unref_sync_object(ctx, syncObj, 1);
    return ret;
 }
 
 
-void
+GLenum GLAPIENTRY
+_mesa_ClientWaitSync_no_error(GLsync sync, GLbitfield flags, GLuint64 timeout)
+{
+   GET_CURRENT_CONTEXT(ctx);
+
+   struct gl_sync_object *syncObj = _mesa_get_and_ref_sync(ctx, sync, true);
+   return client_wait_sync(ctx, syncObj, flags, timeout);
+}
+
+
+GLenum GLAPIENTRY
+_mesa_ClientWaitSync(GLsync sync, GLbitfield flags, GLuint64 timeout)
+{
+   GET_CURRENT_CONTEXT(ctx);
+   struct gl_sync_object *syncObj;
+
+   ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_WAIT_FAILED);
+
+   if ((flags & ~GL_SYNC_FLUSH_COMMANDS_BIT) != 0) {
+      _mesa_error(ctx, GL_INVALID_VALUE, "glClientWaitSync(flags=0x%x)", flags);
+      return GL_WAIT_FAILED;
+   }
+
+   syncObj = _mesa_get_and_ref_sync(ctx, sync, true);
+   if (!syncObj) {
+      _mesa_error(ctx, GL_INVALID_VALUE,
+                  "glClientWaitSync (not a valid sync object)");
+      return GL_WAIT_FAILED;
+   }
+
+   return client_wait_sync(ctx, syncObj, flags, timeout);
+}
+
+
+static void
+wait_sync(struct gl_context *ctx, struct gl_sync_object *syncObj,
+          GLbitfield flags, GLuint64 timeout)
+{
+   ctx->Driver.ServerWaitSync(ctx, syncObj, flags, timeout);
+   _mesa_unref_sync_object(ctx, syncObj, 1);
+}
+
+
+void GLAPIENTRY
+_mesa_WaitSync_no_error(GLsync sync, GLbitfield flags, GLuint64 timeout)
+{
+   GET_CURRENT_CONTEXT(ctx);
+
+   struct gl_sync_object *syncObj = _mesa_get_and_ref_sync(ctx, sync, true);
+   wait_sync(ctx, syncObj, flags, timeout);
+}
+
+
+void GLAPIENTRY
 _mesa_WaitSync(GLsync sync, GLbitfield flags, GLuint64 timeout)
 {
    GET_CURRENT_CONTEXT(ctx);
-   struct gl_sync_object *const syncObj = (struct gl_sync_object *) sync;
-   ASSERT_OUTSIDE_BEGIN_END(ctx);
+   struct gl_sync_object *syncObj;
 
-   if (!_mesa_validate_sync(syncObj)) {
-      _mesa_error(ctx, GL_INVALID_OPERATION, "glWaitSync");
+   if (flags != 0) {
+      _mesa_error(ctx, GL_INVALID_VALUE, "glWaitSync(flags=0x%x)", flags);
       return;
    }
 
-   if (flags != 0) {
-      _mesa_error(ctx, GL_INVALID_ENUM, "glWaitSync(flags=0x%x)", flags);
+   if (timeout != GL_TIMEOUT_IGNORED) {
+      _mesa_error(ctx, GL_INVALID_VALUE, "glWaitSync(timeout=0x%" PRIx64 ")",
+                  (uint64_t) timeout);
       return;
    }
 
-   /* From the GL_ARB_sync spec:
-    *
-    *     If the value of <timeout> is zero, then WaitSync does nothing.
-    */
-   if (timeout == 0) {
+   syncObj = _mesa_get_and_ref_sync(ctx, sync, true);
+   if (!syncObj) {
+      _mesa_error(ctx, GL_INVALID_VALUE,
+                  "glWaitSync (not a valid sync object)");
       return;
    }
 
-   ctx->Driver.ServerWaitSync(ctx, syncObj, flags, timeout);
+   wait_sync(ctx, syncObj, flags, timeout);
 }
 
 
-void
+void GLAPIENTRY
 _mesa_GetSynciv(GLsync sync, GLenum pname, GLsizei bufSize, GLsizei *length,
-               GLint *values)
+                GLint *values)
 {
    GET_CURRENT_CONTEXT(ctx);
-   struct gl_sync_object *const syncObj = (struct gl_sync_object *) sync;
+   struct gl_sync_object *syncObj;
    GLsizei size = 0;
    GLint v[1];
-   ASSERT_OUTSIDE_BEGIN_END(ctx);
 
-   if (!_mesa_validate_sync(syncObj)) {
-      _mesa_error(ctx, GL_INVALID_OPERATION, "glGetSynciv");
+   syncObj = _mesa_get_and_ref_sync(ctx, sync, true);
+   if (!syncObj) {
+      _mesa_error(ctx, GL_INVALID_VALUE,
+                  "glGetSynciv (not a valid sync object)");
       return;
    }
 
    switch (pname) {
    case GL_OBJECT_TYPE:
-      v[0] = syncObj->Type;
+      v[0] = GL_SYNC_FENCE;
       size = 1;
       break;
 
@@ -392,18 +490,27 @@ _mesa_GetSynciv(GLsync sync, GLenum pname, GLsizei bufSize, GLsizei *length,
 
    default:
       _mesa_error(ctx, GL_INVALID_ENUM, "glGetSynciv(pname=0x%x)\n", pname);
+      _mesa_unref_sync_object(ctx, syncObj, 1);
       return;
    }
 
-   if (size > 0) {
+   /* Section 4.1.3 (Sync Object Queries) of the OpenGL ES 3.10 spec says:
+    *
+    *    "An INVALID_VALUE error is generated if bufSize is negative."
+    */
+   if (bufSize < 0) {
+      _mesa_error(ctx, GL_INVALID_VALUE, "glGetSynciv(pname=0x%x)\n", pname);
+   }
+
+   if (size > 0 && bufSize > 0) {
       const GLsizei copy_count = MIN2(size, bufSize);
 
-      _mesa_memcpy(values, v, sizeof(GLint) * copy_count);
+      memcpy(values, v, sizeof(GLint) * copy_count);
    }
 
    if (length != NULL) {
       *length = size;
    }
-}
 
-#endif /* FEATURE_ARB_sync */
+   _mesa_unref_sync_object(ctx, syncObj, 1);
+}