/* * Initialize the system to run the first minithread at * mainproc(mainarg). This procedure should be called from your * main program with the callback procedure and argument specified * as arguments. */ void minithread_system_initialize(proc_t mainproc, arg_t mainarg) { runnable_queue = multilevel_queue_new(MAX_LEVELS); stopped_queue = queue_new(); scheduler_thread = scheduler_thread_create(); assert(scheduler_thread); running_thread = scheduler_thread; int res = network_initialize((network_handler_t) network_handler); assert(res == 0); alarm_system_initialize(); minimsg_initialize(); minisocket_initialize(); reaper_thread = minithread_create(clean_stopped_threads, NULL); minithread_fork(mainproc, mainarg); interrupt_level_t prev_level = set_interrupt_level(ENABLED); minithread_clock_init(PERIOD * MILLISECOND, clock_handler); while (1) { if (!multilevel_queue_is_empty(runnable_queue)) { minithread_yield(); } } set_interrupt_level(prev_level); multilevel_queue_free(runnable_queue); queue_free(stopped_queue); }
/* * Initialization. * * minithread_system_initialize: * This procedure should be called from your C main procedure * to turn a single threaded UNIX process into a multithreaded * program. * * Initialize any private data structures. * Create the idle thread. * Fork the thread which should call mainproc(mainarg) * Start scheduling. * */ int minithread_system_initialize(proc_t mainproc, arg_t mainarg) { dbgprintf("Initializing minithread system...\n"); ready_queue = multilevel_queue_new(2, 0); stop_queue = queue_new(); dead_queue = queue_new(); if (!ready_queue || !stop_queue || !dead_queue ) { return 0; } last_id = 0; current_quanta_end = 0; current = idle = minithread_create(NULL, NULL); minithread_fork(mainproc, mainarg); // initialize alarm subsystem alarm_system_initialize(); minithread_clock_init(minithread_clock_handler); // interrupts currently disabled // schedule will call switch, which will enable interrupts minithread_schedule(); // begin idle thread body minithread_idle(); // system is shutting down now // stop clock interrupts // minithread_clock_stop(); // cleanup alarm system alarm_system_cleanup(); // cleanup thread system minithread_system_cleanup(); return 0; }