📄 sprocthread.c++
字号:
#endif s_masterThreadPriority = Thread::THREAD_PRIORITY_MAX; s_isInitialized = true;}//-----------------------------------------------------------------------------//// Description: Return a pointer to the currently executing thread//// Use: public//Thread *Thread::CurrentThread() { return (*(Thread **)&PRDA->usr_prda);}//-----------------------------------------------------------------------------//// Description: Get a unique identifier for this thread.//// Use: public//int Thread::getThreadId() { SprocThreadPrivateData *pd = static_cast<SprocThreadPrivateData *> (_prvData); return pd->uniqueId;}//-----------------------------------------------------------------------------//// Description: Get the thread's process id//// Use: public//size_t Thread::getProcessId() { SprocThreadPrivateData *pd = static_cast<SprocThreadPrivateData *> (_prvData); if(pd->idSet == false) return getpid(); return (size_t)(pd->pid);}//-----------------------------------------------------------------------------//// Description: Determine if the thread is running//// Use: public//bool Thread::isRunning() { SprocThreadPrivateData *pd = static_cast<SprocThreadPrivateData *> (_prvData); return pd->isRunning;}//-----------------------------------------------------------------------------//// Description: Start the thread.//// Use: public//int Thread::start() { SprocThreadPrivateData *pd = static_cast<SprocThreadPrivateData *> (_prvData); pd->threadStartedBlock.reset(); int pid = sproc(ThreadPrivateActions::StartThread, PR_SALL, static_cast<void *>(this)); // PR_SADDR | PR_SDIR | PR_SUMASK | PR_SULIMIT | PR_SID, if(pid < 0) { perror("sproc encountered an error"); return -1; } //----------------------------------------------------------------- // Make the thread runnable anywhere. // sysmp(MP_RUNANYWHERE_PID, pid); pd->pid = pid; pd->idSet = true; // wait till the thread has actually started. pd->threadStartedBlock.block(); return 0;}//-----------------------------------------------------------------------------//// Description: Alternate thread start routine.//// Use: public//int Thread::startThread(){ if (_prvData) return start(); else return 0;}//-----------------------------------------------------------------------------//// Description: Join the thread.//// Use: public//int Thread::detach() { int status = 0; SprocThreadPrivateData *pd = static_cast<SprocThreadPrivateData *> (_prvData); pd->detached=true; sigset(SIGCLD, sproc_dead_child_sig_handler); return status;}//-----------------------------------------------------------------------------//// Description: Join the thread.//// Use: public//int Thread::join() { int status; return waitpid((pid_t)getProcessId(), &status, 0); //return status;}//-----------------------------------------------------------------------------//// Description: test the cancel state of the thread.//// Use: public//int Thread::testCancel() { if(getpid() != getProcessId()) return -1; ThreadPrivateActions::ThreadCancelTest(); return 0;}//-----------------------------------------------------------------------------//// Description: Cancel the thread.//// Use: public//int Thread::cancel() { int status = 0; SprocThreadPrivateData *pd = static_cast<SprocThreadPrivateData *> (_prvData); if(pd->cancelActive) { status = kill((pid_t)getProcessId(), SIGINT); }; return status;}//-----------------------------------------------------------------------------//// Description: Disable cancelibility//// Use: public//int Thread::setCancelModeDisable() { SprocThreadPrivateData *pd = static_cast<SprocThreadPrivateData *> (_prvData); pd->cancelActive = false; return 0;}//-----------------------------------------------------------------------------//// Description: set the thread to cancel immediately//// Use: public//int Thread::setCancelModeAsynchronous() { SprocThreadPrivateData *pd = static_cast<SprocThreadPrivateData *> (_prvData); pd->cancelActive = true; return 0;}//-----------------------------------------------------------------------------//// Description: set the thread to cancel at the next convienent point.//// Use: public//int Thread::setCancelModeDeferred() { SprocThreadPrivateData *pd = static_cast<SprocThreadPrivateData *> (_prvData); pd->cancelActive = true; return 0;}//-----------------------------------------------------------------------------//// Description: Set the thread's schedule priority (if able)//// Use: public//int Thread::setSchedulePriority(ThreadPriority priority) { SprocThreadPrivateData *pd = static_cast<SprocThreadPrivateData *> (_prvData); pd->threadPriority = priority; if(pd->isRunning) return ThreadPrivateActions::SetThreadSchedulingParams(this); else return 0;}//-----------------------------------------------------------------------------//// Description: Get the thread's schedule priority (if able)//// Use: public//int Thread::getSchedulePriority() { SprocThreadPrivateData *pd = static_cast<SprocThreadPrivateData *> (_prvData); return pd->threadPriority;}//-----------------------------------------------------------------------------//// Description: Set the thread's scheduling policy (if able)//// Use: public//int Thread::setSchedulePolicy(ThreadPolicy policy) { return 0;}//-----------------------------------------------------------------------------//// Description: Set the thread's scheduling policy (if able)//// Use: public//int Thread::getSchedulePolicy() { SprocThreadPrivateData *pd = static_cast<SprocThreadPrivateData *> (_prvData); return pd->threadPolicy;}//-----------------------------------------------------------------------------//// Description: Set the thread's desired stack size//// Use: public//int Thread::setStackSize(size_t stackSize) { SprocThreadPrivateData *pd = static_cast<SprocThreadPrivateData *> (_prvData); if(pd->stackSizeLocked == true) return 13; // EACESS pd->stackSize = stackSize; return 0;}//-----------------------------------------------------------------------------//// Description: Get the thread's stack size.//// Use: public//size_t Thread::getStackSize() { SprocThreadPrivateData *pd = static_cast<SprocThreadPrivateData *> (_prvData); return pd->stackSize;}//-----------------------------------------------------------------------------//// Description: Print the thread's scheduling information to stdout.//// Use: public//void Thread::printSchedulingInfo() { ThreadPrivateActions::PrintThreadSchedulingInfo(this);}//-----------------------------------------------------------------------------//// Description: Yield the processor//// Use: protected//int Thread::YieldCurrentThread() { return sched_yield();}//-----------------------------------------------------------------------------// Description: sleep//// Use: public//int Thread::microSleep(unsigned int microsec){ return ::usleep(microsec);}static void sproc_dead_child_sig_handler(int sigid) {#ifdef DEBUG int pid, status; pid = wait(&status); DPRINTF(("(SPROC THREAD) Dead Child Handler Caught Signal, Reaped %d\n", pid));#endif sigset(SIGCLD, sproc_dead_child_sig_handler);}int Thread::setProcessorAffinity( unsigned int cpunum ){ return -1;}//-----------------------------------------------------------------------------//// Description: Get the number of processors//int OpenThreads::GetNumberOfProcessors(){ return 1;}int OpenThreads::SetProcessorAffinityOfCurrentThread(unsigned int cpunum){ Thread::Init(); Thread* thread = Thread::CurrentThread(); if (thread) { return thread->setProcessorAffinity(cpunum); } else { // non op right now, needs implementation. return -1; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -