* \file syncobj.c
* Sync object management.
*
+ * Unlike textures and other objects that are shared between contexts, sync
+ * objects are not bound to the context. As a result, the reference counting
+ * and delete behavior of sync objects is slightly different. References to
+ * sync objects are added:
+ *
+ * - By \c glFencSynce. This sets the initial reference count to 1.
+ * - At the start of \c glClientWaitSync. The reference is held for the
+ * duration of the wait call.
+ *
+ * References are removed:
+ *
+ * - By \c glDeleteSync.
+ * - At the end of \c glClientWaitSync.
+ *
+ * Additionally, drivers may call \c _mesa_ref_sync_object and
+ * \c _mesa_unref_sync_object as needed to implement \c ServerWaitSync.
+ *
+ * As with shader objects, sync object names become invalid as soon as
+ * \c glDeleteSync is called. For this reason \c glDeleteSync sets the
+ * \c DeletePending flag. All functions validate object handles by testing
+ * this flag.
+ *
+ * \note
+ * Only \c GL_ARB_sync objects are shared between contexts. If support is ever
+ * added for either \c GL_NV_fence or \c GL_APPLE_fence different semantics
+ * will need to be implemented.
+ *
* \author Ian Romanick <ian.d.romanick@intel.com>
*/
#include "glheader.h"
-#include "hash.h"
#include "imports.h"
#include "context.h"
+#include "macros.h"
+#include "mfeatures.h"
+#include "get.h"
+#include "dispatch.h"
+#include "mtypes.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, GLenum type)
{
struct gl_sync_object *s = MALLOC_STRUCT(gl_sync_object);
(void) ctx;
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);
}
static void
-_mesa_fence_sync(GLcontext *ctx, struct gl_sync_object *syncObj,
+_mesa_fence_sync(struct gl_context *ctx, struct gl_sync_object *syncObj,
GLenum condition, GLbitfield flags)
{
(void) ctx;
(void) condition;
(void) flags;
- syncObj->Status = 1;
+ syncObj->StatusFlag = 1;
}
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;
static void
-_mesa_wait_sync(GLcontext *ctx, struct gl_sync_object *syncObj,
+_mesa_wait_sync(struct gl_context *ctx, struct gl_sync_object *syncObj,
GLbitfield flags, GLuint64 timeout)
{
(void) ctx;
(void) flags;
(void) timeout;
-
/* No-op for software rendering. Hardware drivers will need to wait until
* the state of the sync object changes or the timeout expires.
*/
}
+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.
*/
void
-_mesa_init_sync(GLcontext *ctx)
+_mesa_init_sync(struct gl_context *ctx)
{
(void) 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;
}
-GLboolean
-_mesa_IsSync(GLsync sync)
+static int
+_mesa_validate_sync(struct gl_sync_object *syncObj)
{
- 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 (syncObj != NULL)
+ && (syncObj->Type == GL_SYNC_FENCE)
+ && !syncObj->DeletePending;
+}
- return ((syncObj != NULL) && (syncObj->Type == GL_SYNC_FENCE))
- ? GL_TRUE : GL_FALSE;
+void
+_mesa_ref_sync_object(struct gl_context *ctx, struct gl_sync_object *syncObj)
+{
+ _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
+ syncObj->RefCount++;
+ _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
}
-static void
-_mesa_unref_sync_object(GLcontext *ctx, struct gl_sync_object *syncObj)
+void
+_mesa_unref_sync_object(struct gl_context *ctx, struct gl_sync_object *syncObj)
{
+ _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
syncObj->RefCount--;
if (syncObj->RefCount == 0) {
- (*ctx->Driver.DeleteSyncObject)(ctx, syncObj);
+ remove_from_list(& syncObj->link);
+ _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
+
+ ctx->Driver.DeleteSyncObject(ctx, syncObj);
} else {
- syncObj->DeletePending = 1;
+ _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
}
}
-void
+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;
+}
+
+
+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);
-
/* From the GL_ARB_sync spec:
*
* DeleteSync will silently ignore a <sync> value of zero. An
return;
}
- if (syncObj->Type != GL_SYNC_FENCE) {
+ if (!_mesa_validate_sync(syncObj)) {
_mesa_error(ctx, GL_INVALID_OPERATION, "glDeleteSync");
return;
}
-
/* If there are no client-waits or server-waits pending on this sync, delete
* the underlying object.
*/
+ syncObj->DeletePending = GL_TRUE;
_mesa_unref_sync_object(ctx, syncObj);
}
-
-GLsync
+GLsync GLAPIENTRY
_mesa_FenceSync(GLenum condition, GLbitfield flags)
{
GET_CURRENT_CONTEXT(ctx);
struct gl_sync_object *syncObj;
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;
}
- syncObj = (*ctx->Driver.NewSyncObject)(ctx, GL_SYNC_FENCE);
+ syncObj = ctx->Driver.NewSyncObject(ctx, GL_SYNC_FENCE);
if (syncObj != NULL) {
syncObj->Type = GL_SYNC_FENCE;
/* The name is not currently used, and it is never visible to
syncObj->DeletePending = GL_FALSE;
syncObj->SyncCondition = condition;
syncObj->Flags = flags;
- syncObj->Status = 0;
+ syncObj->StatusFlag = 0;
- (*ctx->Driver.FenceSync)(ctx, syncObj, condition, 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);
return (GLsync) syncObj;
}
}
-GLenum
+GLenum GLAPIENTRY
_mesa_ClientWaitSync(GLsync sync, GLbitfield flags, GLuint64 timeout)
{
GET_CURRENT_CONTEXT(ctx);
GLenum ret;
ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_WAIT_FAILED);
-
- if ((syncObj == NULL) || (syncObj->Type != GL_SYNC_FENCE)) {
+ if (!_mesa_validate_sync(syncObj)) {
_mesa_error(ctx, GL_INVALID_OPERATION, "glClientWaitSync");
return GL_WAIT_FAILED;
}
return GL_WAIT_FAILED;
}
+ _mesa_ref_sync_object(ctx, syncObj);
/* From the GL_ARB_sync spec:
*
* ClientWaitSync was called. ALREADY_SIGNALED will always be returned
* if <sync> was signaled, even if the value of <timeout> is zero.
*/
- (*ctx->Driver.CheckSync)(ctx, syncObj);
-
- if (syncObj->Status) {
- return GL_ALREADY_SIGNALED;
- }
-
-
- (*ctx->Driver.ClientWaitSync)(ctx, syncObj, flags, timeout);
-
- ret = syncObj->Status ? GL_CONDITION_SATISFIED : GL_TIMEOUT_EXPIRED;
+ ctx->Driver.CheckSync(ctx, syncObj);
+ if (syncObj->StatusFlag) {
+ ret = GL_ALREADY_SIGNALED;
+ } else {
+ ctx->Driver.ClientWaitSync(ctx, syncObj, flags, timeout);
- if (syncObj->DeletePending && syncObj->Status) {
- _mesa_unref_sync_object(ctx, syncObj);
+ ret = syncObj->StatusFlag ? GL_CONDITION_SATISFIED : GL_TIMEOUT_EXPIRED;
}
+ _mesa_unref_sync_object(ctx, syncObj);
return ret;
}
-void
+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 ((syncObj == NULL) || (syncObj->Type != GL_SYNC_FENCE)) {
+ if (!_mesa_validate_sync(syncObj)) {
_mesa_error(ctx, GL_INVALID_OPERATION, "glWaitSync");
return;
}
return;
}
- (*ctx->Driver.ServerWaitSync)(ctx, syncObj, flags, timeout);
+ ctx->Driver.ServerWaitSync(ctx, syncObj, flags, timeout);
}
-void
+void GLAPIENTRY
_mesa_GetSynciv(GLsync sync, GLenum pname, GLsizei bufSize, GLsizei *length,
GLint *values)
{
GLint v[1];
ASSERT_OUTSIDE_BEGIN_END(ctx);
-
- if ((syncObj == NULL) || (syncObj->Type != GL_SYNC_FENCE)) {
+ if (!_mesa_validate_sync(syncObj)) {
_mesa_error(ctx, GL_INVALID_OPERATION, "glGetSynciv");
return;
}
-
switch (pname) {
case GL_OBJECT_TYPE:
v[0] = syncObj->Type;
* this call won't block. It just updates state in the common object
* data from the current driver state.
*/
- (*ctx->Driver.CheckSync)(ctx, syncObj);
+ ctx->Driver.CheckSync(ctx, syncObj);
- v[0] = (syncObj->Status) ? GL_SIGNALED : GL_UNSIGNALED;
+ v[0] = (syncObj->StatusFlag) ? GL_SIGNALED : GL_UNSIGNALED;
size = 1;
break;
return;
}
-
if (size > 0) {
- const GLsizei copy_count = (size > bufSize) ? bufSize : size;
+ 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;
}