void ServiceStop(void) { DWORD i; for(i=0;i<GetThreadCount();i++) if(hServerStopEvent[i]) SetEvent(hServerStopEvent[i]); FreeInfo(); WaitForMultipleObjects(GetThreadCount(),threads,TRUE,INFINITE); for(i=0;i<GetThreadCount();i++) CloseHandle(threads[i]); FreeNotify(); free(pipe_sd); pipe_sd=NULL; }
int main(int argc, char **argv) { unsigned numThreads = GetThreadCount(argc, argv); if (!numThreads) return 1; std::cout << "Testing with threads: " << std::dec << numThreads << std::endl; // Tell the Pin tool the number of worker threads. // volatile FUNPTR2 tellPinCount = TellPinThreadCount; tellPinCount(numThreads); HANDLE *thds = new HANDLE[numThreads]; for (unsigned i = 0; i < numThreads; i++) { thds[i] = CreateThread(0, 0, Worker, 0, 0, 0); if (!thds[i]) { std::cerr << "Unable to create worker thread #" << std::dec << i << std::endl; return 1; } } // Tell all the worker threads to go. // ATOMIC::OPS::Store(&Ready, true); for (unsigned i = 0; i < numThreads; i++) WaitForSingleObject(thds[i], INFINITE); return 0; }
void dgWorld::StepDynamics (dgFloat32 timestep) { //SerializeToFile ("xxx.bin"); dgAssert (m_inUpdate == 0); dgAssert (GetThreadCount() >= 1); m_inUpdate ++; DG_TRACKTIME(__FUNCTION__); UpdateSkeletons(); UpdateBroadphase(timestep); UpdateDynamics (timestep); if (m_listeners.GetCount()) { for (dgListenerList::dgListNode* node = m_listeners.GetFirst(); node; node = node->GetNext()) { dgListener& listener = node->GetInfo(); if (listener.m_onPostUpdate) { listener.m_onPostUpdate(this, listener.m_userData, timestep); } } } m_inUpdate --; }
void dgWorld::StepDynamics (dgFloat32 timestep) { //static int xxx ; //xxx ++; //dgTrace (("%d\n", xxx)); //if (xxx >= 2000) //xxx *=1; //xxxxx(); dgAssert (m_inUpdate == 0); //SerializeToFile ("xxx.bin"); m_inUpdate ++; dgAssert (GetThreadCount() >= 1); m_broadPhase->UpdateContacts (timestep); UpdateDynamics (timestep); if (m_postListener.GetCount()) { for (dgListenerList::dgListNode* node = m_postListener.GetFirst(); node; node = node->GetNext()) { dgListener& listener = node->GetInfo(); listener.m_onListenerUpdate (this, listener.m_userData, timestep); } } m_inUpdate --; }
ProfileThreadPtr ProfileFrame::GetThread(int32 threadIndex) const { if (threadIndex < GetThreadCount()) { return m_threads[threadIndex]; } else { return nullptr; } }
void dgWorld::UpdateTransforms(dgBodyMasterList::dgListNode* node, dgInt32 threadID) { const dgInt32 threadsCount = GetThreadCount(); while (node) { dgBody* const body = node->GetInfo().GetBody(); if (body->m_transformIsDirty && body->m_matrixUpdate) { body->m_matrixUpdate (*body, body->m_matrix, threadID); } body->m_transformIsDirty = false; for (dgInt32 i = 0; i < threadsCount; i++) { node = node ? node->GetNext() : NULL; } } }
void dgWorld::RunStep () { static int zzzz; if (zzzz == 50) { DG_START_RECORDING("../../../../sdk/dProfiler/xxxx.tt"); } if (zzzz == 500) { DG_STOP_RECORDING(); } zzzz++; DG_TRACKTIME(__FUNCTION__); dgUnsigned64 timeAcc = dgGetTimeInMicrosenconds(); dgFloat32 step = m_savetimestep / m_numberOfSubsteps; for (dgUnsigned32 i = 0; i < m_numberOfSubsteps; i ++) { dgInterlockedExchange(&m_delayDelateLock, 1); StepDynamics (step); dgInterlockedExchange(&m_delayDelateLock, 0); dgDeadBodies& bodyList = *this; dgDeadJoints& jointList = *this; jointList.DestroyJoints (*this); bodyList.DestroyBodies (*this); } const dgBodyMasterList* const masterList = this; dgBodyMasterList::dgListNode* node = masterList->GetFirst(); const dgInt32 threadsCount = GetThreadCount(); for (dgInt32 i = 0; i < threadsCount; i++) { QueueJob(UpdateTransforms, this, node, "dgWorld::UpdateTransforms"); node = node ? node->GetNext() : NULL; } SynchronizationBarrier(); if (m_postUpdateCallback) { m_postUpdateCallback (this, m_savetimestep); } m_lastExecutionTime = (dgGetTimeInMicrosenconds() - timeAcc) * dgFloat32 (1.0e-6f); if (!m_concurrentUpdate) { m_mutex.Release(); } }
inline void ThreadPool::Add(Task* t) { assert(t != NULL); queue.Push(t); if (queue.Size() > GetIdleThreadCount()) // may be need add new thread { int need1 = maxThread - GetThreadCount(); int need2 = queue.Size() - GetIdleThreadCount(); int count = (need1 > need2) ? need2 : need1; AddThread(count); } else // may be need delete idle timeout thread { while (RemoveOneIdleTimeoutThread()) { } } RemoveStopedThreads(); }
void NetworkManager::ShutDownAllThreads() { for (int i = 0; i < m_nThreadCount; i++) { m_ptrThread[i]->SignalShutDownEvent(); } DWORD dwWaitResult = WaitForMultipleObjects(GetThreadCount(), m_hThreadPool, TRUE, INFINITE); switch (dwWaitResult) { case WAIT_OBJECT_0: for (int i = 0; i < m_nThreadCount; i++) { m_ptrThread[i]->ReleaseHandles(); delete m_ptrThread[i]; } break; default: std::cout << "Unable to close threads " << GetLastError() << std::endl; } }
void CNetEngine::BeginStripJob(const RENDER_INFO& Info, const BigRect& Bounds, const CSnapshot& Snap) { CStripEngine::BeginStripJob(Info, Bounds); if (!m_RenderToFile) { // if file render, derived engine must initialize these members m_NextRow = 0; m_RowsDone = 0; m_RedoStrip.RemoveAll(); } m_StripRows = 1; // tradeoff is granularity versus overhead m_StripSize = m_EscFrameSize.cx * m_StripRows; int threads = GetThreadCount(); m_Strip.SetSize(m_StripSize * threads); m_ServersDlg->OnBeginStripJob(); // reset all render counts for (int i = 0; i < threads; i++) NextLocalStrip(i); m_BusyCount = m_RenderCount; // this engine is busy if (m_Recorder->IsDistributed()) { m_Recorder->AttachEngine(this); // attach to servers m_Snap = Snap; // copy rendering parameters PrimeServers(); } }
void dgWorld::StepDynamics (dgFloat32 timestep) { //static int xxx ; //xxx ++; //dgTrace (("%d\n", xxx)); //if (xxx >= 2000) //xxx *=1; //xxxxx(); dgAssert (m_inUpdate == 0); //SerializeToFile ("xxx.bin"); dgThreadHive::ClearTimers(); memset (m_perfomanceCounters, 0, sizeof (m_perfomanceCounters)); dgUnsigned32 ticks = m_getPerformanceCount(); m_inUpdate ++; dgAssert (GetThreadCount() >= 1); m_broadPhase->UpdateContacts (timestep); UpdateDynamics (timestep); if (m_postListener.GetCount()) { dgUnsigned32 ticks = m_getPerformanceCount(); for (dgListenerList::dgListNode* node = m_postListener.GetFirst(); node; node = node->GetNext()) { dgListener& listener = node->GetInfo(); listener.m_onListenerUpdate (this, listener.m_userData, timestep); } m_perfomanceCounters[m_postUpdataListerTicks] = m_getPerformanceCount() - ticks; } m_inUpdate --; m_perfomanceCounters[m_worldTicks] = m_getPerformanceCount() - ticks; }
void ServiceStart(BOOL aService) { DWORD ThreadID,i; wchar_t filename[MAX_PATH],access_filename[MAX_PATH]; InitInfo(); InitNotify(); { HANDLE token; PTOKEN_USER token_user=NULL; SID_IDENTIFIER_AUTHORITY SIDAuthSystem={SECURITY_NT_AUTHORITY}; PSID pSystemSid=NULL; if(AllocateAndInitializeSid(&SIDAuthSystem,1,SECURITY_LOCAL_SYSTEM_RID,0,0,0,0,0,0,0,&pSystemSid)) { if(OpenProcessToken(GetCurrentProcess(),TOKEN_QUERY,&token)) { token_user=(PTOKEN_USER)DefaultTokenInformation(token,TokenUser); if(token_user) { if((token_user->User.Sid)&&(pSystemSid)&&(IsValidSid(token_user->User.Sid))&&(IsValidSid(pSystemSid))&&(EqualSid(token_user->User.Sid,pSystemSid))) IsSystem=TRUE; free(token_user); } CloseHandle(token); } FreeSid(pSystemSid); } } //get security from file. EnablePrivilege(L"SeSecurityPrivilege"); if(!pipe_sd&&GetModuleFileNameW(NULL,filename,sizeofa(filename))) { wchar_t *filename_ptr; DWORD res=GetFullPathNameW(filename,sizeofa(access_filename),access_filename,&filename_ptr); if(res&&(res<sizeofa(access_filename))&&filename_ptr) { DWORD needed; wcscpy(filename_ptr,ACCESS_NAMEW); if(!GetFileSecurityW(access_filename,DACL_SECURITY_INFORMATION|SACL_SECURITY_INFORMATION,NULL,0,&needed)) if(GetLastError()==ERROR_INSUFFICIENT_BUFFER) { pipe_sd=(PSECURITY_DESCRIPTOR)malloc(needed); if(pipe_sd) { if(!GetFileSecurityW(access_filename,DACL_SECURITY_INFORMATION|SACL_SECURITY_INFORMATION,pipe_sd,needed,&needed)) { free(pipe_sd); pipe_sd=NULL; } } } } } //create default security if(!pipe_sd) { PSID pAccessSid=NULL; PSID pSystemSid=NULL; SID_IDENTIFIER_AUTHORITY SIDAuthLocal={SECURITY_LOCAL_SID_AUTHORITY}; SID_IDENTIFIER_AUTHORITY SIDAuthEveryone={SECURITY_WORLD_SID_AUTHORITY}; SID_IDENTIFIER_AUTHORITY SIDAuthSystem={SECURITY_NT_AUTHORITY}; DWORD sd_size=SECURITY_DESCRIPTOR_MIN_LENGTH+sizeof(ACL); PACL pAcl=NULL; if(GetAllowNetwork()?AllocateAndInitializeSid(&SIDAuthEveryone,1,SECURITY_WORLD_RID,0,0,0,0,0,0,0,&pAccessSid):AllocateAndInitializeSid(&SIDAuthLocal,1,SECURITY_LOCAL_RID,0,0,0,0,0,0,0,&pAccessSid)) { if(AllocateAndInitializeSid(&SIDAuthSystem,1,SECURITY_LOCAL_SYSTEM_RID,0,0,0,0,0,0,0,&pSystemSid)) { sd_size+=2*(sizeof(ACCESS_ALLOWED_ACE)-sizeof(DWORD))+GetLengthSid(pAccessSid)+GetLengthSid(pSystemSid); pipe_sd=(PSECURITY_DESCRIPTOR)malloc(sd_size); if(pipe_sd) { pAcl=(PACL)(((char *)pipe_sd)+SECURITY_DESCRIPTOR_MIN_LENGTH); if(!(InitializeAcl(pAcl,sd_size-SECURITY_DESCRIPTOR_MIN_LENGTH,ACL_REVISION)&&AddAccessAllowedAce(pAcl,ACL_REVISION,FILE_ALL_ACCESS,pAccessSid)&&AddAccessAllowedAce(pAcl,ACL_REVISION,FILE_ALL_ACCESS,pSystemSid)&&InitializeSecurityDescriptor(pipe_sd,SECURITY_DESCRIPTOR_REVISION)&&SetSecurityDescriptorDacl(pipe_sd,TRUE,pAcl,FALSE))) { free(pipe_sd); pipe_sd=NULL; } } FreeSid(pSystemSid); } FreeSid(pAccessSid); } } for(i=0;i<GetThreadCount();i++) { threads[i]=CreateThread(NULL,0,ServiceStartThread,(void *)(DWORD_PTR)i,CREATE_SUSPENDED,&ThreadID); if(threads[i]) { SetThreadPriority(threads[i],GetHearPriority()); ResumeThread(threads[i]); } } WaitStartEvent(aService); if(aService) ReportStatusToSCMgr(SERVICE_RUNNING,NO_ERROR,0); }
static DWORD WINAPI ServiceStartThread(LPVOID lpvThreadParm) { HANDLE hPipe=INVALID_HANDLE_VALUE; HANDLE hEvents[2]={NULL,NULL}; OVERLAPPED os; SECURITY_ATTRIBUTES sa; DWORD transferred; DWORD dCode; DWORD dFlags; DWORD dCount; struct FileRec *files=NULL; struct FileRecs *sfiles=NULL; void *add_data=NULL; struct SmallInfoRec *alldata=NULL; struct StrRec *rec=NULL; HANDLE hThread; DWORD ThreadID; BOOL connected=FALSE; DWORD index=(DWORD)(DWORD_PTR)lpvThreadParm; // Service initialization hServerStopEvent[index]=CreateEventW(NULL,TRUE,FALSE,NULL); if(!hServerStopEvent[index]) goto cleanup; hEvents[0]=hServerStopEvent[index]; // create the event object object use in overlapped i/o hEvents[1]=CreateEventW(NULL,TRUE,FALSE,NULL); if(!hEvents[1]) goto cleanup; sa.nLength=sizeof(sa); sa.lpSecurityDescriptor=pipe_sd; sa.bInheritHandle=FALSE; // open our named pipe... hPipe=CreateNamedPipeW(PIPE_NAMEW,FILE_FLAG_OVERLAPPED|PIPE_ACCESS_DUPLEX,PIPE_TYPE_BYTE|PIPE_READMODE_BYTE|PIPE_WAIT,GetThreadCount(),0,0,1000,&sa); if(hPipe==INVALID_HANDLE_VALUE) goto cleanup; // End of initialization if(!index) ResetStartEvent(); // Service is now running, perform work until shutdown for(;;connected?(DisconnectNamedPipe(hPipe),connected=FALSE):0) { //check stack if(!HeapValidate(GetProcessHeap(),0,NULL)) { LogSys(L"HeapValidate",NULL,NULL); break; } //clean up if(files) { free(files); files=NULL; } if(sfiles) { free(sfiles); sfiles=NULL; } if(add_data) { free(add_data); add_data=NULL; } if(rec) { free(rec); rec=NULL; } if(alldata) { free(alldata); alldata=NULL; } TRANSFER_INIT if(!ConnectNamedPipe(hPipe,&os)) { DWORD Error=GetLastError(); if(Error==ERROR_IO_PENDING) { if(WaitForMultipleObjects(2,hEvents,FALSE,INFINITE)!=WAIT_OBJECT_0+1) break; if(!GetOverlappedResult(hPipe,&os,&transferred,FALSE)) Error=GetLastError(); else Error=ERROR_SUCCESS; } if(Error!=ERROR_PIPE_CONNECTED&&Error!=ERROR_SUCCESS) { if(WaitForSingleObject(hEvents[0],200)==WAIT_OBJECT_0) break; continue; } } connected=TRUE; // grab whatever's coming through the pipe... TRANSFER_DATA(ReadFile,&dCode,sizeof(dCode)) TRANSFER_DATA(ReadFile,&dFlags,sizeof(dFlags)) //check network if(dCode==OPERATION_COPY) { if(dFlags©FLAG_ADDITIONAL_DATA) { DWORD dAddSize; TRANSFER_DATA(ReadFile,&dAddSize,sizeof(dAddSize)) add_data=malloc(dAddSize); if(add_data) { TRANSFER_DATA(ReadFile,add_data,dAddSize) } else continue; } TRANSFER_DATA(ReadFile,&dCount,sizeof(dCount)) if(dCount>2) { files=(struct FileRec *)malloc(sizeof(struct FileRec)*dCount); if(files) { TRANSFER_DATA(ReadFile,files,sizeof(struct FileRec)*dCount) sfiles=(struct FileRecs *)malloc(sizeof(struct FileRecs)); if(sfiles) { HANDLE wait_event=CreateEventW(NULL,TRUE,FALSE,NULL); sfiles->event=wait_event; sfiles->flags=dFlags; sfiles->count=dCount; sfiles->files=files; sfiles->add=add_data; hThread=CreateThread(NULL,0,CopyThread,sfiles,CREATE_SUSPENDED,&ThreadID); if(hThread) { SetThreadPriority(hThread,GetWorkPriority()); if(IsSystem) PipeToThread(hPipe,hThread); ResumeThread(hThread); if(wait_event) { struct InfoRec RetData; WaitForSingleObject(wait_event,INFINITE); CloseHandle(wait_event); RetData.info.ThreadId=ThreadID; GetInfo(ThreadID,&RetData); TRANSFER_DATA(WriteFile,&RetData.info,sizeof(RetData.info)) } CloseHandle(hThread); files=NULL; sfiles=NULL; add_data=NULL; } else CloseHandle(wait_event); } }
size_t PlatformInterface::GetCoreCount() const { return GetThreadCount(); }