static void vrend_free_fences_for_context(struct vrend_context *ctx)
{
   struct vrend_fence *fence, *stor;

   if (vrend_state.sync_thread) {
      mtx_lock(&vrend_state.fence_mutex);
      LIST_FOR_EACH_ENTRY_SAFE(fence, stor, &vrend_state.fence_list, fences) {
         if (fence->ctx == ctx)
            free_fence_locked(fence);
      }
      LIST_FOR_EACH_ENTRY_SAFE(fence, stor, &vrend_state.fence_wait_list, fences) {
         if (fence->ctx == ctx)
            free_fence_locked(fence);
      }
      if (vrend_state.fence_waiting) {
         /* mark the fence invalid as the sync thread is still waiting on it */
         vrend_state.fence_waiting->ctx = NULL;
      }
      mtx_unlock(&vrend_state.fence_mutex);
   } else {
      LIST_FOR_EACH_ENTRY_SAFE(fence, stor, &vrend_state.fence_list, fences) {
         if (fence->ctx == ctx)
            free_fence_locked(fence);
      }
   }
}
static void vrend_free_fences_for_context(struct vrend_context *ctx)
{
   struct vrend_fence *fence, *stor;

   if (vrend_state.sync_thread) {
      mtx_lock(&vrend_state.fence_mutex);
      LIST_FOR_EACH_ENTRY_SAFE(fence, stor, &vrend_state.fence_list, fences) {
         if (fence->ctx == ctx)
            free_fence_locked(fence);
      }
      LIST_FOR_EACH_ENTRY_SAFE(fence, stor, &vrend_state.fence_wait_list, fences) {
         if (fence->ctx == ctx)
            free_fence_locked(fence);
      }
      if (vrend_state.fence_waiting) {
          
         vrend_state.fence_waiting->ctx = NULL;
      }
      mtx_unlock(&vrend_state.fence_mutex);
   } else {
      LIST_FOR_EACH_ENTRY_SAFE(fence, stor, &vrend_state.fence_list, fences) {
         if (fence->ctx == ctx)
            free_fence_locked(fence);
      }
   }
}