struct node *breadth_first_search(struct node *n, int val) { struct queue q = {0}; queue_push(&q, n); while ( (n = queue_shift(&q))) { #ifdef VERBOSE printf(" node: %i (%p), match? %s\n", n->val, n, (n->val == val) ? "yes" : "no"); #endif if (n->val == val) break; if (n->left != NULL) queue_push(&q, n->left); if (n->right != NULL) queue_push(&q, n->right); } flush_queue(&q); return n; }
const message* S_queue_dequeue(pTHX_ message_queue* queue) { const message* ret; MUTEX_LOCK(&queue->mutex); while (!queue->front) COND_WAIT(&queue->condvar, &queue->mutex); ret = queue_shift(queue); MUTEX_UNLOCK(&queue->mutex); return ret; }
const message* S_queue_dequeue_nb(pTHX_ message_queue* queue) { MUTEX_LOCK(&queue->mutex); if (queue->front) { const message* ret = queue_shift(queue); MUTEX_UNLOCK(&queue->mutex); return ret; } else { MUTEX_UNLOCK(&queue->mutex); return NULL; } }
static void consumer(void *v_t) { struct test *t = v_t; for (;;) { int *item = queue_shift(&t->queue, &t->consumer); if (!item) { t->consumer_blocked = TRUE; cond_broadcast(&t->cond); return; } assert(*item == t->consumer_count); free(item); t->consumer_count++; } }
/* This function takes the oldest request on the queue * (maxconn_cf->waiting_requests) and dispatches it to the backends. This * calls ngx_http_upstream_connect() which will in turn call the peer get * callback, peer_get(). peer_get() will do * the actual selection of backend. Here we're just giving the request the * go-ahead to proceed. */ static void dispatch (max_connections_srv_conf_t *maxconn_cf) { if (ngx_queue_empty(&maxconn_cf->waiting_requests)) return; if (maxconn_cf->connections >= maxconn_cf->max_connections) return; max_connections_peer_data_t *peer_data = queue_shift(maxconn_cf); ngx_http_request_t *r = peer_data->r; ngx_log_error(NGX_LOG_INFO, r->connection->log, 0, "max_connections dispatch (queue_length: %ui, conn: %ui)", maxconn_cf->queue_length, maxconn_cf->connections); peer_data->processing = 1; maxconn_cf->connections++; /* keep track of how many slots are occupied */ ngx_http_upstream_connect(r, r->upstream); }
void problem_ruleinfos_iterate( Problem *problem, int (*callback)(Ruleinfo *ri, void *user_data), void *user_data ) { #if SATSOLVER_VERSION > 1600 Queue rules; Id rule; queue_init(&rules); solver_findallproblemrules(problem->solver, problem->id, &rules); while ((rule = queue_shift(&rules))) { Ruleinfo *ri = ruleinfo_new( problem->solver, rule, problem->request ); if (callback( ri, user_data )) break; } #endif return; }
static void queue_check_event(ngx_event_t *ev) { max_connections_srv_conf_t *maxconn_cf = ev->data; max_connections_peer_data_t *oldest; while ( (oldest = queue_oldest(maxconn_cf)) && ngx_current_msec - oldest->accessed > maxconn_cf->queue_timeout ) { max_connections_peer_data_t *peer_data = queue_shift(maxconn_cf); ngx_log_error(NGX_LOG_INFO, peer_data->r->connection->log, 0, "max_connections expire"); ngx_http_finalize_request(peer_data->r, NGX_HTTP_GATEWAY_TIME_OUT); } /* check the check timer */ if( !ngx_queue_empty(&maxconn_cf->waiting_requests) && !maxconn_cf->queue_check_event.timer_set ) { ngx_add_timer((&maxconn_cf->queue_check_event), maxconn_cf->queue_timeout); } }