static void animate_fire(timer *t, void *v) { animation *data = (animation *)v; (data->callback)(animation_multiplicands[data->i], data->v); timer_cancel(t); if (data->forwards) { data->i++; if (data->i == sizeof(animation_multiplicands) / sizeof(float)) { if (data->finalize != NULL) (data->finalize)(data->v); free(data); } else { timer_new(ANIMATION_INTERVAL, animate_fire, v); } } else { data->i--; if (data->i < 0) { if (data->finalize != NULL) (data->finalize)(data->v); free(data); } else { timer_new(ANIMATION_INTERVAL, animate_fire, v); } } }
/************************************************************************** Measure the time between the calls. Used to see where in the AI too much CPU is being used. **************************************************************************/ void TIMING_LOG(enum ai_timer timer, enum ai_timer_activity activity) { static int turn = -1; int i; if (turn == -1) { for (i = 0; i < AIT_LAST; i++) { aitimer[i][0] = timer_new(TIMER_CPU, TIMER_ACTIVE); aitimer[i][1] = timer_new(TIMER_CPU, TIMER_ACTIVE); recursion[i] = 0; } } if (game.info.turn != turn) { turn = game.info.turn; for (i = 0; i < AIT_LAST; i++) { timer_clear(aitimer[i][0]); } fc_assert(activity == TIMER_START); } if (activity == TIMER_START && recursion[timer] == 0) { timer_start(aitimer[timer][0]); timer_start(aitimer[timer][1]); recursion[timer]++; } else if (activity == TIMER_STOP && recursion[timer] == 1) { timer_stop(aitimer[timer][0]); timer_stop(aitimer[timer][1]); recursion[timer]--; } }
void hello_send(void *arg) { RREP *rrep; AODV_ext *ext = NULL; u_int8_t flags = 0; u_int64_t time_expired; rt_table_t *entry; int msg_size = RREP_SIZE; time_expired = get_currtime() - this_host->bcast_time; /* This check will ensure we don't send unnecessary hello msgs, in case we have sent other bcast msgs within HELLO_INTERVAL */ if (time_expired >= HELLO_INTERVAL) { #ifdef DEBUG_HELLO log(LOG_DEBUG, 0, "SEND_BCAST: sending Hello to %s", ip_to_str(AODV_BROADCAST)); #endif rrep = rrep_create(flags, 0, 0, this_host->ipaddr, this_host->seqno, this_host->ipaddr, ALLOWED_HELLO_LOSS*HELLO_INTERVAL); /* Assemble a RREP extension which contain our neighbor set... */ if(unidir_hack) { u_int32_t neigh; int i; ext = (AODV_ext *)((char *)rrep + RREP_SIZE); ext->type = RREP_HELLO_NEIGHBOR_SET_EXT; ext->length = 0; for(i = 0; i < RT_TABLESIZE; i++) { entry = routing_table[i]; while(entry != NULL) { /* If an entry has an active hello timer, we asume that we are receiving hello messages from that node... */ if(entry->hello_timer_id != 0) { #ifdef DEBUG_HELLO log(LOG_INFO, 0, "hello_send: Adding %s to hello neighbor set ext", ip_to_str(entry->dest)); #endif neigh = htonl(entry->dest); memcpy(AODV_EXT_NEXT(ext), &neigh, 4); ext->length += 4; } entry = entry->next; } } if(ext->length) msg_size = RREP_SIZE + AODV_EXT_SIZE(ext); } aodv_socket_send((AODV_msg *)rrep, AODV_BROADCAST, msg_size, 1); timer_new(HELLO_INTERVAL, hello_send, NULL); } else { timer_new((HELLO_INTERVAL - time_expired), hello_send, NULL); } }
void route_discovery_timeout(void *arg) { seek_list_t *seek_entry; seek_entry = (seek_list_t *)arg; /* Sanity check... */ if(seek_entry == NULL) return; #ifdef DEBUG log(LOG_DEBUG, 0, "route_discovery_timeout: %s", ip_to_str(seek_entry->dest_addr)); #endif if(seek_entry->reqs < RREQ_RETRIES) { RREQ *rreq; if(use_expanding_ring_search) { if(seek_entry->ttl < TTL_THRESHOLD) seek_entry->ttl += TTL_INCREMENT; else { seek_entry->ttl = NET_DIAMETER; seek_entry->reqs++; } /* Set a new timer for seeking this destination */ seek_entry->timer_id = timer_new(2*seek_entry->ttl*NODE_TRAVERSAL_TIME, route_discovery_timeout, seek_entry); } else { seek_entry->reqs++; seek_entry->timer_id = timer_new(NET_TRAVERSAL_TIME, route_discovery_timeout, seek_entry); } rreq = rreq_create(seek_entry->flags, seek_entry->dest_addr, seek_entry->dest_seqno); aodv_socket_send((AODV_msg *)rreq, AODV_BROADCAST, RREQ_SIZE, seek_entry->ttl); } else { packet_buff_drop(seek_entry->dest_addr); #ifdef DEBUG log(LOG_DEBUG, 0, "route_discovery_timeout: NO ROUTE FOUND!"); #endif seek_entry->timer_id = 0; /* Send an ICMP Destination Host Unreachable to the application: */ if(seek_entry->ipd) icmp_send_host_unreachable(this_host->ipaddr, seek_entry->ipd->data, seek_entry->ipd->len); seek_list_remove(seek_entry->dest_addr); } }
struct service_t * service_new(struct gsq_t * g2s_queue, struct gsq_t * s2g_queue, const char* scriptpath) { struct service_t * service = (struct service_t*)MALLOC(sizeof(*service)); if (service) { service->g2s_queue = g2s_queue; service->s2g_queue = s2g_queue; service->timer = timer_new(10*60*5); //1/10秒精度的定时器, 缓存为5分钟(当然超出5分钟也是可以的) service->lparser = lua_open(); service->tick = time_real_ms()/100; luaL_openlibs(service->lparser); //注入c接口 if(luaL_dostring(service->lparser, "local class = require (\"lualib.class\") return class.singleton(\"external\")") != 0) { fprintf(stderr, "%s\n", lua_tostring(service->lparser, -1)); goto fail; } #define INJECT_C_FUNC(func, name) lua_pushlightuserdata(service->lparser, service); lua_pushcclosure(service->lparser, func, 1); lua_setfield(service->lparser, -2, name); INJECT_C_FUNC(c_listen, "listen"); INJECT_C_FUNC(c_connect, "connect"); INJECT_C_FUNC(c_send, "send"); INJECT_C_FUNC(c_close, "close"); INJECT_C_FUNC(c_timeout, "timeout"); INJECT_C_FUNC(c_unixtime, "unixtime"); INJECT_C_FUNC(c_unixtime_ms, "unixms"); //设置脚本搜索路径 size_t plen=0; lua_getglobal(service->lparser, "package"); lua_getfield(service->lparser, -1, "path"); const char* path = luaL_checklstring(service->lparser, -1, &plen); char* npath = MALLOC(plen + strlen(scriptpath) + 8); sprintf(npath, "%s;%s/?.lua", path, scriptpath); lua_pushstring(service->lparser, npath); lua_setfield(service->lparser, -3, "path"); FREE(npath); //加载lua脚本的首个文件(文件名已定死) char* loadf = MALLOC(strlen(scriptpath) + sizeof("/interface.lua") + 1); strcpy(loadf, scriptpath); strcat(loadf, "/interface.lua"); if (luaL_dofile(service->lparser, loadf) != 0) { FREE(loadf); fprintf(stderr, "%s\n", lua_tostring(service->lparser, -1)); goto fail; } FREE(loadf); //缓存lua层事件处理方法 #define CACHE_L_EVHANDLE(name, idx) lua_getglobal(service->lparser, name); if (!lua_isfunction (service->lparser, -1)) {fprintf(stderr, "cannot find event handle'%s'",name);goto fail;} else {*idx=lua_gettop(service->lparser);} CACHE_L_EVHANDLE("c_onTcpAccepted", &service->idx_accepted); CACHE_L_EVHANDLE("c_onTcpConnected", &service->idx_connected); CACHE_L_EVHANDLE("c_onTcpClosed", &service->idx_closed); CACHE_L_EVHANDLE("c_onTcpListened", &service->idx_listened); CACHE_L_EVHANDLE("c_onTcpData", &service->idx_data); CACHE_L_EVHANDLE("c_onTimer", &service->idx_timer); } return service; fail: if (service) { if (service->lparser) lua_close(service->lparser); FREE(service); } return NULL; }
static void asdbus_set_dbus_timer (struct timeval *expires, DBusTimeout *timeout) { int interval = dbus_timeout_get_interval(timeout); gettimeofday (expires, NULL); tv_add_ms(expires, interval); show_debug(__FILE__,__FUNCTION__,__LINE__,"time = %d, adding dbus timeout data=%p, interval = %d\n", time(NULL), timeout, interval); timer_new (interval, asdbus_handle_timer, timeout); }
static void volume_changed(GVolumeMonitor *monitor, GVolume *volume, GObject *unused) { SHOW_CHECKPOINT; timer_remove_by_data (volume); timer_new (300, &volume_timer_handler, (void *)volume); addTimeout(); }
INLINE emu_timer *_timer_alloc_common(running_machine *machine, timer_fired_func callback, void *ptr, const char *file, int line, const char *func, int temp) { attotime time = get_current_time(machine); emu_timer *timer = timer_new(machine); /* fill in the record */ timer->callback = callback; timer->ptr = ptr; timer->param = 0; timer->enabled = FALSE; timer->temporary = temp; timer->period = attotime_zero; timer->file = file; timer->line = line; timer->func = func; /* compute the time of the next firing and insert into the list */ timer->start = time; timer->expire = attotime_never; timer_list_insert(timer); /* if we're not temporary, register ourselves with the save state system */ if (!temp) { if (!state_save_registration_allowed(machine)) fatalerror("timer_alloc() called after save state registration closed! (file %s, line %d)\n", file, line); timer_register_save(timer); } /* return a handle */ return timer; }
mame_timer *_mame_timer_alloc(void (*callback)(int), const char *file, int line) { mame_time time = get_current_time(); mame_timer *timer = timer_new(); /* fail if we can't allocate a new entry */ if (!timer) return NULL; /* fill in the record */ timer->callback = callback; timer->callback_param = 0; timer->enabled = 0; timer->temporary = 0; timer->tag = get_resource_tag(); timer->period = time_zero; timer->file = file; timer->line = line; /* compute the time of the next firing and insert into the list */ timer->start = time; timer->expire = time_never; timer_list_insert(timer); /* return a handle */ return timer; }
mame_timer *timer_alloc(void (*callback)(int)) { double time = get_relative_time(); mame_timer *timer = timer_new(); /* fail if we can't allocate a new entry */ if (!timer) return NULL; /* fill in the record */ timer->callback = callback; timer->callback_param = 0; timer->enabled = 0; timer->temporary = 0; timer->tag = get_resource_tag(); timer->period = 0; /* compute the time of the next firing and insert into the list */ timer->start = time; timer->expire = TIME_NEVER; timer_list_insert(timer); /* return a handle */ return timer; }
void sys_timer_set( SysTimer timer, SysTime when, SysCallback _callback, void* opaque ) { QEMUTimerCB* callback = (QEMUTimerCB*)_callback; if (callback == NULL) { /* unsetting the timer */ if (timer->timer) { timer_del( timer->timer ); timer_free( timer->timer ); timer->timer = NULL; } timer->callback = callback; timer->opaque = NULL; return; } if ( timer->timer ) { if ( timer->callback == callback && timer->opaque == opaque ) goto ReuseTimer; /* need to replace the timer */ timer_free( timer->timer ); } timer->timer = timer_new(QEMU_CLOCK_REALTIME, SCALE_MS, callback, opaque ); timer->callback = callback; timer->opaque = opaque; ReuseTimer: timer_mod( timer->timer, when ); }
Conn_t * conn_add(ConnType_t type, int fd, LecId_t pvc_lecid) { Conn_t *tmp; LecId_t lecid = 0; if (type != CT_MAIN && type != CT_PVC_CD) { /* Find next available LECID */ for (tmp = connlist; tmp != NULL; tmp = tmp->next){ if (lecid < tmp->lecid) { lecid = tmp->lecid; } } lecid++; } else if (type == CT_MAIN) { lecid = 0; } else /* PVC */ lecid = pvc_lecid; tmp = (Conn_t *)mem_alloc(&conn_unit, sizeof(Conn_t)); memset(tmp, 0, sizeof(*tmp)); tmp->fd = fd; tmp->state = CS_IDLE; tmp->lecid = lecid; tmp->type = type; tmp->next = connlist; tmp->timer = timer_new(&conn_unit); tmp->proxy = BL_FALSE; connlist = tmp; event_add_fd(fd, tmp); return tmp; }
static bool httpd_output_open(struct audio_output *ao, struct audio_format *audio_format, GError **error) { struct httpd_output *httpd = (struct httpd_output *)ao; bool success; g_mutex_lock(httpd->mutex); /* open the encoder */ success = httpd_output_encoder_open(httpd, audio_format, error); if (!success) { g_mutex_unlock(httpd->mutex); return false; } /* initialize other attributes */ httpd->clients = NULL; httpd->clients_cnt = 0; httpd->timer = timer_new(audio_format); httpd->open = true; g_mutex_unlock(httpd->mutex); return true; }
INLINE emu_timer *_timer_alloc_common(timer_fired_func callback, void *ptr, const char *file, int line, const char *func, int temp) { attotime time = get_current_time(); emu_timer *timer = timer_new(); /* fill in the record */ timer->callback = callback; timer->ptr = ptr; timer->param = 0; timer->enabled = FALSE; timer->temporary = temp; timer->period = attotime_zero; timer->file = file; timer->line = line; timer->func = func; /* compute the time of the next firing and insert into the list */ timer->start = time; timer->expire = attotime_never; timer_list_insert(timer); /* if we're not temporary, register ourselves with the save state system */ if (!temp) { timer_register_save(timer); restrack_register_object(OBJTYPE_TIMER, timer, 0, file, line); } /* return a handle */ return timer; }
int main(int argc, char** argv) { if (argc != 3) { fprintf(stderr, "Usage: %s <size> <n>\n", argv[0]); return 1; } int size = atoi(argv[1]); int n = atoi(argv[2]); printf("%d\n", n); matrix* m = matrix_new(size, size); int num_cells = size * size; int print_interval = num_cells / n; int i; timer* t = timer_new(); for (i = 0; i < num_cells; i++) { int next = i; int row = next / size; int col = next % size; matrix_set(m, row, col, (double) i); if (i % print_interval == 0) { timer_start(t); m = matrix_mmul(m, m); printf("%2f %lu\n", ((i + 0.0) / num_cells) * 100, timer_nsec(t)); } } matrix_free(m); return 0; }
static void timer_test(void) { htimer_t *tm; tm = timer_new("1"); add_timer(tm, 1); tm = timer_new("2"); add_timer(tm, 2); tm = timer_new("3"); add_timer(tm, 3); tm = timer_new("11"); add_timer(tm, 11); timer_thread(1); timer_thread(10); timer_thread(12); }
GLvoid init_scene(GLvoid) { u_int i; // u_int d; grav_vector = v3_new(0,-g,0); gr = v3_copy(grav_vector); glutTimerFunc(100, update_world, 0); view_main = view_new(0, VIEW_NONE, GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, 0, 0, 1, 1); view_main->draw_func = draw_scene; for (i = 0; i < PARTICLES; i++) { ps[i].c = v3_new(2 * B * frandom() - B, 2 * B * frandom() - B, 2 * B * frandom() - B); ps[i].v = v3_new((frandom() * 2 - 1), (frandom() * 2 - 1), (frandom() * 2 - 1)); // ps[i].v = v3_new(0,0,0); // ps[i].c = v3_new(0,//frandom() * 0.001 - 0.0005, // B - RADIUS - i * RADIUS * 2 * 1.01, // 0);//frandom() * 0.001 - 0.0005); // } /* ps[0].v = v3_new(3,0,0); ps[1].v = v3_new(-3,0,0); ps[0].c = v3_new(-B + RADIUS, -B * 0 + RADIUS, 0); ps[1].c = v3_new(B - RADIUS, -B * 0 + RADIUS, 0); */ timer_frames = timer_new(); timer_bounce = timer_new(); mutex_frames = 0; timer_start(timer_frames); timer_start(timer_bounce); }
static bool fifo_output_open(void *data, struct audio_format *audio_format, G_GNUC_UNUSED GError **error) { struct fifo_data *fd = (struct fifo_data *)data; fd->timer = timer_new(audio_format); return true; }
SE_HANDLE vpn_NewTimer (SE_SYS_CALLBACK_TIMER *callback, void *param) { void *p; p = timer_new (callback, param); if (!p) panic ("NewTimer failed"); return p; }
rt_table_t *rt_table_update_timeout(rt_table_t *entry, u_int32_t life) { entry->expire = get_currtime() + life; timer_remove(entry->timer_id); #ifdef DEBUG /* log(LOG_INFO, 0, "rt_table_update(): New timer for %s, life=%d", */ /* ip_to_str(entry->dest), newlife); */ #endif entry->timer_id = timer_new(life, route_expire_timeout, entry); return entry; }
static void idman_init (void) { void *handle; if (!config.vmm.iccard.enable) return; handle = timer_new (iccard_init_timer, NULL); ASSERT (handle); timer_set (handle, 1000000); }
static void arm_cpu_initfn(Object *obj) { CPUState *cs = CPU(obj); ARMCPU *cpu = ARM_CPU(obj); static bool inited; cs->env_ptr = &cpu->env; cpu_exec_init(&cpu->env); cpu->cp_regs = g_hash_table_new_full(g_int_hash, g_int_equal, g_free, g_free); #ifndef CONFIG_USER_ONLY /* Our inbound IRQ and FIQ lines */ if (kvm_enabled()) { qdev_init_gpio_in(DEVICE(cpu), arm_cpu_kvm_set_irq, 2); } else { qdev_init_gpio_in(DEVICE(cpu), arm_cpu_set_irq, 2); } cpu->gt_timer[GTIMER_PHYS] = timer_new(QEMU_CLOCK_VIRTUAL, GTIMER_SCALE, arm_gt_ptimer_cb, cpu); cpu->gt_timer[GTIMER_VIRT] = timer_new(QEMU_CLOCK_VIRTUAL, GTIMER_SCALE, arm_gt_vtimer_cb, cpu); qdev_init_gpio_out(DEVICE(cpu), cpu->gt_timer_outputs, ARRAY_SIZE(cpu->gt_timer_outputs)); #endif /* DTB consumers generally don't in fact care what the 'compatible' * string is, so always provide some string and trust that a hypothetical * picky DTB consumer will also provide a helpful error message. */ cpu->dtb_compatible = "qemu,unknown"; cpu->psci_version = 1; /* By default assume PSCI v0.1 */ cpu->kvm_target = QEMU_KVM_ARM_TARGET_NONE; if (tcg_enabled() && !inited) { inited = true; arm_translate_init(); } }
static void* throttlePipe_init( void* hwpipe, void* svcOpaque, const char* args ) { ThrottlePipe* pipe; ANEW0(pipe); pingPongPipe_init0(&pipe->pingpong, hwpipe, svcOpaque); pipe->timer = timer_new(QEMU_CLOCK_VIRTUAL, SCALE_NS, throttlePipe_timerFunc, pipe); /* For now, limit to 500 KB/s in both directions */ pipe->sendRate = 1e9 / (500*1024*8); pipe->recvRate = pipe->sendRate; return pipe; }
animation *animate(callback_fn callback, finalize_fn finalize, void *v) { animation *data; data = malloc(sizeof(animation)); if (data == NULL) return NULL; data->i = 0; data->v = v; data->callback = callback; data->finalize = finalize; data->forwards = 1; timer_new(ANIMATION_INTERVAL, animate_fire, (void *)data); return data; }
void desk_anim_slide (ScreenInfo * scr, Window cover, int dirx, int diry, unsigned int steps) { struct ASDeskAniMove *data = safecalloc (1, sizeof (struct ASDeskAniMove)); data->cover = cover; data->dirx = dirx; data->diry = diry; data->steps = steps; timer_new (20, do_anim_slide, data); }
static void vmmcall_iccard_init (void) { void *handle; if (!config.vmm.iccard.status) return; rw_spinlock_init (&cardtest); iccard_status = IS_NOT_READY; vmmcall_register ("iccard", iccard); handle = timer_new (iccard_timer, NULL); ASSERT (handle); timer_set (handle, 1000000); }
static HwSensorClient* _hwSensorClient_new( HwSensors* sensors ) { HwSensorClient* cl; ANEW0(cl); cl->sensors = sensors; cl->enabledMask = 0; cl->delay_ms = 800; cl->timer = timer_new(QEMU_CLOCK_VIRTUAL, SCALE_NS, _hwSensorClient_tick, cl); cl->next = sensors->clients; sensors->clients = cl; return cl; }
void desk_anim_shrink (ScreenInfo * scr, Window cover, int dirx, int diry, unsigned int steps) { struct ASDeskAniMove *data = safecalloc (1, sizeof (struct ASDeskAniMove)); data->cover = cover; data->dirx = dirx; data->diry = diry; data->steps = steps; data->px = scr->MyDisplayWidth; data->py = scr->MyDisplayHeight; timer_new (20, do_anim_shrink, data); }
int main(int argc, char *argv[]) { struct timer *timer = timer_new(); sleep(1); printf("Elapsed: %lu ms\n", timer_elapsed(timer, NULL)); timer_stop(timer); sleep(1); printf("Elapsed: %lu ms\n", timer_elapsed(timer, NULL)); timer_continue(timer); sleep(1); printf("Elapsed: %lu ms\n", timer_elapsed(timer, NULL)); timer_destroy(&timer); return 0; }
static bool httpd_output_open(void *data, struct audio_format *audio_format, GError **error) { struct httpd_output *httpd = data; bool success; GIOChannel *channel; g_mutex_lock(httpd->mutex); /* create and set up listener socket */ httpd->fd = socket_bind_listen(PF_INET, SOCK_STREAM, 0, (struct sockaddr *)&httpd->address, httpd->address_size, 16, error); if (httpd->fd < 0) { g_mutex_unlock(httpd->mutex); return false; } channel = g_io_channel_unix_new(httpd->fd); httpd->source_id = g_io_add_watch(channel, G_IO_IN, httpd_listen_in_event, httpd); g_io_channel_unref(channel); /* open the encoder */ success = httpd_output_encoder_open(httpd, audio_format, error); if (!success) { g_source_remove(httpd->source_id); close(httpd->fd); g_mutex_unlock(httpd->mutex); return false; } /* initialize other attributes */ httpd->clients = NULL; httpd->timer = timer_new(audio_format); g_mutex_unlock(httpd->mutex); return true; }