Ejemplo n.º 1
0
void
queue_tail_del(struct queue * q)
{
    if (!queue_is_empty(q)) {
        queue_item_del(q->elems[q->tail]);
        free(q->elems[q->tail]);
        q->elems[q->tail] = NULL;
        q->tail = (q->tail + 1) % q->max_size;
        pthread_mutex_lock(&q->mux);
        {
            q->nr_elems--;
            if (queue_is_empty(q))
                pthread_cond_wait(&q->not_empty, &q->mux);
            pthread_cond_signal(&q->not_full);
        }
        pthread_mutex_unlock(&q->mux);
    }
}
} END_TEST


/* ************************************************************ */
/* bug tests                                                    */
/* ************************************************************ */
START_TEST(test_bug005562) {
	/*
	 * BUG 005562: Segfault after sending REGISTER to an invalid address (no registrar aat this address) and waiting >30sec
	 */

	sipstack_event *event;

	sipstack_init();

	int counter = 0;

	/* send REGISTER */
	int regId =
		sipstack_send_register("sip:"TEST_SIPSTACK_USER"@192.168.0.1", "sip:192.168.0.99", 1800);
	fail_unless(regId > -1, "Sending REGISTER failed. (result = %i)", regId);

	/* receive response */
	while (queue_is_empty(event_queue) && counter < 60) {
		sleep(1);
		counter++;
	}

	fail_unless(!queue_is_empty(event_queue), TEST_SIPSTACK_PREFIX "No response for registering received.");

	event = queue_front_and_dequeue(event_queue);

	/* expect REGISTRATION_FAILURE (2) */
	/* test failed*/
	if(event->type != 2) {
		/* free allocated memory before test fails */
		sipstack_event_free(event);
	}
	fail_unless(event->type == 2,
				"No event of type REGISTRATION_FAILURE received. (type = %i)", event->type);

	sipstack_event_free(event);
	sipstack_quit();
} END_TEST
Ejemplo n.º 3
0
void
floodfill(unsigned long * pixels, int x, int y, int width, int height,unsigned long color) {

    printf("\nEntrando to floodfill\n");
    queue *lista_xy;
    
    lista_xy = queue_init();

    int color_start = pixels[x + y * width];
   
    if (color != color_start) {
        queue_enqueue(x, lista_xy);
        queue_enqueue(y, lista_xy);

        pixels[x + y * width] = color;

        while (!queue_is_empty(lista_xy)) {
            if (x + 1 < width) {
                if (pixels[(x + 1) + y * width] == color_start) {
                    pixels[(x + 1) + y * width] = color;
                    queue_enqueue(x+1, lista_xy);
                    queue_enqueue(y, lista_xy);
                }
            }

            if (x - 1 >= 0){
                if (pixels[(x - 1) + y * width] == color_start) {
                    pixels[(x - 1) + y * width] = color;
                    queue_enqueue(x-1, lista_xy);
                    queue_enqueue(y, lista_xy);
                }
            }

            if (y + 1 < height){
                if (pixels[x + (y + 1) * width] == color_start) {
                    pixels[x + (y + 1) * width] = color;
                    queue_enqueue(x, lista_xy);
                    queue_enqueue(y+1, lista_xy);
                }
            }

            if (y - 1 >= 0){
                if (pixels[x + (y - 1) * width] == color_start){
                    pixels[x + (y - 1) * width] = color;
                    queue_enqueue(x, lista_xy);
                    queue_enqueue(y-1, lista_xy);
                }
            }
            x = lista_xy->front->info;
            queue_dequeue(lista_xy);
            y = lista_xy->front->info;
            queue_dequeue(lista_xy);
       }
   }
   queue_destroy(lista_xy);
} 
Ejemplo n.º 4
0
void
queue_invert (queue *q)
{
    if (queue_is_empty(q))
        return;

    pos c = queue_dequeue(q);
    queue_invert(q);
    queue_enqueue(q, c);
}
Ejemplo n.º 5
0
Archivo: queue.c Proyecto: dkuner/dict
static void queue_insert_node(Queue *queue, 
						struct node *key) {
	if (queue_is_empty(queue)) {
		queue->head = key;
		queue->tail = key;
	} else {
		queue->tail->next = key;
		queue->tail = key;
	}
}
Ejemplo n.º 6
0
/*!
  * \brief
  *    Returns the key to the caller.
  *    If we have bt_wait flag set, it waits for the user choice.
  *
  * \param  wait  Wait for key flag
  * \retval key pressed or -1(EOF) if none.
  */
keys_t btn_getkey (uint8_t wait)
{
   keys_t ret=BTN_NULL;

   // wait for user's action
   while (wait && queue_is_empty (&btn_q))
      ;
   queue_get (&btn_q, (void*)&ret);
   return ret;
}
Ejemplo n.º 7
0
void *recExec() {
	while (1) {
		if (!queue_is_empty(pColaReady) && !queue_is_empty(pColaCpu)) {

			sem_wait(&mut_r);
			ptmp = queue_pop(pColaReady);
			sem_post(&mut_r);
			sem_wait(&mut_c);
			pcpu = queue_pop(pColaCpu);
			sem_post(&mut_c);
			ptmp->cpuexec = pcpu;

			//printf("Exec  proceso %d tiempo cpu %d\n",ptmp->pid,tact);
			sem_wait(&mut_e);
			queue_push(pColaExec, ptmp);
			sem_post(&mut_e);
		}
	}
}
Ejemplo n.º 8
0
/*!
  * \brief
  *   This function gets a byte from queue.
  * \param  pointer to byte
  * \return
  *   \arg  0  Empty queue
  *   \arg  1  Done
 */
__Os__ int queue_get (queue_t *queue, void *b)
{
   if ( queue_is_empty (queue) )    //Empty queue
      return 0;
   memcpy (b, (const void*)&queue->buf[queue->head*queue->item_size], queue->item_size);
   //rotate pointers
   if ( ++queue->head >= queue->items )
      queue->head = 0;
   return 1;
}
Ejemplo n.º 9
0
void
playlist_update_queued_song(struct playlist *playlist,
			    struct player_control *pc,
			    const struct song *prev)
{
	int next_order;
	const struct song *next_song;

	if (!playlist->playing)
		return;

	assert(!queue_is_empty(&playlist->queue));
	assert((playlist->queued < 0) == (prev == NULL));

	next_order = playlist->current >= 0
		? queue_next_order(&playlist->queue, playlist->current)
		: 0;

	if (next_order == 0 && playlist->queue.random &&
	    !playlist->queue.single) {
		/* shuffle the song order again, so we get a different
		   order each time the playlist is played
		   completely */
		unsigned current_position =
			queue_order_to_position(&playlist->queue,
						playlist->current);

		queue_shuffle_order(&playlist->queue);

		/* make sure that the playlist->current still points to
		   the current song, after the song order has been
		   shuffled */
		playlist->current =
			queue_position_to_order(&playlist->queue,
						current_position);
	}

	if (next_order >= 0)
		next_song = queue_get_order(&playlist->queue, next_order);
	else
		next_song = NULL;

	if (prev != NULL && next_song != prev) {
		/* clear the currently queued song */
		pc_cancel(pc);
		playlist->queued = -1;
	}

	if (next_order >= 0) {
		if (next_song != prev)
			playlist_queue_song_order(playlist, pc, next_order);
		else
			playlist->queued = next_order;
	}
}
Ejemplo n.º 10
0
uint32_t queue_get_ptr(queue_t * queue, uint8_t ** element)
{
    if (queue_is_empty(queue))
        return ERROR_NOT_FOUND;

    *element = &queue->elements[queue->head * queue->element_size];

    queue->head = (queue->head + 1u) % queue->size;

    return SUCCESS;
}
Ejemplo n.º 11
0
void queue_cleanup(queue* q)
{
    while(!queue_is_empty(q)){
	queue_pop(q);
    }

    free(q->array);
    sem_destroy(q->content_sem);
    sem_destroy(q->freespace_sem);
    pthread_mutex_destroy(q->mutex);
}
Ejemplo n.º 12
0
void queue_free (Queue* queue) {
  /* Empty the queue */

  while (!queue_is_empty (queue)) {
    queue_pop_head (queue);
    }

  /* Free back the queue */

  free (queue);
  }
Ejemplo n.º 13
0
//-----------------------------------------------------------------
//  need to use free() to free up memory
//-----------------------------------------------------------------
boolean_t queue_remove(queue_t q, queue_element_t * e) {
    queue_link_t oldHead;
    assert(q != NULL);
    if(queue_is_empty(q))
        return FALSE;
    *e = q->head->e;
    oldHead = q->head;
    q->head = q->head->next;

    return TRUE;
}
bool empty_queue(Queue *self) {
	if (!self) {
		return false;
	}
	while (!queue_is_empty(self)) {
		int *popped = queue_pop_tail(self);
		//printf("popped %d from queue\n", *popped);
		free(popped);
	}
	return true;
}
int main()
{
	int i;
	queue q = queue_create();
	assert(queue_is_empty(q));
	queue_enqueue(q, 3);
	assert(queue_dequeue(q) == 3);
	assert(queue_is_empty(q));
	for (i= 0; i < 99; i++) {
		queue_enqueue(q, i);
	}
	assert(queue_is_full(q));
	assert(queue_dequeue(q) == 0);
	queue_enqueue(q, 1);
	queue_enqueue(q, 1);
	assert(queue_is_full(q));
	queue_destroy(&q);
	assert(q == NULL);
	return 0;
}
Ejemplo n.º 16
0
Archivo: queue.c Proyecto: creisman/os
void queue_reverse(queue* q) {
  assert(q != NULL);

  // If empty, do nothing.
  if (queue_is_empty(q)) {
    return;
  }

  queue_link* end = queue_reverse_helper(q, q->head);
  end->next = NULL;
}
Ejemplo n.º 17
0
Archivo: queue.c Proyecto: orumin/Graph
/*
 * Dequeueing.
 */
void *dequeue() {
    if(queue_is_empty()) {
        printf("Fatal error: The queue is empty.\n");
        exit(-1);
    }

    void *item;
    item = queue[out_index];
    out_index = (out_index+1) % memory_size;
    return item;
}
Ejemplo n.º 18
0
int h2o_http2_scheduler_run(h2o_http2_scheduler_node_t *root, h2o_http2_scheduler_run_cb cb, void *cb_arg)
{
    if (root->_queue != NULL) {
        while (!queue_is_empty(root->_queue)) {
            int bail_out = proceed(root, cb, cb_arg);
            if (bail_out)
                return bail_out;
        }
    }
    return 0;
}
Ejemplo n.º 19
0
uint32_t queue_get(queue_t * queue, uint8_t * element)
{
    if (queue_is_empty(queue))
        return ERROR_NOT_FOUND;

    memcpy(element, &queue->elements[queue->head * queue->element_size], queue->element_size);

    queue->head = (queue->head + 1u) % queue->size;

    return SUCCESS;
}
Ejemplo n.º 20
0
static void *
async_queue_timed_pop_unlocked(UAsyncQueue *queue, uint64_t end_time)
{
    if (queue_is_empty(queue->queue)) {
        assert(!queue->is_waiting);
        ++queue->is_waiting;
        if (!end_time)
            pthread_cond_wait(&queue->cond, &queue->mutex);
        else {
            struct timespec timeout;
            timeout.tv_sec  = end_time / 1000000;
            timeout.tv_nsec = 1000 * (end_time % 1000000);
            pthread_cond_timedwait(&queue->cond, &queue->mutex, &timeout);
        }
        --queue->is_waiting;
        if (queue_is_empty(queue->queue))
            return NULL;
    }
    return queue_pop(queue->queue);
}
Ejemplo n.º 21
0
pos
queue_start (queue *q)
{
    if (queue_is_empty(q))
    {
        printf("ERROR: Empty queue. (start)\n");
        return position(0,0);
    }

    return q->content[q->start];
}
Ejemplo n.º 22
0
Archivo: queue.c Proyecto: crinykr/blue
BOOL dequeue(queue_t* queue, void* item) {
	if (queue_is_empty(queue) == TRUE)
		return FALSE;

	memcpy(item,
			(char*) queue->array_addr + (queue->item_size * queue->get_index),
			queue->item_size);

	queue->get_index = (queue->get_index + 1) % queue->max_item_count;
	queue->last_op = QUEUE_DEQUEUE;
	return TRUE;
}
Ejemplo n.º 23
0
static int
queue_read (volatile struct queue *q)
{
	if (queue_is_empty (q))
		return -1;

	unsigned char c = q->data[q->start++];
	q->length--;
	if (q->start == sizeof q->data)
		q->start = 0;
	return c;
}
Ejemplo n.º 24
0
Archivo: queue.c Proyecto: dkuner/dict
void queue_destroy(Queue *queue, void (*destroy)(void *))
{
	void *data;

	while(! queue_is_empty(queue)) {
		data = queue_dequeue(queue);
		if (destroy) {
			destroy(data);
		}
	}
	queue->counter = 0;
}
Ejemplo n.º 25
0
/** バディページから所定のオーダのページを取り出しページフレーム番号を返す
    @param[in] buddy バディページ
    @param[in] order   取得するページのオーダ
    @param[out] pfnp   取得したページのページフレーム番号を返却する領域
    @retval     0      正常にページを獲得した
    @retval    -EINVAL ページフレーム情報返却域が不正か要求したページオーダが不正
    @retval    -ENOMEM 空きページがない
 */
int
page_buddy_dequeue(page_buddy *buddy, page_order order, obj_cnt_type *pfnp){
	page_order cur_order;
	page_frame *cur_page;

	kassert( spinlock_locked_by_self(&buddy->lock) );

	if ( (pfnp == NULL) || 
	    (order >= PAGE_POOL_MAX_ORDER) )
		return -EINVAL;  /* ページフレーム情報返却域 or 要求したページオーダが不正 */

	cur_order = order;

	while (cur_order < PAGE_POOL_MAX_ORDER) { /* 空きページがあるキューを順番に調べる */

		if ( !queue_is_empty(&buddy->page_list[cur_order]) ) {

			cur_page = CONTAINER_OF(
				queue_get_top(&buddy->page_list[cur_order]),
				page_frame, link); /* 空きページを取り出す */
			--buddy->free_nr[cur_order];

                        /* 空きページから要求オーダのページを切り出す */
			cur_page = remove_page_from_page_queue(buddy, 
			    cur_page, order, cur_order);  

			cur_page->state |= PAGE_CSTATE_USED; /* ページを使用中にする */

			*pfnp = cur_page->pfn;  /* ページフレーム番号を返却する  */

			if ( cur_page->order != order ) {
				
				/*  ページオーダが一致しない場合は内部整合性異常  */
				kprintf(KERN_CRI, "Invalid order page is allocated:%p "
				    "pfn:%u flags=%x order:%d reqest-order:%d find:%d\n", 
				    cur_page, cur_page->pfn, cur_page->state,
				    cur_page->order, order, cur_order);
				kassert(0);
			}

			kassert( ( buddy->start_pfn <= cur_page->pfn ) &&
			    ( cur_page->pfn < ( buddy->start_pfn + buddy->nr_pages ) ) );

			return 0;
		}

		++cur_order;  /*  より上のオーダからページを切り出す  */
	}


	return -ENOMEM;
}
enum playlist_result
playlist_play(struct playlist *playlist, struct player_control *pc,
	      int song)
{
	unsigned i = song;

	pc_clear_error(pc);

	if (song == -1) {
		/* play any song ("current" song, or the first song */

		if (queue_is_empty(&playlist->queue))
			return PLAYLIST_RESULT_SUCCESS;

		if (playlist->playing) {
			/* already playing: unpause playback, just in
			   case it was paused, and return */
			pc_set_pause(pc, false);
			return PLAYLIST_RESULT_SUCCESS;
		}

		/* select a song: "current" song, or the first one */
		i = playlist->current >= 0
			? playlist->current
			: 0;
	} else if (!queue_valid_position(&playlist->queue, song))
		return PLAYLIST_RESULT_BAD_RANGE;

	if (playlist->queue.random) {
		if (song >= 0)
			/* "i" is currently the song position (which
			   would be equal to the order number in
			   no-random mode); convert it to a order
			   number, because random mode is enabled */
			i = queue_position_to_order(&playlist->queue, song);

		if (!playlist->playing)
			playlist->current = 0;

		/* swap the new song with the previous "current" one,
		   so playback continues as planned */
		queue_swap_order(&playlist->queue,
				 i, playlist->current);
		i = playlist->current;
	}

	playlist->stop_on_error = false;
	playlist->error_count = 0;

	playlist_play_order(playlist, pc, i);
	return PLAYLIST_RESULT_SUCCESS;
}
Ejemplo n.º 27
0
void* queue_pop(queue* q){
    void* ret_payload;
	
    if(queue_is_empty(q)){
	return NULL;
    }
	
    ret_payload = q->array[q->front].payload;
    q->array[q->front].payload = NULL;
    q->front = ((q->front + 1) % q->maxSize);

    return ret_payload;
}
Ejemplo n.º 28
0
int uartRx_getNb(uartRx_t *pThis, chunk_t *pChunk)
{
    /* Block till a chunk arrives on the rx queue */
    if( queue_is_empty(&pThis->queue) )
	{
    	//printf("RX Queue is empty\r\n");
//		powerMode_change(PWR_ACTIVE);
        //asm("idle;");
		return FAIL;
    }
    //powerMode_change(PWR_FULL_ON);
	return uartRx_getChunk(pThis, pChunk);    
}
Ejemplo n.º 29
0
pos
queue_dequeue (queue *q)
{
    if (queue_is_empty(q))
    {
        printf("ERROR: Empty queue. (dequeue)\n");
        return position(0,0);
    }

    pos e = queue_start(q); q->length--;
    q->start = ++q->start % q->size;
    return e;
}
Ejemplo n.º 30
0
/** スレッドキューの先頭のスレッドを取り出す
    @param[in] q      操作対象キュー
    @param[in] thrp   キューの先頭スレッドを設定するポインタ変数のアドレス
    @retval 取り出し後のスレッド格納数
    @note キュー q のロックを呼出元で獲得しておき, キューが
    空でないことを呼出元責任で保証すること
 */
int
tq_get_top(thread_queue *q, struct _thread **thrp) {

	kassert( q != NULL);
	kassert( spinlock_locked_by_self( &q->lock ) );
	kassert( !queue_is_empty( &q->que ) );

	*thrp = CONTAINER_OF( queue_get_top( &q->que ), 
	    thread, link);   /*  キューからスレッドを取り出す                */
	--q->cnt;            /*  取り出し後のキュー内のスレッド数を減算する  */

	return q->cnt;       /*  取り出し後のキュー内のスレッド数を返却する  */
}