示例#1
0
int Thread::start(int32_t id) {
    Thread *thread = getThread(id);

    if (thread == NULL || thread->starting || id==main_threadid)
        return 1;

    if(thread->state == THREAD_RUNNING)
        return 2;

    if(thread->terminated)
        return 4;

    thread->exited = false;
    thread->state = THREAD_CREATED;
    thread->starting = 1;
#ifdef WIN32_
    thread->thread = CreateThread(
            NULL,                   // default security attributes
            0,                      // use default stack size
            &vm->InterpreterThreadStart,       // thread function caller
            thread,                 // thread self when thread is created
            0,                      // use default creation flags
            NULL);
    if(thread->thread == NULL) return 3; // thread was not started
    else return waitForThread(thread);
#endif
#ifdef POSIX_
    if(pthread_create( &thread->thread, NULL, vm->InterpreterThreadStart, (void*) thread))
        return 3; // thread was not started
    else {
        return waitForThread(thread);
    }
#endif

}
示例#2
0
int Thread::startDaemon(
#ifdef WIN32_
        DWORD WINAPI
#endif
#ifdef POSIX_
void*
#endif
(*threadFunc)(void *), Thread *thread) {
    if (thread == NULL || !thread->daemon)
        return 1;

    if(thread->state == THREAD_RUNNING)
        return 2;

    thread->exited = false;
#ifdef WIN32_
    thread->thread = CreateThread(
            NULL,                   // default security attributes
            0,                      // use default stack size
            (LPTHREAD_START_ROUTINE)threadFunc,       // thread function caller
            thread,                 // thread self when thread is created
            0,                      // use default creation flags
            NULL);
    if(thread->thread == NULL) return 3; // thread was not started
    else
        return waitForThread(thread);
#endif
#ifdef POSIX_
    if(pthread_create( &thread->thread, NULL, threadFunc, (void*) thread)!=0)
        return 3; // thread was not started
    else
        return waitForThread(thread);
#endif
}
示例#3
0
TEST(TestMultipleSharedSection, General)
{
    CSharedSection sec;

    CEvent event;
    std::atomic<long> mutex(0L);

    locker<CSharedLock> l1(sec,&mutex, &event);

    {
        CSharedLock lock(sec);
        thread waitThread1(l1);

        EXPECT_TRUE(waitForThread(mutex,1,10000));
        SleepMillis(10);

        EXPECT_TRUE(l1.haslock);

        event.Set();

        EXPECT_TRUE(waitThread1.timed_join(MILLIS(10000)));
    }

    locker<CSharedLock> l2(sec,&mutex,&event);
    locker<CSharedLock> l3(sec,&mutex,&event);
    locker<CSharedLock> l4(sec,&mutex,&event);
    locker<CSharedLock> l5(sec,&mutex,&event);
    {
        CExclusiveLock lock(sec);
        thread waitThread1(l2);
        thread waitThread2(l3);
        thread waitThread3(l4);
        thread waitThread4(l5);

        EXPECT_TRUE(waitForThread(mutex,4,10000));
        SleepMillis(10);

        EXPECT_TRUE(!l2.haslock);
        EXPECT_TRUE(!l3.haslock);
        EXPECT_TRUE(!l4.haslock);
        EXPECT_TRUE(!l5.haslock);

        lock.Leave();

        EXPECT_TRUE(waitForWaiters(event,4,10000));

        EXPECT_TRUE(l2.haslock);
        EXPECT_TRUE(l3.haslock);
        EXPECT_TRUE(l4.haslock);
        EXPECT_TRUE(l5.haslock);

        event.Set();

        EXPECT_TRUE(waitThread1.timed_join(MILLIS(10000)));
        EXPECT_TRUE(waitThread2.timed_join(MILLIS(10000)));
        EXPECT_TRUE(waitThread3.timed_join(MILLIS(10000)));
        EXPECT_TRUE(waitThread4.timed_join(MILLIS(10000)));
    }
}
示例#4
0
//--------------------------
bool ofxTCPServer::close(){
    stopThread();
	if( !TCPServer.Close() ){
		ofLogWarning("ofxTCPServer") << "close(): couldn't close connections";
		waitForThread(false); // wait for the thread to finish
		return false;
    }else{
        ofLogVerbose("ofxTCPServer") << "Closing server";
		waitForThread(false); // wait for the thread to finish
		return true;
	}
}
示例#5
0
//--------------------------------------------------------------------------------
void ofxKinectV2::close(){
    if( bOpened ){
        waitForThread(true);
        protonect.closeKinect();
        bOpened = false;
    }
}
DetachThreadQueue::~DetachThreadQueue(){
	
	timeToStop = true;	//lets flag the thread so that it doesnt try access stuff while we delete things around	
	if(verbose) printf("DetachThreadQueue::~DetachThreadQueue() waiting for queue thread...\n");
	if (isThreadRunning())
		waitForThread();
	
	if(verbose) printf("DetachThreadQueue::~DetachThreadQueuedeleting deleting pending, processing and processed work units...\n");
	while ( processing.size() > 0 ){
		GenericWorkUnit * w = processing[0];
		if ( w->isThreadRunning() ){
			w->weAreBeingDeleted = true;
			w->cancel();
			if(verbose) printf("WorkUnit(%d) waitForThread at destructor \n", w->getID());
			w->waitForThread();
		}
		processing.erase( processing.begin() );
		delete w;
	}	
	while ( pending.size() > 0 ){		
		GenericWorkUnit * w = pending[0];
		pending.erase( pending.begin() );
		delete w;		
	}
	while ( processed.size() > 0 ){		
		GenericWorkUnit * w = processed[0];
		processed.erase( processed.begin() );
		delete w;
	}
	if(verbose) printf("~DetachThreadQueue() done!\n");
}
void ofxThreadedMidiPlayer::stop(){
    cout << __PRETTY_FUNCTION__ << endl;
    stopThread();
    waitForThread();
    // clean();
    
}
//--------------------------------------------------------------
void ofxVideoRecorder::threadedFunction()
{
    if(bRecordVideo && bRecordAudio) {
        while(frames.size() > 0 && audioFrames.size() > 0) {
            // if there are frames in the queue or the thread is writing, signal them until the work is done.
            videoThread.signal();
            audioThread.signal();
        }
    }
    else if(bRecordVideo) {
        while(frames.size() > 0) {
            // if there are frames in the queue or the thread is writing, signal them until the work is done.
            videoThread.signal();
        }
    }
    else if(bRecordAudio) {
        while(audioFrames.size() > 0) {
            // if there are frames in the queue or the thread is writing, signal them until the work is done.
            audioThread.signal();
        }
    }
    
    waitForThread();
    
    outputFileComplete();
}
示例#9
0
//---------------------------------------------------------------------------
void ofxKinect::close(){
	if(isThreadRunning()){
		waitForThread(true);
	}
	
	bNeedsUpdate	= false;
	bUpdateTex		= false;
}
KinectGrabber::~KinectGrabber(){
	// when the class is destroyed
	// close both channels and wait for
	// the thread to finish
    //	toAnalyze.close();
    //	analyzed.close();
	waitForThread(true);
}
示例#11
0
void ofxColorStream::exit()
{
	stopThread();
	waitForThread();

	stream->stop();
	stream->destroy();
}
//--------------------------------------------------------------
IntelFaceScanner::~IntelFaceScanner()
{
	scanningData.close();
	
	waitForThread(true);

	close();
}
示例#13
0
 //--------------------------------------------------------------
 void Reactor::exit(){
     if (context != NULL)
     {
         waitForThread(true);
         libwebsocket_context_destroy(context);
         context = NULL;
     }
 }
ImgAnalysisThread::~ImgAnalysisThread(){
	// when the class is destroyed
	// close both channels and wait for
	// the thread to finish
	toAnalyze.close();
	analyzed.close();
	waitForThread(true);
}
示例#15
0
//--------------------------------------------------------------------------------
void ofxKinectV2::close() {
	if (!bOpened)
		return;

	waitForThread(true);
	closeKinect();
	bOpened = false;
}
//--------------------------------------------------------------
void CloudsVHXAuth::update(ofEventArgs& args)
{
    if (bNotifyComplete) {
        
        //Wait to ensure the thread has stopped
        //JG Added this... Elie please advise
        waitForThread(true);
        
        Mode completedMode = mode;
        mode = WAITING;
        if (completedMode == REQUEST_TOKEN) {
            ofNotifyEvent(requestTokenComplete, completeArgs);
        }
        else if (completedMode == REFRESH_TOKEN) {
            ofNotifyEvent(refreshTokenComplete, completeArgs);
        }
        else if (completedMode == REQUEST_CODE) {
            ofNotifyEvent(requestCodeComplete, completeArgs);
        }
        else if (completedMode == LINK_CODE) {
            ofNotifyEvent(linkCodeComplete, completeArgs);
        }
        else if (completedMode == VERIFY_PACKAGE) {
            ofNotifyEvent(verifyPackageComplete, completeArgs);
        }

        bNotifyComplete = false;
    }
    
    time_t nowTime = (ofGetSystemTime() / 1000.f);
    
    // Refresh token if it's expired.
    if (mode == WAITING && _tokenExpiry && nowTime > _tokenExpiry) {
        if (_refreshToken.empty()) {
            requestToken();
        }
        else {
            refreshToken();
        }
    }
    
    // Reset code if it's expired.
    if (_codeExpiry && nowTime > _codeExpiry) {
        _code = "";
        _codeExpiry = 0;
        
        CloudsVHXEventArgs args;
        ofNotifyEvent(codeExpired, args);
    }
    
    // Update state if package expired.
    if (state == RENTAL && _packageExpiry && nowTime > _packageExpiry) {
        state = EXPIRED;

        CloudsVHXEventArgs args;
        ofNotifyEvent(packageExpired, args);
    }
}
示例#17
0
void tst_QWriteLocker::unlockAndRelockTest()
{
    class UnlockAndRelockThread : public tst_QWriteLockerThread
    {
    public:
        void run()
        {
            QWriteLocker locker(&lock);

            waitForTest();

            locker.unlock();

            waitForTest();

            locker.relock();

            waitForTest();
        }
    };

    thread = new UnlockAndRelockThread;
    thread->start();

    waitForThread();
    // lock should be locked by the QWriteLocker
    QVERIFY(!thread->lock.tryLockForWrite());
    releaseThread();

    waitForThread();
    // lock has been explicitly unlocked via QWriteLocker
    QVERIFY(thread->lock.tryLockForWrite());
    thread->lock.unlock();
    releaseThread();

    waitForThread();
    // lock has been explicity relocked via QWriteLocker
    QVERIFY(!thread->lock.tryLockForWrite());
    releaseThread();

    QVERIFY(thread->wait());

    delete thread;
    thread = 0;
}
示例#18
0
void tst_QMutexLocker::unlockAndRelockTest()
{
    class UnlockAndRelockThread : public tst_QMutexLockerThread
    {
    public:
        void run()
        {
            QMutexLocker locker(&mutex);

            waitForTest();

            locker.unlock();

            waitForTest();

            locker.relock();

            waitForTest();
        }
    };

    thread = new UnlockAndRelockThread;
    thread->start();

    waitForThread();
    // mutex should be locked by the QMutexLocker
    QVERIFY(!thread->mutex.tryLock());
    releaseThread();

    waitForThread();
    // mutex has been explicitly unlocked via QMutexLocker
    QVERIFY(thread->mutex.tryLock());
    thread->mutex.unlock();
    releaseThread();

    waitForThread();
    // mutex has been explicitly relocked via QMutexLocker
    QVERIFY(!thread->mutex.tryLock());
    releaseThread();

    QVERIFY(thread->wait());

    delete thread;
    thread = 0;
}
示例#19
0
void ofxOMXPlayer::close(ofEventArgs & a)
{
	
	ofLogVerbose() << "start ofxOMXPlayer::close";
	waitForThread(true);
	
	ofRemoveListener(ofEvents().exit, this, &ofxOMXPlayer::close);
	ofLogVerbose() << "reached end of ofxOMXPlayer::close";
}
示例#20
0
//---------------------------------------------------------------------------
void ofxKinect::close() {
    if(isThreadRunning()) {
        waitForThread(true);
    }

    //usleep(500000); // some time while thread is stopping ...

    //libusb_exit(NULL);
}
bool ofxNetworkSyncClientState::close(){
	if(isThreadRunning()){
		stopThread();
		waitForThread();
	}
	calibrator.close();
	server = NULL;
	tcpServer = NULL;
}
ofxThreadedImage::~ofxThreadedImage(){
	if (isThreadRunning()){
		try {
			waitForThread(true);
		}catch(Exception &ex) {
			ofLogError("ofxThreadedImage", "Exception at waitForThread %s\n", ex.displayText().c_str() );
		}
	}
}
示例#23
0
TEST(TestSharedSection, GetSharedLockWhileTryingExclusiveLock)
{
    std::atomic<long> mutex(0L);
    CEvent event;

    CSharedSection sec;

    CSharedLock l1(sec); // get a shared lock

    locker<CExclusiveLock> l2(sec,&mutex);
    thread waitThread1(l2); // try to get an exclusive lock

    EXPECT_TRUE(waitForThread(mutex,1,10000));
    SleepMillis(10);  // still need to give it a chance to move ahead

    EXPECT_TRUE(!l2.haslock);  // this thread is waiting ...
    EXPECT_TRUE(!l2.obtainedlock);  // this thread is waiting ...

    // now try and get a SharedLock
    locker<CSharedLock> l3(sec,&mutex,&event);
    thread waitThread3(l3); // try to get a shared lock
    EXPECT_TRUE(waitForThread(mutex,2,10000));
    SleepMillis(10);
    EXPECT_TRUE(l3.haslock);

    event.Set();
    EXPECT_TRUE(waitThread3.timed_join(MILLIS(10000)));

    // l3 should have released.
    EXPECT_TRUE(!l3.haslock);

    // but the exclusive lock should still not have happened
    EXPECT_TRUE(!l2.haslock);  // this thread is waiting ...
    EXPECT_TRUE(!l2.obtainedlock);  // this thread is waiting ...

    // let it go
    l1.Leave(); // the last shared lock leaves.

    EXPECT_TRUE(waitThread1.timed_join(MILLIS(10000)));

    EXPECT_TRUE(l2.obtainedlock);  // the exclusive lock was captured
    EXPECT_TRUE(!l2.haslock);  // ... but it doesn't have it anymore
}
    void cancel(){
		if(loading){
			ofRemoveListener(ofEvents().update, this, &ofxImageSequenceLoader::updateThreadedLoad);
            lock();
			cancelLoading = true;
            unlock();
            loading = false;
			waitForThread(true);
		}
    }
ThreadedAudioDevice::~ThreadedAudioDevice()
{
	// This code handles the rare case where the child thread is starting
	// at the same instant that the device is being destroyed.
	PRINT1("~ThreadedAudioDevice\n");
	if (starting() && _thread != 0) {
		waitForThread();
		starting(false);
	}
}
//--------------------------------------------------------------
ofxThreadedVideo::~ofxThreadedVideo(){

    // stop threading
    waitForThread(true);

    // close anything left open
    videos[0].close();
    videos[1].close();

}
示例#27
0
void tst_QWriteLocker::scopeTest()
{
    class ScopeTestThread : public tst_QWriteLockerThread
    {
    public:
        void run()
        {
            waitForTest();

            {
                QWriteLocker locker(&lock);
                waitForTest();
            }

            waitForTest();
        }
    };

    thread = new ScopeTestThread;
    thread->start();

    waitForThread();
    // lock should be unlocked before entering the scope that creates the QWriteLocker
    QVERIFY(thread->lock.tryLockForWrite());
    thread->lock.unlock();
    releaseThread();

    waitForThread();
    // lock should be locked by the QWriteLocker
    QVERIFY(!thread->lock.tryLockForWrite());
    releaseThread();

    waitForThread();
    // lock should be unlocked when the QWriteLocker goes out of scope
    QVERIFY(thread->lock.tryLockForWrite());
    thread->lock.unlock();
    releaseThread();

    QVERIFY(thread->wait());

    delete thread;
    thread = 0;
}
//--------------------------------------------------------------
void Reactor::exit() {
    if (context != NULL)
    {
        waitForThread(true);
        // on windows the app does crash if the context is destroyed
        // while the thread or the library still might hold pointers
        // better to live with non deleted memory, or?
        //libwebsocket_context_destroy(context);
        context = NULL;
    }
}
示例#29
0
//---------------------------------------------------------------------------
void ofxKinect::close() {
	if(isThreadRunning()) {
		waitForThread(true);
	}

	deviceId = -1;
	serial = "";
	bIsFrameNew = false;
	bNeedsUpdate = false;
	bUpdateTex = false;
}
int ThreadedAudioDevice::doStop()
{
	if (!stopping()) {
		PRINT1("\tThreadedAudioDevice::doStop\n");
		stopping(true);		// signals play thread
		paused(false);
		waitForThread();
		starting(false);
	}
	return 0;
}