Example #1
0
OTSYS_THREAD_RETURN Dispatcher::dispatcherThread(void* p)
{
	#if defined __EXCEPTION_TRACER__
	ExceptionHandler dispatcherExceptionHandler;
	dispatcherExceptionHandler.InstallHandler();
	#endif
	srand((uint32_t)OTSYS_TIME());

	OutputMessagePool* outputPool = NULL;
	while(Dispatcher::m_threadState != Dispatcher::STATE_TERMINATED)
	{
		Task* task = NULL;
		// check if there are tasks waiting
		OTSYS_THREAD_LOCK(getDispatcher().m_taskLock, "");
		if(getDispatcher().m_taskList.empty()) //if the list is empty wait for signal
			OTSYS_THREAD_WAITSIGNAL(getDispatcher().m_taskSignal, getDispatcher().m_taskLock);

		if(!getDispatcher().m_taskList.empty() && Dispatcher::m_threadState != Dispatcher::STATE_TERMINATED)
		{
			// take the first task
			task = getDispatcher().m_taskList.front();
			getDispatcher().m_taskList.pop_front();
		}

		OTSYS_THREAD_UNLOCK(getDispatcher().m_taskLock, "");
		// finally execute the task...
		if(!task)
			continue;

		if(!task->hasExpired())
		{
			if((outputPool = OutputMessagePool::getInstance()))
				outputPool->startExecutionFrame();

			(*task)();
			if(outputPool)
				outputPool->sendAll();

			g_game.clearSpectatorCache();
		}

		delete task;
	}

	#if defined __EXCEPTION_TRACER__
	dispatcherExceptionHandler.RemoveHandler();
	#endif
	#if not defined(WIN32)
	return NULL;
	#endif
}
void Dispatcher::dispatcherThread(void* p)
{
	Dispatcher* dispatcher = (Dispatcher*)p;
	#if defined __EXCEPTION_TRACER__
	ExceptionHandler dispatcherExceptionHandler;
	dispatcherExceptionHandler.InstallHandler();
	#endif
	srand((uint32_t)OTSYS_TIME());

	OutputMessagePool* outputPool = NULL;
	boost::unique_lock<boost::mutex> taskLockUnique(dispatcher->m_taskLock, boost::defer_lock);
	while(Dispatcher::m_threadState != Dispatcher::STATE_TERMINATED)
	{
		Task* task = NULL;
		// check if there are tasks waiting
		taskLockUnique.lock();
		if(dispatcher->m_taskList.empty()) //if the list is empty wait for signal
			dispatcher->m_taskSignal.wait(taskLockUnique);

		if(!dispatcher->m_taskList.empty() && Dispatcher::m_threadState != Dispatcher::STATE_TERMINATED)
		{
			// take the first task
			task = dispatcher->m_taskList.front();
			dispatcher->m_taskList.pop_front();
		}

		taskLockUnique.unlock();
		// finally execute the task...
		if(!task)
			continue;

		if(!task->hasExpired())
		{
			if((outputPool = OutputMessagePool::getInstance()))
				outputPool->startExecutionFrame();

			(*task)();
			if(outputPool)
				outputPool->sendAll();

			g_game.clearSpectatorCache();
		}

		delete task;
	}

	#if defined __EXCEPTION_TRACER__
	dispatcherExceptionHandler.RemoveHandler();
	#endif
}
Example #3
0
void Dispatcher::flush()
{
    while (!m_taskList.empty()) {
        Task* task = m_taskList.front();
        m_taskList.pop_front();
        (*task)();
        delete task;

        OutputMessagePool* outputPool = OutputMessagePool::getInstance();
        if (outputPool) {
            outputPool->sendAll();
        }

        g_game.clearSpectatorCache();
    }
}
Example #4
0
void Dispatcher::flush()
{
	Task* task = NULL;
	while(!m_taskList.empty()){
		task = m_taskList.front();
		m_taskList.pop_front();
		(*task)();
		delete task;
		OutputMessagePool* outputPool = OutputMessagePool::getInstance();
		if(outputPool)
			outputPool->sendAll();
		g_game.clearSpectatorCache();
	}
	#ifdef __DEBUG_SCHEDULER__
	std::cout << "Flushing Dispatcher" << std::endl;
	#endif
}
Example #5
0
void Dispatcher::dispatcherThread()
{
    OutputMessagePool* outputPool = OutputMessagePool::getInstance();

    // NOTE: second argument defer_lock is to prevent from immediate locking
    std::unique_lock<std::mutex> taskLockUnique(m_taskLock, std::defer_lock);

    while (m_threadState != STATE_TERMINATED) {
        Task* task = nullptr;

        // check if there are tasks waiting
        taskLockUnique.lock();

        if (m_taskList.empty()) {
            //if the list is empty wait for signal
            m_taskSignal.wait(taskLockUnique);
        }

        if (!m_taskList.empty() && (m_threadState != STATE_TERMINATED)) {
            // take the first task
            task = m_taskList.front();
            m_taskList.pop_front();
        }

        taskLockUnique.unlock();

        // finally execute the task...
        if (task) {
            if (!task->hasExpired()) {
                outputPool->startExecutionFrame();
                (*task)();
                outputPool->sendAll();

                g_game.clearSpectatorCache();
            }

            delete task;
        }
    }
}
Example #6
0
void Dispatcher::dispatcherThread()
{
	OutputMessagePool* outputPool = OutputMessagePool::getInstance();

	// NOTE: second argument defer_lock is to prevent from immediate locking
	std::unique_lock<std::mutex> taskLockUnique(taskLock, std::defer_lock);

	while (getState() != THREAD_STATE_TERMINATED) {
		// check if there are tasks waiting
		taskLockUnique.lock();

		if (taskList.empty()) {
			//if the list is empty wait for signal
			taskSignal.wait(taskLockUnique);
		}

		if (!taskList.empty()) {
			// take the first task
			Task* task = taskList.front();
			taskList.pop_front();
			taskLockUnique.unlock();

			if (!task->hasExpired()) {
				// execute it
				outputPool->startExecutionFrame();
				(*task)();
				outputPool->sendAll();

				g_game.map.clearSpectatorCache();
			}
			delete task;
		} else {
			taskLockUnique.unlock();
		}
	}
}
Example #7
0
void Dispatcher::dispatcherThread(void* p)
{
	#if defined __EXCEPTION_TRACER__
	ExceptionHandler dispatcherExceptionHandler;
	dispatcherExceptionHandler.InstallHandler();
	#endif
	srand((unsigned int)OTSYS_TIME());
	#ifdef __DEBUG_SCHEDULER__
	std::cout << "Starting Dispatcher" << std::endl;
	#endif

	OutputMessagePool* outputPool;

	// NOTE: second argument defer_lock is to prevent from immediate locking
	boost::unique_lock<boost::mutex> taskLockUnique(getDispatcher().m_taskLock, boost::defer_lock);

	while(Dispatcher::m_threadState != Dispatcher::STATE_TERMINATED){
		Task* task = NULL;

		// check if there are tasks waiting
		taskLockUnique.lock();//getDispatcher().m_taskLock.lock();

		if(getDispatcher().m_taskList.empty()){
			//if the list is empty wait for signal
			#ifdef __DEBUG_SCHEDULER__
			std::cout << "Dispatcher: Waiting for task" << std::endl;
			#endif
			getDispatcher().m_taskSignal.wait(taskLockUnique);
		}

		#ifdef __DEBUG_SCHEDULER__
		std::cout << "Dispatcher: Signalled" << std::endl;
		#endif

		if(!getDispatcher().m_taskList.empty() && (Dispatcher::m_threadState != Dispatcher::STATE_TERMINATED)){
			// take the first task
			task = getDispatcher().m_taskList.front();
			getDispatcher().m_taskList.pop_front();
		}

		taskLockUnique.unlock();

		// finally execute the task...
		if(task){
			OutputMessagePool::getInstance()->startExecutionFrame();
			(*task)();
			delete task;
			
			outputPool = OutputMessagePool::getInstance();
			if(outputPool){
				outputPool->sendAll();
			}

			g_game.clearSpectatorCache();
			#ifdef __DEBUG_SCHEDULER__
			std::cout << "Dispatcher: Executing task" << std::endl;
			#endif
		}
	}
#if defined __EXCEPTION_TRACER__
	dispatcherExceptionHandler.RemoveHandler();
#endif
}
Example #8
0
void Dispatcher::dispatcherThread(void* p)
{
	Dispatcher* dispatcher = static_cast<Dispatcher*>(p);
	ExceptionHandler dispatcherExceptionHandler;
	dispatcherExceptionHandler.InstallHandler();
#ifdef __DEBUG_SCHEDULER__
	std::cout << "Starting Dispatcher" << std::endl;
#endif
	OutputMessagePool* outputPool;
	// NOTE: second argument defer_lock is to prevent from immediate locking
	boost::unique_lock<boost::mutex> taskLockUnique(dispatcher->m_taskLock, boost::defer_lock);

	while (dispatcher->m_threadState != STATE_TERMINATED)
	{
		Task* task = NULL;
		// check if there are tasks waiting
		taskLockUnique.lock();

		if (dispatcher->m_taskList.empty())
		{
			//if the list is empty wait for signal
#ifdef __DEBUG_SCHEDULER__
			std::cout << "Dispatcher: Waiting for task" << std::endl;
#endif
			dispatcher->m_taskSignal.wait(taskLockUnique);
		}

#ifdef __DEBUG_SCHEDULER__
		std::cout << "Dispatcher: Signalled" << std::endl;
#endif

		if (!dispatcher->m_taskList.empty() && (dispatcher->m_threadState != STATE_TERMINATED))
		{
			// take the first task
			task = dispatcher->m_taskList.front();
			dispatcher->m_taskList.pop_front();
		}

		taskLockUnique.unlock();

		// finally execute the task...
		if (task)
		{
			if (!task->hasExpired())
			{
				OutputMessagePool::getInstance()->startExecutionFrame();
				(*task)();
				outputPool = OutputMessagePool::getInstance();

				if (outputPool)
				{
					outputPool->sendAll();
				}

				g_game.clearSpectatorCache();
			}

			delete task;
#ifdef __DEBUG_SCHEDULER__
			std::cout << "Dispatcher: Executing task" << std::endl;
#endif
		}
	}

	dispatcherExceptionHandler.RemoveHandler();
}