bool Thread::kill() { if (!m_running) { wait(); return false; } m_running = false; #ifdef _WIN32 TerminateThread(m_thread_handle, 0); CloseHandle(m_thread_handle); #else // We need to pthread_kill instead on Android since NDKv5's pthread // implementation is incomplete. # ifdef __ANDROID__ pthread_kill(getThreadHandle(), SIGKILL); # else pthread_cancel(getThreadHandle()); # endif wait(); #endif m_retval = NULL; m_joinable = false; m_request_stop = false; return true; }
bool Thread::bindToProcessor(unsigned int proc_number) { #if defined(__ANDROID__) return false; #elif defined(_WIN32) return SetThreadAffinityMask(getThreadHandle(), 1 << proc_number); #elif __FreeBSD_version >= 702106 || defined(__linux__) cpu_set_t cpuset; CPU_ZERO(&cpuset); CPU_SET(proc_number, &cpuset); return pthread_setaffinity_np(getThreadHandle(), sizeof(cpuset), &cpuset) == 0; #elif defined(__sun) || defined(sun) return processor_bind(P_LWPID, P_MYID, proc_number, NULL) == 0 #elif defined(_AIX) return bindprocessor(BINDTHREAD, m_kernel_thread_id, proc_number) == 0; #elif defined(__hpux) || defined(hpux) pthread_spu_t answer; return pthread_processor_bind_np(PTHREAD_BIND_ADVISORY_NP, &answer, proc_number, getThreadHandle()) == 0; #elif defined(__APPLE__) struct thread_affinity_policy tapol; thread_port_t threadport = pthread_mach_thread_np(getThreadHandle()); tapol.affinity_tag = proc_number + 1; return thread_policy_set(threadport, THREAD_AFFINITY_POLICY, (thread_policy_t)&tapol, THREAD_AFFINITY_POLICY_COUNT) == KERN_SUCCESS; #else return false; #endif }
Nav2::Nav2(Nav2StartupData &sdata) : m_gui(NULL), m_bt(NULL), initialized(0), shutdownstarted(0), mainlog(NULL) { // Create the global data object m_globalData = new GlobalData; #ifdef __SYMBIAN32__ m_archGlobalData = new ArchGlobalData; m_tlsGlobalData = new TlsGlobalData; m_tlsGlobalDataId = getThreadHandle(); m_tlsGlobalData->m_global = m_globalData; m_tlsGlobalData->m_archGlobal = m_archGlobalData; setTlsGlobalData(m_tlsGlobalData); #else Nav2GlobalData = m_globalData; #endif // Copy objects we do not assume ownership of. m_globalData->m_writable_files_path = strdup_new(sdata.writableFilesPath); m_globalData->m_readonly_files_path = strdup_new(sdata.readOnlyFilesPath); // Save things we do take ownership of. m_errorTable = sdata.errorTable; m_audioSyntax = sdata.audioSyntax; }
isab::TlsGlobalData* TTlsData::GetTls(){ TUint64 uId = getThreadHandle(); iMutex.Wait(); isab::TlsGlobalData* ret = iTlsMap[uId]; iMutex.Signal(); return ret; }
bool ThreadInfo::recalcUsage(int sampleTimeDiff) { cpuUsage = -1; totalCpuTimeMs = -1; HANDLE thread_handle = getThreadHandle(); if (thread_handle == NULL) return false; FILETIME CreationTime, ExitTime, KernelTime, UserTime; if (!GetThreadTimes( thread_handle, &CreationTime, &ExitTime, &KernelTime, &UserTime )) return false; __int64 kernel_diff = getDiff(prevKernelTime, KernelTime); __int64 user_diff = getDiff(prevUserTime, UserTime); prevKernelTime = KernelTime; prevUserTime = UserTime; if (sampleTimeDiff > 0) { __int64 total_diff = ((kernel_diff + user_diff) / 10000) * 100; cpuUsage = static_cast<int>(total_diff / sampleTimeDiff); } totalCpuTimeMs = (getTotal(KernelTime) + getTotal(UserTime)) / 10000; return true; }
bool Thread::setPriority(int prio) { #if defined(_WIN32) return SetThreadPriority(getThreadHandle(), prio); #else struct sched_param sparam; int policy; if (pthread_getschedparam(getThreadHandle(), &policy, &sparam) != 0) return false; int min = sched_get_priority_min(policy); int max = sched_get_priority_max(policy); sparam.sched_priority = min + prio * (max - min) / THREAD_PRIORITY_HIGHEST; return pthread_setschedparam(getThreadHandle(), policy, &sparam) == 0; #endif }
bool VDAudioOutputDirectSoundW32::Init(uint32 bufsize, uint32 bufcount, const tWAVEFORMATEX *wf, const wchar_t *preferredDevice) { mBufferSize = bufsize * bufcount; mBuffer.resize(mBufferSize); mBufferReadOffset = 0; mBufferWriteOffset = 0; mBufferLevel = 0; if (wf->wFormatTag == WAVE_FORMAT_PCM) { mInitFormat.resize(sizeof(tWAVEFORMATEX)); memcpy(&*mInitFormat, wf, sizeof(PCMWAVEFORMAT)); mInitFormat->cbSize = 0; } else mInitFormat.assign(wf, sizeof(tWAVEFORMATEX) + wf->cbSize); mMutex.Lock(); mbThreadInited = false; mbThreadInitSucceeded = false; mMutex.Unlock(); if (!ThreadStart()) return false; mMutex.Lock(); while(!mbThreadInited) { mMutex.Unlock(); HANDLE h[2] = { getThreadHandle(), mUpdateEvent.getHandle() }; if (WaitForMultipleObjects(2, h, FALSE, INFINITE) != WAIT_OBJECT_0 + 1) break; mMutex.Lock(); } bool succeeded = mbThreadInitSucceeded; mMutex.Unlock(); return succeeded; }
//----------------------------------------------------------------------------- // Our main thread function //----------------------------------------------------------------------------- unsigned long ThreadSyncTask::mainThreadFunc() { unsigned long rtn = 0; // Configure this thread bool ok = configThread(); if ( getParent()->isMessageEnabled(MSG_INFO) ) { std::cout << "ThreadSyncTask(" << this << ")::mainThreadFunc(): thread handle = " << getThreadHandle() << std::endl; } // Main start-complete loop ... while ( ok && getParent()->isNotShutdown() ) { // Wait for the start signal waitForStart(); // Just in case we've been shutdown while we were waiting if (getParent()->isShutdown()) { signalCompleted(); break; } // User defined tasks this->userFunc(); // Signal that we've completed signalCompleted(); } return rtn; }
//----------------------------------------------------------------------------- // Our main thread function //----------------------------------------------------------------------------- unsigned long ThreadSingleTask::mainThreadFunc() { unsigned long rtn = 0; // Configure this thread configThread(); if ( getParent()->isMessageEnabled(MSG_INFO) ) { std::cout << "ThreadSingleTask(" << this << ")::mainThreadFunc(): thread handle = " << getThreadHandle() << std::endl; } // Call the user function only once rtn = this->userFunc(); return rtn; }
void TTlsData::DeleteTls(isab::TlsGlobalData* aData){ DeleteTls(aData, getThreadHandle()); }
void TTlsData::SetTls(isab::TlsGlobalData* aData) { TUint64 uId = getThreadHandle(); iMutex.Wait(); iTlsMap[uId] = aData; iMutex.Signal(); }