Пример #1
0
  /**
   * @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;
      }
    }
  }