Exemplo n.º 1
0
/*! allocate and initialize queues, etc
  \param[in, out] context
  \returns 1 on success, 0 on failure
*/
int worker_init(struct thread_context *context) {
  assert(context);
  int rv;
  struct worker_data *data = context->data;

  rv = ip4_module_init(&data->ip4_module, &context->shared->ip4_module,
                        data->pcbmap_capacity);
  if (rv < 1)
    return 0;

  rv = tcp_module_init(&data->tcp_module, &context->shared->tcp_module,
                        data->tcp_pcb_capacity);
  if (rv < 1)
    return 0;

  rv = udp_module_init(&data->udp_module, &context->shared->udp_module,
                        data->udp_pcb_capacity);
  if (rv < 1)
    return 0;

  context->msg_q = squeue_new(data->msg_q_capacity, data->msg_q_elem_size);
  if (!context->msg_q)
    return 0;

  context->pkt_xmit_q = tqueue_new(data->xmit_q_transactions,
                                        data->xmit_q_actions_per_transaction);
  if (!context->pkt_xmit_q) {
    squeue_delete(&context->msg_q);
    return 0;
  }
  data->xmit_transaction = NULL;

  context->pkt_recv_q = tqueue_new(data->recv_q_transactions,
                                    data->recv_q_actions_per_transaction);
  if (!context->pkt_recv_q) {
    squeue_delete(&context->msg_q);
    tqueue_delete(&context->pkt_xmit_q);
    return 0;
  }

  rv = pktbuff_allocator_init(&data->pktbuff_allocator,
                                data->pktbuff_allocator_capacity,
                                FCL_ALLOCATOR_OOM_POLICY_NONE, 0,
                                packet_buffer_init);
  if (rv < 0) {
    squeue_delete(&context->msg_q);
    tqueue_delete(&context->pkt_xmit_q);
    tqueue_delete(&context->pkt_recv_q);
    return 0;
  }

  return 1;
}
Exemplo n.º 2
0
static int
_callback(lua_State *L) {
    struct snlua *lua = lua_touserdata(L, lua_upvalueindex(1));
    if (lua == NULL || lua->ctx == NULL) {
        return luaL_error(L, "Init skynet context first");
    }
    struct skynet_context * context = lua->ctx;

    luaL_checktype(L,1,LUA_TFUNCTION);
    lua_settop(L,1);
    lua_rawsetp(L, LUA_REGISTRYINDEX, _cb);

    lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_MAINTHREAD);
    lua_State *gL = lua_tothread(L,-1);

    struct stat * S = lua_newuserdata(L, sizeof(*S));
    memset(S, 0, sizeof(*S));
    S->L = gL;
    S->trace = trace_create();
    S->tq = tqueue_new();
    S->lua = lua;

    lua_createtable(L,0,1);
    lua_pushcfunction(L, _delete_stat);
    lua_setfield(L,-2,"__gc");
    lua_setmetatable(L, -2);

    lua_rawsetp(L, LUA_REGISTRYINDEX, _stat);

    skynet_callback(context, S, _cb);

    return 0;
}
Exemplo n.º 3
0
static void tComm_init ( COMM *sio )
{
	TQueue	*t	;

	t = tqueue_new(tComm_wakeup,tComm_finish) ;
	TASK_ASSERT(t,"TaskComm_init: memory not enough") ;

#if defined(CODE_PROTECTED)
	_lock_data(t,sizeof(TQueue)) ;
#endif

	/* links the two objects (TQueue*) & (COMM*) */
	t->cargo = (void*)sio ;
	sio->_qCOMM = (void*)t ;
	sio->taskFlag = (int*)&(t->data) ;
}
Exemplo n.º 4
0
/*! allocate and initialize queues, etc
  \param[in, out] context
  \returns 1 on success, 0 on failure
*/
int arpd_init(struct thread_context *context) {
  struct arpd_data *data;
  int rv;

  assert(context);
  data = context->data;

  context->msg_q = squeue_new(data->msg_q_capacity, data->msg_q_elem_size);
  if (!context->msg_q)
    return 0;

  context->pkt_xmit_q = cqueue_spsc_new(data->xmit_q_capacity,
                                        data->xmit_q_elem_size);
  if (!context->pkt_xmit_q) {
    squeue_delete(&context->msg_q);
    return 0;
  }

  rv = xmit_queue_init(context->pkt_xmit_q, &context->shared->inet_info->addr,
                        &context->shared->if_info->mac);
  if (!rv) {
    squeue_delete(&context->msg_q);
    return 0;
  }

  context->pkt_recv_q = tqueue_new(data->recv_q_transactions,
                                    data->recv_q_actions_per_transaction);
  if (!context->pkt_recv_q) {
    squeue_delete(&context->msg_q);
    cqueue_spsc_delete(&context->pkt_xmit_q);
    return 0;
  }

  data->arp_cache = arp_cache_new(context);
  if (!data->arp_cache) {
    squeue_delete(&context->msg_q);
    cqueue_spsc_delete(&context->pkt_xmit_q);
    tqueue_delete(&context->pkt_recv_q);
    return 0;
  }

  return 1;
}
Exemplo n.º 5
0
void timed_testfunc(int nthreads, long nitems) {
	tqueue_t *q;
	long i, res, expected;
	struct tqueue_options opts;

	memset(&opts, 0, sizeof(opts));
	opts.nthreads = nthreads;
	opts.worker = &ww;
	q = tqueue_new(&opts);
	EXPECT(tqueue_get(q, 0) == NULL, "expected NULL result on tqueue_get");

	for(i=0; i<nitems; i++) {
		EXPECT(tqueue_put(q, (void*)(i+1)) == 0, "tqueue_put failure");
	}

	expected = 0;
	for(i=res=expected=0; i<nitems; i++) {
		expected += i+2;
		res += (long)tqueue_get(q, 1);
	}

	EXPECT(res == expected, "sum: expected %ld, was %ld", expected, res);
	tqueue_free(q);
}