📄 pminit.cpp
字号:
// routine to make sure that registry settings are present for all the power
// states they expect to support. If the registry is not configured, the OEM
// code can treat it as a fatal error or perform its own initialization.
if(fOk) {
DWORD dwStatus = PlatformValidatePMRegistry();
if(dwStatus != ERROR_SUCCESS) {
PMLOGMSG(ZONE_ERROR, (_T("%s: PlatformValidatePMRegistry() failed %d\r\n"),
pszFname, dwStatus));
fOk = FALSE;
} else {
// read the list of interface types we will monitor
fOk = DeviceListsInit();
}
}
// create events
if(fOk) {
ghevPowerManagerReady = CreateEvent(NULL, TRUE, FALSE, _T("SYSTEM/PowerManagerReady"));
ghevResume = CreateEvent(NULL, FALSE, FALSE, NULL);
ghevTimerResume = CreateEvent(NULL, FALSE, FALSE, NULL);
hevPnPReady = CreateEvent(NULL, FALSE, FALSE, NULL);
hevResumeReady = CreateEvent(NULL, FALSE, FALSE, NULL);
hevSystemReady = CreateEvent(NULL, FALSE, FALSE, NULL);
hevActivityTimersReady = CreateEvent(NULL, FALSE, FALSE, NULL);
hevDummy = CreateEvent(NULL, FALSE, FALSE, NULL);
// check everything
if(hevPnPReady == NULL
|| hevResumeReady == NULL
|| hevSystemReady == NULL
|| hevActivityTimersReady == NULL
|| hevDummy == NULL
|| ghevPowerManagerReady == NULL
|| ghevTimerResume == NULL
|| ghevResume == NULL) {
PMLOGMSG(ZONE_ERROR, (_T("%s: event creation failure\r\n"), pszFname));
fOk = FALSE;
}
}
// start threads
if(fOk) {
ghtPnP = CreateThread(NULL, 0, PnpThreadProc, (LPVOID) hevPnPReady, 0, NULL);
ghtResume = CreateThread(NULL, 0, ResumeThreadProc, (LPVOID) hevResumeReady, 0, NULL);
ghtActivityTimers = CreateThread(NULL, 0, ActivityTimersThreadProc,
(LPVOID) hevActivityTimersReady, 0, NULL);
// check everything
if(ghtPnP == NULL
|| ghtResume == NULL
|| ghtActivityTimers == NULL) {
PMLOGMSG(ZONE_ERROR, (_T("%s: thread creation failure\r\n"), pszFname));
fOk = FALSE;
}
}
// wait for threads to initialize (or fail to initialize)
#define NUM_OF_READY_EXIT_PAIR 3
HANDLE hEvents[] = { hevPnPReady, hevResumeReady, hevActivityTimersReady,
ghtPnP, ghtResume, ghtActivityTimers };
int iReady = 0;
while( iReady < NUM_OF_READY_EXIT_PAIR && fOk) {
DWORD dwStatus = WaitForMultipleObjects(dim(hEvents), hEvents, FALSE, INFINITE);
switch(dwStatus) {
// thread ready events
case (WAIT_OBJECT_0 + 0): // pnp ready
case (WAIT_OBJECT_0 + 1): // resume ready
case (WAIT_OBJECT_0 + 2): // activity timers ready
// don't watch for the thread exiting now -- some may do
// so if they don't have work to do.
hEvents[dwStatus - WAIT_OBJECT_0 + NUM_OF_READY_EXIT_PAIR] = hevDummy;
iReady++;
break;
// thread exiting events
case (WAIT_OBJECT_0 + 3): // pnp exited
case (WAIT_OBJECT_0 + 4): // resume exited
case (WAIT_OBJECT_0 + 5): // activity timers exited
PMLOGMSG(ZONE_ERROR, (_T("%s: thread initialization failure\r\n"), pszFname));
fOk = FALSE;
break;
default:
PMLOGMSG(ZONE_ERROR, (_T("%s: WaitForMultipleObjects() returnd %d, status is %d\r\n"),
pszFname, GetLastError()));
fOk = FALSE;
break;
}
}
// if everything is initialized, we're ready start the system thread
if(fOk) {
// check that the thread started
ghtSystem = CreateThread(NULL, 0, SystemThreadProc, (LPVOID) hevSystemReady, 0, NULL);
if(ghtSystem == NULL) {
PMLOGMSG(ZONE_ERROR, (_T("%s: system thread creation failure\r\n"), pszFname));
fOk = FALSE;
}
// wait for it to initialize or exit
HANDLE hSystemEvents[] = { hevSystemReady, ghtSystem };
DWORD dwStatus = WaitForMultipleObjects(dim(hSystemEvents), hSystemEvents, FALSE, INFINITE);
if(dwStatus != WAIT_OBJECT_0) {
PMLOGMSG(ZONE_ERROR, (_T("%s: system thread initialization failure\r\n"), pszFname));
fOk = FALSE;
}
}
// should we signal that our API is ready?
if(fOk) {
// yes, the PM is initialized
SetEvent(ghevPowerManagerReady);
} else {
// no, clean up
if(ghevPmShutdown != NULL) {
// tell threads to shut down
SetEvent(ghevPmShutdown);
}
// wait for threads to exit
if(ghtPnP != NULL) {
PMLOGMSG(ZONE_ERROR, (_T("%s: shutting down PnP thread\r\n"), pszFname));
WaitForSingleObject(ghtPnP, INFINITE);
CloseHandle(ghtPnP);
}
if(ghtResume != NULL) {
PMLOGMSG(ZONE_ERROR, (_T("%s: shutting down resume thread\r\n"), pszFname));
WaitForSingleObject(ghtResume, INFINITE);
CloseHandle(ghtResume);
}
if(ghtSystem != NULL) {
PMLOGMSG(ZONE_ERROR, (_T("%s: shutting down system thread\r\n"), pszFname));
WaitForSingleObject(ghtSystem, INFINITE);
CloseHandle(ghtSystem);
}
if(ghtActivityTimers != NULL) {
PMLOGMSG(ZONE_ERROR, (_T("%s: shutting down activity timers thread\r\n"), pszFname));
WaitForSingleObject(ghtActivityTimers, INFINITE);
CloseHandle(ghtActivityTimers);
}
PMLOGMSG(ZONE_ERROR, (_T("%s: closing handles\r\n"), pszFname));
if(ghevPmShutdown != NULL) CloseHandle(ghevPmShutdown);
if(ghevPowerManagerReady != NULL) CloseHandle(ghevPowerManagerReady);
if(ghevResume != NULL) CloseHandle(ghevResume);
if(ghevTimerResume != NULL) CloseHandle(ghevTimerResume);
}
// clean up status handles
if(hevPnPReady != NULL) CloseHandle(hevPnPReady);
if(hevResumeReady != NULL) CloseHandle(hevResumeReady);
if(hevSystemReady != NULL) CloseHandle(hevSystemReady);
if(hevActivityTimersReady != NULL) CloseHandle(hevActivityTimersReady);
if(hevDummy != NULL) CloseHandle(hevDummy);
PMLOGMSG((!fOk && ZONE_ERROR) || ZONE_INIT || ZONE_API,
(_T("-%s: returning %d\r\n"), pszFname, fOk));
DEBUGCHK(fOk); // make sure we raise a red flag if the init fails
return fOk;
}
// This routine receives notifications from the system about various system
// events, including processes exiting. Some of the messages have the same
// names as DllEntry() messages, but the resemblance is superficial. Note
// that unlike DLL entry point message handlers, these notifications allow
// the message handler to use synchronization objects without deadlocking
// the system.
EXTERN_C VOID WINAPI
PmNotify(DWORD dwFlags, HPROCESS hProcess, HTHREAD hThread)
{
SETFNAME(_T("PmNotify"));
UnusedParameter(hThread);
switch(dwFlags) {
case DLL_PROCESS_DETACH:
// release any resources held by this process
PMLOGMSG(ZONE_API, (_T("+%s: hProcess 0x%08x exiting\r\n"), pszFname, hProcess));
DeleteProcessRequirements(hProcess);
DeleteProcessNotifications(hProcess);
PMLOGMSG(ZONE_API, (_T("-%s\r\n"), pszFname));
break;
default:
break;
}
}
// dll entry point
BOOL WINAPI
DllEntry(HANDLE hInst, DWORD dwReason, LPVOID lpvReserved)
{
BOOL bRc = TRUE;
SETFNAME(_T("Power Manager"));
UnusedParameter(hInst);
UnusedParameter(lpvReserved);
switch (dwReason) {
case DLL_PROCESS_ATTACH:
ghInst = (HINSTANCE) hInst;
RETAILREGISTERZONES(ghInst); // zones in both debug and retail builds
PMLOGMSG(ZONE_INIT,(TEXT("*** %s: DLL_PROCESS_ATTACH - Current Process: 0x%x, ID: 0x%x ***\r\n"),
pszFname, GetCurrentProcess(), GetCurrentProcessId()));
DisableThreadLibraryCalls((HMODULE) hInst);
break;
case DLL_PROCESS_DETACH:
PMLOGMSG(ZONE_INIT,(TEXT("*** %s: DLL_PROCESS_DETACH - Current Process: 0x%x, ID: 0x%x ***\r\n"),
pszFname, GetCurrentProcess(), GetCurrentProcessId()));
if (g_pSysRegistryAccess != NULL)
delete g_pSysRegistryAccess;
g_pSysRegistryAccess = NULL;
break;
default:
break;
}
return bRc;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -