Beispiel #1
0
static int
c_worker_thread_final_init(struct c_worker_ctx *w_ctx)
{
    cpu_set_t           cpu;
    char                ipc_path_str[64];
    struct timeval      tv = { C_PER_WORKER_TIMEO, 0 };
    int                 i = 0;
    int                 c_listener = 0;
    extern ctrl_hdl_t   ctrl_hdl;

    w_ctx->cmn_ctx.base = event_base_new();
    assert(w_ctx->cmn_ctx.base);

    snprintf(ipc_path_str, 63, "%s%d", C_IPC_PATH, w_ctx->thread_idx);
    w_ctx->main_wrk_conn.rd_fd = open(ipc_path_str, O_RDONLY | O_NONBLOCK);
    assert(w_ctx->main_wrk_conn.rd_fd > 0);

    w_ctx->main_wrk_conn.rd_event = event_new(w_ctx->cmn_ctx.base,
                                    w_ctx->main_wrk_conn.rd_fd,
                                    EV_READ|EV_PERSIST,
                                    c_worker_ipc_read, (void*)w_ctx);
    event_add(w_ctx->main_wrk_conn.rd_event, NULL);

    w_ctx->worker_timer_event = evtimer_new(w_ctx->cmn_ctx.base,
                                            c_per_worker_timer_event,
                                            (void *)w_ctx);
    evtimer_add(w_ctx->worker_timer_event, &tv);

    for (i = 0; i < ctrl_hdl.n_appthreads; i++) {
        nbq_init(&w_ctx->work_qs[i].q);
    }

    c_listener = c_server_socket_create(INADDR_ANY,
                                        C_APP_WQ_LISTEN_PORT+w_ctx->thread_idx);
    assert(c_listener);
    w_ctx->c_app_accept_event = event_new(w_ctx->cmn_ctx.base,
                                          c_listener,
                                          EV_READ|EV_PERSIST,
                                          c_app_wq_accept,
                                          (void*)w_ctx);
    event_add(w_ctx->c_app_accept_event, NULL);

    /* Set cpu affinity */
    CPU_ZERO(&cpu);
    CPU_SET(w_ctx->thread_idx, &cpu);
    pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpu);

    w_ctx->cmn_ctx.run_state = THREAD_STATE_RUNNING;

    return 0;
}
Beispiel #2
0
// This is where the switch threads get created. 
static int
c_main_thread_final_init(struct c_main_ctx *m_ctx)
{
    evutil_socket_t             c_listener;
    struct c_worker_ctx         *w_ctx, **w_ctx_slot;
    struct c_vty_ctx            *vty_ctx;
    struct c_app_ctx            *app_ctx, **app_ctx_slot;
    char                        ipc_path_str[64];
    int                         thread_idx;
    ctrl_hdl_t                  *ctrl_hdl = m_ctx->cmn_ctx.c_hdl;
    struct thread_alloc_args    t_args = { 0, 0, 
                                           THREAD_WORKER, 
                                           0, 
                                           m_ctx->cmn_ctx.c_hdl };

    // Kajal: No event handling
    //m_ctx->cmn_ctx.base = event_base_new();
    //assert(m_ctx->cmn_ctx.base); 

//    m_ctx->worker_pool = calloc(m_ctx->nthreads, sizeof(void *));
//    assert(m_ctx->worker_pool);

/*
    // Worker thread creation 
    for (thread_idx = 0; thread_idx < m_ctx->nthreads; thread_idx++) {
	
	// Indexing the worker thread in the main_ctx
        w_ctx_slot = c_tid_to_ctx_slot(m_ctx, thread_idx);

        t_args.thread_idx = thread_idx;
        w_ctx = c_alloc_thread_ctx(&t_args);
        assert(w_ctx);

        *w_ctx_slot = w_ctx;
        
	// Create Named pipes for IPC communication with main thread
        memset(ipc_path_str, 0, sizeof(ipc_path_str));
        snprintf(ipc_path_str, 63, "%s%d", C_IPC_PATH, thread_idx); 
        if (mkfifo(ipc_path_str, S_IRUSR | S_IWUSR | S_IWGRP) == -1
            && errno != EEXIST) {
            perror("");
            assert(0);
        }

	// Worker context is created for each thread, but, we will not
	// be using it.
        pthread_create(&w_ctx->cmn_ctx.thread, NULL, c_thread_main, w_ctx);

	// Save the fd for the named pipes in the worker context
	// Main ctx has the w_ctx saved. So main thread knows which is the 
	// fd on which the IPC communication needs to begin 
        w_ctx->main_wrk_conn.conn_type = C_CONN_TYPE_FILE;
        w_ctx->main_wrk_conn.fd = open(ipc_path_str, O_WRONLY);
        assert(w_ctx->main_wrk_conn.fd > 0);

	// Worker thread context is saved in the main_ctx and the controller handle
        ctrl_hdl->worker_ctx_list[thread_idx] = (void *)w_ctx;

    }

    // Switch listener 
    // Kajal: No need to create a socket to listen on. 
    // Library will use the callback defined in MUL to do processing.
    c_listener = c_server_socket_create(INADDR_ANY, C_LISTEN_PORT);

    assert(c_listener > 0);
    m_ctx->c_accept_event = event_new(m_ctx->cmn_ctx.base, -1, 
                                      EV_READ|EV_PERSIST,
                                      cc_of_new_conn_event_handler, (void*)m_ctx);
    event_add(m_ctx->c_accept_event, NULL);
*/

    m_ctx->app_pool = calloc(m_ctx->n_appthreads, sizeof(void *));
    assert(m_ctx->app_pool);

    /* Application thread creation */
    for (thread_idx = 0; thread_idx < m_ctx->n_appthreads; thread_idx++) {
        app_ctx_slot = c_tid_to_app_ctx_slot(m_ctx, thread_idx);

        t_args.thread_type = THREAD_APP;
        t_args.thread_idx = thread_idx;
        app_ctx = c_alloc_thread_ctx(&t_args);
        assert(app_ctx);

        *app_ctx_slot = app_ctx;

        memset(ipc_path_str, 0, sizeof(ipc_path_str));
        snprintf(ipc_path_str, 63, "%s%d", C_IPC_APP_PATH, thread_idx); 
        if (mkfifo(ipc_path_str, S_IRUSR | S_IWUSR | S_IWGRP) == -1
            && errno != EEXIST) {
            perror("");
            assert(0);
        }

        pthread_create(&app_ctx->cmn_ctx.thread, NULL, c_thread_main, app_ctx);

        app_ctx->main_wrk_conn.conn_type = C_CONN_TYPE_FILE;
        app_ctx->main_wrk_conn.fd = open(ipc_path_str, O_WRONLY);
        assert(app_ctx->main_wrk_conn.fd > 0);
    }

    /* VTY thread creation */    
    t_args.thread_type = THREAD_VTY;
    vty_ctx = c_alloc_thread_ctx(&t_args);
    assert(vty_ctx);
    pthread_create(&vty_ctx->cmn_ctx.thread, NULL, c_thread_main, vty_ctx);


    /* Application listener */
    c_listener = c_server_socket_create(INADDR_ANY, C_APP_LISTEN_PORT);
    assert(c_listener);
    m_ctx->c_app_accept_event = event_new(m_ctx->cmn_ctx.base, c_listener, 
                                          EV_READ|EV_PERSIST,
                                          c_app_accept, (void*)m_ctx);
    event_add(m_ctx->c_app_accept_event, NULL);

    c_listener = c_server_socket_create(INADDR_ANY, C_APP_AUX_LISTEN_PORT);
    assert(c_listener);
    m_ctx->c_app_aux_accept_event= event_new(m_ctx->cmn_ctx.base, c_listener, 
                                          EV_READ|EV_PERSIST,
                                          c_aux_app_accept, (void*)m_ctx);
    event_add(m_ctx->c_app_aux_accept_event, NULL);

    m_ctx->cmn_ctx.run_state = THREAD_STATE_RUNNING;

    c_set_thread_dfl_affinity();

    c_log_debug("%s: running tid(%u)", __FUNCTION__, (unsigned int)pthread_self());
    return 0;
}
Beispiel #3
0
static int
c_main_thread_final_init(struct c_main_ctx *m_ctx)
{
    evutil_socket_t             c_listener;
    struct c_worker_ctx         *w_ctx, **w_ctx_slot;
    struct c_vty_ctx            *vty_ctx;
    struct c_app_ctx            *app_ctx, **app_ctx_slot;
    char                        ipc_path_str[64];
    int                         thread_idx;
    ctrl_hdl_t                  *ctrl_hdl = m_ctx->cmn_ctx.c_hdl;
    struct thread_alloc_args    t_args = { 0, 0,
               THREAD_WORKER,
               0,
               m_ctx->cmn_ctx.c_hdl
    };

    m_ctx->cmn_ctx.base = event_base_new();
    assert(m_ctx->cmn_ctx.base);

    m_ctx->worker_pool = calloc(m_ctx->nthreads, sizeof(void *));
    assert(m_ctx->worker_pool);

    m_ctx->app_pool = calloc(m_ctx->n_appthreads, sizeof(void *));
    assert(m_ctx->app_pool);

    /* Worker thread creation */
    for (thread_idx = 0; thread_idx < m_ctx->nthreads; thread_idx++) {
        w_ctx_slot = c_tid_to_ctx_slot(m_ctx, thread_idx);

        t_args.thread_idx = thread_idx;
        w_ctx = c_alloc_thread_ctx(&t_args);
        assert(w_ctx);

        *w_ctx_slot = w_ctx;

        memset(ipc_path_str, 0, sizeof(ipc_path_str));
        snprintf(ipc_path_str, 63, "%s%d", C_IPC_PATH, thread_idx);
        if (mkfifo(ipc_path_str, S_IRUSR | S_IWUSR | S_IWGRP) == -1
                && errno != EEXIST) {
            perror("");
            assert(0);
        }

        pthread_create(&w_ctx->cmn_ctx.thread, NULL, c_thread_main, w_ctx);

        w_ctx->main_wrk_conn.conn_type = C_CONN_TYPE_FILE;
        w_ctx->main_wrk_conn.fd = open(ipc_path_str, O_WRONLY);
        assert(w_ctx->main_wrk_conn.fd > 0);

        ctrl_hdl->worker_ctx_list[thread_idx] = (void *)w_ctx;

    }

    /* Application thread creation */
    for (thread_idx = 0; thread_idx < m_ctx->n_appthreads; thread_idx++) {
        app_ctx_slot = c_tid_to_app_ctx_slot(m_ctx, thread_idx);

        t_args.thread_type = THREAD_APP;
        t_args.thread_idx = thread_idx;
        app_ctx = c_alloc_thread_ctx(&t_args);
        assert(app_ctx);

        *app_ctx_slot = app_ctx;

        memset(ipc_path_str, 0, sizeof(ipc_path_str));
        snprintf(ipc_path_str, 63, "%s%d", C_IPC_APP_PATH, thread_idx);
        if (mkfifo(ipc_path_str, S_IRUSR | S_IWUSR | S_IWGRP) == -1
                && errno != EEXIST) {
            perror("");
            assert(0);
        }

        pthread_create(&app_ctx->cmn_ctx.thread, NULL, c_thread_main, app_ctx);

        app_ctx->main_wrk_conn.conn_type = C_CONN_TYPE_FILE;
        app_ctx->main_wrk_conn.fd = open(ipc_path_str, O_WRONLY);
        assert(app_ctx->main_wrk_conn.fd > 0);

    }

    /* VTY thread creation */
    t_args.thread_type = THREAD_VTY;
    vty_ctx = c_alloc_thread_ctx(&t_args);
    assert(vty_ctx);
    pthread_create(&vty_ctx->cmn_ctx.thread, NULL, c_thread_main, vty_ctx);


    /* Switch listener */
    c_listener = c_server_socket_create(INADDR_ANY, ctrl_hdl->c_port);
    assert(c_listener > 0);
    m_ctx->c_accept_event = event_new(m_ctx->cmn_ctx.base, c_listener,
                                      EV_READ|EV_PERSIST,
                                      c_accept, (void*)m_ctx);
    event_add(m_ctx->c_accept_event, NULL);

    /* Application listener */
    c_listener = c_server_socket_create(INADDR_ANY, C_APP_LISTEN_PORT);
    assert(c_listener);
    m_ctx->c_app_accept_event = event_new(m_ctx->cmn_ctx.base, c_listener,
                                          EV_READ|EV_PERSIST,
                                          c_app_accept, (void*)m_ctx);
    event_add(m_ctx->c_app_accept_event, NULL);

    c_listener = c_server_socket_create(INADDR_ANY, C_APP_AUX_LISTEN_PORT);
    assert(c_listener);
    m_ctx->c_app_aux_accept_event= event_new(m_ctx->cmn_ctx.base, c_listener,
                                   EV_READ|EV_PERSIST,
                                   c_aux_app_accept, (void*)m_ctx);
    event_add(m_ctx->c_app_aux_accept_event, NULL);

    m_ctx->cmn_ctx.run_state = THREAD_STATE_RUNNING;

    c_set_thread_dfl_affinity();

    c_log_debug("%s: running tid(%u)", __FUNCTION__, (unsigned int)pthread_self());
    return 0;
}
Beispiel #4
0
        serv_inst->conn.wr_event = event_new(serv_inst->ev_base,
                                               serv_inst->conn.fd,
                                               EV_WRITE, //|EV_PERSIST,
                                               c_service_write_event, 
                                               &serv_inst->conn);
        event_add((struct event *)(serv_inst->conn.rd_event), NULL);
    }
}


static inline int
c_service_server_sock_init(mul_service_t *serv, char *server UNUSED)
{
    c_log_err("Service Create %s:%d", serv->service_name, serv->serv_port);

    serv->conn.fd = c_server_socket_create(INADDR_ANY, serv->serv_port);
    if (serv->conn.fd <= 0) { 
        return -1;
    }

    serv->conn.rd_event = event_new(serv->ev_base, serv->conn.fd,
                                    EV_READ|EV_PERSIST,
                                    c_service_accept, (void*)serv);

    event_add(serv->conn.rd_event, NULL);

    return 0;
}

static int
c_service_client_sock_init(mul_service_t *serv, const char *__server)