// 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; }
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; }