• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**************************************************************************
2  *
3  * Copyright 2009 VMware, Inc.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21  * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 
28 
29 #include "pipe/p_screen.h"
30 #include "util/u_memory.h"
31 #include "lp_debug.h"
32 #include "lp_fence.h"
33 
34 
35 #include "util/timespec.h"
36 
37 
38 /**
39  * Create a new fence object.
40  *
41  * The rank will be the number of bins in the scene.  Whenever a rendering
42  * thread hits a fence command, it'll increment the fence counter.  When
43  * the counter == the rank, the fence is finished.
44  *
45  * \param rank  the expected finished value of the fence counter.
46  */
47 struct lp_fence *
lp_fence_create(unsigned rank)48 lp_fence_create(unsigned rank)
49 {
50    static unsigned fence_id = 0;
51    struct lp_fence *fence = CALLOC_STRUCT(lp_fence);
52 
53    if (!fence)
54       return NULL;
55 
56    pipe_reference_init(&fence->reference, 1);
57 
58    (void) mtx_init(&fence->mutex, mtx_plain);
59    cnd_init(&fence->signalled);
60 
61    fence->id = p_atomic_inc_return(&fence_id) - 1;
62    fence->rank = rank;
63 
64    if (LP_DEBUG & DEBUG_FENCE)
65       debug_printf("%s %d\n", __func__, fence->id);
66 
67    return fence;
68 }
69 
70 
71 /** Destroy a fence.  Called when refcount hits zero. */
72 void
lp_fence_destroy(struct lp_fence * fence)73 lp_fence_destroy(struct lp_fence *fence)
74 {
75    if (LP_DEBUG & DEBUG_FENCE)
76       debug_printf("%s %d\n", __func__, fence->id);
77 
78    mtx_destroy(&fence->mutex);
79    cnd_destroy(&fence->signalled);
80    FREE(fence);
81 }
82 
83 
84 /**
85  * Called by the rendering threads to increment the fence counter.
86  * When the counter == the rank, the fence is finished.
87  */
88 void
lp_fence_signal(struct lp_fence * fence)89 lp_fence_signal(struct lp_fence *fence)
90 {
91    if (LP_DEBUG & DEBUG_FENCE)
92       debug_printf("%s %d\n", __func__, fence->id);
93 
94    mtx_lock(&fence->mutex);
95 
96    fence->count++;
97    assert(fence->count <= fence->rank);
98 
99    if (LP_DEBUG & DEBUG_FENCE)
100       debug_printf("%s count=%u rank=%u\n", __func__,
101                    fence->count, fence->rank);
102 
103    /* Wakeup all threads waiting on the mutex:
104     */
105    cnd_broadcast(&fence->signalled);
106 
107    mtx_unlock(&fence->mutex);
108 }
109 
110 
111 bool
lp_fence_signalled(struct lp_fence * f)112 lp_fence_signalled(struct lp_fence *f)
113 {
114    return f->count == f->rank;
115 }
116 
117 
118 void
lp_fence_wait(struct lp_fence * f)119 lp_fence_wait(struct lp_fence *f)
120 {
121    if (LP_DEBUG & DEBUG_FENCE)
122       debug_printf("%s %d\n", __func__, f->id);
123 
124    mtx_lock(&f->mutex);
125    assert(f->issued);
126    while (f->count < f->rank) {
127       cnd_wait(&f->signalled, &f->mutex);
128    }
129    mtx_unlock(&f->mutex);
130 }
131 
132 
133 bool
lp_fence_timedwait(struct lp_fence * f,uint64_t timeout)134 lp_fence_timedwait(struct lp_fence *f, uint64_t timeout)
135 {
136    struct timespec ts, abs_ts;
137 
138    timespec_get(&ts, TIME_UTC);
139 
140    bool ts_overflow = timespec_add_nsec(&abs_ts, &ts, timeout);
141 
142    if (LP_DEBUG & DEBUG_FENCE)
143       debug_printf("%s %d\n", __func__, f->id);
144 
145    mtx_lock(&f->mutex);
146    assert(f->issued);
147    while (f->count < f->rank) {
148       int ret;
149       if (ts_overflow)
150          ret = cnd_wait(&f->signalled, &f->mutex);
151       else
152          ret = cnd_timedwait(&f->signalled, &f->mutex, &abs_ts);
153       if (ret != thrd_success)
154          break;
155    }
156 
157    const bool result = (f->count >= f->rank);
158    mtx_unlock(&f->mutex);
159 
160    return result;
161 }
162