Пример #1
0
void merge(int num_threads, int length, pivot_data *before_merge, int* merged){
	//printf("In merge num_threads %d\n", num_threads);
	int i;
	for(i=0; i<num_threads; i++){
		before_merge[i].ctr=0;
		//printf("i %d length %d\n", i, before_merge[i].length);
	}
	//printf("length %d\n", length);
	int ctr = 0;
	pivot_data* dq;

	PQueue *pq =  pqueue_new(heap_compare_pivots, num_threads);

	for(i=0; i<num_threads; i++){
		pqueue_enqueue(pq, &before_merge[i]);
		//printf("Enqueue %d\n", before_merge[i].pivots[0]);
	}

	while(pq->size != 0){
		dq = (pivot_data*)pqueue_dequeue(pq);
		//printf("Dequeue %d\n", dq->pivots[dq->ctr]);
		merged[ctr++] = dq->pivots[dq->ctr++];
		if(dq->ctr != dq->length){
			pqueue_enqueue(pq, dq);
			//printf("Enqueue %d\n", dq->pivots[dq->ctr]);
		}
	}
	pqueue_delete(pq);

	aligned_free(before_merge);
}
/*
 * Get the top priority queue
 */
PNODE pqueue_pop(PPQUEUE pQueue, PNODE pNode)
{
   if(pQueue->cNodes < 1)
   {
      return NULL;
   }
   memcpy(pNode, &pQueue->aNodes[1], sizeof(NODE));

   pqueue_delete(pQueue);

   return pNode;
}
/*
 * Since node 0 isn't used, it can be used as a temporary storage
 * for the pop:ed node.
 * Important node: Unless you protect the first node with a sempahore
 * (which isn't good design) this version of getting the top priority node
 * is not thread safe!
 */
PNODE pqueue_pop2(PPQUEUE pQueue)
{
   if(pQueue->cNodes < 1)
   {
      return NULL;
   }
   memcpy(&pQueue->aNodes[0], &pQueue->aNodes[1], sizeof(NODE));

   pqueue_delete(pQueue);

   return &pQueue->aNodes[0];
}
Пример #4
0
Файл: alarm.c Проект: obsc/os
/* unregister an alarm.  Returns 0 if the alarm had not been executed, 1
 * otherwise.
 */
int
deregister_alarm(alarm_id alarm) {
    interrupt_level_t old_level;

    if ( !alarm ) return -1;

    old_level = set_interrupt_level(DISABLED);
    if (pqueue_delete(alarm_pqueue, alarm) == 0) {
        free(alarm); // Only frees if alarm is found
        set_interrupt_level(old_level);
        return 0;
    }
    set_interrupt_level(old_level);
    return 1;
}
Пример #5
0
/* RFC2740 3.8.1.  Calculating the shortest path tree for an area */
void ospf6_spf_calculation(u_int32_t router_id,
                           struct ospf6_route_table * result_table,
                           struct ospf6_area * oa, unsigned int hostnum)
{
  struct pqueue * candidate_list;
  struct ospf6_vertex * root, * v, * w;
  int i;
  int size;
  caddr_t lsdesc;
  struct ospf6_lsa * lsa;

  if(IS_OSPF6_SIBLING_DEBUG_SPF)
  {
    zlog_debug("Starting spf calculation...");
  }

  /* initialize */
  candidate_list = pqueue_create();
  candidate_list->cmp = ospf6_vertex_cmp;

  ospf6_spf_table_finish (result_table);

  /* Install the calculating router itself as the root of the SPF tree */
  /* construct root vertex */
  lsa = ospf6_lsdb_lookup (htons (OSPF6_LSTYPE_ROUTER), htonl (0),
                                   router_id, oa->lsdb);
  if (lsa == NULL)
  {
    if(IS_OSPF6_SIBLING_DEBUG_SPF)
    {
      zlog_debug("Looking for type %d from lsdb %p with router id %d", htons(OSPF6_LSTYPE_ROUTER), oa->lsdb, router_id);
      zlog_debug("No router LSAs present, quiting spf calculation");
    }
    return;
  }

  root = ospf6_vertex_create (lsa);
  root->area = oa;
  root->cost = 0;
  root->hops = 0;
  root->nexthop[0].ifindex = 0; /* loopbak I/F is better ... */
  inet_pton (AF_INET6, "::1", &root->nexthop[0].address);

  /* Actually insert root to the candidate-list as the only candidate */
  pqueue_enqueue (root, candidate_list);

  /* Iterate until candidate-list becomes empty */
  while (candidate_list->size)
  {
    /* get closest candidate from priority queue */
    v = pqueue_dequeue (candidate_list);

    /* installing may result in merging or rejecting of the vertex */
    if (ospf6_spf_install (v, result_table, hostnum) < 0)
      continue;

    /* For each LS description in the just-added vertex V's LSA */
    size = (VERTEX_IS_TYPE (ROUTER, v) ?
            sizeof (struct ospf6_router_lsdesc) :
            sizeof (struct ospf6_network_lsdesc));
    for (lsdesc = OSPF6_LSA_HEADER_END (v->lsa->header) + 4;
         lsdesc + size <= OSPF6_LSA_END (v->lsa->header); lsdesc += size)
    {
      lsa = ospf6_lsdesc_lsa (lsdesc, v);
      if (lsa == NULL)
        continue;

      if (! ospf6_lsdesc_backlink (lsa, lsdesc, v))
        continue;

      w = ospf6_vertex_create (lsa);
      w->area = oa;
      w->parent = v;
      if (VERTEX_IS_TYPE (ROUTER, v))
      {
        w->cost = v->cost + ROUTER_LSDESC_GET_METRIC (lsdesc);
        w->hops = v->hops + (VERTEX_IS_TYPE (NETWORK, w) ? 0 : 1);
      }
      else /* NETWORK */
      {
        w->cost = v->cost;
        w->hops = v->hops + 1;
      }
      /* nexthop calculation */
      if (w->hops == 0)
        w->nexthop[0].ifindex = ROUTER_LSDESC_GET_IFID (lsdesc);
      else if (w->hops == 1 && v->hops == 0)
        ospf6_nexthop_calc (w, v, lsdesc);
      else
      {
        for (i = 0; ospf6_nexthop_is_set (&v->nexthop[i]) &&
             i < OSPF6_MULTI_PATH_LIMIT; i++)
          ospf6_nexthop_copy (&w->nexthop[i], &v->nexthop[i]);
      }

      /* add new candidate to the candidate_list */
      if (IS_OSPF6_SIBLING_DEBUG_SPF)
        zlog_debug ("  New candidate: %s hops %d cost %d",
                    w->name, w->hops, w->cost);
        pqueue_enqueue (w, candidate_list);
    }
  }

  pqueue_delete (candidate_list);
}