예제 #1
0
//===========================================================================
static unsigned THREADCALL ThreadTaskProc (AsyncThread * thread) {
    PerfAddCounter(kAsyncPerfThreadTaskThreadsActive, 1);

    for (;;) {
        long desired = AsyncPerfGetCounter(kAsyncPerfThreadTaskThreadsDesired);
        if (AsyncPerfGetCounter(kAsyncPerfThreadTaskThreadsRunning) > desired) {
            long runningCount = PerfSubCounter(kAsyncPerfThreadTaskThreadsRunning, 1) - 1;
            if (runningCount >= desired) {
                if (runningCount > desired)
                    PostQueuedCompletionStatus(s_taskPort, 0, 0, 0);
                break;
            }
            PerfAddCounter(kAsyncPerfThreadTaskThreadsRunning, 1);
        }

        // Get the next work item
        DWORD bytes;
        ThreadTask * task;
        LPOVERLAPPED op;
        PerfSubCounter(kAsyncPerfThreadTaskThreadsActive, 1);
        (void) GetQueuedCompletionStatus(
            s_taskPort,
            &bytes,
            #ifdef _WIN64
            (PULONG_PTR) &task,
            #else
            (LPDWORD) &task,
            #endif
            &op,
            INFINITE
        );
        PerfAddCounter(kAsyncPerfThreadTaskThreadsActive, 1);

        if (task) {
            task->callback(task->param, task->taskList->error);

            task->taskList->Ref("task");
            delete task;
        }
    }
    PerfSubCounter(kAsyncPerfThreadTaskThreadsActive, 1);

    return 0;
}
/**
 * Static method that can get passed to RTThreadCreate to have a
 * thread started for a Task.
 */
/* static */ DECLCALLBACK(int) ThreadTask::taskHandlerThreadProc(RTTHREAD /* thread */, void *pvUser)
{
    if (pvUser == NULL)
        return VERR_INVALID_POINTER; /* nobody cares */

    ThreadTask *pTask = static_cast<ThreadTask *>(pvUser);

    LogFunc(("Started \"%s\"\n", pTask->m_strTaskName.c_str()));

    /*
     *  handler shall catch and process all possible cases as errors and exceptions.
     */
    pTask->handler();

    LogFunc(("Ended \"%s\"\n", pTask->m_strTaskName.c_str()));

    delete pTask;
    return VINF_SUCCESS;
}
예제 #3
0
void WorkerThread::DoTasks() {
	
	ThreadTask* task = 0;

	do
	{
		//Process any tasks in our queue
		while (task = PopTask())
		{
			task->Run();
			delete task;
		}

		//We ran out of tasks, so try to see if we can steal one
		task = StealTask();
		if (task) {
			task->Run();
			delete task;
		}

	} while (task);	
}
예제 #4
0
파일: Thread.cpp 프로젝트: neodyme60/Xli
    void Thread::thread_func(void* arg)
    {
        Thread* thread = (Thread*)arg;
        ThreadTask* task = thread->_task;
        
        task->_stopRequested = false;
        SetCurrentThreadName(task->ToString());

        try
        {
            task->Run();
        }
        catch (const Exception& e)
        {
            CoreLib::OnUnhandledException(e, task->ToString());
        }
        catch (...)
        {
            Xli::Exception e("An unsupported C++ exception was thrown");
            CoreLib::OnUnhandledException(e, task->ToString());
        }

        thread->_state = ThreadStateStopped;
    }