static public int app_timer_init() { Console.WriteLine("app timer init..."); //创建定时器列表 for (int i = 0; i < APP_TIMER_COUNT; i++) { app_timer_list[i] = new AppTimerMember(); app_timer_list[i].id = i; app_timer_list[i].isValid = 0; } curren_deal_timer = null; //初始化信号量 //timer_sync_semaphore = AK_Create_Semaphore(0,AK_PRIORITY); //创建定时器处理线程 app_timer_task = new Thread(thread_app_timer_deal); app_timer_task.Start(); //初始化定时器 hTimer = new Timer(app_timer_update); hTimer.Change(0, 50); return(0); }
static void thread_app_timer_deal() { int i; Console.WriteLine("thread_app_timer_deal...\r\n"); while (true) { //AK_Obtain_Semaphore(timer_sync_semaphore, AK_SUSPEND); //等待信号量 //AK_Reset_Semaphore(timer_sync_semaphore, 0); lock (timer_sync_semaphore) { Monitor.Wait(timer_sync_semaphore); } //printf("timer deal thread run...\r\n"); for (i = 0; i < APP_TIMER_COUNT; i++) { lock (timer_lock) { if (app_timer_list[i].isValid > 0 && app_timer_list[i].timeout > 0 && app_timer_list[i].isStart > 0) { curren_deal_timer = app_timer_list[i]; app_timer_list[i].counter = 0; app_timer_list[i].timeout = 0; if (app_timer_list[i].life > 0) { app_timer_list[i].life--; if (app_timer_list[i].func != null) { app_timer_list[i].func(app_timer_list[i].argc, app_timer_list[i].argv); } if (app_timer_list[i].life == 0) { app_timer_list[i].isValid = 0; Console.WriteLine("timer" + app_timer_list[i].id + " exit!\n"); } } else { if (app_timer_list[i].func != null) { app_timer_list[i].func(app_timer_list[i].argc, app_timer_list[i].argv); } } } //release_nest_lock(timer_lock); } } } }
static public int start_timer(AppTimerMember timer) { lock (timer_lock) { if (timer == null || timer.isValid == 0) { Console.WriteLine("Start_Timer:timer无效"); //release_nest_lock(timer_lock); return(-1); } timer.isStart = 1; } Console.WriteLine("Start Timer" + timer.id); return(0); }
static public int reset_timer_count(AppTimerMember timer) { lock (timer_lock) { if (timer == null || timer.isValid == 0) { Console.WriteLine("set_timer_count:timer无效"); //release_nest_lock(timer_lock); return(-1); } timer.counter = 0; timer.timeout = 0; //release_nest_lock(timer_lock); } return(0); }
static public int get_timer_life(AppTimerMember timer) { int life; lock (timer_lock) { if (timer == null || timer.isValid == 0) { Console.WriteLine("get_timer_life:timer无效\r\n"); //release_nest_lock(timer_lock); return(-1); } life = timer.life; //release_nest_lock(timer_lock); } return(life); }
static public int destroy_timer(AppTimerMember timer) { if (timer == null) { Console.WriteLine("Destroy_Timer:timer无效\r\n"); return(-1); } lock (timer_lock) { if (timer.isValid > 0) { timer.isValid = 0; timer.name = ""; timer.argv = null; } } Console.WriteLine("Destroy timer" + timer.id); return(0); }