void silly_run(struct silly_config *config) { int i; int err; pthread_t pid[3]; R.run = 1; R.exit = 0; pthread_mutex_init(&R.mutex, NULL); pthread_cond_init(&R.cond, NULL); if (config->daemon) silly_daemon(config); signal_init(); silly_timer_init(); err = silly_socket_init(); if (err < 0) { fprintf(stderr, "%s socket init fail:%d\n", config->selfname, err); exit(-1); } silly_worker_init(); srand(time(NULL)); thread_create(&pid[0], thread_socket, NULL); thread_create(&pid[1], thread_timer, NULL); thread_create(&pid[2], thread_worker, config); fprintf(stdout, "%s is running ...\n", config->selfname); for (i = 0; i < 3; i++) pthread_join(pid[i], NULL); pthread_mutex_destroy(&R.mutex); pthread_cond_destroy(&R.cond); silly_worker_exit(); silly_timer_exit(); silly_socket_exit(); fprintf(stdout, "%s has already exit...\n", config->selfname); return ; }
int luaopen_silly(lua_State *L) { luaL_Reg tbl[] = { //core {"workid", _lworkid}, {"getenv", _lgetenv}, {"setenv", _lsetenv}, {"exit", _lexit}, //timer {"timerentry", _ltimer_entry}, {"timeradd", _ltimer_add}, {"timernow", _ltimer_now}, //socket {"socketentry", _lsocket_entry}, {"socketconnect", _lsocket_connect}, {"socketclose", _lsocket_close}, {"socketsend", _lsocket_send}, {"dropmessage", _ldrop}, //end {NULL, NULL}, }; luaL_checkversion(L); luaL_newmetatable(L, "silly_socket_data"); luaL_newmetatable(L, "silly_socket_packet"); lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_MAINTHREAD); lua_State *m = lua_tothread(L, -1); lua_pop(L, 1); lua_pushlightuserdata(L, (void *)m); lua_gettable(L, LUA_REGISTRYINDEX); struct silly_worker *w = lua_touserdata(L, -1); assert(w); silly_worker_message(w, _process_msg); silly_worker_exit(w, _exit); luaL_newlibtable(L, tbl); lua_pushlightuserdata(L, (void *)w); luaL_setfuncs(L, tbl, 1); return 1; }