Beispiel #1
0
/****************************************************************************************
* Function name - heap_pop
*
* Description - Takes the root node out of the heap and restores heap structure
*
* Input -       *h - pointer to an initialized heap
* Return Code/Output - On success - pointer to hnode, on error - NULL
****************************************************************************************/
hnode* heap_pop (heap*const h, int keep_timer_id)
{
  if (!h || h->curr_heap_size <= 0)
    {
      fprintf(stderr, "%s - error: wrong input\n", __func__);
      return 0;
    }
	
  return heap_remove_node (h, 0, keep_timer_id);
}
Beispiel #2
0
/****************************************************************************************
* Function name - tq_cancel_timers
*
* Description - Cancels all timers in timer queue, scheduled for some pointer to context
*
* Input -       *tq    - pointer to a timer queue, e.g. heap
*               *tnode - pointer to timer context (timer-node) to be searched for
*
* Return Code/Output - On success - zero of positive number of cancelled timers, on error -1
****************************************************************************************/
int tq_cancel_timers (timer_queue*const tq, struct timer_node* const tnode)
{
    hnode* node = 0;
    size_t index = 0;
    int counter = 0;
    heap* h = (heap *) tq;

    if (!tq || !tnode)
    {
        fprintf (stderr, "%s - error: wrong input.\n", __func__);
        return -1;
    }

    for (index = 0; index < h->curr_heap_size;)
    {
        if (h->heap[index]->ctx == tnode)
        {
            node = heap_remove_node (h, index, 0);

            if (node)
            {
              node_reset (node);

              if (mpool_return_obj (h->nodes_mpool, (allocatable *) node) == -1)
              {
                fprintf (stderr, "%s - error: mpool_return_obj () failed.\n", __func__);
                return -1;
              }
              counter++;
            }
            else
            {
              fprintf (stderr, "%s - error: node removal failed.\n", __func__);
              return -1;
            }
        }
        else
        {
          ++index;
        }
    }

    return counter;
}
Beispiel #3
0
/****************************************************************************************
* Function name - tq_cancel_timer
*
* Description - Cancels timer, using timer-id returned by tq_schedule_timer ()
*
* Input -       *tq      - pointer to a timer queue, e.g. heap
*               timer_id - number returned by tq_schedule_timer ()
*
* Return Code/Output - On success - 0, on error -1
****************************************************************************************/
int tq_cancel_timer (timer_queue*const tq, long timer_id)
{
    heap* h = (heap *) tq;

    if (!tq || timer_id < 0 || (size_t) timer_id > h->max_heap_size)
    {
        fprintf (stderr, "%s - error: wrong input.\n", __func__);
        return -1;
    }

    long node_slot = h->ids_arr[timer_id];

    if (node_slot < 0)
    {
        //fprintf (stderr,
        //         "%s - error: the timer-id is not valid any more (cancelled, expired?).\n",
        //         __func__);
        return -1;
    }

    if (timer_id != h->heap[node_slot]->node_id)
    {
        fprintf (stderr, "%s - error: internal timer mismatch.\n", __func__);
        return -1;
    }

    // last zero means - not keeping the timer-id/slot
    hnode* node = heap_remove_node (h, node_slot, 0);

    if (!node)
    {
        fprintf (stderr, "%s - error: hnode removal failed.\n", __func__);
        return -1;
    }
    else
    {
      node_reset (node);
      mpool_return_obj (h->nodes_mpool, (allocatable *) node);
    }

    return 0;
}