Merge branch '7.8'
[mesa.git] / src / gallium / drivers / llvmpipe / lp_fence.c
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 "util/u_inlines.h"
32 #include "lp_debug.h"
33 #include "lp_fence.h"
34
35
36 struct lp_fence *
37 lp_fence_create(unsigned rank)
38 {
39 struct lp_fence *fence = CALLOC_STRUCT(lp_fence);
40
41 pipe_reference_init(&fence->reference, 1);
42
43 pipe_mutex_init(fence->mutex);
44 pipe_condvar_init(fence->signalled);
45
46 fence->rank = rank;
47
48 return fence;
49 }
50
51
52 static void
53 lp_fence_destroy(struct lp_fence *fence)
54 {
55 pipe_mutex_destroy(fence->mutex);
56 pipe_condvar_destroy(fence->signalled);
57 FREE(fence);
58 }
59
60
61 static void
62 llvmpipe_fence_reference(struct pipe_screen *screen,
63 struct pipe_fence_handle **ptr,
64 struct pipe_fence_handle *fence)
65 {
66 struct lp_fence *old = (struct lp_fence *) *ptr;
67 struct lp_fence *f = (struct lp_fence *) fence;
68
69 if (pipe_reference(&old->reference, &f->reference)) {
70 lp_fence_destroy(old);
71 }
72 }
73
74
75 static int
76 llvmpipe_fence_signalled(struct pipe_screen *screen,
77 struct pipe_fence_handle *fence,
78 unsigned flag)
79 {
80 struct lp_fence *f = (struct lp_fence *) fence;
81
82 return f->count == f->rank;
83 }
84
85
86 static int
87 llvmpipe_fence_finish(struct pipe_screen *screen,
88 struct pipe_fence_handle *fence_handle,
89 unsigned flag)
90 {
91 struct lp_fence *fence = (struct lp_fence *) fence_handle;
92
93 pipe_mutex_lock(fence->mutex);
94 while (fence->count < fence->rank) {
95 pipe_condvar_wait(fence->signalled, fence->mutex);
96 }
97 pipe_mutex_unlock(fence->mutex);
98
99 return 0;
100 }
101
102
103 void
104 lp_fence_signal(struct lp_fence *fence)
105 {
106 pipe_mutex_lock(fence->mutex);
107
108 fence->count++;
109 assert(fence->count <= fence->rank);
110
111 LP_DBG(DEBUG_RAST, "%s count=%u rank=%u\n", __FUNCTION__,
112 fence->count, fence->rank);
113
114 pipe_condvar_signal(fence->signalled);
115
116 pipe_mutex_unlock(fence->mutex);
117 }
118
119
120 void
121 llvmpipe_init_screen_fence_funcs(struct pipe_screen *screen)
122 {
123 screen->fence_reference = llvmpipe_fence_reference;
124 screen->fence_signalled = llvmpipe_fence_signalled;
125 screen->fence_finish = llvmpipe_fence_finish;
126 }