Example #1
0
bool ThreadImpl::waitForQuit()
{
    if(getThread(this)->state == _ThreadImpl::NotStarted)
        return false;

    WaitForSingleObject(getThread(this)->thread, INFINITE);
    return true;
}
Example #2
0
bool ThreadManager::init(void)
{
    bool returnValue = true;

    FDEBUG(("OSGThreadManager init\n"))

#if defined(OSG_USE_SPROC)
    usconfig(CONF_AUTOGROW,   1);
    usconfig(CONF_INITUSERS, 20);
    usconfig(CONF_INITSIZE, 10 * 1048576);
    usconfig(CONF_CHMOD, 0666);

    _pArena = usinit("/dev/zero");

    if(_pArena == NULL)
    {
        SFATAL << "OSGTM : could not initialize arena " << errno << std::endl;
        returnValue = false;
    }
    else
    {
        SINFO << "OSGTM : got arena " << _pArena << std::endl;
    }
#endif

    _storePLock = _sLockStore.getMPField("OSGTMStoreLock", "OSGLock", true);
    
    if(_storePLock == NULL)
    {
        SFATAL << "OSGTM : could not get table lock" << std::endl;

        returnValue = false;
    }
    else
    {
        SINFO << "OSGTM : got table lock " << _storePLock << std::endl;
    }

    if(_szAppThreadType == NULL)
    {
        FINFO(("OSGTM : create -OSGBaseThread- app thread\n"));

        _pAppThread = getThread("OSGAppThread", true, "OSGBaseThread");
    }
    else
    {
        FINFO(("OSGTM : create -%s- app thread\n", _szAppThreadType));

        _pAppThread = getThread("OSGAppThread", true, _szAppThreadType);
    }

    FFASSERT((_pAppThread != NULL), 1, 
             ("OSGTM : could not get application thread \n"););
Example #3
0
void Needle::setTransformFromEndEffectorBoxConstrained(const Vector3d& new_ee_pos, const Matrix3d& new_ee_rot, Box* box)
{
	Vector3d old_ee_pos;
	Matrix3d old_ee_rot;
	getEndEffectorTransform(old_ee_pos, old_ee_rot);

	Vector3d old_proj = (old_ee_pos - position);
	old_proj.normalize();
	Vector3d proj = (new_ee_pos-position).dot(rotation.col(0))*rotation.col(0) + (new_ee_pos-position).dot(rotation.col(2))*rotation.col(2);
	proj.normalize();

	glColor3f(0.8,0.8,0);
	drawArrow(position, 20*proj);
	glColor3f(0.8,0,0);
	drawArrow(position, 20*old_proj);

	Vector3d normal = proj.cross(old_proj);
	normal.normalize();

	double angle = angle_between(proj, old_proj)*180.0/M_PI;
	if (angle > 90.0) {
		angle = 0.0;
		//TODO return;
	} else {
		double sign = ((normal - rotation.col(1)).squaredNorm() < 1e-5) ? -1 : 1;
		angle = sign*min(angle_between(proj, old_proj)*180.0/M_PI, 5.0);
	}

	if (!isnan(angle) && abs(angle)>1e-5 && angle>0.0) { //TODO for now, only allows forward movement of needle through box
		rotateAboutAxis(angle);
		if(isThreadAttached()) {
			thread->updateConstrainedTransform(constraint_ind, getEndPosition(), getEndRotation());
		}
	}
	
	if (isThreadAttached()) {
		Vector3d direction;
		if (!box->isThreadAttached() && (sphereBoxDistance(getEndPosition(), getThicknessRadius(), box->getPosition(), box->getHalfLength(), direction) < 0)) {
		  cout << "thread gets into the box" << endl;
		  //box->attach(getThread());
			box->attachThreadIn(getThread(), getEndPosition(), getEndRotation());
		}
		if (box->isThreadAttached() && !boxCollision(box->getPosition(), box->getHalfLength())) {
		  cout << "thread gets out of the box" << endl;
		  box->attachThreadOut(getThread(), getEndPosition(), getEndRotation());
		}
	}
}
Example #4
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

}
Example #5
0
int Thread::destroy(int64_t id) {
    if(id == thread_self->id || id==main_threadid)
        return 1; // cannot destroy thread_self or main

    Thread* thread = getThread(id);
    if(thread == NULL || thread->daemon)
        return 1;

    if (thread->state == THREAD_KILLED || thread->terminated)
    {
        if (thread->id == main_threadid)
        {
            return 3; // should never happen
        }
        else
        {
            popThread(thread);
            thread->term();
            std::free (thread);
            return 0;
        }
    } else {
        return 2;
    }
}
Example #6
0
uint32_t ThreadImpl::setAffinityMask(uint32_t mask)
{
    if(mask)
    {
        // store affinity
        getThread(this)->affinityMask = mask;

        // if thread already started apply immediately
        if(getThread(this)->state == _ThreadImpl::Started)
        {
            uint32_t err = uint32_t(SetThreadAffinityMask(getThread(this)->thread, mask));
            return err;
        }
    }

    return 0;
}
/**
 * Instructs the MonitorThread to terminate.
 */
void MonitorThread::terminate() {
	mutex.lock();
	shouldTerminate = true;
	mutex.unlock();

	// Wake up the MonitorThread in case it is sleeping.
	Thread::notify(getThread());
}
Example #8
0
	Handle<Value> BThread::_start(const Arguments& args){
		HandleScope store;
		base::Thread* th = getThread(args.Holder());
		int v;
		if(args.Length()>0) v = args[0]->Uint32Value();
		else v = 0;
		th->CallStd<void>(&BThread::_threadProc,(void*)(INT_PTR)v);
		return Undefined();
	}
Example #9
0
void NMS_SceneRenderer::down()
{
	rendering=false;
	SDL_Thread* me = getThread();
	if(me)	
		SDL_WaitThread(me, NULL);
	sceneGraphRoot = NULL;
	sceneGraphGuard = NULL;
	inputGuard=NULL;
}
Example #10
0
    bool isThreadUrlValid(const QString &url)
    {
        if ( getThread(url).isEmpty() ||
             getBoard(url).isEmpty() ||
             getHost(url).isEmpty() )
        {
            return false;
        }

        return true;
    }
Example #11
0
int Thread::join(int32_t id) {
    if (id == thread_self->id || id==main_threadid)
        return 1;

    Thread* thread = getThread(id);
    if (thread == NULL || thread->daemon)
        return 1;
    if(thread->terminated)
        return 2;

    return threadjoin(thread);
}
Example #12
0
void ThreadImpl::start(uint32_t stackSize, Runnable* runnable)
{
    if(getThread(this)->state != _ThreadImpl::NotStarted)
        return;
    getThread(this)->state = _ThreadImpl::Started;

    if(runnable && !getThread(this)->arg && !getThread(this)->fn)
        getThread(this)->arg = runnable;

    getThread(this)->thread =
        CreateThread(NULL, stackSize, NvThreadStart, (LPVOID) this, CREATE_SUSPENDED, &getThread(this)->threadID);

    // set affinity and resume
    if(getThread(this)->affinityMask)
        setAffinityMask(getThread(this)->affinityMask);

    ResumeThread(getThread(this)->thread);
}
Example #13
0
void ThreadImpl::setPriority(ThreadPriority::Enum prio)
{
    switch(prio)
    {
    case ThreadPriority::eHIGH:
        SetThreadPriority(getThread(this)->thread, THREAD_PRIORITY_HIGHEST);
        break;
    case ThreadPriority::eABOVE_NORMAL:
        SetThreadPriority(getThread(this)->thread, THREAD_PRIORITY_ABOVE_NORMAL);
        break;
    case ThreadPriority::eNORMAL:
        SetThreadPriority(getThread(this)->thread, THREAD_PRIORITY_NORMAL);
        break;
    case ThreadPriority::eBELOW_NORMAL:
        SetThreadPriority(getThread(this)->thread, THREAD_PRIORITY_BELOW_NORMAL);
        break;
    case ThreadPriority::eLOW:
        SetThreadPriority(getThread(this)->thread, THREAD_PRIORITY_LOWEST);
        break;
    default:
        break;
    }
}
Example #14
0
int gtthread_join(gtthread_t thread, void **status)
{
  gtthread *target_thread, *callingThread;
  sigset_t oldset;

  /* Block alarms */
  sigprocmask(SIG_BLOCK, &alrm, &oldset);

  /* find target thread in globalQ */
  target_thread = getThread(thread);

  if(target_thread != NULL) /*target thread found */
  {
  	/* Check if it has finished */
	if(target_thread -> finished) 
	{
		/*If it has finished, unblock alarm and then set status and return*/
		sigprocmask(SIG_UNBLOCK,&alrm, NULL);
	}
	else
	{
		/* If not finished, get the currently running thread(calling thread)
		   and queue it to join target */
		callingThread = (gtthread *) steque_pop(&currently_running);
		steque_enqueue(&target_thread -> joining, callingThread);
		/* schedule next thread */
		scheduleNextAndSwap(callingThread);
		/* Now unblock alarms */
		sigprocmask(SIG_UNBLOCK, &alrm, NULL);
	}


	/* Set status */
	if(status != NULL)
	{
		*status = target_thread -> retval;
	}

	/* successful so return 0 */
	return 0;
  
  }
  else /* if target thread not found  */
  {
	/* unclock alarms and return */
	sigprocmask(SIG_UNBLOCK, &alrm, NULL);
	return 1;
  }
}
Example #15
0
void Thread::exitCancel(Cancel old)
{
	Thread *th = getThread();

	if(!th)
		return;

	if(old != th->_cancel)
	{
#ifndef	WIN32
		pthread_testcancel();
#endif
		th->setCancel(old);
	}
}
Example #16
0
int Thread::interrupt(int32_t id) {
    if(id == thread_self->id)
        return 1; // cannot interrupt thread_self

    Thread* thread = getThread(id);
    if(thread == NULL || thread->daemon)
        return 1;
    if(thread->terminated)
        return 2;

    int result = interrupt(thread);
    if(result==0 && !masterShutdown)
        waitForThreadExit(thread);
    return result;
}
Example #17
0
// NOTE: Do not modify _suspendcount here, one program can call
// Suspend 2 or more time but this function can be called only once
inline RETSIGTYPE ThreadImpl::ThreadSigSuspend(int)
{
	sigset_t sigs;

	sigemptyset(&sigs);
	sigaddset(&sigs, SIGUSR3);
	while ( (getThread()->priv->_suspendcount) > 0)
	{
#ifdef	HAVE_SIGWAIT2
		int signo;
		sigwait(&sigs, &signo);
#else
		sigwait(&sigs);
#endif
	}
}
Example #18
0
void printMyThreadForThread0(int iter)
{
    int tid = getThread();

    int i;
    int j;
    spawnThread(printMyThread,iter);
    // delay
    for(i=0; i<NUMYIELDS; i++) {
        // Delay(100);
        Printf("%d ",iter);
        for(j=0; (j<tid); j++)
            Printf(".");
        Printf("T%d \n", tid);
        yieldThread((tid+1)%MAXTHREADS);
    }
}
        void AnimationBoot::start(Callback callback)
        {
            animationSystem_ = new Graphics::Render::AnimationSystem(
            getApplication().getEcsWorld(),
            getApplication().getEventManager().getEventQueue()
            );
            animationSystem_->registerListeners(
            getApplication().getEventManager().getListenerRegister()
            );

            std::vector<Threading::ThreadableInterface*> animThreadables;
            animThreadables.push_back(animationSystem_);

            setThread(new Threading::Thread(animThreadables, 60));
            getThread().start();
            callback();
        }
Example #20
0
void Main ()
{
    int i, me;
    void printMyThread();

    initThreads ();

    me = getThread ();
    for(i=1; i<MAXTHREADS; i++)
        spawnThread(printMyThread,0);

    for (i = 0; i < NUMYIELDS; i++) {
        Printf("0 T%d\n",me);
        yieldThread(1);
    }

    exitThread ();
}
Example #21
0
void ThreadImpl::setName(const char* name)
{
    THREADNAME_INFO info;
    info.dwType = 0x1000;
    info.szName = name;
    info.dwThreadID = getThread(this)->threadID;
    info.dwFlags = 0;

    // C++ Exceptions are disabled for this project, but SEH is not (and cannot be)
    // http://stackoverflow.com/questions/943087/what-exactly-will-happen-if-i-disable-c-exceptions-in-a-project
    __try
    {
        RaiseException(NS_MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR*)&info);
    }
    __except(EXCEPTION_EXECUTE_HANDLER)
    {
        // this runs if not attached to a debugger (thus not really naming the thread)
    }
}
Example #22
0
void nTextureManager::nTextureJob::process() {
    unsigned int sum = 0;
    list<pair<double, nTexture *> > toTrash;
    list<nTexture *> toLoad;
    for(unsigned int i = 0, size = manager->getTextures().size(); i != size; i++) {
        nTexture *t = manager->getTextures()[i];
        unsigned int mip = t->getCurrentMipMapLevel();
        sum += t->getSize().surface() * 4;
        if(t->getNextMipMapLevel() == mip) {
            double mipTime = t->getCurrentMipMapLevel() == t->getNextMipMapLevel() ? t->getMipMapTime() * 2 : 0;
            double time = t->getBindTime() * min(1.0, mipTime);
            if(mip < t->getMaxMipMapLevel() - 3 && time > 0.25) {
                toTrash.push_back(pair<double, nTexture*>(time, t));
            } else if(t->getBindTime() < 0.05 && mip) {
                toLoad.push_back(t);
            }
        }
    }
    //cout<<round(((double)sum / (1024 * 1024)) * 10) / 10<<"Mb texture data"<<endl;
    //cout<<toTrash.size()<<" textures to trash"<<endl;

    unsigned int texOp = 0;
    unsigned int maxOp = nGine::instance()->getJobPool()->getRenderJobCount() >= 4 ? 0 : 4 - nGine::instance()->getJobPool()->getRenderJobCount();

    for(list<nTexture *>::iterator it = toLoad.begin(); it != toLoad.end() && texOp != maxOp; it++) {
        (*it)->setMipMapLevel(0);
        nGine::instance()->getJobPool()->addRenderJob(new nTextureUpdateJob(*it));
        texOp++;
    }

    if(texOp != maxOp) {
        toTrash.sort(compTex);
    }

    for(list<pair<double, nTexture *> >::iterator it = toTrash.begin(); it != toTrash.end() && texOp != maxOp; it++) {
        (*it).second->setMipMapLevel((*it).second->getCurrentMipMapLevel() + 1);
        nGine::instance()->getJobPool()->addRenderJob(new nTextureUpdateJob((*it).second));
        texOp++;
    }
    getThread()->sleep(0.01);
    nGine::instance()->getJobPool()->addJob(this, nJobPool::Medium);
}
Example #23
0
Thread::Cancel Thread::enterCancel(void)
{
	Thread *th = getThread();

	if(!th)
		return cancelInitial;

	Cancel old = th->_cancel;	
	if(old != cancelDisabled && old != cancelImmediate)
	{
		th->setCancel(cancelImmediate);
#ifdef	WIN32
		Thread::yield();
#else
		pthread_testcancel();
#endif
	}

	return old;
}
Example #24
0
int gtthread_cancel(gtthread_t thread)
{
	gtthread *targetTh;
	sigset_t oldset;
	
	sigprocmask(SIG_BLOCK, &alrm, &oldset);
	targetTh = getThread(thread);
	
	sigprocmask(SIG_UNBLOCK, &alrm, NULL);
	
	if(targetTh == NULL)
	{
		return 1;/* failed to cancel */
	}
	else 
	{
		targetTh -> cancel = 1;	/* if thread found, set cancel = 1 */
		return 0;
	}
}
Example #25
0
void
Task::threadMain(void)
{
#if defined(DEBUG)
  os::diag::trace.putMemberFunctionWithName();
#endif

  //os::timerTicks.sleep(10);
  m_thread.sleepFor(10);

  os::core::timer::ticks_t ticksBegin = os::timerTicks.getCurrentTicks();

  os::core::timer::ticks_t cnt;
  for (cnt = 1; cnt <= REPEAT; cnt++)
    {
      os::core::timer::ticks_t i;
      for (i = FROM; i <= TO; i++)
        {
          //os::timerTicks.sleep(i);
          m_thread.sleepFor(i);

          m_count += i;
          ++m_sleepCalls;
        }
    }

  os::core::timer::ticks_t ticksEnd = os::timerTicks.getCurrentTicks();

  m_deltaTicks = ticksEnd - ticksBegin;

  //os::timerTicks.sleep(50);
  m_thread.sleepFor(50);

  ts << "Multiple sleeps of " << SUM << " ticks took " << m_deltaTicks
      << " counted ticks, \"" << getThread().getName() << "\"" << os::std::endl;

}
Example #26
0
void ThreadImpl::signalQuit()
{
    InterlockedIncrement(&(getThread(this)->quitNow));
}
Example #27
0
/* add the thread with nextId to the currently_running queue */
void addToQueue(gtthread_t nextId)
{
	gtthread *nextThread;
	nextThread = (gtthread *) getThread(nextId);
	steque_enqueue(&currently_running, nextThread);
}
Example #28
0
bool ThreadImpl::quitIsSignalled()
{
    return InterlockedCompareExchange(&(getThread(this)->quitNow), 0, 0) != 0;
}
Example #29
0
void ThreadImpl::quit()
{
    getThread(this)->state = _ThreadImpl::Stopped;
    ExitThread(0);
}
Example #30
0
void ThreadImpl::kill()
{
    if(getThread(this)->state == _ThreadImpl::Started)
        TerminateThread(getThread(this)->thread, 0);
    getThread(this)->state = _ThreadImpl::Stopped;
}