/** 删除被停止的任务 */ private void sweepGarbage() { // // Check both task list and frame list for tasks that were marked // for deletion by the Terminate() method. This implementation is // pretty brute-force; garbage collection could be run as an // idle processing task. // // printf("SWEEP BEGIN\n"); TaskInfo pTaskInfo = m_pTaskList; TaskInfo pPrev = null; while (pTaskInfo != null) { if (pTaskInfo.status == TASK_DELETE) { TaskInfo pTemp = pTaskInfo; pTaskInfo = pTaskInfo.pNext; if (pTemp.equals(m_pTaskList)) { m_pTaskList = pTaskInfo; } else { pPrev.pNext = pTaskInfo; // printf("Sched: Deleted %d\n",pTemp->id); // delete pTemp; } } else { pPrev = pTaskInfo; pTaskInfo = pTaskInfo.pNext; } } pTaskInfo = m_pFrameList; pPrev = null; while (pTaskInfo != null) { if (pTaskInfo.status == TASK_DELETE) { TaskInfo pTemp = pTaskInfo; pTaskInfo = pTaskInfo.pNext; if (pTemp.equals(m_pFrameList)) { m_pFrameList = pTaskInfo; } else { pPrev.pNext = pTaskInfo; // printf("Sched: Deleted %d\n",pTemp->id); // delete pTemp; } } else { pPrev = pTaskInfo; pTaskInfo = pTaskInfo.pNext; } } // printf("SWEEP END\n"); }
private void insertTimeTask(TaskInfo pTaskInfo) { // // inserted into list in time order // if (m_pTaskList == null) { pTaskInfo.pNext = null; m_pTaskList = pTaskInfo; } else if (m_pTaskList.time.next > pTaskInfo.time.next) { pTaskInfo.pNext = m_pTaskList; m_pTaskList = pTaskInfo; } else { TaskInfo pInfo = m_pTaskList; while (pInfo != null) { if (pInfo.pNext == null) { pTaskInfo.pNext = null; pInfo.pNext = pTaskInfo; break; } else if (pInfo.pNext.time.next > pTaskInfo.time.next) { pTaskInfo.pNext = pInfo.pNext; pInfo.pNext = pTaskInfo; break; } pInfo = pInfo.pNext; } } }
/** * @param type 任务类型(TASK_TIME,TASK_RENDER,TASK_FRAME) * @param start 任务开始时间或者帧 * @param period 任务间隔触发时间或者帧数 * @param duration 任务持续时间或者帧数 * @param pTask 请求任务 * @param pUser 用户数据 * @return 系统生成任务ID */ public long schedule(int type, long start, int period, int duration, ITask pTask, Object pUser) { // // this schedules a task of the appropriate type (RENDER, FRAME, or // TIME). Time tasks and // frame tasks are kept in separate lists for ease of handling. // Render tasks are stored as a single special-case entry. // // time and frame tasks have a start time, a duration, and a period. // 时间和帧任务都有一个开始时间,持续时间,时间间隔。 // the duration is relative to the start time, except for duration 0 // which is a special case. // 如果持续时间为0,那么就是一个无限循环的任务 // since the scheduler doesn't care about the duration itself, it // converts it into an end time // and stores that instead. the render task does ignores // start/duration/end. // // pUser is a user data pointer. // // a unique task id is generated and returned in pID. if you don't care // about an ID, // pass in NULL instead. // if (type == TASK_RENDER) { renderTask.pTask = pTask; renderTask.pUser = pUser; renderTask.id = RENDER_TASK_ID; return RENDER_TASK_ID; } else { // // Allocating memory like this has high overhead. It would be much // better to use a // fast allocator which preallocates and/or reuses TaskInfo // structures. // TaskInfo pTaskInfo = new TaskInfo(); { pTaskInfo.pTask = pTask; pTaskInfo.pNext = m_pTaskList; pTaskInfo.status = TASK_ACTIVE; pTaskInfo.id = m_nextId++; // if (pID != 0) // pID = pTaskInfo.id; pTaskInfo.pUser = pUser; pTaskInfo.time.start = start; pTaskInfo.time.period = period; if (duration == 0) { pTaskInfo.time.duration = 0; // infinite } else { pTaskInfo.time.duration = start + duration - 1; // compute } // end time pTaskInfo.time.next = start; // printf("Scheduling %s task %u from %3u to %3u, every %2u %s\n", // type==TASK_TIME?"time ":"frame", // pTaskInfo.id, // pTaskInfo.time.start, // pTaskInfo.time.duration, // pTaskInfo.time.period, // type==TASK_TIME?"ms":"frames"); if (type == TASK_TIME) { insertTimeTask(pTaskInfo); } else if (type == TASK_FRAME) { insertFrameTask(pTaskInfo); } return pTaskInfo.id; } } }