Exemple #1
0
NPT_Result TaskGroup::startTask(Task *task)
{
	WriteLocker locker(m_stateLock);

	if (m_state != State_Running) {
		delete task;
		return NPT_ERROR_INVALID_STATE;
	}

	joinThreads();

	task->m_taskGroup = this;

	{
		TaskThread *thread = NULL;
		WriteLocker locker(m_freeThreadListLock);
		NPT_List<TaskThread*>::Iterator it = m_freeThreadList.GetFirstItem();
		if (it) {
			thread = *it;
			m_freeThreadList.Erase(it);
			thread->setTask(task);
			return NPT_SUCCESS;
		}
	}

	return startNewThread(task);
}
Exemple #2
0
void	*TaskThread::loop(void *param)
{
  TaskThread*     obj = reinterpret_cast<TaskThread*>(param);

  obj->mainloop();
  return 0;
}
Exemple #3
0
inline void ThreadPool::AddThread(int count)
{
    for (int i = 0; i < count; i++)
    {
        TaskThread* thread = new TaskThread(queue);
        thread->Start();
        threads.push_back(thread);
        histThread++;
    }
}
void ThreadPool::start() {
	if (started) return;

	for (int i=0; i<threadCount; ++i) {
		TaskThread* thread = new TaskThread();
		threads << thread;
		thread->start();
	}
	started = true;
}
Exemple #5
0
inline void ThreadPool::Priority(Task* t)
{
    if (queue.Priority(t))
    {
        TaskThread* thread = new TaskThread(queue);
        thread->Start();
        thread->SetStop();
        stopThreads.push_back(thread);
        histThread++;
    }
}
Exemple #6
0
void TaskGroup::abort()
{
	WriteLocker locker(m_stateLock);
	if (m_state == State_Running) {
		m_state = State_Stopping;
		for (NPT_Ordinal i = 0; i < m_threadList.GetItemCount(); i++) {
			TaskThread *thread = *m_threadList.GetItem(i);
			thread->abort();
		}
	}
}
Exemple #7
0
NPT_Result TaskGroup::startNewThread(Task *task)
{
	TaskThread *thread = new TaskThread(this);
	WriteLocker locker(m_threadListLock);
	m_threadList.Add(thread);
	NPT_Result nr = thread->start(task);
	if (NPT_FAILED(nr)) {
		m_threadList.Remove(thread);
		delete thread;
		delete task;
	}
	return nr;
}
void ThreadPool::start()
{
    if (m_started)
    {
        return;
    }

    for (int i = 0;i<m_threadCount;++i)
    {
        TaskThread * thread = new TaskThread();
        m_threads << thread;
        thread->start();
    }
    m_started = true;
}
Exemple #9
0
void TaskGroup::joinThreads()
{
	NPT_Cardinal count = m_threadList.GetItemCount();
	NPT_Ordinal i = 0;
	while (i < count) {
		NPT_List<TaskThread*>::Iterator it = m_threadList.GetItem(i);
		TaskThread *thread = *it;
		if (NPT_SUCCEEDED(thread->Wait(0))) {
			m_threadList.Erase(it);
			delete thread;
			--count;
		} else {
			i++;
		}
	}
}
Exemple #10
0
void
TaskService::adjustThreadCount()
{
    OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(_threadMutex);
    removeFinishedThreads();
    int numActiveThreads = 0;
    for( TaskThreads::iterator i = _threads.begin(); i != _threads.end(); i++ )
    {
        if (!(*i)->getDone()) numActiveThreads++;
    }

    int diff = _numThreads - numActiveThreads;
    if (diff > 0)
    {
        OE_DEBUG << LC << "Adding " << diff << " threads to TaskService " << std::endl;
        //We need to add some threads
        for (int i = 0; i < diff; ++i)
        {
            TaskThread* thread = new TaskThread( _queue.get() );
            _threads.push_back( thread );
            thread->start();
        }       
    }
    else if (diff < 0)
    {
        diff = osg::absolute( diff );
        OE_DEBUG << LC << "Removing " << diff << " threads from TaskService " << std::endl;
        int numRemoved = 0;
        //We need to remove some threads
        for( TaskThreads::iterator i = _threads.begin(); i != _threads.end(); i++ )
        {
            if (!(*i)->getDone())
            {
                (*i)->setDone( true );
                numRemoved++;
                if (numRemoved == diff) break;
            }
        }
    }  

    OE_INFO << LC << "TaskService [" << _name << "] using " << _numThreads << " threads" << std::endl;
}
Exemple #11
0
void PeriodicTaskScheduler::schedulerThreadRun(TaskThread& task) {
  while (sleepThread(task.tmoMs)) {
    try {
      task.func(*this);
    } catch (const std::exception& ex) {
      LOG_FAILURE("mcrouter", failure::Category::kOther,
                  "Error while executing periodic function: {}", ex.what());
      break;
    } catch (...) {
      LOG_FAILURE("mcrouter", failure::Category::kOther,
                  "Unknown error occured while executing periodic function");
      break;
    }
  }
}
Exemple #12
0
NPT_Result TaskGroup::wait(NPT_Timeout timeout)
{
	NPT_Result nr;
	{
		WriteLocker locker(m_stateLock);
		if (m_state != State_Stopping) {
			return NPT_ERROR_INVALID_STATE;
		}
	}

	if (timeout == NPT_TIMEOUT_INFINITE) {
		/*for (NPT_Ordinal i = 0; i < m_threadList.GetItemCount(); i++) {
			TaskThread *thread = *m_threadList.GetItem(i);
			thread->Wait();
			delete thread;
		}
		m_threadList.Clear();*/
		NPT_LOG_INFO_2("TaskGroup %p waiting for %d threads", this, m_threadList.GetItemCount());
		while (m_threadList.GetItemCount() > 0) {
			NPT_Cardinal count = m_threadList.GetItemCount();
			for (NPT_Ordinal i = 0; i < count; ) {
				NPT_List<TaskThread*>::Iterator it = m_threadList.GetItem(i);
				TaskThread *thread = *it;
				NPT_LOG_INFO_2("TaskGroup %p waiting for thread %p", this, thread);
				if (NPT_SUCCEEDED(thread->Wait(3000))) {
					NPT_LOG_INFO_2("TaskGroup %p waited thread %p, fine!", this, thread);
					delete thread;
					m_threadList.Erase(it);
					count--;
				} else {
					NPT_LOG_INFO_2("TaskGroup %p thread %p still pending, continue", this, thread);
					i++;
				}
			}
		}
	} else {
		NPT_Timeout waitTick = 10;
		if (waitTick > timeout) {
			waitTick = timeout;
		}

		NPT_TimeStamp ts1, ts2;
		NPT_Timeout totalTick = 0;

		while (m_threadList.GetItemCount() != 0 && timeout >= totalTick) {

			NPT_Cardinal count = m_threadList.GetItemCount();
			NPT_Ordinal i = 0;
			while (i < count) {
				NPT_List<TaskThread*>::Iterator it = m_threadList.GetItem(i);
				TaskThread *thread = *it;
				NPT_System::GetCurrentTimeStamp(ts1);
				nr = thread->Wait(waitTick);
				NPT_System::GetCurrentTimeStamp(ts2);
				totalTick += (ts2 - ts1).ToMillis();

				if (NPT_SUCCEEDED(nr)) {
					m_threadList.Erase(it);
					delete thread;
					--count;
				} else {
					i++;
				}

				if (timeout >= totalTick) {
					NPT_Timeout remainTick = timeout - totalTick;
					if (waitTick > remainTick) {
						waitTick = remainTick;
					}
				} else {
					waitTick = 0;
				}
			}
		}
	}

	if (m_threadList.GetItemCount() == 0) {
		m_state = State_Stopped;
	}

	return m_threadList.GetItemCount() == 0 ? NPT_SUCCESS : NPT_ERROR_TIMEOUT;
}