-/*
- * Sync execution so that the OpenGL context is switched onto this thread.
- * Anything after this call will be in a valid context.
- */
-VG_STATIC void vg_acquire_thread_sync(void)
-{
- /* We dont want to do anything if this is the main thread */
-
- if( vg_thread_purpose() == k_thread_purpose_loader ){
- VG_SYNC_LOG( "[%d] vg_acquire_thread_sync()\n" );
- _vg_ensure_engine_running();
-
- SDL_AtomicLock( &vg.sl_context );
- if( vg.context_ownership_depth == 0 ){
- vg.context_ownership_depth ++;
- vg.exec_context = 1;
- SDL_AtomicUnlock( &vg.sl_context );
-
- /* wait until told we can go */
- VG_SYNC_LOG( "[%d] Waiting to acuire sync.\n" );
- SDL_SemWait( vg.sem_allow_exec );
-
- _vg_ensure_engine_running();
-
- SDL_GL_MakeCurrent( vg.window, vg.gl_context );
- VG_SYNC_LOG( "[%d] granted\n" );
- }
- else{
- vg.context_ownership_depth ++;
- VG_SYNC_LOG( "[%d] granted\n" );
- SDL_AtomicUnlock( &vg.sl_context );
- }
- }
-}
-
-/*
- * Signify that we are done with the OpenGL context in this thread.
- * Anything after this call will be in an undefined context.
- */
-VG_STATIC void vg_release_thread_sync(void)
-{
- if( vg_thread_purpose() == k_thread_purpose_loader ){
- VG_SYNC_LOG( "[%d] vg_release_thread_sync()\n" );
-
- SDL_AtomicLock( &vg.sl_context );
- vg.context_ownership_depth --;
-
- if( vg.context_ownership_depth == 0 ){
- SDL_AtomicUnlock( &vg.sl_context );
- VG_SYNC_LOG( "[%d] Releasing context.\n" );
- SDL_GL_MakeCurrent( NULL, NULL );
- SDL_SemPost( vg.sem_exec_finished );
- }
- else
- SDL_AtomicUnlock( &vg.sl_context );
- }
-}
-
-VG_STATIC void _vg_run_synced(void)
-{
- SDL_AtomicLock( &vg.sl_context );
-
- if( vg.exec_context != 0 ){
- VG_SYNC_LOG( "[%d] _vg_run_synced() (%d).\n", vg.exec_context );
- vg.exec_context = 0;
- SDL_AtomicUnlock( &vg.sl_context );
-
- /* allow operations to go */
- SDL_GL_MakeCurrent( NULL, NULL );
- SDL_SemPost( vg.sem_allow_exec );
-
- /* wait for operations to complete */
- VG_SYNC_LOG( "[%d] Waiting for content.\n" );
- SDL_SemWait( vg.sem_exec_finished );
-
- /* check if we killed the engine */
- _vg_ensure_engine_running();
-
- /* re-engage main thread */
- VG_SYNC_LOG( "[%d] Re-engaging.\n" );
- SDL_GL_MakeCurrent( vg.window, vg.gl_context );
- }
- else{
- VG_SYNC_LOG( "[%d] Nothing to do.\n" );
- SDL_AtomicUnlock( &vg.sl_context );
- }
-}
-