/** 系统事件处理线程 */ static void SystemEventThread(void *arg) { System.event.is_running = TRUE; while( System.event.is_running ) { DEBUG_MSG("waiting event...\n"); LCUICond_Wait( &System.event.cond ); DEBUG_MSG("get event.\n"); LCUIEventBox_Dispatch( System.event.box ); } }
static void LCUIWorker_Thread( void *arg ) { LCUI_Worker worker = arg; while( worker->active ) { if( LCUIWorker_RunTask( worker ) ) { continue; } if( worker->active ) { LCUICond_Wait( &worker->cond, &worker->mutex ); } } LCUIThread_Exit( NULL ); }
void FrameControl_Remain( FrameControl ctx ) { int64_t current_time; unsigned int n_ms, lost_ms; if( ctx->state == STATE_QUIT ) { return; } lost_ms = 0; current_time = LCUI_GetTime(); LCUIMutex_Lock( &ctx->mutex ); n_ms = (unsigned int)(current_time - ctx->prev_frame_start_time); if( n_ms > ctx->one_frame_remain_time ) { goto normal_exit; } n_ms = ctx->one_frame_remain_time - n_ms; if( n_ms < 1 ) { goto normal_exit; } /* 睡眠一段时间 */ while( lost_ms < n_ms && ctx->state == STATE_RUN ) { LCUICond_TimedWait( &ctx->cond, &ctx->mutex, n_ms - lost_ms ); lost_ms = (unsigned int)LCUI_GetTimeDelta( current_time ); } /* 睡眠结束后,如果当前状态为 PAUSE,则说明睡眠是因为要暂停而终止的 */ if( ctx->state == STATE_PAUSE ) { current_time = LCUI_GetTime(); /* 等待状态改为“继续” */ while( ctx->state == STATE_PAUSE ) { LCUICond_Wait( &ctx->cond, &ctx->mutex ); } lost_ms = (unsigned int)LCUI_GetTimeDelta( current_time ); ctx->pause_time = lost_ms; ctx->prev_frame_start_time += lost_ms; LCUIMutex_Unlock( &ctx->mutex ); return; } normal_exit:; current_time = LCUI_GetTime(); if( current_time - ctx->prev_fps_update_time >= 1000 ) { ctx->current_fps = ctx->temp_fps; ctx->prev_fps_update_time = current_time; ctx->temp_fps = 0; } ctx->prev_frame_start_time = current_time; ++ctx->temp_fps; LCUIMutex_Unlock( &ctx->mutex ); }
/* 销毁程序占用的资源 */ static void LCUIApp_Destroy(void) { if( LinkedList_GetTotal(&MainApp.loop_list) > 0 ) { /* 等待其它线程上的主循环都完全退出 */ LCUICond_Wait( &MainApp.loop_list_empty ); } /* 开始清理 */ LCUICond_Destroy( &MainApp.loop_list_empty ); LCUICond_Destroy( &MainApp.loop_cond ); LCUIMutex_Destroy( &MainApp.loop_mutex ); LCUIMutex_Destroy( &MainApp.loop_changed ); LCUIMutex_Destroy( &MainApp.task_run_mutex ); LCUIMutex_Destroy( &MainApp.task_list_mutex ); LinkedList_Destroy( &MainApp.loop_list ); }
static void TestWorker_Thread( void *arg ) { TestWorker worker = arg; worker->active = TRUE; worker->data_count = 0; LCUIMutex_Lock( &worker->mutex ); while( worker->active ) { TEST_LOG( "waiting...\n" ); LCUICond_Wait( &worker->cond, &worker->mutex ); TEST_LOG( "get data: %s\n", worker->data ); worker->data_count += 1; } LCUIMutex_Unlock( &worker->mutex ); TEST_LOG( "count: %lu\n", worker->data_count ); }
/** 让当前帧停留一定时间 */ void FrameControl_Remain( FrameCtrlCtx ctx ) { unsigned int n_ms, lost_ms; int64_t current_time; if( ctx->state == FRAME_CTRL_STATE_QUIT ) { return; } current_time = LCUI_GetTickCount(); n_ms = (int)(current_time - ctx->prev_frame_start_time); if( n_ms > ctx->one_frame_remain_time ) { goto normal_exit; } n_ms = ctx->one_frame_remain_time - n_ms; if( n_ms < 1 ) { goto normal_exit; } /* 进行睡眠,直到需要暂停为止 */ LCUICond_TimedWait( &ctx->wait_pause, &ctx->mutex, n_ms ); /* 睡眠结束后,如果当前状态不为PAUSE,则说明睡眠不是因为要暂停而终止的 */ if( ctx->state != FRAME_CTRL_STATE_PAUSE ) { goto normal_exit; } current_time = LCUI_GetTickCount(); /* 需要暂停,进行睡眠,直到需要继续为止 */ LCUICond_Wait( &ctx->wait_continue, &ctx->mutex ); lost_ms = (unsigned int)LCUI_GetTicks( current_time ); ctx->pause_time = lost_ms; ctx->prev_frame_start_time += lost_ms; return; normal_exit:; current_time = LCUI_GetTickCount(); if( current_time - ctx->prev_fps_update_time >= 1000 ) { ctx->current_fps = ctx->temp_fps; ctx->prev_fps_update_time = current_time; ctx->temp_fps = 0; } ctx->prev_frame_start_time = current_time; ++ctx->temp_fps; }