syncobj.c revision 4c99e3ae78ed3524d188f00b558f803a943aaa00
1/* 2 * Copyright © 2009 Intel Corporation 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 21 * DEALINGS IN THE SOFTWARE. 22 */ 23 24/** 25 * \file syncobj.c 26 * Sync object management. 27 * 28 * Unlike textures and other objects that are shared between contexts, sync 29 * objects are not bound to the context. As a result, the reference counting 30 * and delete behavior of sync objects is slightly different. References to 31 * sync objects are added: 32 * 33 * - By \c glFencSynce. This sets the initial reference count to 1. 34 * - At the start of \c glClientWaitSync. The reference is held for the 35 * duration of the wait call. 36 * 37 * References are removed: 38 * 39 * - By \c glDeleteSync. 40 * - At the end of \c glClientWaitSync. 41 * 42 * Additionally, drivers may call \c _mesa_ref_sync_object and 43 * \c _mesa_unref_sync_object as needed to implement \c ServerWaitSync. 44 * 45 * As with shader objects, sync object names become invalid as soon as 46 * \c glDeleteSync is called. For this reason \c glDeleteSync sets the 47 * \c DeletePending flag. All functions validate object handles by testing 48 * this flag. 49 * 50 * \note 51 * Only \c GL_ARB_sync objects are shared between contexts. If support is ever 52 * added for either \c GL_NV_fence or \c GL_APPLE_fence different semantics 53 * will need to be implemented. 54 * 55 * \author Ian Romanick <ian.d.romanick@intel.com> 56 */ 57 58#include <inttypes.h> 59#include "glheader.h" 60#include "imports.h" 61#include "context.h" 62#include "macros.h" 63#include "get.h" 64#include "dispatch.h" 65#include "mtypes.h" 66#include "util/hash_table.h" 67#include "util/set.h" 68 69#include "syncobj.h" 70 71static struct gl_sync_object * 72_mesa_new_sync_object(struct gl_context *ctx, GLenum type) 73{ 74 struct gl_sync_object *s = CALLOC_STRUCT(gl_sync_object); 75 (void) ctx; 76 (void) type; 77 78 return s; 79} 80 81 82static void 83_mesa_delete_sync_object(struct gl_context *ctx, struct gl_sync_object *syncObj) 84{ 85 (void) ctx; 86 free(syncObj->Label); 87 free(syncObj); 88} 89 90 91static void 92_mesa_fence_sync(struct gl_context *ctx, struct gl_sync_object *syncObj, 93 GLenum condition, GLbitfield flags) 94{ 95 (void) ctx; 96 (void) condition; 97 (void) flags; 98 99 syncObj->StatusFlag = 1; 100} 101 102 103static void 104_mesa_check_sync(struct gl_context *ctx, struct gl_sync_object *syncObj) 105{ 106 (void) ctx; 107 (void) syncObj; 108 109 /* No-op for software rendering. Hardware drivers will need to determine 110 * whether the state of the sync object has changed. 111 */ 112} 113 114 115static void 116_mesa_wait_sync(struct gl_context *ctx, struct gl_sync_object *syncObj, 117 GLbitfield flags, GLuint64 timeout) 118{ 119 (void) ctx; 120 (void) syncObj; 121 (void) flags; 122 (void) timeout; 123 124 /* No-op for software rendering. Hardware drivers will need to wait until 125 * the state of the sync object changes or the timeout expires. 126 */ 127} 128 129 130void 131_mesa_init_sync_object_functions(struct dd_function_table *driver) 132{ 133 driver->NewSyncObject = _mesa_new_sync_object; 134 driver->FenceSync = _mesa_fence_sync; 135 driver->DeleteSyncObject = _mesa_delete_sync_object; 136 driver->CheckSync = _mesa_check_sync; 137 138 /* Use the same no-op wait function for both. 139 */ 140 driver->ClientWaitSync = _mesa_wait_sync; 141 driver->ServerWaitSync = _mesa_wait_sync; 142} 143 144/** 145 * Allocate/init the context state related to sync objects. 146 */ 147void 148_mesa_init_sync(struct gl_context *ctx) 149{ 150 (void) ctx; 151} 152 153 154/** 155 * Free the context state related to sync objects. 156 */ 157void 158_mesa_free_sync_data(struct gl_context *ctx) 159{ 160 (void) ctx; 161} 162 163 164/** 165 * Check if the given sync object is: 166 * - non-null 167 * - not in sync objects hash table 168 * - type is GL_SYNC_FENCE 169 * - not marked as deleted 170 */ 171bool 172_mesa_validate_sync(struct gl_context *ctx, 173 const struct gl_sync_object *syncObj) 174{ 175 return (syncObj != NULL) 176 && _mesa_set_search(ctx->Shared->SyncObjects, 177 _mesa_hash_pointer(syncObj), 178 syncObj) != NULL 179 && (syncObj->Type == GL_SYNC_FENCE) 180 && !syncObj->DeletePending; 181} 182 183 184void 185_mesa_ref_sync_object(struct gl_context *ctx, struct gl_sync_object *syncObj) 186{ 187 mtx_lock(&ctx->Shared->Mutex); 188 syncObj->RefCount++; 189 mtx_unlock(&ctx->Shared->Mutex); 190} 191 192 193void 194_mesa_unref_sync_object(struct gl_context *ctx, struct gl_sync_object *syncObj) 195{ 196 struct set_entry *entry; 197 198 mtx_lock(&ctx->Shared->Mutex); 199 syncObj->RefCount--; 200 if (syncObj->RefCount == 0) { 201 entry = _mesa_set_search(ctx->Shared->SyncObjects, 202 _mesa_hash_pointer(syncObj), 203 syncObj); 204 assert (entry != NULL); 205 _mesa_set_remove(ctx->Shared->SyncObjects, entry); 206 mtx_unlock(&ctx->Shared->Mutex); 207 208 ctx->Driver.DeleteSyncObject(ctx, syncObj); 209 } else { 210 mtx_unlock(&ctx->Shared->Mutex); 211 } 212} 213 214 215GLboolean GLAPIENTRY 216_mesa_IsSync(GLsync sync) 217{ 218 GET_CURRENT_CONTEXT(ctx); 219 struct gl_sync_object *const syncObj = (struct gl_sync_object *) sync; 220 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE); 221 222 return _mesa_validate_sync(ctx, syncObj) ? GL_TRUE : GL_FALSE; 223} 224 225 226void GLAPIENTRY 227_mesa_DeleteSync(GLsync sync) 228{ 229 GET_CURRENT_CONTEXT(ctx); 230 struct gl_sync_object *const syncObj = (struct gl_sync_object *) sync; 231 232 /* From the GL_ARB_sync spec: 233 * 234 * DeleteSync will silently ignore a <sync> value of zero. An 235 * INVALID_VALUE error is generated if <sync> is neither zero nor the 236 * name of a sync object. 237 */ 238 if (sync == 0) { 239 return; 240 } 241 242 if (!_mesa_validate_sync(ctx, syncObj)) { 243 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteSync (not a valid sync object)"); 244 return; 245 } 246 247 /* If there are no client-waits or server-waits pending on this sync, delete 248 * the underlying object. 249 */ 250 syncObj->DeletePending = GL_TRUE; 251 _mesa_unref_sync_object(ctx, syncObj); 252} 253 254 255GLsync GLAPIENTRY 256_mesa_FenceSync(GLenum condition, GLbitfield flags) 257{ 258 GET_CURRENT_CONTEXT(ctx); 259 struct gl_sync_object *syncObj; 260 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0); 261 262 if (condition != GL_SYNC_GPU_COMMANDS_COMPLETE) { 263 _mesa_error(ctx, GL_INVALID_ENUM, "glFenceSync(condition=0x%x)", 264 condition); 265 return 0; 266 } 267 268 if (flags != 0) { 269 _mesa_error(ctx, GL_INVALID_VALUE, "glFenceSync(flags=0x%x)", 270 condition); 271 return 0; 272 } 273 274 syncObj = ctx->Driver.NewSyncObject(ctx, GL_SYNC_FENCE); 275 if (syncObj != NULL) { 276 syncObj->Type = GL_SYNC_FENCE; 277 /* The name is not currently used, and it is never visible to 278 * applications. If sync support is extended to provide support for 279 * NV_fence, this field will be used. We'll also need to add an object 280 * ID hashtable. 281 */ 282 syncObj->Name = 1; 283 syncObj->RefCount = 1; 284 syncObj->DeletePending = GL_FALSE; 285 syncObj->SyncCondition = condition; 286 syncObj->Flags = flags; 287 syncObj->StatusFlag = 0; 288 289 ctx->Driver.FenceSync(ctx, syncObj, condition, flags); 290 291 mtx_lock(&ctx->Shared->Mutex); 292 _mesa_set_add(ctx->Shared->SyncObjects, 293 _mesa_hash_pointer(syncObj), 294 syncObj); 295 mtx_unlock(&ctx->Shared->Mutex); 296 297 return (GLsync) syncObj; 298 } 299 300 return NULL; 301} 302 303 304GLenum GLAPIENTRY 305_mesa_ClientWaitSync(GLsync sync, GLbitfield flags, GLuint64 timeout) 306{ 307 GET_CURRENT_CONTEXT(ctx); 308 struct gl_sync_object *const syncObj = (struct gl_sync_object *) sync; 309 GLenum ret; 310 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_WAIT_FAILED); 311 312 if (!_mesa_validate_sync(ctx, syncObj)) { 313 _mesa_error(ctx, GL_INVALID_VALUE, "glClientWaitSync (not a valid sync object)"); 314 return GL_WAIT_FAILED; 315 } 316 317 if ((flags & ~GL_SYNC_FLUSH_COMMANDS_BIT) != 0) { 318 _mesa_error(ctx, GL_INVALID_VALUE, "glClientWaitSync(flags=0x%x)", flags); 319 return GL_WAIT_FAILED; 320 } 321 322 _mesa_ref_sync_object(ctx, syncObj); 323 324 /* From the GL_ARB_sync spec: 325 * 326 * ClientWaitSync returns one of four status values. A return value of 327 * ALREADY_SIGNALED indicates that <sync> was signaled at the time 328 * ClientWaitSync was called. ALREADY_SIGNALED will always be returned 329 * if <sync> was signaled, even if the value of <timeout> is zero. 330 */ 331 ctx->Driver.CheckSync(ctx, syncObj); 332 if (syncObj->StatusFlag) { 333 ret = GL_ALREADY_SIGNALED; 334 } else { 335 if (timeout == 0) { 336 ret = GL_TIMEOUT_EXPIRED; 337 } else { 338 ctx->Driver.ClientWaitSync(ctx, syncObj, flags, timeout); 339 340 ret = syncObj->StatusFlag ? GL_CONDITION_SATISFIED : GL_TIMEOUT_EXPIRED; 341 } 342 } 343 344 _mesa_unref_sync_object(ctx, syncObj); 345 return ret; 346} 347 348 349void GLAPIENTRY 350_mesa_WaitSync(GLsync sync, GLbitfield flags, GLuint64 timeout) 351{ 352 GET_CURRENT_CONTEXT(ctx); 353 struct gl_sync_object *const syncObj = (struct gl_sync_object *) sync; 354 355 if (!_mesa_validate_sync(ctx, syncObj)) { 356 _mesa_error(ctx, GL_INVALID_VALUE, "glWaitSync (not a valid sync object)"); 357 return; 358 } 359 360 if (flags != 0) { 361 _mesa_error(ctx, GL_INVALID_VALUE, "glWaitSync(flags=0x%x)", flags); 362 return; 363 } 364 365 if (timeout != GL_TIMEOUT_IGNORED) { 366 _mesa_error(ctx, GL_INVALID_VALUE, "glWaitSync(timeout=0x%" PRIx64 ")", 367 (uint64_t) timeout); 368 return; 369 } 370 371 ctx->Driver.ServerWaitSync(ctx, syncObj, flags, timeout); 372} 373 374 375void GLAPIENTRY 376_mesa_GetSynciv(GLsync sync, GLenum pname, GLsizei bufSize, GLsizei *length, 377 GLint *values) 378{ 379 GET_CURRENT_CONTEXT(ctx); 380 struct gl_sync_object *const syncObj = (struct gl_sync_object *) sync; 381 GLsizei size = 0; 382 GLint v[1]; 383 384 if (!_mesa_validate_sync(ctx, syncObj)) { 385 _mesa_error(ctx, GL_INVALID_VALUE, "glGetSynciv (not a valid sync object)"); 386 return; 387 } 388 389 switch (pname) { 390 case GL_OBJECT_TYPE: 391 v[0] = syncObj->Type; 392 size = 1; 393 break; 394 395 case GL_SYNC_CONDITION: 396 v[0] = syncObj->SyncCondition; 397 size = 1; 398 break; 399 400 case GL_SYNC_STATUS: 401 /* Update the state of the sync by dipping into the driver. Note that 402 * this call won't block. It just updates state in the common object 403 * data from the current driver state. 404 */ 405 ctx->Driver.CheckSync(ctx, syncObj); 406 407 v[0] = (syncObj->StatusFlag) ? GL_SIGNALED : GL_UNSIGNALED; 408 size = 1; 409 break; 410 411 case GL_SYNC_FLAGS: 412 v[0] = syncObj->Flags; 413 size = 1; 414 break; 415 416 default: 417 _mesa_error(ctx, GL_INVALID_ENUM, "glGetSynciv(pname=0x%x)\n", pname); 418 return; 419 } 420 421 if (size > 0 && bufSize > 0) { 422 const GLsizei copy_count = MIN2(size, bufSize); 423 424 memcpy(values, v, sizeof(GLint) * copy_count); 425 } 426 427 if (length != NULL) { 428 *length = size; 429 } 430} 431