mesa: fix format conversion bug in get_tex_rgba_uncompressed()
[mesa.git] / src / mesa / main / syncobj.c
index cc541c83496a1cee8999febbf2c81e5f86d641c0..be758dd1241f03fddf2f199a245cc8d2b844d5bc 100644 (file)
 #include "imports.h"
 #include "context.h"
 #include "macros.h"
-#include "mfeatures.h"
 #include "get.h"
 #include "dispatch.h"
 #include "mtypes.h"
+#include "util/hash_table.h"
+#include "util/set.h"
 
 #include "syncobj.h"
 
 static struct gl_sync_object *
 _mesa_new_sync_object(struct gl_context *ctx, GLenum type)
 {
-   struct gl_sync_object *s = MALLOC_STRUCT(gl_sync_object);
+   struct gl_sync_object *s = CALLOC_STRUCT(gl_sync_object);
    (void) ctx;
    (void) type;
 
@@ -82,6 +83,7 @@ static void
 _mesa_delete_sync_object(struct gl_context *ctx, struct gl_sync_object *syncObj)
 {
    (void) ctx;
+   free(syncObj->Label);
    free(syncObj);
 }
 
@@ -139,20 +141,6 @@ _mesa_init_sync_object_functions(struct dd_function_table *driver)
    driver->ServerWaitSync = _mesa_wait_sync;
 }
 
-
-void
-_mesa_init_sync_dispatch(struct _glapi_table *disp)
-{
-   SET_IsSync(disp, _mesa_IsSync);
-   SET_DeleteSync(disp, _mesa_DeleteSync);
-   SET_FenceSync(disp, _mesa_FenceSync);
-   SET_ClientWaitSync(disp, _mesa_ClientWaitSync);
-   SET_WaitSync(disp, _mesa_WaitSync);
-   SET_GetInteger64v(disp, _mesa_GetInteger64v);
-   SET_GetSynciv(disp, _mesa_GetSynciv);
-}
-
-
 /**
  * Allocate/init the context state related to sync objects.
  */
@@ -173,36 +161,57 @@ _mesa_free_sync_data(struct gl_context *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
+ *  - type is GL_SYNC_FENCE
+ *  - 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)
+   struct gl_sync_object *syncObj = (struct gl_sync_object *) sync;
+   mtx_lock(&ctx->Shared->Mutex);
+   if (syncObj != NULL
+      && _mesa_set_search(ctx->Shared->SyncObjects, syncObj) != NULL
       && (syncObj->Type == GL_SYNC_FENCE)
-      && !syncObj->DeletePending;
+      && !syncObj->DeletePending) {
+     if (incRefCount) {
+       syncObj->RefCount++;
+     }
+   } else {
+     syncObj = NULL;
+   }
+   mtx_unlock(&ctx->Shared->Mutex);
+   return syncObj;
 }
 
 
 void
-_mesa_ref_sync_object(struct gl_context *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(struct gl_context *ctx, struct gl_sync_object *syncObj)
-{
-   _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
-   syncObj->RefCount--;
+   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);
+      mtx_unlock(&ctx->Shared->Mutex);
 
       ctx->Driver.DeleteSyncObject(ctx, syncObj);
    } else {
-      _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
+      mtx_unlock(&ctx->Shared->Mutex);
    }
 }
 
@@ -211,10 +220,9 @@ 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;
 }
 
 
@@ -222,8 +230,7 @@ void GLAPIENTRY
 _mesa_DeleteSync(GLsync sync)
 {
    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:
     *
@@ -235,16 +242,19 @@ _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 (!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);
 }
 
 
@@ -284,9 +294,9 @@ _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);
+      mtx_lock(&ctx->Shared->Mutex);
+      _mesa_set_add(ctx->Shared->SyncObjects, syncObj);
+      mtx_unlock(&ctx->Shared->Mutex);
 
       return (GLsync) syncObj;
    }
@@ -299,21 +309,20 @@ GLenum GLAPIENTRY
 _mesa_ClientWaitSync(GLsync sync, GLbitfield flags, GLuint64 timeout)
 {
    GET_CURRENT_CONTEXT(ctx);
-   struct gl_sync_object *const syncObj = (struct gl_sync_object *) sync;
+   struct gl_sync_object *syncObj;
    GLenum ret;
    ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_WAIT_FAILED);
 
-   if (!_mesa_validate_sync(syncObj)) {
-      _mesa_error(ctx, GL_INVALID_OPERATION, "glClientWaitSync");
-      return 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;
    }
 
-   _mesa_ref_sync_object(ctx, syncObj);
+   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;
+   }
 
    /* From the GL_ARB_sync spec:
     *
@@ -335,7 +344,7 @@ _mesa_ClientWaitSync(GLsync sync, GLbitfield flags, GLuint64 timeout)
       }
    }
 
-   _mesa_unref_sync_object(ctx, syncObj);
+   _mesa_unref_sync_object(ctx, syncObj, 1);
    return ret;
 }
 
@@ -344,13 +353,7 @@ 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);
-
-   if (!_mesa_validate_sync(syncObj)) {
-      _mesa_error(ctx, GL_INVALID_OPERATION, "glWaitSync");
-      return;
-   }
+   struct gl_sync_object *syncObj;
 
    if (flags != 0) {
       _mesa_error(ctx, GL_INVALID_VALUE, "glWaitSync(flags=0x%x)", flags);
@@ -363,7 +366,14 @@ _mesa_WaitSync(GLsync sync, GLbitfield flags, GLuint64 timeout)
       return;
    }
 
+   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);
+   _mesa_unref_sync_object(ctx, syncObj, 1);
 }
 
 
@@ -372,13 +382,13 @@ _mesa_GetSynciv(GLsync sync, GLenum pname, GLsizei bufSize, GLsizei *length,
                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;
    }
 
@@ -411,10 +421,11 @@ _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) {
+   if (size > 0 && bufSize > 0) {
       const GLsizei copy_count = MIN2(size, bufSize);
 
       memcpy(values, v, sizeof(GLint) * copy_count);
@@ -423,4 +434,6 @@ _mesa_GetSynciv(GLsync sync, GLenum pname, GLsizei bufSize, GLsizei *length,
    if (length != NULL) {
       *length = size;
    }
+
+   _mesa_unref_sync_object(ctx, syncObj, 1);
 }