Merge remote branch 'origin/master' into lp-binning
[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 "lp_fence.h"
32
33
34 struct lp_fence *
35 lp_fence_create(unsigned rank)
36 {
37 struct lp_fence *fence = CALLOC_STRUCT(lp_fence);
38
39 pipe_reference_init(&fence->reference, 1);
40
41 pipe_mutex_init(fence->mutex);
42 pipe_condvar_init(fence->signalled);
43
44 fence->rank = rank;
45
46 return fence;
47 }
48
49
50 static void
51 lp_fence_destroy(struct lp_fence *fence)
52 {
53 pipe_mutex_destroy(fence->mutex);
54 pipe_condvar_destroy(fence->signalled);
55 FREE(fence);
56 }
57
58
59 static void
60 llvmpipe_fence_reference(struct pipe_screen *screen,
61 struct pipe_fence_handle **ptr,
62 struct pipe_fence_handle *fence)
63 {
64 struct lp_fence *old = (struct lp_fence *) *ptr;
65 struct lp_fence *f = (struct lp_fence *) fence;
66
67 if (pipe_reference(&old->reference, &f->reference)) {
68 lp_fence_destroy(old);
69 }
70 }
71
72
73 static int
74 llvmpipe_fence_signalled(struct pipe_screen *screen,
75 struct pipe_fence_handle *fence,
76 unsigned flag)
77 {
78 struct lp_fence *f = (struct lp_fence *) fence;
79
80 return f->count == f->rank;
81 }
82
83
84 static int
85 llvmpipe_fence_finish(struct pipe_screen *screen,
86 struct pipe_fence_handle *fence_handle,
87 unsigned flag)
88 {
89 struct lp_fence *fence = (struct lp_fence *) fence_handle;
90
91 pipe_mutex_lock(fence->mutex);
92 while (fence->count < fence->rank) {
93 pipe_condvar_wait(fence->signalled, fence->mutex);
94 }
95 pipe_mutex_unlock(fence->mutex);
96
97 return 0;
98 }
99
100
101
102
103 void
104 llvmpipe_init_screen_fence_funcs(struct pipe_screen *screen)
105 {
106 screen->fence_reference = llvmpipe_fence_reference;
107 screen->fence_signalled = llvmpipe_fence_signalled;
108 screen->fence_finish = llvmpipe_fence_finish;
109 }