void
ArchMultithreadWindows::doThreadFunc(ArchThread thread)
{
    // wait for parent to initialize this object
    lockMutex(m_threadMutex);
    unlockMutex(m_threadMutex);

    void* result = NULL;
    try {
        // go
        result = (*thread->m_func)(thread->m_userData);
    }

    catch (XThreadCancel&) {
        // client called cancel()
    }
    catch (...) {
        // note -- don't catch (...) to avoid masking bugs
        SetEvent(thread->m_exit);
        closeThread(thread);
        throw;
    }

    // thread has exited
    lockMutex(m_threadMutex);
    thread->m_result = result;
    unlockMutex(m_threadMutex);
    SetEvent(thread->m_exit);

    // done with thread
    closeThread(thread);
}
예제 #2
0
bool
ArchMultithreadPosix::wait(ArchThread target, double timeout)
{
	assert(target != NULL);

	lockMutex(m_threadMutex);

	// find current thread
	ArchThreadImpl* self = findNoRef(pthread_self());

	// ignore wait if trying to wait on ourself
	if (target == self) {
		unlockMutex(m_threadMutex);
		return false;
	}

	// ref the target so it can't go away while we're watching it
	refThread(target);

	unlockMutex(m_threadMutex);

	try {
		// do first test regardless of timeout
		testCancelThreadImpl(self);
		if (isExitedThread(target)) {
			closeThread(target);
			return true;
		}

		// wait and repeat test if there's a timeout
		if (timeout != 0.0) {
			const double start = ARCH->time();
			do {
				// wait a little
				ARCH->sleep(0.05);

				// repeat test
				testCancelThreadImpl(self);
				if (isExitedThread(target)) {
					closeThread(target);
					return true;
				}

				// repeat wait and test until timed out
			} while (timeout < 0.0 || (ARCH->time() - start) <= timeout);
		}

		closeThread(target);
		return false;
	}
	catch (...) {
		closeThread(target);
		throw;
	}
}
static DWORD WINAPI MPThreadProc(LPVOID lpParameter) 
{
    javacall_amms_media_processor_s* pMP = 
            (javacall_amms_media_processor_s*)lpParameter;

    if (lpParameter == NULL)
        return (DWORD)-1;

    {
        javacall_amms_media_processor_s* pMP = 
                (javacall_amms_media_processor_s*)lpParameter;
        javacall_amms_frame* cur = javacall_amms_addref_frame(pMP->inputData);
        int i;

        if (cur == NULL) {
            javanotify_on_amms_notification(
                JAVACALL_EVENT_AMMS_MEDIA_PROCESSOR_ERROR, 
                pMP->media_processor_id, NULL);
            closeThread(pMP);
            return 0;
        }

        pMP->outputData = javacall_amms_release_frame(pMP->outputData);

        for (i = 0; i < pMP->filtersCnt; i++) {
            javacall_amms_frame* out;
            javacall_image_filter_handle pIF = pMP->filters[i];
            javacall_result result = pIF->process(pIF, cur, &out);
            javacall_amms_release_frame(cur);

            if (!JAVACALL_SUCCEEDED(result)) {
                javanotify_on_amms_notification(
                    JAVACALL_EVENT_AMMS_MEDIA_PROCESSOR_ERROR, 
                    pMP->media_processor_id, NULL);
                closeThread(pMP);
                return 0;
            }
            cur = out;
        }
        
        /// If there was no filters, pMP->outputData will be the same as input
        pMP->outputData = cur;

        javanotify_on_amms_notification(
            JAVACALL_EVENT_AMMS_MEDIA_PROCESSOR_COMPLETED, 
            pMP->media_processor_id, NULL);
        closeThread(pMP);
        return 0;
    }
}
예제 #4
0
DWORD TOOLSCLASS::ThreadFunc()
{
	MSG		messages;
	LOG("Start thread\n");

	GetLastError();
	hwnd = CreateDialog(hInstance, MAKEINTRESOURCE(idd), NULL, (DLGPROC) dlgproc);

	if (!hwnd) 
	{
		LOG("error creating dialog\n");
		return (-2);
	}

	ShowWindow(hwnd, SW_SHOW);
	UpdateWindow(hwnd);
	
	while (GetMessage (&messages, NULL, 0, 0))
	{
		TranslateMessage(&messages);
		DispatchMessage(&messages);
	}

	unregClass();
	hwnd = NULL;
	
	closeThread();
	return 0;
}
예제 #5
0
파일: threading.c 프로젝트: CSU-GH/okl4_3.0
void cleanUp(test_ll_t *test) {
	test_ll_t *pTmpTest = test;
	test_ll_t *pLastTest;
	while (pTmpTest != NULL) {
		pLastTest = pTmpTest;
		pTmpTest = pTmpTest->next;
		closeThread(pLastTest->hThread);
		FREE(pLastTest->args);
		FREE(pLastTest->env);
		FREE(pLastTest);
	}
}
예제 #6
0
/**
 * @brief EngineClient::~LocalServer
 *  Destructor
 */
EngineClient::~EngineClient()
{
    if(ipClient)
    {
        disconnect(this, SIGNAL(sendMessage(QString)), ipClient, SLOT(sendMessage(QString)));
        disconnect(ipClient, SIGNAL(messageIn(QString)), this, SLOT(slotOnData(QString)));
        disconnect(this, SIGNAL(closed()), ipClient, SLOT(doClose()));
        disconnect(this, SIGNAL(open()), ipClient, SLOT(doOpen()));
        disconnect(ipClient, SIGNAL(closeThread()), this, SLOT(connectionLost()));
        delete ipClient;
    }
    ipClient=NULL;
}
예제 #7
0
void
ArchMultithreadPosix::doThreadFunc(ArchThread thread)
{
	// default priority is slightly below normal
	setPriorityOfThread(thread, 1);

	// wait for parent to initialize this object
	lockMutex(m_threadMutex);
	unlockMutex(m_threadMutex);

	void* result = NULL;
	try {
		// go
		result = (*thread->m_func)(thread->m_userData);
	}

	catch (XThreadCancel&) {
		// client called cancel()
	}
	catch (...) {
		// note -- don't catch (...) to avoid masking bugs
		lockMutex(m_threadMutex);
		thread->m_exited = true;
		unlockMutex(m_threadMutex);
		closeThread(thread);
		throw;
	}

	// thread has exited
	lockMutex(m_threadMutex);
	thread->m_result = result;
	thread->m_exited = true;
	unlockMutex(m_threadMutex);

	// done with thread
	closeThread(thread);
}
예제 #8
0
/*
 * This routine will sit waiting for all threads to exit.  In
 * unix, this is done through pthread_join.  In Windows we
 * use a sleeping loop.
 */
void cleanUpTestChildren(test_ll_t *test)
{
	thread_struct_t *pTmpThread = NULL, *pTmpThreadLast = NULL;

	while (test->env->pThreads) {
		pTmpThread = test->env->pThreads->next;
		pTmpThreadLast = test->env->pThreads;

		closeThread(pTmpThreadLast->hThread);

		test->env->pThreads = pTmpThread;
		FREE(pTmpThreadLast);
		test->env->kids--;
	}
}
예제 #9
0
EngineClient::EngineClient() : QThread(NULL)
{
    if(!ipClient)
    {
        ipClient = new IpsEngineClient();
        connect(this, SIGNAL(sendMessage(QString)), ipClient, SLOT(sendMessage(QString)));
        connect(ipClient, SIGNAL(messageIn(QString)), this, SLOT(slotOnData(QString)));
        connect(this, SIGNAL(closed()), ipClient, SLOT(doClose()));
        connect(this, SIGNAL(open()), ipClient, SLOT(doOpen()));
        connect(ipClient, SIGNAL(closeThread()), this, SLOT(connectionLost()));
    }
    readyToSendLvlx = false;
    _connected = false;
    _busy = false;
    working=false;
    doSendData = false;
}
예제 #10
0
//------------------------------------------------------------
bool AflThread::startThread(AflThreadProc* paflThreadProc,LPVOID pvData)
{
	//スレッドに渡す値の設定
	LPVOID adwThreadData[] = {this,pvData};
	LPVOID pdwThreadData = new LPVOID[sizeof(adwThreadData)/sizeof(LPVOID)];
	memcpy(pdwThreadData,adwThreadData,sizeof(adwThreadData));
	
	closeThread();	//既に存在するスレッドを停止
	m_paflThreadProc = paflThreadProc;

	m_bEnable = true;
	//スレッドの作成
	m_hThread = createThread((LPVOID)threadProcServer,pdwThreadData,&m_dwThreadID);
    //スレッド作成後のウエイト
    while(!m_bEnable)
		Sleep(0);
	return m_hThread != 0;
}
예제 #11
0
 void MainWindow::creatconnect()
 {
     //slot
     connect(load, SIGNAL(clicked()), this, SLOT(openfile()));
     connect(count, SIGNAL(clicked()), this, SLOT(countholl()));
     connect(send,SIGNAL(clicked()),this,SLOT(wirtecomm()));
     connect(time,SIGNAL(timeout()),this,SLOT(readcomm()));//定时溢出实现读串口操作
     connect(finish,SIGNAL(clicked()),this,SLOT(closeThread()));//测试用的
     connect(compiler,SIGNAL(clicked()),this->textTop,SLOT(compilegcode()));
     //        connect(commthread,SIGNAL(finished()),this,SLOT(displaycomm()));      //接收信号实现显示
     connect(simulation,SIGNAL(clicked()),this,SLOT(simulater()));
     connect(zoomInIcon, SIGNAL(clicked()), this, SLOT(zoomIn()));
     connect(zoomOutIcon, SIGNAL(clicked()), this, SLOT(zoomOut()));
     connect(zoomSlider, SIGNAL(valueChanged(int)), this, SLOT(setupMatrix()));
     connect(configAct, SIGNAL(triggered()),
             this, SLOT(configsetting()));
     connect(openIniFileAct,SIGNAL(triggered()),
             this,SLOT(openfile()));
 }
예제 #12
0
void ThreadHandler::processCloseRequest(ImageThread* it, int reason) {
    closeThread(it);
}
bool
ArchMultithreadWindows::wait(ArchThread target, double timeout)
{
    assert(target != NULL);

    lockMutex(m_threadMutex);

    // find current thread
    ArchThreadImpl* self = findNoRef(GetCurrentThreadId());

    // ignore wait if trying to wait on ourself
    if (target == self) {
        unlockMutex(m_threadMutex);
        return false;
    }

    // ref the target so it can't go away while we're watching it
    refThread(target);

    unlockMutex(m_threadMutex);

    // convert timeout
    DWORD t;
    if (timeout < 0.0) {
        t = INFINITE;
    }
    else {
        t = (DWORD)(1000.0 * timeout);
    }

    // wait for this thread to be cancelled or woken up or for the
    // target thread to terminate.
    HANDLE handles[2];
    handles[0] = target->m_exit;
    handles[1] = self->m_cancel;
    DWORD result = WaitForMultipleObjects(2, handles, FALSE, t);

    // cancel takes priority
    if (result != WAIT_OBJECT_0 + 1 &&
        WaitForSingleObject(handles[1], 0) == WAIT_OBJECT_0) {
        result = WAIT_OBJECT_0 + 1;
    }

    // release target
    closeThread(target);

    // handle result
    switch (result) {
    case WAIT_OBJECT_0 + 0:
        // target thread terminated
        return true;

    case WAIT_OBJECT_0 + 1:
        // this thread was cancelled.  does not return.
        testCancelThreadImpl(self);

    default:
        // timeout or error
        return false;
    }
}
예제 #14
0
SquirrelThread::~SquirrelThread()
{
    closeThread();
}
int do_everything(int argc, LPCWSTR argv[]) {
    HRESULT hr = S_OK;

    // parse command line
    CPrefs prefs(argc, argv, hr);
    if (FAILED(hr)) {
        ERR(L"CPrefs::CPrefs constructor failed: hr = 0x%08x", hr);
        return -__LINE__;
    }
    if (S_FALSE == hr) {
        // nothing to do
        return 0;
    }

    // create a "loopback capture has started" event
    HANDLE hStartedEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
    if (NULL == hStartedEvent) {
        ERR(L"CreateEvent failed: last error is %u", GetLastError());
        return -__LINE__;
    }
    CloseHandleOnExit closeStartedEvent(hStartedEvent);

    // create a "stop capturing now" event
    HANDLE hStopEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
    if (NULL == hStopEvent) {
        ERR(L"CreateEvent failed: last error is %u", GetLastError());
        return -__LINE__;
    }
    CloseHandleOnExit closeStopEvent(hStopEvent);

    // create arguments for loopback capture thread
    LoopbackCaptureThreadFunctionArguments threadArgs;
    threadArgs.hr = E_UNEXPECTED; // thread will overwrite this
    threadArgs.pMMDevice = prefs.m_pMMDevice;
    threadArgs.bInt16 = prefs.m_bInt16;
    threadArgs.hFile = prefs.m_hFile;
    threadArgs.hStartedEvent = hStartedEvent;
    threadArgs.hStopEvent = hStopEvent;
    threadArgs.nFrames = 0;

    HANDLE hThread = CreateThread(
        NULL, 0,
        LoopbackCaptureThreadFunction, &threadArgs,
        0, NULL
    );
    if (NULL == hThread) {
        ERR(L"CreateThread failed: last error is %u", GetLastError());
        return -__LINE__;
    }
    CloseHandleOnExit closeThread(hThread);

    // wait for either capture to start or the thread to end
    HANDLE waitArray[2] = { hStartedEvent, hThread };
    DWORD dwWaitResult;
    dwWaitResult = WaitForMultipleObjects(
        _countof(waitArray), waitArray,
        FALSE, INFINITE
    );

    if (WAIT_OBJECT_0 + 1 == dwWaitResult) {
        ERR(L"Thread aborted before starting to loopback capture: hr = 0x%08x", threadArgs.hr);
        return -__LINE__;
    }

    if (WAIT_OBJECT_0 != dwWaitResult) {
        ERR(L"Unexpected WaitForMultipleObjects return value %u", dwWaitResult);
        return -__LINE__;
    }

    // at this point capture is running
    // wait for the user to press a key or for capture to error out
    {
        WaitForSingleObjectOnExit waitForThread(hThread);
        SetEventOnExit setStopEvent(hStopEvent);
        HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);

        if (INVALID_HANDLE_VALUE == hStdIn) {
            ERR(L"GetStdHandle returned INVALID_HANDLE_VALUE: last error is %u", GetLastError());
            return -__LINE__;
        }

        LOG(L"%s", L"Press Enter to quit...");

        HANDLE rhHandles[2] = { hThread, hStdIn };

        bool bKeepWaiting = true;
        while (bKeepWaiting) {

            dwWaitResult = WaitForMultipleObjects(2, rhHandles, FALSE, INFINITE);

            switch (dwWaitResult) {

            case WAIT_OBJECT_0: // hThread
                ERR(L"%s", L"The thread terminated early - something bad happened");
                bKeepWaiting = false;
                break;

            case WAIT_OBJECT_0 + 1: // hStdIn
                // see if any of them was an Enter key-up event
                INPUT_RECORD rInput[128];
                DWORD nEvents;
                if (!ReadConsoleInput(hStdIn, rInput, _countof(rInput), &nEvents)) {
                    ERR(L"ReadConsoleInput failed: last error is %u", GetLastError());
                    bKeepWaiting = false;
                }
                else {
                    for (DWORD i = 0; i < nEvents; i++) {
                        if (
                            KEY_EVENT == rInput[i].EventType &&
                            VK_RETURN == rInput[i].Event.KeyEvent.wVirtualKeyCode &&
                            !rInput[i].Event.KeyEvent.bKeyDown
                            ) {
                            LOG(L"%s", L"Stopping capture...");
                            bKeepWaiting = false;
                            break;
                        }
                    }
                    // if none of them were Enter key-up events,
                    // continue waiting
                }
                break;

            default:
                ERR(L"WaitForMultipleObjects returned unexpected value 0x%08x", dwWaitResult);
                bKeepWaiting = false;
                break;
            } // switch
        } // while
    } // naked scope

    // at this point the thread is definitely finished

    DWORD exitCode;
    if (!GetExitCodeThread(hThread, &exitCode)) {
        ERR(L"GetExitCodeThread failed: last error is %u", GetLastError());
        return -__LINE__;
    }

    if (0 != exitCode) {
        ERR(L"Loopback capture thread exit code is %u; expected 0", exitCode);
        return -__LINE__;
    }

    if (S_OK != threadArgs.hr) {
        ERR(L"Thread HRESULT is 0x%08x", threadArgs.hr);
        return -__LINE__;
    }

    // everything went well... fixup the fact chunk in the file
    MMRESULT result = mmioClose(prefs.m_hFile, 0);
    prefs.m_hFile = NULL;
    if (MMSYSERR_NOERROR != result) {
        ERR(L"mmioClose failed: MMSYSERR = %u", result);
        return -__LINE__;
    }

    // reopen the file in read/write mode
    MMIOINFO mi = {0};
    prefs.m_hFile = mmioOpen(const_cast<LPWSTR>(prefs.m_szFilename), &mi, MMIO_READWRITE);
    if (NULL == prefs.m_hFile) {
        ERR(L"mmioOpen(\"%ls\", ...) failed. wErrorRet == %u", prefs.m_szFilename, mi.wErrorRet);
        return -__LINE__;
    }

    // descend into the RIFF/WAVE chunk
    MMCKINFO ckRIFF = {0};
    ckRIFF.ckid = MAKEFOURCC('W', 'A', 'V', 'E'); // this is right for mmioDescend
    result = mmioDescend(prefs.m_hFile, &ckRIFF, NULL, MMIO_FINDRIFF);
    if (MMSYSERR_NOERROR != result) {
        ERR(L"mmioDescend(\"WAVE\") failed: MMSYSERR = %u", result);
        return -__LINE__;
    }

    // descend into the fact chunk
    MMCKINFO ckFact = {0};
    ckFact.ckid = MAKEFOURCC('f', 'a', 'c', 't');
    result = mmioDescend(prefs.m_hFile, &ckFact, &ckRIFF, MMIO_FINDCHUNK);
    if (MMSYSERR_NOERROR != result) {
        ERR(L"mmioDescend(\"fact\") failed: MMSYSERR = %u", result);
        return -__LINE__;
    }

    // write the correct data to the fact chunk
    LONG lBytesWritten = mmioWrite(
        prefs.m_hFile,
        reinterpret_cast<PCHAR>(&threadArgs.nFrames),
        sizeof(threadArgs.nFrames)
    );
    if (lBytesWritten != sizeof(threadArgs.nFrames)) {
        ERR(L"Updating the fact chunk wrote %u bytes; expected %u", lBytesWritten, (UINT32)sizeof(threadArgs.nFrames));
        return -__LINE__;
    }

    // ascend out of the fact chunk
    result = mmioAscend(prefs.m_hFile, &ckFact, 0);
    if (MMSYSERR_NOERROR != result) {
        ERR(L"mmioAscend(\"fact\") failed: MMSYSERR = %u", result);
        return -__LINE__;
    }

    // let prefs' destructor call mmioClose
    
    return 0;
}
예제 #16
0
void IpsEngineClient::disconnected()
{
    qDebug() << "disconnected...";
    emit closeThread();
}
예제 #17
0
//------------------------------------------------------------
bool AflThread::closeThread()
{
	bool bRet = closeThread(m_hThread);
	m_hThread = 0;
	return bRet;
}
예제 #18
0
THREADCLASS::~THREADCLASS()
{
	closeThread();
}
예제 #19
0
//------------------------------------------------------------
AflThread::~AflThread()
{
	closeThread();
}