Exemplo n.º 1
0
void JobManager::EndJob(InternalJob::Pointer ptr_job, IStatus::Pointer result, bool notify)
{
  Poco::Timestamp::TimeDiff rescheduleDelay(InternalJob::T_NONE);
  {
    Poco::ScopedLock<Poco::Mutex> lock ( m_mutex);

    //  if the job is finishing asynchronously, there is nothing more to do for now
    if (result == Job::ASYNC_FINISH)
      return;

    //if job is not known then it cannot be done
    if (ptr_job->GetState() == Job::NONE)
       return;
    ptr_job->SetResult(result);
    ptr_job->SetProgressMonitor(IProgressMonitor::Pointer(nullptr));
    ptr_job->SetThread(nullptr);
    rescheduleDelay = ptr_job->GetStartTime().epochMicroseconds();
    InternalJob::Pointer sptr_job(ptr_job);
    ChangeState(sptr_job, Job::NONE);
  }

  //notify listeners outside sync block
  bool reschedule = m_active && rescheduleDelay > InternalJob::T_NONE && ptr_job->ShouldSchedule();
  if (notify)
    m_JobListeners.Done(ptr_job.Cast<Job>(), result, reschedule);
  //reschedule the job if requested and we are still active
  if (reschedule)
    Schedule(ptr_job, rescheduleDelay, reschedule);
}
Exemplo n.º 2
0
bool JobManager::IsBlocking(InternalJob::Pointer sptr_runningJob)
{
  {
    Poco::ScopedLock<Poco::Mutex> lockMe (m_mutex);
    // if this job isn't running, it can't be blocking anyone
    if (sptr_runningJob->GetState() != Job::RUNNING)
    return false;
    // if any job is queued behind this one, it is blocked by it
    InternalJob::Pointer ptr_previous = sptr_runningJob->Previous();
    while (ptr_previous != 0)
    {
      // ignore jobs of lower priority (higher priority value means lower priority)
      if (ptr_previous->GetPriority() < sptr_runningJob->GetPriority())
      {
        if (!ptr_previous->IsSystem())
        return true;
        // TODO Implicit Jobs
        // implicit jobs should interrupt unless they act on behalf of system jobs
        // if (previous instanceof ThreadJob && ((ThreadJob) previous).shouldInterrupt())
        // return true;
      }
      ptr_previous = ptr_previous->previous;
    }
    // none found
    return false;
  }
}
Exemplo n.º 3
0
IProgressMonitor::Pointer JobManager::CreateMonitor(InternalJob::Pointer sptr_job, IProgressMonitor::Pointer group, int ticks)
{
 {
     Poco::ScopedLock<Poco::Mutex> managerLock(m_mutex);

    //group must be set before the job is scheduled
    //this includes the ABOUT_TO_SCHEDULE state, during which it is still
    //valid to set the progress monitor
    if (sptr_job->GetState() != Job::NONE)
    {
      IProgressMonitor::Pointer dummy(nullptr);
      return dummy;
    }
    IProgressMonitor::Pointer sptr_monitor(nullptr);
    if (m_sptr_progressProvider != 0)
      sptr_monitor = m_sptr_progressProvider->CreateMonitor(sptr_job.Cast<Job>() , group, ticks);
    if (sptr_monitor == 0)
    {
      // return a default NullprogressMonitor
      NullProgressMonitor::Pointer sptr_defaultMonitor(new NullProgressMonitor() );
      return sptr_defaultMonitor;
    }

    return sptr_monitor;
  }
}
Exemplo n.º 4
0
bool JobManager::Sleep(InternalJob::Pointer job)
{
  {
    Poco::ScopedLock<Poco::Mutex> lockMe (m_mutex);
    InternalJob::Pointer sptr_job(job);
    switch (job->GetState())
    {
      case Job::RUNNING :
      //cannot be paused if it is already running (as opposed to ABOUT_TO_RUN)
      if (job->InternalGetState() == Job::RUNNING)
      return false;
      //job hasn't started running yet (aboutToRun listener)
      break;
      case Job::SLEEPING :
      //update the job wake time
      job->SetStartTime(InternalJob::T_INFINITE);
      //change state again to re-shuffle the sleep queue

      ChangeState(sptr_job, Job::SLEEPING);
      return true;
      case Job::NONE :
      return true;
      case Job::WAITING :
      //put the job to sleep
      break;
    }
    job->SetStartTime(InternalJob::T_INFINITE);
    ChangeState(sptr_job, Job::SLEEPING);
  }
  m_JobListeners.Sleeping(job.Cast<Job>());
  return true;
}
Exemplo n.º 5
0
void JobManager::Schedule(InternalJob::Pointer job, Poco::Timestamp::TimeDiff delay, bool reschedule)
{
  if (!m_active)
  throw Poco::IllegalStateException("Job manager has been shut down.");

  poco_assert(job); // "Job is null"
  poco_assert(delay >= 0); // "Scheduling delay is negative"

  {
    Poco::ScopedLock<Poco::Mutex> managerLock (m_mutex);
    //if the job is already running, set it to be rescheduled when done
    if (job->GetState() == Job::RUNNING)
    {
      job->SetStartTime(delay);
      return;
    }
    //can't schedule a job that is waiting or sleeping
    if (job->InternalGetState() != Job::NONE)
    return;

    //remember that we are about to schedule the job
    //to prevent multiple schedule attempts from succeeding (bug 68452)
    InternalJob::Pointer sptr_job(job);
    ChangeState(sptr_job, InternalJob::ABOUT_TO_SCHEDULE);
  }
  //notify listeners outside sync block
  m_JobListeners.Scheduled(job.Cast<Job>(), delay, reschedule);
  //schedule the job
  DoSchedule(job, delay);
  //call the pool outside sync block to avoid deadlock
  m_Pool->JobQueued();
}
Exemplo n.º 6
0
void JobManager::WakeUp(InternalJob::Pointer job, Poco::Timestamp::TimeDiff delay)
{
  poco_assert(delay >= 0); // "Scheduling delay is negative"

  {
    Poco::ScopedLock<Poco::Mutex> m_managerLock (m_mutex);
    //cannot wake up if it is not sleeping
    if (job->GetState() != Job::SLEEPING)
    return;
    DoSchedule(job, delay);
  }
  //call the pool outside sync block to avoid deadlock
  m_Pool->JobQueued();

  /// IListenerExtension only notify of wake up if immediate
  if (delay == 0)
  m_JobListeners.Awake(job.Cast<Job>());
}
Exemplo n.º 7
0
bool JobManager::Cancel(InternalJob::Pointer sptr_job)
{
  IProgressMonitor::Pointer sptr_progressMonitor(nullptr);
  bool runCanceling = false;
  {
    Poco::ScopedLock<Poco::Mutex> mangerMutex (m_mutex);

    switch (sptr_job->GetState())
    {
      case Job::NONE :
      return true;
      case Job::RUNNING :
      //cannot cancel a job that has already started (as opposed to ABOUT_TO_RUN)
      if (sptr_job->InternalGetState() == Job::RUNNING)
      {
      sptr_progressMonitor = sptr_job->GetProgressMonitor();
      runCanceling = sptr_job->IsRunCanceled();
      if(runCanceling)
          sptr_job->SetRunCanceled(true);
      break ;
      }
      //signal that the job should be canceled before it gets a chance to run
      sptr_job->SetAboutToRunCanceled(true);
      return false;
   default :
      ChangeState(sptr_job, Job::NONE);
    }
  }
  //call monitor outside sync block
  if (sptr_progressMonitor != 0)
  {
    if(runCanceling)
      {
       if (!sptr_progressMonitor->IsCanceled())
          sptr_progressMonitor->SetCanceled(true);
       sptr_job->Canceling();

      }
    return false;
  }
  //only notify listeners if the job was waiting or sleeping
  m_JobListeners.Done(sptr_job.Cast<Job>(), Status::CANCEL_STATUS(BERRY_STATUS_LOC), false);
  return true;
}