Path: blob/21.2-virgl/src/gallium/drivers/llvmpipe/lp_fence.c
4570 views
/**************************************************************************1*2* Copyright 2009 VMware, Inc.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/262728#include "pipe/p_screen.h"29#include "util/u_memory.h"30#include "lp_debug.h"31#include "lp_fence.h"323334/**35* Create a new fence object.36*37* The rank will be the number of bins in the scene. Whenever a rendering38* thread hits a fence command, it'll increment the fence counter. When39* the counter == the rank, the fence is finished.40*41* \param rank the expected finished value of the fence counter.42*/43struct lp_fence *44lp_fence_create(unsigned rank)45{46static int fence_id;47struct lp_fence *fence = CALLOC_STRUCT(lp_fence);4849if (!fence)50return NULL;5152pipe_reference_init(&fence->reference, 1);5354(void) mtx_init(&fence->mutex, mtx_plain);55cnd_init(&fence->signalled);5657fence->id = fence_id++;58fence->rank = rank;5960if (LP_DEBUG & DEBUG_FENCE)61debug_printf("%s %d\n", __FUNCTION__, fence->id);6263return fence;64}656667/** Destroy a fence. Called when refcount hits zero. */68void69lp_fence_destroy(struct lp_fence *fence)70{71if (LP_DEBUG & DEBUG_FENCE)72debug_printf("%s %d\n", __FUNCTION__, fence->id);7374mtx_destroy(&fence->mutex);75cnd_destroy(&fence->signalled);76FREE(fence);77}787980/**81* Called by the rendering threads to increment the fence counter.82* When the counter == the rank, the fence is finished.83*/84void85lp_fence_signal(struct lp_fence *fence)86{87if (LP_DEBUG & DEBUG_FENCE)88debug_printf("%s %d\n", __FUNCTION__, fence->id);8990mtx_lock(&fence->mutex);9192fence->count++;93assert(fence->count <= fence->rank);9495if (LP_DEBUG & DEBUG_FENCE)96debug_printf("%s count=%u rank=%u\n", __FUNCTION__,97fence->count, fence->rank);9899/* Wakeup all threads waiting on the mutex:100*/101cnd_broadcast(&fence->signalled);102103mtx_unlock(&fence->mutex);104}105106boolean107lp_fence_signalled(struct lp_fence *f)108{109return f->count == f->rank;110}111112void113lp_fence_wait(struct lp_fence *f)114{115if (LP_DEBUG & DEBUG_FENCE)116debug_printf("%s %d\n", __FUNCTION__, f->id);117118mtx_lock(&f->mutex);119assert(f->issued);120while (f->count < f->rank) {121cnd_wait(&f->signalled, &f->mutex);122}123mtx_unlock(&f->mutex);124}125126127boolean128lp_fence_timedwait(struct lp_fence *f, uint64_t timeout)129{130struct timespec ts;131int ret;132133timespec_get(&ts, TIME_UTC);134135ts.tv_nsec += timeout % 1000000000L;136ts.tv_sec += timeout / 1000000000L;137if (ts.tv_nsec >= 1000000000L) {138ts.tv_sec++;139ts.tv_nsec -= 1000000000L;140}141142if (LP_DEBUG & DEBUG_FENCE)143debug_printf("%s %d\n", __FUNCTION__, f->id);144145mtx_lock(&f->mutex);146assert(f->issued);147while (f->count < f->rank) {148ret = cnd_timedwait(&f->signalled, &f->mutex, &ts);149if (ret != thrd_success)150break;151}152const boolean result = (f->count >= f->rank);153mtx_unlock(&f->mutex);154return result;155}156157158