build/pymake/pymake/win32process.py
author Aaron Klotz <aklotz@mozilla.com>
Mon, 15 Aug 2016 15:05:28 -0600
changeset 309556 a0085eb5ffe746352ea4cf4f357344443e8ddf90
parent 95077 e5e7f60fe0bed65a97963e5d5c9adc0d379a8f3e
child 523296 240bd8fe241e5c10c35c9b5cd94b1608582ad4fa
permissions -rw-r--r--
Bug 1268544: Followup for atk missing header build error; r=me MozReview-Commit-ID: KJva79swOtr

from ctypes import windll, POINTER, byref, WinError
from ctypes.wintypes import WINFUNCTYPE, HANDLE, DWORD, BOOL

INFINITE = -1
WAIT_FAILED = 0xFFFFFFFF

LPDWORD = POINTER(DWORD)
_GetExitCodeProcessProto = WINFUNCTYPE(BOOL, HANDLE, LPDWORD)
_GetExitCodeProcess = _GetExitCodeProcessProto(("GetExitCodeProcess", windll.kernel32))
def GetExitCodeProcess(h):
    exitcode = DWORD()
    r = _GetExitCodeProcess(h, byref(exitcode))
    if r is 0:
        raise WinError()
    return exitcode.value

_WaitForMultipleObjectsProto = WINFUNCTYPE(DWORD, DWORD, POINTER(HANDLE), BOOL, DWORD)
_WaitForMultipleObjects = _WaitForMultipleObjectsProto(("WaitForMultipleObjects", windll.kernel32))

def WaitForAnyProcess(processes):
    arrtype = HANDLE * len(processes)
    harray = arrtype(*(int(p._handle) for p in processes))

    r = _WaitForMultipleObjects(len(processes), harray, False, INFINITE)
    if r == WAIT_FAILED:
        raise WinError()

    return processes[r], GetExitCodeProcess(int(processes[r]._handle)) <<8