Example #1
0
void worker_main(int _worker_n)
{
    worker_n = _worker_n;
    attach_on_exit(on_exit_handler);

    if(is_daemon == 1 && !getarg("gcore")) {
        set_process_user(/*user*/ NULL, /*group*/ NULL);
    }

    if(!init_ntoa_cache() || !init_access_cache()) {
        LOGF(ERR, "Couldn't init rbtree");
        exit(1);
    }

    init_mime_types();
    shm_serv_status = _shm_serv_status->p;

    if(luaL_loadfile(_L, "core.lua")) {
        LOGF(ERR, "Couldn't load file: %s", lua_tostring(_L, -1));
        exit(1);
    }

    int mrkey = luaL_ref(_L, LUA_REGISTRYINDEX);

    lua_pushstring(_L, "__main");
    lua_rawgeti(_L, LUA_REGISTRYINDEX, mrkey);
    lua_rawset(_L, LUA_GLOBALSINDEX);

    int thread_count = 1000;

    if(getarg("thread")) {
        thread_count = atoi(getarg("thread"));

        if(thread_count < 100) {
            thread_count = 100;

        } else if(thread_count > 10000) {
            thread_count = 10000;
        }
    }

    init_lua_threads(_L, thread_count);

    /// 进入 loop 处理循环
    loop_fd = se_create(4096);
    set_loop_fd(loop_fd, _worker_n); // for coevent module
    se_accept(loop_fd, server_fd, be_accept);

    if(ssl_server_fd > 0) {
        se_accept(loop_fd, ssl_server_fd, be_ssl_accept);
    }

    se_loop(loop_fd, 10, other_simple_jobs); // loop

    exit(0);
}
Example #2
0
void worker_main(int _worker_n)
{
    worker_n = _worker_n;
    attach_on_exit(on_exit_handler);
    set_process_user(/*user*/ NULL, /*group*/ NULL);

    /// 进入 loop 处理循环
    loop_fd = se_create(4096);
    se_accept(loop_fd, server_fd, be_accept);

    se_loop(loop_fd, 10, other_simple_jobs); // loop

    exit(0);
}
Example #3
0
void network_worker ( void *_process_func, int process_count, int process_at )
{
    if ( sizeof ( epdata_t ) != 4096 ) {
        printf ( "warning sizof(epdata_t) = %ld\n", sizeof ( epdata_t ) );    // must be 4096
    }

    signal ( SIGPIPE, SIG_IGN );

    _shm_serv_status = shm_malloc ( sizeof ( serv_status_t ) );

    if ( _shm_serv_status == NULL ) {
        perror ( "shm malloc failed\n" );
        signal ( SIGHUP, SIG_IGN );
        exit ( 1 );
    }

    shm_serv_status = _shm_serv_status->p;
    memcpy ( shm_serv_status, &serv_status, sizeof ( serv_status_t ) );

    process_func = _process_func;

    init_mime_types();

    loop_fd = se_create ( EPD_POOL_SIZE );
    set_loop_fd ( loop_fd, process_count );

    se_ptr_t *se = se_add ( loop_fd, server_fd, NULL );

    se_be_read ( se, network_be_accept );

    while ( 1 ) {

        se_loop ( loop_fd, EPOLL_WAITOUT, without_jobs );

        if ( checkProcessForExit() || has_error_for_exit ) {
            break;
        }
    }
}
int lua_f_startloop(lua_State *L)
{
    if(_loop_fd == -1) {
        _loop_fd = se_create(4096);
        attach_on_exit(on_exit_handler);
        atexit(on_exit_handler);
    }

    luaL_argcheck(L, lua_isfunction(L, 1)
                  && !lua_iscfunction(L, 1), 1, "Lua function expected");
    job_L = lua_newthread(L);
    /*
        lua_pushvalue(L, LUA_GLOBALSINDEX);
        lua_xmove(L, job_L, 1);
        lua_replace(job_L, LUA_GLOBALSINDEX);
        lua_xmove(LM, L, 1);*/

    lua_pushvalue(L, 1);    /* copy entry function to top of L*/
    lua_xmove(L, job_L, 1);    /* move entry function from L to co */

    lua_pushvalue(L, 1);    /* move function to top */
    lua_xmove(L, job_L, 1);    /* move function from L to job_L */

    if(lua_resume(job_L, 0) == LUA_ERRRUN) {
        lua_gc(job_L, LUA_GCCOLLECT, 0);

        if(lua_isstring(job_L, -1)) {
            luaL_error(L, lua_tostring(job_L, -1));
        }

        lua_pop(job_L, 1);
    }

    LM = job_L;

    se_loop(_loop_fd, 10, _do_other_jobs);

    return 0;
}