bool timer (struct message *m) { if (m != NULL) { if (verbose_flag) printf("timer receives %d\n", m->type); if (m->type == INIT_MESSAGE_TYPE) { struct timer_init *init = (struct timer_init*) m->data; delay = init->delay; verbose_flag = init->verbose_flag; int i; for (i = 0; i < N_TIMER; i++) { back[i] = -1; } } else if (m->type == ALARM_MESSAGE_TYPE) { struct alarm *alrm = (struct alarm *) m->data; timer_timeout[alrm->id] = alrm->timeout; inbox[alrm->id] = alrm->inbox; push_heap(alrm->id); check_timers(); } else { assert(m->type == STOP_MESSAGE_TYPE); } } else { if (verbose_flag) printf("timer receives NULL\n"); // the minimum time that can be asked is MIN(delay, 3*delay) = delay int time_to_sleep = delay; if (n > 0) { time_to_sleep = MIN(time_to_sleep, timer_timeout[top_heap()]); } int err = usleep(time_to_sleep); if (err != 0) { myperror("usleep"); } else { check_timers(); } } return n == 0; }
int main(void) { unsigned int i; init_rand(); for (i = 0; i < NUM_TIMERS; i++) { init_timer(&timers[i], expiry, NULL); schedule_timer(&timers[i], random() >> rand_shift); } count = NUM_TIMERS; while(count) { check_timers(false); stamp++; } return 0; }
static PyObject* PyEvents_WaitForEvent(PyObject* unused, PyObject* args) { int nfds; fd_set readfds; fd_set writefds; fd_set errorfds; struct timeval timeout; unsigned long waittime; long milliseconds; long result = 0; if (!PyArg_ParseTuple(args, "k", &milliseconds)) return NULL; waittime = check_timers(); if (waittime > 0) { if (waittime < milliseconds) milliseconds = waittime; timeout.tv_sec = milliseconds / 1000; timeout.tv_usec = 1000 * (milliseconds % 1000); nfds = set_fds(&readfds, &writefds, &errorfds); if (select(nfds, &readfds, &writefds, &errorfds, &timeout)==-1) return PyErr_SetFromErrno(PyExc_RuntimeError); } return PyInt_FromLong(result); }
/* Called once a minute. */ static void core_minutely() { check_tcl_time(&nowtm); check_timers(&timer); flush_and_check_logs(LOGS_MINUTELY); }
/* Called once a second. */ static void core_secondly() { static int cnt = 0; int miltime; check_timers(&utimer); /* Secondly timers */ cnt++; if (cnt >= 10) { /* Every 10 seconds */ cnt = 0; check_expired_dcc(); if (con_chan && !backgrd) { dprintf(DP_STDOUT, "\033[2J\033[1;1H"); tell_verbose_status(DP_STDOUT); do_module_report(DP_STDOUT, 0, "server"); do_module_report(DP_STDOUT, 0, "channels"); tell_mem_status(DP_STDOUT); } } egg_memcpy(&nowtm, localtime(&now), sizeof(struct tm)); if (nowtm.tm_min != lastmin) { int i = 0; /* Once a minute */ lastmin = (lastmin + 1) % 60; call_hook(HOOK_MINUTELY); check_expired_ignores(); autolink_cycle(NULL); /* Attempt autolinks. */ /* In case for some reason more than 1 min has passed: */ while (nowtm.tm_min != lastmin) { /* Timer drift, dammit */ debug2("timer: drift (lastmin=%d, now=%d)", lastmin, nowtm.tm_min); i++; lastmin = (lastmin + 1) % 60; call_hook(HOOK_MINUTELY); } if (i > 1) putlog(LOG_MISC, "*", "(!) timer drift -- spun %d minutes", i); miltime = (nowtm.tm_hour * 100) + nowtm.tm_min; if (((int) (nowtm.tm_min / 5) * 5) == nowtm.tm_min) { /* 5-minutely. */ call_hook(HOOK_5MINUTELY); check_botnet_pings(); flush_and_check_logs(LOGS_FIVEMINUTELY); /* Is it midnight? */ if (miltime == MILTIME_MIDNIGHT) { char s[25]; strncpyz(s, ctime(&now), sizeof s); putlog(LOG_ALL, "*", "--- %.11s%s", s, s + 20); call_hook(HOOK_BACKUP); close_logs_at_midnight(); } } if (nowtm.tm_min == notify_users_at) call_hook(HOOK_HOURLY); if (miltime == switch_logfiles_at) { call_hook(HOOK_DAILY); switch_logs(); } } }
int main(int argc, char *argv[]) { mrp_mainloop_t *ml; mrp_clear(&cfg); parse_cmdline(&cfg, argc, argv); mrp_log_set_mask(cfg.log_mask); mrp_log_set_target(cfg.log_target); ml = mainloop_create(&cfg); if (ml == NULL) fatal("failed to create main loop."); dbus_test.ml = ml; setup_dbus_tests(ml); ml = dbus_test.ml; setup_timers(ml); setup_io(ml); setup_signals(ml); MRP_UNUSED(setup_deferred); /* XXX TODO: add deferred tests... */ #ifdef GLIB_ENABLED if (cfg.mainloop_type != MAINLOOP_GLIB && cfg.mainloop_type != MAINLOOP_QT) { if (cfg.ngio > 0 || cfg.ngtimer > 0) glib_pump_setup(ml); } setup_glib_io(); setup_glib_timers(); #endif if (mrp_add_timer(ml, 1000, check_quit, NULL) == NULL) fatal("failed to create quit-check timer"); setup_wakeup(ml); mainloop_run(&cfg); check_io(); check_timers(); check_signals(); #ifdef GLIB_ENABLED check_glib_io(); check_glib_timers(); #endif if (dbus_test.client != 0) close(dbus_test.pipe[1]); /* let the client continue */ check_dbus(); #ifdef GLIB_ENABLED if (cfg.mainloop_type != MAINLOOP_GLIB) { if (cfg.ngio > 0 || cfg.ngtimer > 0) glib_pump_cleanup(); } #endif cleanup_wakeup(); mainloop_cleanup(&cfg); }
int start_rendering() { static int done = 0; static void * network_thread_data[2] = { NULL, NULL }; static Uint32 last_frame_and_command_update = 0; SDL_Thread *network_thread; queue_t *message_queue; #ifndef WINDOWS SDL_EventState(SDL_SYSWMEVENT,SDL_ENABLE); #endif queue_initialise(&message_queue); network_thread_data[0] = message_queue; network_thread_data[1] = &done; network_thread = SDL_CreateThread(get_message_from_server, network_thread_data); /* Loop until done. */ while( !done ) { SDL_Event event; // handle SDL events in_main_event_loop = 1; while( SDL_PollEvent( &event ) ) { done = HandleEvent(&event); } in_main_event_loop = 0; //advance the clock cur_time = SDL_GetTicks(); //check for network data if(!queue_isempty(message_queue)) { message_t *message; while((message = queue_pop(message_queue)) != NULL) { process_message_from_server(message->data, message->length); free(message->data); free(message); } } #ifdef OLC olc_process(); #endif //OLC my_tcp_flush(my_socket); // make sure the tcp output buffer is set if (have_a_map && cur_time > last_frame_and_command_update + 60) { LOCK_ACTORS_LISTS(); next_command(); UNLOCK_ACTORS_LISTS(); move_to_next_frame(); last_frame_and_command_update = cur_time; } while (cur_time > next_second_time && real_game_second < 59) { real_game_second += 1; new_second(); next_second_time += 1000; } #ifdef NEW_SOUND weather_sound_control(); #endif //NEW_SOUND if(!limit_fps || (cur_time-last_time && 1000/(cur_time-last_time) <= limit_fps)) { weather_update(); animate_actors(); //draw everything draw_scene(); last_time=cur_time; } else { SDL_Delay(1);//give up timeslice for anyone else } #ifdef TIMER_CHECK //Check the timers to make sure that they are all still alive... check_timers(); #endif //cache handling if(cache_system)cache_system_maint(); //see if we need to exit if(exit_now) { done = 1; break; } } if(!done) { done = 1; } LOG_INFO("Client closed"); SDL_WaitThread(network_thread,&done); queue_destroy(message_queue); if(pm_log.ppl)free_pm_log(); //save all local data save_local_data(NULL, 0); #ifdef PAWN cleanup_pawn (); #endif #ifdef NEW_SOUND destroy_sound(); // Cleans up physical elements of the sound system and the streams thread clear_sound_data(); // Cleans up the config data #endif // NEW_SOUND ec_destroy_all_effects(); if (have_a_map) { destroy_map(); free_buffers(); } unload_questlog(); save_item_lists(); free_emotes(); free_actor_defs(); free_books(); free_vars(); cleanup_rules(); save_exploration_map(); cleanup_counters(); cleanup_chan_names(); cleanup_hud(); SDL_RemoveTimer(draw_scene_timer); SDL_RemoveTimer(misc_timer); end_particles (); free_bbox_tree(main_bbox_tree); main_bbox_tree = NULL; free_astro_buffer(); free_translations(); free_skybox(); /* Destroy our GL context, etc. */ SDL_QuitSubSystem(SDL_INIT_AUDIO); SDL_QuitSubSystem(SDL_INIT_TIMER); /*#ifdef WINDOWS // attempt to restart if requested if(restart_required > 0){ LOG_INFO("Restarting %s", win_command_line); SDL_CreateThread(system, win_command_line); } #endif //WINDOWS */ #ifdef NEW_SOUND final_sound_exit(); #endif #ifdef CUSTOM_UPDATE stopp_custom_update(); #endif /* CUSTOM_UPDATE */ clear_zip_archives(); destroy_tcp_out_mutex(); if (use_frame_buffer) free_reflection_framebuffer(); printf("doing SDL_Quit\n"); fflush(stderr); SDL_Quit( ); printf("done SDL_Quit\n"); fflush(stderr); cleanup_mem(); xmlCleanupParser(); FreeXML(); exit_logging(); return(0); }
// // // Main procedure // // int main(int argc, char **argv) { int shutdown = 0; // Shutdown flag struct timer_list timers; // Timers int payload_len; // length of payload returned struct payload_pkt app_data; // App payload data char node_name[HOSTNAME_LEN]; // Node name parse_options(argc, argv); // Parse command line parameters debug(DEBUG_ESSENTIAL, "Mesh starting in mode: %d\n", operating_mode); initialise_network(sizeof(struct payload_pkt),notify_link_up, notify_link_down); // Initialise the network details with callbacks initialise_timers(&timers); // and set all timers switch (operating_mode) { case OPMODE_MASTER: // Only Master nodes are responsible for broadcasting add_timer(TIMER_BROADCAST, 5); // Set to refresh network in y seconds break; case OPMODE_SLAVE: // add_timer(TIMER_APPLICATION, 15); // Set to refresh network in y seconds // now done when link comes up break; } add_timer(TIMER_STATS, timeto1hour()); // Report Network Efficiency stats hourly while (!shutdown) { // While NOT shutdown wait_on_network_timers(&timers); // Wait for message or timer expiory if (check_network_msg()) { // If a message is available handle_network_msg(&node_name[0], (char *)&app_data, &payload_len); // handle the network message handle_app_msg(&app_data, payload_len); // handle application specific messages } switch (check_timers(&timers)) { // check which timer has expired case TIMER_BROADCAST: // On Broadcast timer broadcast_network(); // send out broadcast message to contact other nodes add_timer(TIMER_BROADCAST, 20); // and set to broadcast again in y seconds break; case TIMER_PING: if (check_live_nodes()) { // On Ping check the network add_timer(TIMER_REPLY, 2); // Expire replies if not received within x secoonds add_timer(TIMER_PING, 20); // and set to Ping again in y seconds } break; case TIMER_REPLY: expire_live_nodes(); // Expire other nodes where reply has timed out break; case TIMER_STATS: report_network_stats(); // Report network efficiency stats hourly add_timer(TIMER_STATS, timeto1hour()); // Set to refresh network in 1 hour break; case TIMER_PAYLOAD_ACK: timeout_payload(); break; case TIMER_APPLICATION: handle_app_timer(); // Handle the timer for the App break; default: break; } DEBUG_FUNCTION( DEBUG_DETAIL, display_live_network()); DEBUG_FUNCTION( DEBUG_DETAIL, display_timers(&timers)); } debug(DEBUG_ESSENTIAL, "Mesh node shut down\n"); return 0; }