int poweroffInit() { int res; static int _init_count = -1; if(_init_count == _iop_reboot_count) return 0; _init_count = _iop_reboot_count; while(((res = SifBindRpc(&cd0, PWROFF_IRX, 0)) >= 0) && (cd0.server == NULL)) nopdelay(); ee_thread_t thread; ee_thread_status_t thisThread; ee_sema_t sema; // Terminate and delete any previously created threads if (powerOffThreadId >= 0) { TerminateThread(powerOffThreadId); DeleteThread(powerOffThreadId); powerOffThreadId = -1; } // Delete any previously created semaphores if (PowerOffSema >= 0) { DeleteSema(PowerOffSema); PowerOffSema = -1; } sema.init_count = 0; sema.max_count = 1; sema.option = 0; PowerOffSema = CreateSema(&sema); ReferThreadStatus(GetThreadId(), &thisThread); if (thisThread.current_priority == 0) { ChangeThreadPriority(GetThreadId(), 51); thread.initial_priority = 50; } else thread.initial_priority = thisThread.current_priority - 1; thread.stack_size = 512 * 16; thread.gp_reg = &_gp; thread.func = PowerOffThread; thread.stack = (void *)poffThreadStack; powerOffThreadId = CreateThread(&thread); StartThread(powerOffThreadId, NULL); DIntr(); SifAddCmdHandler(POFF_SIF_CMD, _poff_intr_callback, NULL); EIntr(); int autoShutdown = 0; SifCallRpc(&cd0, PWROFF_ENABLE_AUTO_SHUTOFF, 0, NULL, 0, &autoShutdown, sizeof(autoShutdown), 0, 0); return res; }
int thrd_equal(thrd_t thr0, thrd_t thr1) { #if defined(_TTHREAD_WIN32_) return GetThreadId(thr0) == GetThreadId(thr1); #else return pthread_equal(thr0, thr1); #endif }
int unabto_thread_equal(unabto_thread_t thread1, unabto_thread_t thread2) { #ifdef WIN32 return GetThreadId(thread1) == GetThreadId(thread2); #else return pthread_equal(thread1, thread2); #endif }
int embb_thread_equal(const embb_thread_t* lhs, const embb_thread_t* rhs) { embb_thread_id_t idLhs = GetThreadId(lhs->embb_internal_handle); embb_thread_id_t idRhs = GetThreadId(rhs->embb_internal_handle); if (idLhs == idRhs) { return 1; } return 0; }
int DeviceManagerThread::Run() { ThreadCommand::PopBuffer command; SetThreadName("OVR::DeviceManagerThread"); LogText("OVR::DeviceManagerThread - running (ThreadId=%p).\n", GetThreadId()); while(!IsExiting()) { // PopCommand will reset event on empty queue. if (PopCommand(&command)) { command.Execute(); } else { bool commands = 0; do { int n = poll(&PollFds[0], PollFds.GetSize(), -1); for (int i = 0; i < PollFds.GetSize(); i++) { if (PollFds[i].revents & POLLERR) { OVR_DEBUG_LOG(("poll: error on [%d]: %d", i, PollFds[i].fd)); } else if (PollFds[i].revents & POLLIN) { if (FdNotifiers[i]) FdNotifiers[i]->OnEvent(i, PollFds[i].fd); else if (i == 0) // command { char dummy[128]; read(PollFds[i].fd, dummy, 128); commands = 1; } } if (PollFds[i].revents & POLLHUP) PollFds[i].events = 0; if (PollFds[i].revents != 0) { n--; if (n == 0) break; } } } while (PollFds.GetSize() > 0 && !commands); } } LogText("OVR::DeviceManagerThread - exiting (ThreadId=%p).\n", GetThreadId()); return 0; }
void gsiEnterCriticalSection(GSICriticalSection *theCrit) { // If we're not already in it, wait for it if (GetThreadId() != theCrit->mOwnerThread) { gsiWaitForSemaphore(theCrit->mSemaphore, 0); theCrit->mOwnerThread = GetThreadId(); } // Increment entry count theCrit->mEntryCount++; }
u32_t sys_arch_sem_wait(sys_sem_t Sema,u32_t u32Timeout) { //Wait u32Timeout msec for the Sema to receive a signal. dbgprintf("sys_arch_sem_wait: Sema: %d, Timeout: %x (TID: %d)\n",Sema,u32Timeout,GetThreadId()); if(u32Timeout==0) { //Wait with no timeouts. return WaitSema(Sema)==0 ? 0:SYS_ARCH_TIMEOUT; } else if(u32Timeout==1) { //Poll. return PollSema(Sema)==0 ? 0:SYS_ARCH_TIMEOUT; } else { //Use alarm to timeout. iop_sys_clock_t ClockTicks; iop_sys_clock_t Start; iop_sys_clock_t End; int iPID=GetThreadId(); u32_t u32WaitTime; GetSystemTime(&Start); USec2SysClock(u32Timeout*1000,&ClockTicks); SetAlarm(&ClockTicks,TimeoutHandler,(void*)iPID); if(WaitSema(Sema)!=0) { return SYS_ARCH_TIMEOUT; } CancelAlarm(TimeoutHandler,(void*)iPID); GetSystemTime(&End); u32WaitTime=ComputeTimeDiff(&Start,&End); return u32WaitTime<=u32Timeout ? u32WaitTime:u32Timeout; } }
void sys_sem_signal(sys_sem_t Sema) { dbgprintf("sys_sem_signal: Sema: %d (TID: %d)\n",Sema,GetThreadId()); SignalSema(Sema); }
bool CAsynPipe::Connect(const CAddress& Address) { m_eState = eAPS_Connecting; CPipeThread* pThread = CPipeThreadMgr::Inst()->GetThread(GetThreadId()); (new(pThread) CPipeConnectJob(GetLocalID(), Address, pThread))->Add(pThread); return true; }
void RankSystem::updatePos_init(RankStats* rr, Stats* s, bool sync) { if(rr == NULL) // Verify Pointer to RankStats return; if(s != NULL) // NULL Stats Update (Only for Synchronization) rr->addStats(s); if(sync == true) updatePos_exec(rr); else { // Prevent Waiting on MAIN Thread HANDLE h_temp = CreateThread(NULL, 0, updatePos_thread, rr, CREATE_SUSPENDED, NULL); if(h_temp == NULL) { MF_SyncLog("updatePos_init: Couldn't create thread for updating Ranks"); } else { #ifdef _DEBUG MF_SyncLog("updatePos_init: Creating Thread #%d", GetThreadId(h_temp)); #endif SetThreadPriority(h_temp, THREAD_PRIORITY_LOWEST); ResumeThread(h_temp); CloseHandle(h_temp); } } }
void rmInit() { gsGlobal = gsKit_init_global(); rm_mode_table[RM_VMODE_AUTO].mode = gsGlobal->Mode; rm_mode_table[RM_VMODE_AUTO].height = gsGlobal->Height; dmaKit_init(D_CTRL_RELE_OFF, D_CTRL_MFD_OFF, D_CTRL_STS_UNSPEC, D_CTRL_STD_OFF, D_CTRL_RCYC_8, 1 << DMA_CHANNEL_GIF); // Initialize the DMAC dmaKit_chan_init(DMA_CHANNEL_GIF); rmSetMode(1); order = 0; aspectWidth = 1.0f; aspectHeight = 1.0f; shiftYVal = 1.0f; shiftY = &shiftYFunc; transX = 0.0f; transY = 0.0f; guiThreadID = GetThreadId(); gsKit_add_vsync_handler(&rmOnVSync); }
int thrd_is_current(thrd_t thr) { #if defined(_TTHREAD_WIN32_) return GetThreadId(thr) == GetCurrentThreadId(); #else return (pthread_self() == thr); #endif }
void fileXio_Thread(void* param) { int OldState; printf("fileXio: fileXio RPC Server v1.00\nCopyright (c) 2003 adresd\n"); #ifdef DEBUG printf("fileXio: RPC Initialize\n"); #endif SifInitRpc(0); RWBufferSize=DEFAULT_RWSIZE; CpuSuspendIntr(&OldState); rwbuf = AllocSysMemory(ALLOC_FIRST, RWBufferSize, NULL); CpuResumeIntr(OldState); if (rwbuf == NULL) { #ifdef DEBUG printf("Failed to allocate memory for RW buffer!\n"); #endif SleepThread(); } SifSetRpcQueue(&qd, GetThreadId()); SifRegisterRpc(&sd0, FILEXIO_IRX, &fileXio_rpc_server, fileXio_rpc_buffer, NULL, NULL, &qd); SifRpcLoop(&qd); }
void log_init_thread() { if (!logger_attached) return; LPCWSTR pipeName = L"\\\\.\\pipe\\flog_server"; for (;;) { hLoggerPipe = CreateFileW(pipeName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if (hLoggerPipe == INVALID_HANDLE_VALUE) { /* Non critical error code, just wait and try connecting again */ if (GetLastError() != ERROR_PIPE_BUSY || !WaitNamedPipeW(pipeName, NMPWAIT_WAIT_FOREVER)) { logger_attached = 0; break; } continue; } /* Send initial request */ struct request request; request.magic = PROTOCOL_MAGIC; request.version = PROTOCOL_VERSION; request.pid = GetProcessId(GetCurrentProcess()); request.tid = GetThreadId(GetCurrentThread()); DWORD written; if (!WriteFile(hLoggerPipe, &request, sizeof(request), &written, NULL)) { CloseHandle(hLoggerPipe); logger_attached = 0; } break; } }
int InitThread(void) { ee_sema_t sema; ee_thread_t thread; sema.max_count = 255; sema.init_count = 0; sema.option = (u32)"KernelTopThread"; if((topSema = CreateSema(&sema)) < 0) return -1; thread.func = &topThread; thread.stack = stack; thread.stack_size = sizeof(stack); thread.gp_reg = &_gp; thread.option = (u32)"KernelTopThread"; thread.initial_priority = 0; if((topId = CreateThread(&thread)) < 0) { DeleteSema(topSema); return -1; } topArg.requestOut = 0; topArg.requestIn = 0; StartThread(topId, &topArg); ChangeThreadPriority(GetThreadId(), 1); return topId; }
bool RenderThread::update() { /* - Tant qu'il n'y a pas de command -> je pop des task - Une fois que j'ai mes commandes -> pour chacunes d'elles -> je regarde si c'est a moi de les traiter (ex : changement de scene) -> si ca n'est pas a moi de les traiter -> je les passe au render context actif -> si il n'y a pas de render context actif, c'est que j'ai fait une erreur, et j'assert dans ta face :D */ _registerId(); _run = true; _insideRun = true; DWORD threadId = GetThreadId(static_cast<HANDLE>(_threadHandle.native_handle())); SetThreadName(threadId, this->_name.c_str()); TMQ::MessageBase *task = nullptr; while (_run && _insideRun) { SCOPE_profile_cpu_i("RenderTimer", "Update"); if (TMQ::TaskManager::RenderThreadGetTask(task)) { SCOPE_profile_cpu_i("RenderTimer", "Execute task"); auto success = execute(task); // we receive a task that we cannot treat AGE_ASSERT(success); } } return true; }
//Create a new thread. sys_thread_t sys_thread_new(void (*pFunction)(void* pvArg), void* pvArg, int iPrio) { iop_thread_t thp; int tid, rv; thp.attr = TH_C; thp.option = 0; thp.thread = pFunction; thp.stacksize = 0x900; // why this magic number?? thp.priority = iPrio + SYS_THREAD_PRIO_BASE; dbgprintf("sys_thread_new: Thread new (TID: %d)\n",GetThreadId()); if((tid = CreateThread(&thp)) < 0) { dbgprintf("sys_thread_new: CreateThread failed, EC: %d\n", tid); return -1; } if((rv = StartThread(tid, pvArg)) < 0) { dbgprintf("sys_thread_new: StartThread failed, EC: %d\n", rv); DeleteThread(tid); return(-1); } thread_count++; return((sys_thread_t) tid); }
/* ------------------------------------------------------------------------ Main thread for the ezbgm module. After execution, initialize interrupt environment, register command, and then wait until there is a request from the EE. ------------------------------------------------------------------------ */ int sce_bgm_loop() { sceSifQueueData qd; sceSifServeData sd; //-- Initialize interrupt environment in advance. CpuEnableIntr(); EnableIntr( INUM_DMA_4 ); EnableIntr( INUM_DMA_7 ); //--- Register function that is called according to request sceSifInitRpc(0); sceSifSetRpcQueue( &qd, GetThreadId() ); sceSifRegisterRpc( &sd, EZBGM_DEV, bgmFunc, (void*)gRpcArg, NULL, NULL, &qd ); PRINTF(("goto bgm cmd loop\n")); //--- Command-wait loop sceSifRpcLoop(&qd); return 0; }
//Create a new thread. sys_thread_t sys_thread_new(const char *name, lwip_thread_fn thread, void *arg, int stacksize, int prio) { iop_thread_t thp; int tid, rv; thp.attr = TH_C; thp.option = 0; thp.thread = thread; thp.stacksize = stacksize; thp.priority = prio; dbgprintf("sys_thread_new: Thread new (TID: %d)\n", GetThreadId()); if((tid = CreateThread(&thp)) < 0) { dbgprintf("sys_thread_new: CreateThread failed, EC: %d\n", tid); return ERR_MEM; } if((rv = StartThread(tid, arg)) < 0) { dbgprintf("sys_thread_new: StartThread failed, EC: %d\n", rv); DeleteThread(tid); return ERR_MEM; } return((sys_thread_t) tid); }
void sys_init(void) { int i; Timeout** ppTimeout=&pFreeTimeouts; dbgprintf("sys_init: Initializing (TID: %d)\n",GetThreadId()); for(i = 0; i < SYS_MAX_MESSAGES; i++) { msg_pool[i].next = NULL; msg_pool[i].sys_msg = NULL; } for (i = 0; i < SYS_TIMEOUT_MAX; i++) { Timeout* pTimeout=&aTimeouts[i]; *ppTimeout=pTimeout; ppTimeout=&pTimeout->pNext; } *ppTimeout=NULL; }
int NetManRpcNetIFSendPacket(const void *packet, unsigned int length){ struct PacketTag *PacketTag; WaitSema(NetManTxSemaID); NetmanTxWaitingThread=GetThreadId(); WaitSema(TxBankAccessSema); //Check is there is space in the current Tx FIFO. If not, wait for the Tx thread to empty out the other FIFO. */ while(CurrentTxFIFOData->PacketReqs.NumPackets>=NETMAN_RPC_BLOCK_SIZE){ SignalSema(TxBankAccessSema); WakeupThread(TxThreadID); SleepThread(); WaitSema(TxBankAccessSema); } memcpy(&CurrentTxFIFOData->FrameBuffer[CurrentTxFIFOData->PacketReqs.TotalLength], packet, length); PacketTag=&CurrentTxFIFOData->PacketReqs.tags[CurrentTxFIFOData->PacketReqs.NumPackets]; PacketTag->offset=CurrentTxFIFOData->PacketReqs.TotalLength; PacketTag->length=length; CurrentTxFIFOData->PacketReqs.TotalLength+=(length+3)&~3; CurrentTxFIFOData->PacketReqs.NumPackets++; WakeupThread(TxThreadID); SignalSema(TxBankAccessSema); NetmanTxWaitingThread=-1; SignalSema(NetManTxSemaID); return 0; }
///////////////////////////////////////////////////////////////////////[17] int SifGetOtherData(struct sifcmd_RPC_RECEIVE_DATA *rd, void *src, void *dst, int size, int mode){ RPC_PACKET_RDATA *packet; if ((packet=(RPC_PACKET_RDATA *)rpc_get_packet(&rpc_common))==0) return -1; rd->hdr.pkt_addr=packet; rd->hdr.rpc_id =packet->packet.pid; packet->packet.paddr=packet; packet->client=(struct sifcmd_RPC_CLIENT_DATA*)rd; packet->src=src; packet->dst=dst; packet->size=size; if (mode & 1==0){ rd->hdr.tid=GetThreadId(); if (SifSendCmd(0x8000000C, packet, 0x40, 0, 0, 0)==0) return -2; SleepThread(); }else{ //async rd->hdr.tid=-1; if (SifSendCmd(0x8000000C, packet, 0x40, 0, 0, 0)==0) return -2; } return 0; }
void Profiler::endTrace(bool historical) { if (!m_isRun) return; unsigned int id = GetThreadId(); auto it = m_profilingTrees.find(id); if (it != m_profilingTrees.end()) { it->second->treeMutex.lock(); Node* cur = it->second->current; double instantTime = m_timer.getTime() - cur->startTime; cur->statistics.callsCount++; if (historical) { cur->statistics.history.push_back(instantTime); } if (cur->statistics.callsCount > 1) { cur->statistics.averageTime = (cur->statistics.averageTime + instantTime) / 2.0; if (instantTime < cur->statistics.minTime) cur->statistics.minTime = instantTime; if (instantTime > cur->statistics.maxTime) cur->statistics.maxTime = instantTime; } else { cur->statistics.minTime = instantTime; cur->statistics.maxTime = instantTime; cur->statistics.averageTime = instantTime; } it->second->current = cur->parent; it->second->treeMutex.unlock(); } }
///////////////////////////////////////////////////////////////////////[0F] int SifBindRpc(struct sifcmd_RPC_CLIENT_DATA *client, unsigned int number, unsigned int mode) { RPC_PACKET_BIND *packet; client->command=0; client->server=0; packet = (RPC_PACKET_BIND *)rpc_get_packet(&rpc_common); if (packet==NULL) return -1; client->hdr.pkt_addr = packet; client->hdr.rpc_id = packet->packet.pid; packet->packet.paddr = packet; packet->client = client; packet->fno = number; if (mode & 1==0){ client->hdr.tid=GetThreadId(); if (SifSendCmd(0x80000009, packet, 0x40, 0, 0, 0)==0) return -2; SleepThread(); }else{ //async client->hdr.tid=-1; if (SifSendCmd(0x80000009, packet, 0x40, 0, 0, 0)==0) return -2; } return 0; }
void System::SetThreadName( const char* name ) { #ifdef _WIN32 const DWORD MS_VC_EXCEPTION=0x406D1388; # pragma pack( push, 8 ) struct THREADNAME_INFO { DWORD dwType; LPCSTR szName; DWORD dwThreadID; DWORD dwFlags; }; # pragma pack(pop) DWORD ThreadId = GetThreadId( GetCurrentThread() ); THREADNAME_INFO info; info.dwType = 0x1000; info.szName = name; info.dwThreadID = ThreadId; info.dwFlags = 0; __try { RaiseException( MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info ); } __except(EXCEPTION_EXECUTE_HANDLER) { } #else pthread_setname_np( pthread_self(), name ); #endif }
///////////////////////////////////////////////////////////////////////[10] int SifCallRpc(struct sifcmd_RPC_CLIENT_DATA *client, unsigned int fno, unsigned int mode, void *send, int ssize, void *receive, int rsize, void (*end_func)(void*), void *end_para){ RPC_PACKET_CALL *packet; if ((packet=(RPC_PACKET_CALL *)rpc_get_packet(&rpc_common))==0) return -1; client->hdr.pkt_addr=(void*)packet; client->func = end_func; client->param = end_para; client->hdr.rpc_id= packet->packet.packet.pid; packet->packet.packet.paddr = packet; packet->packet.client = client; packet->packet.fno = fno; packet->size = ssize; packet->receive= receive; packet->rsize = rsize; packet->server = client->server; if (mode & 1){ packet->rmode=(end_func!=0); client->hdr.tid=-1; if (SifSendCmd(0x8000000A, packet, 0x40, send, client->buff, ssize)) return 0; return -2; }else{ packet->rmode=1; client->hdr.tid=GetThreadId(); if (SifSendCmd(0x8000000A, packet, 0x40, send, client->buff, ssize)==0) return -2; SleepThread(); return 0; } }
void sprawl::threading::Thread::Start() { m_handle.GetNativeHandle() = CreateThread( nullptr, 0, &ThreadStatic::EntryPoint, this, 0, nullptr); if(m_handle.GetNativeHandle() != INVALID_HANDLE_VALUE) { if(m_threadName != nullptr) { const DWORD MS_VC_EXCEPTION = 0x406D1388; #pragma pack(push,8) struct THREADNAME_INFO { DWORD dwType; // Must be 0x1000. LPCSTR szName; // Pointer to name (in user addr space). DWORD dwThreadID; // Thread ID (-1=caller thread). DWORD dwFlags; // Reserved for future use, must be zero. }; #pragma pack(pop) THREADNAME_INFO info; info.dwType = 0x1000; info.szName = m_threadName; info.dwThreadID = GetThreadId(m_handle.GetNativeHandle()); info.dwFlags = 0; __try { RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR*)&info); } __except(EXCEPTION_EXECUTE_HANDLER) { } } } }
void Thread::SetThreadName( const char* name ) { #if !defined(OVR_BUILD_SHIPPING) || defined(OVR_BUILD_PROFILING) // Looks ugly, but it is the recommended way to name a thread. typedef struct tagTHREADNAME_INFO { DWORD dwType; // Must be 0x1000 LPCSTR szName; // Pointer to name (in user address space) DWORD dwThreadID; // Thread ID (-1 for caller thread) DWORD dwFlags; // Reserved for future use; must be zero } THREADNAME_INFO; THREADNAME_INFO info; info.dwType = 0x1000; info.szName = name; info.dwThreadID = reinterpret_cast<DWORD>(GetThreadId()); info.dwFlags = 0; __try { #ifdef _WIN64 RaiseException( 0x406D1388, 0, sizeof(info)/sizeof(DWORD), (const ULONG_PTR *)&info ); #else RaiseException( 0x406D1388, 0, sizeof(info)/sizeof(DWORD), (DWORD *)&info ); #endif } __except( GetExceptionCode()==0x406D1388 ? EXCEPTION_CONTINUE_EXECUTION : EXCEPTION_EXECUTE_HANDLER ) { } #endif // OVR_BUILD_SHIPPING }
void ProfilerTimeRecordToBuffer(const char* eventDescription, const long long beginClock, const long long endClock) { std::lock_guard<std::mutex> lock(g_mutex); if (!g_profilerState->enabled) return; auto eventDescriptionBytes = strlen(eventDescription) + 1; auto requiredBufferBytes = eventDescriptionBytes + sizeof(CustomEventRecord); if ((g_profilerState->customEventOffset + requiredBufferBytes) > g_profilerState->customEventBufferBytes) { if (!g_profilerState->customEventBufferFull) { fprintf(stderr, "Warning: Performance Profiler: Buffer is full, no more events will be recorded.\n"); g_profilerState->customEventBufferFull = true; } return; } strcpy(g_profilerState->customEventBuffer.get() + g_profilerState->customEventOffset, eventDescription); g_profilerState->customEventOffset += eventDescriptionBytes; CustomEventRecord eventRecord; eventRecord.beginClock = beginClock; eventRecord.endClock = endClock; eventRecord.threadId = GetThreadId(); memcpy(g_profilerState->customEventBuffer.get() + g_profilerState->customEventOffset, &eventRecord, sizeof(CustomEventRecord)); g_profilerState->customEventOffset += sizeof(CustomEventRecord); }
PVOID WINAPI GetThreadStartAddress(HANDLE hThread) { NTSTATUS ntStatus; HANDLE hDupHandle; PVOID dwStartAddress; if (NtQueryInformationThread == NULL) NtQueryInformationThread = (pNtQIT)GetProcAddress(GetModuleHandle("ntdll.dll"), "NtQueryInformationThread"); if (NtQueryInformationThread == NULL) { MyTrace("%s(): cannot found NtQueryInformationThread()", __FUNCTION__); return 0; } HANDLE hCurrentProcess = GetCurrentProcess(); if (!DuplicateHandle(hCurrentProcess, hThread, hCurrentProcess, &hDupHandle, THREAD_QUERY_INFORMATION, FALSE, 0)){ SetLastError(ERROR_ACCESS_DENIED); MyTrace("%s(): cannot found open thread", __FUNCTION__); return 0; } UINT32 ThreadQuerySetWin32StartAddress = 9; ntStatus = NtQueryInformationThread(hDupHandle, ThreadQuerySetWin32StartAddress, &dwStartAddress, sizeof(PVOID), NULL); CloseHandle(hDupHandle); if (ntStatus != 0) { MyTrace("%s(): NtQueryInformationThread() failed. status: %x, threadHandle: %x, threadId: %d", __FUNCTION__, ntStatus, hThread, GetThreadId(hThread)); return 0; } return dwStartAddress; }