/** * @brief ChibiOS/RT initialization. * @details After executing this function the current instructions stream * becomes the main thread. * @pre Interrupts must disabled before invoking this function. * @post The main thread is created with priority @p NORMALPRIO and * interrupts are enabled. * * @special */ void chSysInit(void) { #if CH_DBG_ENABLE_STACK_CHECK == TRUE extern stkalign_t __main_thread_stack_base__; #endif port_init(); _scheduler_init(); _vt_init(); #if CH_CFG_USE_TM == TRUE _tm_init(); #endif #if CH_CFG_USE_MEMCORE == TRUE _core_init(); #endif #if CH_CFG_USE_HEAP == TRUE _heap_init(); #endif #if CH_DBG_STATISTICS == TRUE _stats_init(); #endif #if CH_DBG_ENABLE_TRACE == TRUE _dbg_trace_init(); #endif #if CH_CFG_NO_IDLE_THREAD == FALSE /* Now this instructions flow becomes the main thread.*/ setcurrp(_thread_init(&ch.mainthread, NORMALPRIO)); #else /* Now this instructions flow becomes the idle thread.*/ setcurrp(_thread_init(&ch.mainthread, IDLEPRIO)); #endif currp->p_state = CH_STATE_CURRENT; #if CH_DBG_ENABLE_STACK_CHECK == TRUE /* This is a special case because the main thread thread_t structure is not adjacent to its stack area.*/ currp->p_stklimit = &__main_thread_stack_base__; #endif chSysEnable(); /* Note, &ch_debug points to the string "main" if the registry is active, else the parameter is ignored.*/ chRegSetThreadName((const char *)&ch_debug); #if CH_CFG_NO_IDLE_THREAD == FALSE /* This thread has the lowest priority in the system, its role is just to serve interrupts in its context while keeping the lowest energy saving mode compatible with the system status.*/ (void) chThdCreateStatic(ch.idle_thread_wa, sizeof(ch.idle_thread_wa), IDLEPRIO, (tfunc_t)_idle_thread, NULL); #endif }
int main(int argc, char *argv[]) { mmatic *mm = mmatic_create(); mmatic *mmtmp = mmatic_create(); struct mg *mg; int i; /* * initialize and parse config */ mg = mmatic_zalloc(mm, sizeof(struct mg)); mg->mm = mm; mg->mmtmp = mmtmp; apply_defaults(mg); /* get my id number from hostname */ fetch_myid(mg); /* parse command line options */ if (parse_argv(mg, argc, argv)) return 1; /* parse configuration file options */ if (mg->options.conf_file) { if (parse_config(mg)) return 4; } /* * config syntax looks OK, see if it is feasible */ /* initialize random number generator */ srand48(mg->options.myid); /* init libevent */ mg->evb = event_init(); event_set_log_callback(libevent_log); /* init stats structures so mgstats_aggregator_add() used somewhere below works */ mgstats_init(mg); /* attach to raw interfaces */ if (mgi_init(mg, handle_packet) <= 0) { dbg(0, "no available interfaces found\n"); return 2; } /* parse traffic file */ if (parse_traffic(mg)) return 3; /* * all OK, prepare to start */ /* synchronize time reference point on all nodes */ mgc_sync(mg); /* schedule stats writing */ mgstats_start(mg); /* attach global stats */ _stats_init(mg); /* schedule heartbeat and disk sync signals */ heartbeat_init(mg); sync_init(mg); /* schedule the real work of this node: line generators */ for (i = 1; i < TRAFFIC_LINE_MAX; i++) { if (!(mg->lines[i] && mg->lines[i]->my)) continue; /* this will schedule first execution */ mgs_sleep(mg->lines[i], NULL); mg->running++; } /* suppose last frame was received now */ gettimeofday(&mg->last, NULL); /* * start! */ dbg(0, "Starting\n"); event_base_dispatch(mg->evb); /*******************************/ /* * cleanup after end of libevent loop */ event_base_free(mg->evb); mmatic_free(mg->mm); mmatic_free(mg->mmtmp); fflush(NULL); sync(); return 0; }
/** * @brief ChibiOS/RT initialization. * @details After executing this function the current instructions stream * becomes the main thread. * @pre Interrupts must disabled before invoking this function. * @post The main thread is created with priority @p NORMALPRIO and * interrupts are enabled. * * @special */ void chSysInit(void) { extern stkalign_t __main_thread_stack_base__; _scheduler_init(); _vt_init(); #if CH_DBG_SYSTEM_STATE_CHECK == TRUE ch.dbg.isr_cnt = (cnt_t)0; ch.dbg.lock_cnt = (cnt_t)0; #endif #if CH_CFG_USE_TM == TRUE _tm_init(); #endif #if CH_CFG_USE_MEMCORE == TRUE _core_init(); #endif #if CH_CFG_USE_HEAP == TRUE _heap_init(); #endif #if CH_DBG_STATISTICS == TRUE _stats_init(); #endif #if CH_DBG_TRACE_MASK != CH_DBG_TRACE_MASK_NONE _dbg_trace_init(); #endif #if CH_CFG_NO_IDLE_THREAD == FALSE /* Now this instructions flow becomes the main thread.*/ #if CH_CFG_USE_REGISTRY == TRUE currp = _thread_init(&ch.mainthread, (const char *)&ch_debug, NORMALPRIO); #else currp = _thread_init(&ch.mainthread, "main", NORMALPRIO); #endif #else /* Now this instructions flow becomes the idle thread.*/ currp = _thread_init(&ch.mainthread, "idle", IDLEPRIO); #endif /* Setting up the base address of the static main thread stack.*/ currp->stklimit = &__main_thread_stack_base__; /* Setting up the caller as current thread.*/ currp->state = CH_STATE_CURRENT; /* Port layer initialization last because it depend on some of the initializations performed before.*/ port_init(); #if CH_DBG_STATISTICS == TRUE /* Starting measurement for this thread.*/ chTMStartMeasurementX(&currp->stats); #endif /* It is alive now.*/ chSysEnable(); #if CH_CFG_NO_IDLE_THREAD == FALSE { static const thread_descriptor_t idle_descriptor = { "idle", THD_WORKING_AREA_BASE(ch_idle_thread_wa), THD_WORKING_AREA_END(ch_idle_thread_wa), IDLEPRIO, _idle_thread, NULL }; /* This thread has the lowest priority in the system, its role is just to serve interrupts in its context while keeping the lowest energy saving mode compatible with the system status.*/ (void) chThdCreate(&idle_descriptor); } #endif }