src/dpdk/lib/librte_eal/common/malloc_mp.c
int
request_to_primary(struct malloc_mp_req *user_req)
{
	struct rte_mp_msg msg;
	struct malloc_mp_req *msg_req = (struct malloc_mp_req *)msg.param;
	struct mp_request *entry;
	struct timespec ts;
	struct timeval now;
	int ret;

	memset(&msg, 0, sizeof(msg));
	memset(&ts, 0, sizeof(ts));

	pthread_mutex_lock(&mp_request_list.lock);

	entry = malloc(sizeof(*entry));
	if (entry == NULL) {
		RTE_LOG(ERR, EAL, "Cannot allocate memory for request\n");
		goto fail;
	}

	memset(entry, 0, sizeof(*entry));

	if (gettimeofday(&now, NULL) < 0) {
		RTE_LOG(ERR, EAL, "Cannot get current time\n");
		goto fail;
	}

	ts.tv_nsec = (now.tv_usec * 1000) % 1000000000;
	ts.tv_sec = now.tv_sec + MP_TIMEOUT_S +
			(now.tv_usec * 1000) / 1000000000;

	/* initialize the request */
	pthread_cond_init(&entry->cond, NULL);

	msg.num_fds = 0;
	msg.len_param = sizeof(*msg_req);
	strlcpy(msg.name, MP_ACTION_REQUEST, sizeof(msg.name));

	/* (attempt to) get a unique id */
	user_req->id = get_unique_id();

	/* copy contents of user request into the message */
	memcpy(msg_req, user_req, sizeof(*msg_req));

	if (rte_mp_sendmsg(&msg)) {
		RTE_LOG(ERR, EAL, "Cannot send message to primary\n");
		goto fail;
	}

	/* copy contents of user request into active request */
	memcpy(&entry->user_req, user_req, sizeof(*user_req));

	/* mark request as in progress */
	entry->state = REQ_STATE_ACTIVE;

	TAILQ_INSERT_TAIL(&mp_request_list.list, entry, next);

	/* finally, wait on timeout */
	do {
		ret = pthread_cond_timedwait(&entry->cond,
				&mp_request_list.lock, &ts);
	} while (ret != 0 && ret != ETIMEDOUT);

	if (entry->state != REQ_STATE_COMPLETE) {
		RTE_LOG(ERR, EAL, "Request timed out\n");
		ret = -1;
	} else {
		ret = 0;
		user_req->result = entry->user_req.result;
	}
	TAILQ_REMOVE(&mp_request_list.list, entry, next);
	free(entry);

	pthread_mutex_unlock(&mp_request_list.lock);
	return ret;
fail:
	pthread_mutex_unlock(&mp_request_list.lock);
	free(entry);
	return -1;
}
/* this is a synchronous wrapper around a bunch of asynchronous requests to
 * primary process. this will initiate a request and wait until responses come.
 */
this is a synchronous wrapper around a bunch of asynchronous requests to
primary process. this will initiate a request and wait until responses come.
this is a synchronous wrapper around a bunch of asynchronous requests to primary process . this will initiate a request and wait until responses come .
int
request_to_primary(struct malloc_mp_req *user_req)
{
	struct rte_mp_msg msg;
	struct malloc_mp_req *msg_req = (struct malloc_mp_req *)msg.param;
	struct mp_request *entry;
	struct timespec ts;
	struct timeval now;
	int ret;
	memset(&msg, 0, sizeof(msg));
	memset(&ts, 0, sizeof(ts));
	pthread_mutex_lock(&mp_request_list.lock);
	entry = malloc(sizeof(*entry));
	if (entry == NULL) {
		RTE_LOG(ERR, EAL, "Cannot allocate memory for request\n");
		goto fail;
	}
	memset(entry, 0, sizeof(*entry));
	if (gettimeofday(&now, NULL) < 0) {
		RTE_LOG(ERR, EAL, "Cannot get current time\n");
		goto fail;
	}
	ts.tv_nsec = (now.tv_usec * 1000) % 1000000000;
	ts.tv_sec = now.tv_sec + MP_TIMEOUT_S +
			(now.tv_usec * 1000) / 1000000000;
	pthread_cond_init(&entry->cond, NULL);
	msg.num_fds = 0;
	msg.len_param = sizeof(*msg_req);
	strlcpy(msg.name, MP_ACTION_REQUEST, sizeof(msg.name));
	user_req->id = get_unique_id();
	memcpy(msg_req, user_req, sizeof(*msg_req));
	if (rte_mp_sendmsg(&msg)) {
		RTE_LOG(ERR, EAL, "Cannot send message to primary\n");
		goto fail;
	}
	memcpy(&entry->user_req, user_req, sizeof(*user_req));
	entry->state = REQ_STATE_ACTIVE;
	TAILQ_INSERT_TAIL(&mp_request_list.list, entry, next);
	do {
		ret = pthread_cond_timedwait(&entry->cond,
				&mp_request_list.lock, &ts);
	} while (ret != 0 && ret != ETIMEDOUT);
	if (entry->state != REQ_STATE_COMPLETE) {
		RTE_LOG(ERR, EAL, "Request timed out\n");
		ret = -1;
	} else {
		ret = 0;
		user_req->result = entry->user_req.result;
	}
	TAILQ_REMOVE(&mp_request_list.list, entry, next);
	free(entry);
	pthread_mutex_unlock(&mp_request_list.lock);
	return ret;
fail:
	pthread_mutex_unlock(&mp_request_list.lock);
	free(entry);
	return -1;
}
int request_to_primary ( struct malloc_mp_req * user_req ) { struct rte_mp_msg msg ; struct malloc_mp_req * msg_req = ( struct malloc_mp_req * ) msg . param ; struct mp_request * entry ; struct timespec ts ; struct timeval now ; int ret ; memset ( & msg , 0 , sizeof ( msg ) ) ; memset ( & ts , 0 , sizeof ( ts ) ) ; pthread_mutex_lock ( & mp_request_list . lock ) ; entry = malloc ( sizeof ( * entry ) ) ; if ( entry == NULL ) { RTE_LOG ( ERR , EAL , " \n " ) ; goto fail ; } memset ( entry , 0 , sizeof ( * entry ) ) ; if ( gettimeofday ( & now , NULL ) < 0 ) { RTE_LOG ( ERR , EAL , " \n " ) ; goto fail ; } ts . tv_nsec = ( now . tv_usec * 1000 ) % 1000000000 ; ts . tv_sec = now . tv_sec + MP_TIMEOUT_S + ( now . tv_usec * 1000 ) / 1000000000 ; pthread_cond_init ( & entry -> cond , NULL ) ; msg . num_fds = 0 ; msg . len_param = sizeof ( * msg_req ) ; strlcpy ( msg . name , MP_ACTION_REQUEST , sizeof ( msg . name ) ) ; user_req -> id = get_unique_id ( ) ; memcpy ( msg_req , user_req , sizeof ( * msg_req ) ) ; if ( rte_mp_sendmsg ( & msg ) ) { RTE_LOG ( ERR , EAL , " \n " ) ; goto fail ; } memcpy ( & entry -> user_req , user_req , sizeof ( * user_req ) ) ; entry -> state = REQ_STATE_ACTIVE ; TAILQ_INSERT_TAIL ( & mp_request_list . list , entry , next ) ; do { ret = pthread_cond_timedwait ( & entry -> cond , & mp_request_list . lock , & ts ) ; } while ( ret != 0 && ret != ETIMEDOUT ) ; if ( entry -> state != REQ_STATE_COMPLETE ) { RTE_LOG ( ERR , EAL , " \n " ) ; ret = -1 ; } else { ret = 0 ; user_req -> result = entry -> user_req . result ; } TAILQ_REMOVE ( & mp_request_list . list , entry , next ) ; free ( entry ) ; pthread_mutex_unlock ( & mp_request_list . lock ) ; return ret ; fail : pthread_mutex_unlock ( & mp_request_list . lock ) ; free ( entry ) ; return -1 ; }
this is a synchronous wrapper around a bunch of asynchronous requests to
 primary process.
this is a synchronous wrapper around a bunch of asynchronous requests to primary process .
/* initialize the request */ /* (attempt to) get a unique id */ /* copy contents of user request into the message */ /* copy contents of user request into active request */ /* mark request as in progress */ /* finally, wait on timeout */