Пример #1
0
std::pair<void*, int> cast_graph::impl::cast(
    void* const p, class_id src, class_id target
  , class_id dynamic_id, void const* dynamic_ptr) const
{
    if (src == target)
        return std::make_pair(p, 0);

    if (src >= m_vertices.size() || target >= m_vertices.size())
        return std::pair<void*, int>((void*)0, -1);

    std::ptrdiff_t const object_offset =
        (char const*)dynamic_ptr - (char const*)p;

    cache_entry cached = m_cache.get(src, target, dynamic_id, object_offset);

    if (cached.first != cache::unknown)
    {
        if (cached.first == cache::invalid)
            return std::pair<void*, int>((void*)0, -1);
        return std::make_pair((char*)p + cached.first, cached.second);
    }

    std::queue<queue_entry> q;
    q.push(queue_entry(p, src, 0));

    boost::dynamic_bitset<> visited(m_vertices.size());

    while (!q.empty())
    {
        queue_entry const qe = q.front();
        q.pop();

        visited[qe.vertex_id] = true;
        vertex const& v = m_vertices[qe.vertex_id];

        if (v.id == target)
        {
            m_cache.put(
                src, target, dynamic_id, object_offset
              , (char*)qe.p - (char*)p, qe.distance
            );

            return std::make_pair(qe.p, qe.distance);
        }

        BOOST_FOREACH(edge const& e, v.edges)
        {
            if (visited[e.target])
                continue;
            if (void* casted = e.cast(qe.p))
                q.push(queue_entry(casted, e.target, qe.distance + 1));
        }
    }

    m_cache.put(src, target, dynamic_id, object_offset, cache::invalid, -1);

    return std::pair<void*, int>((void*)0, -1);
}
void coap_check_timeouts(coap_endpoint_mgr_t * p_endpoint_mgr, int i)
{
	struct queue_hdr_t *p = NULL, *next = NULL;
	coap_endpoint_t * p_endpoint = coap_get_endpoint(p_endpoint_mgr, i);
	if (NULL == p_endpoint)
	{
		return;
	}

	for (p = p_endpoint->m_send_queue.next; p != &p_endpoint->m_send_queue; p = next)
	{
		coap_pkt_t * p_pkt = queue_entry(p, coap_pkt_t, node);
		next = p->next;
		if (p_pkt->last_transmit_time + p_pkt->retransmit_interval < p_endpoint_mgr->m_endpoint_mgr_time_cache) 
		{
			if (p_pkt->retransmit_count < COAP_MAX_RETRANSMIT)
			{
				coap_log_debug_packet((char *)p_pkt);
				sendto(p_endpoint->m_servsock, (const char *)&p_pkt->hdr, p_pkt->packet_length, 0, (struct sockaddr *)&p_endpoint->m_servaddr, sizeof(p_endpoint->m_servaddr));
				
				p_pkt->last_transmit_time = coap_get_milliseconds();
				p_pkt->retransmit_interval <<= 1;
				p_pkt->retransmit_count++;
			}
			else
			{
				coap_free_endpoint(p_endpoint_mgr, i);
				coap_set_endpoint(p_endpoint_mgr, i, NULL);
				break;
			}
		}
	}

	return;
}
int coap_recv_queue_pop_node(coap_endpoint_t * p_endpoint, char * buffer, size_t len)
{
	coap_pkt_t * p_pkt = NULL;
	int ret = 0;

	assert(p_endpoint);
	assert(buffer);
	assert(len > 0);

	if (queue_is_empty(&p_endpoint->m_recv_queue))
	{
		return 0;
	}
	
	p_pkt = queue_entry(p_endpoint->m_recv_queue.next, coap_pkt_t, node);
	
	if (p_pkt->packet_length > len)
	{
		coap_log_debug_string("output buffer is too small to store the packet!\r\n");
		return COAP_RET_BUFFER_TOO_SMALL;
	}
	
	memcpy(buffer, &p_pkt->hdr, p_pkt->packet_length);
	ret = p_pkt->packet_length;
	queue_del(&p_pkt->node);
	free(p_pkt);
	p_endpoint->m_recv_queue_pkt_num--;
	
	return ret;
}
int coap_free_endpoint(coap_endpoint_mgr_t * p_endpoint_mgr, int i)
{
	int ret = 0;
	coap_pkt_t * p_pkt = NULL;
	
	coap_endpoint_t * p_endpoint = coap_get_endpoint(p_endpoint_mgr, i);
	if (NULL == p_endpoint)
	{
		return COAP_RET_INVALID_PARAMETERS;
	}

	while (!queue_is_empty(&p_endpoint->m_send_queue)) 
	{
		p_pkt = queue_entry(p_endpoint->m_send_queue.next, coap_pkt_t, node);
		queue_del(&p_pkt->node);
		free(p_pkt);
	}
	
	while (!queue_is_empty(&p_endpoint->m_recv_queue))
	{
		p_pkt = queue_entry(p_endpoint->m_recv_queue.next, coap_pkt_t, node);
		queue_del(&p_pkt->node);
		free(p_pkt);
	}

	mutex_uninit(&p_endpoint->m_send_mutex);
	mutex_uninit(&p_endpoint->m_recv_mutex);

	free(p_endpoint);

	ret = coap_set_endpoint(p_endpoint_mgr, i, NULL);
	if (ret < 0)
	{
		return ret;
	}

	return 0;
}
Пример #5
0
/**
 * Empties out a queue in the task queue by dequeuing and freeing
 * every task.
 */
static void tq_empty_queue(queue_t* q)
{
    do {
        tq_entry_t      *entry;
        queue_elem_t    *queue_elem;

        if (queue_pop_front (q, &queue_elem) == 0)
            break;

        entry = queue_entry (queue_elem, tq_entry_t, queue_elem);
        assert (entry != NULL);
        phoenix_mem_free (entry);
    } while (1);
}
Пример #6
0
static void *consumer(void *argc)
{
	data_queue_t *datas = (data_queue_t *)argc;
	while(1)
	{
		queue_body_t *pos = queue_get_front(datas->head);
		if( pos ==NULL )
		{
			/*
			 * here we do wait
			 */
			sleep(1);
			continue;
		}
		data_t *entry = queue_entry(pos,data_t,next);
		int l = entry->length;
		pomme_buffer_release(datas->buffer,entry->data_begin,l);
		free(entry);
	}
	return NULL;
}
Пример #7
0
static void * thread_routine(void *arg)
{
    routine_arg *info = arg; 
    info->tid = pthread_self();

    pomme_tpool_t *ptp = info->ptp;
    while(1)
    {
	sem_wait(&ptp->sem);	

	queue_body_t *wr = queue_get_front(ptp->workers);
	pomme_worker_t *w = queue_entry(wr,pomme_worker_t,next);
	/* The sem make this assert true */
	assert( w!=NULL );
	if(w->process == NULL )
	{
	    /* exit from the thread pool */
	    ptp->tids[info->rank] = 0;
	    thread_ids_t *ids = malloc(sizeof(thread_ids_t));

	    assert( ids != NULL );

	    memset(ids, 0, sizeof(thread_ids_t));
	    ids->rank = info->rank;
	    queue_push_back(ptp->finished,&ids->next); 

	    free(w);

	    break;
	}
	w->process(w->arg);	

	free(w->arg);
	free(w);
    }
    free(info);
    //TODO  use lock?
    return NULL; 
}
Пример #8
0
static int add_thread(pomme_tpool_t *ptp)
{
    int ret = 0;
    if( ptp->cur_thread_num >= ptp->max_thread_num )
    {
	debug("exceed max");
	return POMME_TOO_MANY_THREAD; 
    }
    queue_body_t *id = queue_get_front( ptp->finished );
    thread_ids_t *ids = queue_entry(id, thread_ids_t, next);
    assert( ids != NULL );
    routine_arg arg;
    arg.rank = ids->rank;
    if( ( ret = pthread_create(ptp->tids+ids->rank, NULL,
		    ptp->thread_routine, &arg )) < 0 )
    {
	debug("create thread %d fail %s",ret, strerror(ret));
    }else{
    	ptp->cur_thread_num++;
    }
    free(ids);
    return ret;
}
int coap_send_queue_del_node(coap_endpoint_t * p_endpoint, uint16 message_id)
{
	coap_pkt_t * p_pkt = NULL;
	queue_hdr_t * p = NULL;
	queue_hdr_t * next = NULL;

	assert(p_endpoint);
	
	for (p = p_endpoint->m_send_queue.next; p != &p_endpoint->m_send_queue; p = next)
	{
		p_pkt = queue_entry(p, coap_pkt_t, node);
		next = p->next;
		if (p_pkt->hdr.message_id == message_id) 
		{
			queue_del(&p_pkt->node);
			free(p_pkt);
			p_endpoint->m_send_queue_pkt_num--;		
			return 0;
		}
	}

	return -1;
}
Пример #10
0
static inline int tq_dequeue_normal_internal (
    taskQ_t* tq, task_t* task, int lgrp, int tid, dequeue_fn dequeue_fn)
{
    int             i, ret, index;
    queue_elem_t    *queue_elem;
    tq_entry_t      *entry;

    assert (task != NULL);

    mem_memset (task, 0, sizeof (task_t));

    index = (lgrp < 0) ? rand_r(&tq->seeds[tid]) : lgrp;
    index %= tq->num_queues;
    ret = (*dequeue_fn)(tq, index, tid, &queue_elem);

   /* Do task stealing if nothing on our queue.
      Cycle through all indexes until success or exhaustion */
    for (i = (index + 1) % tq->num_queues;
        (ret == 0) && (i != index);
        i = (i + 1) % tq->num_queues)
    {
        ret = (*dequeue_fn)(tq, i, tid, &queue_elem);
    }

    if (ret == 0) {
        /* There really is no more work. */
        return 0;
    }

    entry = queue_entry (queue_elem, tq_entry_t, queue_elem);
    assert (entry != NULL);

    mem_memcpy (task, &entry->task, sizeof (task_t));

    return 1;
}