Skip to content

Instantly share code, notes, and snippets.

@dirocco
Created May 30, 2013 12:23
Show Gist options
  • Save dirocco/5677464 to your computer and use it in GitHub Desktop.
Save dirocco/5677464 to your computer and use it in GitHub Desktop.
Awful snippet for calling awful PogoAutoSweep while still being thread safe with every other function in the program.
#include <pgobootrun.h>
#include <TlHelp32.h>
#pragma comment(lib, "pgobootrun.lib")
//static int threadcount = 0;
inline void sweep()
{
if( getenv("PGOSWEEP") ) // can't #define this or it will undo PGO update
{
//if( ++threadcount == 5 )
{
DWORD processId = GetCurrentProcessId();
HANDLE hThreadSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
THREADENTRY32 threadEntry;
threadEntry.dwSize = sizeof(THREADENTRY32);
Thread32First(hThreadSnapshot, &threadEntry);
printf("Suspending everything for pogo sweep\n");
do
{
if (threadEntry.th32OwnerProcessID == processId)
{
HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, FALSE,
threadEntry.th32ThreadID);
if( GetThreadId( hThread ) != GetCurrentThreadId() )
SuspendThread(hThread);
}
} while (Thread32Next(hThreadSnapshot, &threadEntry));
Sleep(500);
PogoAutoSweepA("this file suffix breaks VS2012 PGO build script no matter what");
Thread32First(hThreadSnapshot, &threadEntry);
do
{
if (threadEntry.th32OwnerProcessID == processId)
{
HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, FALSE,
threadEntry.th32ThreadID);
if( GetThreadId( hThread ) != GetCurrentThreadId() )
ResumeThread(hThread);
}
} while (Thread32Next(hThreadSnapshot, &threadEntry));
printf("Resuming everything\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment