llvmpipe: rename queue size to count
[mesa.git] / src / gallium / drivers / llvmpipe / lp_bin_queue.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 /**
30 * Bin queue. We'll use two queues. One contains "full" bins which
31 * are produced by the "setup" code. The other contains "empty" bins
32 * which are produced by the "rast" code when it finishes rendering a bin.
33 */
34
35
36 #include "pipe/p_thread.h"
37 #include "util/u_memory.h"
38 #include "lp_bin_queue.h"
39
40
41
42 #define MAX_BINS 4
43
44
45 /**
46 * A queue of bins
47 */
48 struct lp_bins_queue
49 {
50 /** XXX might use a linked list here somedone, but the list will
51 * probably always be pretty short.
52 */
53 struct lp_bins *bins[MAX_BINS];
54 unsigned count;
55
56 pipe_condvar count_change;
57 pipe_mutex mutex;
58 };
59
60
61
62 /** Allocate a new bins queue */
63 struct lp_bins_queue *
64 lp_bins_queue_create(void)
65 {
66 struct lp_bins_queue *queue = CALLOC_STRUCT(lp_bins_queue);
67 if (queue) {
68 pipe_condvar_init(queue->count_change);
69 pipe_mutex_init(queue->mutex);
70 }
71 return queue;
72 }
73
74
75 /** Delete a new bins queue */
76 void
77 lp_bins_queue_destroy(struct lp_bins_queue *queue)
78 {
79 pipe_condvar_destroy(queue->count_change);
80 pipe_mutex_destroy(queue->mutex);
81 }
82
83
84 /** Remove first lp_bins from head of queue */
85 struct lp_bins *
86 lp_bins_dequeue(struct lp_bins_queue *queue)
87 {
88 struct lp_bins *bins;
89 unsigned i;
90
91 pipe_mutex_lock(queue->mutex);
92 while (queue->count == 0) {
93 pipe_condvar_wait(queue->count_change, queue->mutex);
94 }
95
96 assert(queue->count >= 1);
97
98 /* get head */
99 bins = queue->bins[0];
100
101 /* shift entries */
102 for (i = 0; i < queue->count - 1; i++) {
103 queue->bins[i] = queue->bins[i + 1];
104 }
105
106 queue->count--;
107
108 /* signal size change */
109 pipe_condvar_signal(queue->count_change);
110
111 pipe_mutex_unlock(queue->mutex);
112
113 return bins;
114 }
115
116
117 /** Add an lp_bins to tail of queue */
118 void
119 lp_bins_enqueue(struct lp_bins_queue *queue, struct lp_bins *bins)
120 {
121 pipe_mutex_lock(queue->mutex);
122
123 assert(queue->count < MAX_BINS);
124
125 /* debug: check that bins is not already in the queue */
126 if (0) {
127 unsigned i;
128 for (i = 0; i < queue->count; i++) {
129 assert(queue->bins[i] != bins);
130 }
131 }
132
133 /* add to end */
134 queue->bins[queue->count++] = bins;
135
136 /* signal size change */
137 pipe_condvar_signal(queue->count_change);
138
139 pipe_mutex_unlock(queue->mutex);
140 }
141
142
143 /** Return number of entries in the queue */
144 unsigned
145 lp_bins_queue_count(struct lp_bins_queue *queue)
146 {
147 unsigned count;
148 pipe_mutex_lock(queue->mutex);
149 count = queue->count;
150 pipe_mutex_unlock(queue->mutex);
151 return count;
152 }
153
154
155 /** Wait until the queue has exactly 'count' entries */
156 void
157 lp_bins_queue_wait_count(struct lp_bins_queue *queue, unsigned count)
158 {
159 pipe_mutex_lock(queue->mutex);
160 while (queue->count != count) {
161 pipe_condvar_wait(queue->count_change, queue->mutex);
162 }
163 pipe_mutex_unlock(queue->mutex);
164 }