1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
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.
36 #include "pipe/p_thread.h"
37 #include "util/u_memory.h"
38 #include "lp_bin_queue.h"
50 /** XXX might use a linked list here somedone, but the list will
51 * probably always be pretty short.
53 struct lp_bins
*bins
[MAX_BINS
];
56 pipe_condvar count_change
;
62 /** Allocate a new bins queue */
63 struct lp_bins_queue
*
64 lp_bins_queue_create(void)
66 struct lp_bins_queue
*queue
= CALLOC_STRUCT(lp_bins_queue
);
68 pipe_condvar_init(queue
->count_change
);
69 pipe_mutex_init(queue
->mutex
);
75 /** Delete a new bins queue */
77 lp_bins_queue_destroy(struct lp_bins_queue
*queue
)
79 pipe_condvar_destroy(queue
->count_change
);
80 pipe_mutex_destroy(queue
->mutex
);
84 /** Remove first lp_bins from head of queue */
86 lp_bins_dequeue(struct lp_bins_queue
*queue
)
91 pipe_mutex_lock(queue
->mutex
);
92 while (queue
->count
== 0) {
93 pipe_condvar_wait(queue
->count_change
, queue
->mutex
);
96 assert(queue
->count
>= 1);
99 bins
= queue
->bins
[0];
102 for (i
= 0; i
< queue
->count
- 1; i
++) {
103 queue
->bins
[i
] = queue
->bins
[i
+ 1];
108 /* signal size change */
109 pipe_condvar_signal(queue
->count_change
);
111 pipe_mutex_unlock(queue
->mutex
);
117 /** Add an lp_bins to tail of queue */
119 lp_bins_enqueue(struct lp_bins_queue
*queue
, struct lp_bins
*bins
)
121 pipe_mutex_lock(queue
->mutex
);
123 assert(queue
->count
< MAX_BINS
);
125 /* debug: check that bins is not already in the queue */
128 for (i
= 0; i
< queue
->count
; i
++) {
129 assert(queue
->bins
[i
] != bins
);
134 queue
->bins
[queue
->count
++] = bins
;
136 /* signal size change */
137 pipe_condvar_signal(queue
->count_change
);
139 pipe_mutex_unlock(queue
->mutex
);
143 /** Return number of entries in the queue */
145 lp_bins_queue_count(struct lp_bins_queue
*queue
)
148 pipe_mutex_lock(queue
->mutex
);
149 count
= queue
->count
;
150 pipe_mutex_unlock(queue
->mutex
);
155 /** Wait until the queue has exactly 'count' entries */
157 lp_bins_queue_wait_count(struct lp_bins_queue
*queue
, unsigned count
)
159 pipe_mutex_lock(queue
->mutex
);
160 while (queue
->count
!= count
) {
161 pipe_condvar_wait(queue
->count_change
, queue
->mutex
);
163 pipe_mutex_unlock(queue
->mutex
);