Lines Matching refs:q
195 void queue_init(queue *q) {
196 gpr_mu_init(&q->mu);
197 gpr_cv_init(&q->non_empty);
198 gpr_cv_init(&q->non_full);
199 q->head = 0;
200 q->length = 0;
204 void queue_destroy(queue *q) {
205 gpr_mu_destroy(&q->mu);
206 gpr_cv_destroy(&q->non_empty);
207 gpr_cv_destroy(&q->non_full);
211 void queue_append(queue *q, int x) {
212 gpr_mu_lock(&q->mu);
219 while (q->length == N) {
220 gpr_cv_wait(&q->non_full, &q->mu, gpr_inf_future);
222 if (q->length == 0) { /* Wake threads blocked in queue_remove(). */
225 gpr_cv_broadcast(&q->non_empty);
227 q->elem[(q->head + q->length) % N] = x;
228 q->length++;
229 gpr_mu_unlock(&q->mu);
234 int queue_try_append(queue *q, int x) {
236 if (gpr_mu_trylock(&q->mu)) {
237 if (q->length != N) {
238 if (q->length == 0) { /* Wake threads blocked in queue_remove(). */
239 gpr_cv_broadcast(&q->non_empty);
241 q->elem[(q->head + q->length) % N] = x;
242 q->length++;
245 gpr_mu_unlock(&q->mu);
253 int queue_remove(queue *q, int *head, gpr_timespec abs_deadline) {
255 gpr_mu_lock(&q->mu);
261 while (q->length == 0 &&
262 !gpr_cv_wait(&q->non_empty, &q->mu, abs_deadline)) {
264 if (q->length != 0) { /* Queue is non-empty. */
266 if (q->length == N) { /* Wake threads blocked in queue_append(). */
267 gpr_cv_broadcast(&q->non_full);
269 *head = q->elem[q->head];
270 q->head = (q->head + 1) % N;
271 q->length--;
273 gpr_mu_unlock(&q->mu);