void tea_init(void) { dosis_atexit("TEA", tea_fini); pthreadex_lock_init(&ttable_lock); pthreadex_lock_name(&ttable_lock, "ttable-lock"); if((ttable = calloc(cfg.maxthreads, sizeof(THREAD_WORK *))) == NULL) FAT("Cannot allocate memory for managing %d threads.", cfg.maxthreads); mqueue_init(); }
static void init(void) { bitmaps_init(); mqueue_init(); tube_init(); bus_init(); train_init(); settings_restore(); win_main_menu_create(); win_main_menu_show(true); analytics_track_event("app.start", " "); time_started = time(NULL); }
/* worker init */ int wtable_worker_init(WTABLE *w, int wid, int64_t childid, int status) { if(w && wid > 0 && wid < W_WORKER_MAX && childid) { memset(&(w->workers[wid]), 0, sizeof(WORKER)); w->workers[wid].msg_qid = mqueue_new(w->queue); w->workers[wid].task_qid = mqueue_new(w->queue); w->workers[wid].childid = childid; w->workers[wid].running = status; if(wid > 1) { w->workers[wid].queue = mqueue_init(); w->workers[wid].map = mtree_init(); mtree_reuse_all(w->workers[wid].map); } MUTEX_INIT(w->workers[wid].mmlock); DEBUG_LOGGER(w->logger, "init workers[%d] msg_qid[%d]",wid,w->workers[wid].msg_qid); } return 0; }
void router_init(Router *r) { int i=0, j=0; for(i=0;i < MAX_TOOLS;i++) { for(j=0;j < MAX_PATTERNS;j++) Glist_initialise(&r->comm_bindings[i].pattern_bindings[j]); r->comm_bindings[i].pattern_count=0; } r->registrar_ptr=0; r->message_counter=0; r->request_reply_counter=0; Glist_initialise(&r->request_tool_list); mqueue_init(&r->mqueue); pthread_mutexattr_init(&r->bindings_mutex_attr); pthread_mutexattr_settype(&r->bindings_mutex_attr,PTHREAD_MUTEX_RECURSIVE_NP); pthread_mutex_init(&r->bindings_mutex, &r->bindings_mutex_attr); }
/* * initialize wtable * */ WTABLE *wtable_init(char *dir) { char path[WT_PATH_MAX], *p = NULL; struct stat st = {0}; WTABLE *w = NULL; int n = 0; if(dir && (w = (WTABLE *)xmm_mnew(sizeof(WTABLE)))) { n = sprintf(path, "%s/%s", dir, WT_LOG_NAME); force_mkdir(path); p = path; LOGGER_INIT(w->logger, p); /* state */ n = sprintf(path, "%s/%s", dir, WT_STATE_NAME); if((w->statefd = open(path, O_CREAT|O_RDWR, 0644)) <= 0 || fstat(w->statefd, &st) != 0) { if(w->statefd > 0) close(w->statefd); FATAL_LOGGER(w->logger, "open state file[%s] failed, %s", path, strerror(errno)); _exit(-1); } else { if(st.st_size < sizeof(WSTATE) && ftruncate(w->statefd, sizeof(WSTATE)) != 0) { _exit(-1); } if((w->state = (WSTATE *)mmap(NULL, sizeof(WSTATE), PROT_READ|PROT_WRITE, MAP_SHARED, w->statefd, 0)) == NULL || w->state == (void *)-1) { FATAL_LOGGER(w->logger, "mmap state failed, %s", strerror(errno)); _exit(-1); } if(st.st_size < sizeof(WSTATE)) memset(((char *)w->state + st.st_size), 0, sizeof(WSTATE) - st.st_size); } w->workers = w->state->workers; memset(w->workers, 0, sizeof(WORKER) * W_WORKER_MAX); w->state->nworkers = 0; w->state->conn_total = 0; n = sprintf(path, "%s/%s", dir, WT_MDB_DIR); w->mdb = db_init(path, DB_USE_MMAP); /* mmtrie */ n = sprintf(path, "%s/%s", dir, WT_MAP_NAME); if((w->map = mmtrie_init(path)) == NULL) _exit(-1); /* appmap */ n = sprintf(path, "%s/%s", dir, WT_APPS_NAME); if((w->appmap = mmtree64_init(path)) == NULL) _exit(-1); mmtree64_use_all(w->appmap); /* logger & mutex & mtree & mqueue */ if((w->queue = mqueue_init()) == NULL) _exit(-1); if((w->mtree = mtree_init()) == NULL) _exit(-1); w->whitelist = mtree_new_tree(w->mtree); //MQ(w->queue)->logger = w->logger; MUTEX_INIT(w->mutex); } return w; }
int pcore_start (void **core_ptr, pcore_params_t *pcore_params, void *application) { int rc = 0, i; pb_core_t *core = NULL; if (!pcore_params || !application) { rc = -1; goto err; } *core_ptr = (pb_core_t *)calloc(1, sizeof (pb_core_t)); if (!*core_ptr) { rc = -1; goto err; } core = *core_ptr; pcore_init(core); if (pcore_params->mode != ePBYTE_SERVER && pcore_params->mode != ePBYTE_CLIENT) { rc = -2; goto err; } core->mode = pcore_params->mode; core->application = application; if (pcore_params->message_handler) core->message_handler = pcore_params->message_handler; if (pcore_params->error_handler) core->error_handler = pcore_params->error_handler; if (pcore_params->logger) { sprintf (core->logger.prefix, "PCORE_%s:", (pcore_params->mode == ePBYTE_SERVER)? "S":"C"); pb_log_set_logger (&core->logger, pcore_params->logger); pb_log_set_prio (&core->logger, pcore_params->logger_prio); } if (pcore_params->thread_init) { core->thread_init = pcore_params->thread_init; } else { pb_log (core, PBYTE_ERR, "Failed to start. " "Thread init callback not set"); rc = -2; goto err; } if (!pcore_params->addr || (pcore_params->port <= 0 || 65535 <= pcore_params->port) || (pcore_params->io_threads <= 0)) { pb_log (core, PBYTE_ERR, "Failed to start. " "Invalid params"); rc = -3; goto err; } if (pcore_params->worker_count <= 0 || pcore_params->worker_count >= MAX_WORKER_COUNT) { core->worker_count = DEF_WORKER_COUNT; } else { core->worker_count = pcore_params->worker_count; } sprintf(core->URL, "tcp://%s:%d", pcore_params->addr, pcore_params->port); core->ctx = zmq_init (pcore_params->io_threads); if (!core->ctx) { pb_log (core, PBYTE_ERR, "Failed to start. " "Can't init context: %s", zmq_strerror(zmq_errno())); rc = -4; goto err; } if (pcore_params->mode == ePBYTE_SERVER) { core->sock = zmq_socket (core->ctx, ZMQ_ROUTER); if (!core->sock) { pb_log (core, PBYTE_ERR, "Failed to start. " "Can't init socket: %s", zmq_strerror(zmq_errno())); rc = -5; goto err; } pb_log (core, PBYTE_INFO, "Try bind in '%s'", core->URL); rc = zmq_bind (core->sock, core->URL); if (rc != 0) { pb_log (core, PBYTE_ERR, "Failed to start. " "Bind failed: %s", zmq_strerror(zmq_errno())); rc = -6; goto err; } } else { core->sock = zmq_socket (core->ctx, ZMQ_DEALER); if (!core->sock) { pb_log (core, PBYTE_ERR, "Failed to start. " "Can't init socket: %s", zmq_strerror(zmq_errno())); rc = -5; goto err; } rc = zmq_setsockopt (core->sock, ZMQ_IDENTITY, pcore_params->identity.identity, strlen(pcore_params->identity.identity)); if (rc != 0) { pb_log (core, PBYTE_ERR, "Failed to set identity: %s", zmq_strerror(zmq_errno())); rc = -6; goto err; } pb_log (core, PBYTE_INFO, "Try connect to '%s'", core->URL); rc = zmq_connect (core->sock, core->URL); if (rc != 0) { pb_log (core, PBYTE_ERR, "Failed to start. " "Connect failed: %s", zmq_strerror(zmq_errno())); rc = -6; goto err; } } mqueue_init (&core->queue_in, MQUEUESIZE); mqueue_init (&core->queue_out, MQUEUESIZE); core->pollitem.socket = core->sock; core->pollitem.fd = 0; core->pollitem.events = ZMQ_POLLIN; core->pollitem.revents = 0; core->active = 1; rc = pthread_create(&core->thread_recv_send, NULL, &pcore_thread_recv_send, (void *)core); if(rc < 0) { pb_log (core, PBYTE_ERR, "Failed to start. " "Can't create recv/send switcher thread: %s", strerror(errno)); rc = -7; goto err; } for (i = 0; i < core->worker_count; i++) { rc = pthread_create (&core->thread_worker[i], NULL, &pcore_thread_worker, (void *)core); if(rc < 0) { pb_log (core, PBYTE_ERR, "Failed to start. " "Can't create primary worker%d thread: %s", i, strerror(errno)); rc = -8 - i; goto err; } } pb_log (core, PBYTE_INFO, "Start success. Worker count: %d", core->worker_count); return 0; err: switch(rc) { case -7: if (core && core->sock) zmq_unbind (core->sock, core->URL); case -6: if (core && core->sock) zmq_close (core->sock); case -5: if (core && core->ctx) zmq_ctx_destroy (core->ctx); case -4: case -3: case -2: free (core); case -1: break; default: pcore_stop (core); break; } return rc; }
int rms_init(void) { int ret; struct hostent *hp; rmsb->version = 0; mqueue_init( &(rmsb->mq_channels), 3); syncqueue_init( &(rmsb->mq_channels_tmp), 3); syncqueue_init( &(rmsb->mq_meta_msg_pool), 10); uthread_mutex_init(&mutx_rmscb); rmsb->state = 0; rmsb->hostname = (char*)malloc(64); if(gethostname(rmsb->hostname, 64) < 0) return -1; hp = gethostbyname(rmsb->hostname); rmsb->id = ((struct in_addr*)hp->h_addr)->s_addr; memcpy(rmsb->ip_addr,hp->h_addr, sizeof(ip_addr)); rms_peer_init(&rmsb->self, rmsb->hostname, rmsb->id); rmsb->initialized = RMS_INITIALIZED; rmsb->maxsock = 0; rmsb->n_channels = 0; rmsb->sock = 0; rmsb->listening = 0; rmsb->error = 0; rmsb->active = 1; //init threads if(uthread_init(& (rmsb->rms_mgr_thr), rms_manager, 1) < 0) { rmsb->error = RMS_ERR_THREADING; return -1; } if(uthread_init(& (rmsb->rms_snd_thr), rms_sender, 1) < 0) { rmsb->error = RMS_ERR_THREADING; return -1; } if(uthread_init(& (rmsb->rms_rcv_thr), rms_receiver, 1) < 0) { rmsb->error = RMS_ERR_THREADING; return -1; } //start threads if(uthread_start(&(rmsb->rms_mgr_thr), rmsb) < 0) { rmsb->error = RMS_ERR_THREADING; return -1; } if(uthread_start(&(rmsb->rms_snd_thr), rmsb) < 0) { rmsb->error = RMS_ERR_THREADING; return -1; } if(uthread_start(&(rmsb->rms_rcv_thr), rmsb) < 0) { rmsb->error = RMS_ERR_THREADING; return -1; } return 0; }