fix and question re: waitpid() under win32
Brian Clarke [Fri, 28 Jul 2000 15:18:29 +0000 (11:18 -0400)]
Message-ID: <3981DC85.290314EB@appliedmeta.com>

Slightly reformatted and WNOHANG # define moved to win32.h
so that also POSIX.xs sees it, as suggsted by Sarathy.

p4raw-id: //depot/perl@6472

win32/win32.c
win32/win32.h

index 60777fa..3c3d1e5 100644 (file)
@@ -53,7 +53,6 @@
 #else
 #include <utime.h>
 #endif
-
 #ifdef __GNUC__
 /* Mingw32 defaults to globing command line 
  * So we turn it off like this:
@@ -1645,8 +1644,12 @@ win32_waitpid(int pid, int *status, int flags)
        long child = find_pseudo_pid(-pid);
        if (child >= 0) {
            HANDLE hThread = w32_pseudo_child_handles[child];
-           DWORD waitcode = WaitForSingleObject(hThread, INFINITE);
-           if (waitcode != WAIT_FAILED) {
+           DWORD timeout = (flags & WNOHANG) ? 0 : INFINITE;
+           DWORD waitcode = WaitForSingleObject(hThread, timeout);
+           if (waitcode == WAIT_TIMEOUT) {
+               return 0;
+           }
+           else if (waitcode != WAIT_FAILED) {
                if (GetExitCodeThread(hThread, &waitcode)) {
                    *status = (int)((waitcode & 0xff) << 8);
                    retval = (int)w32_pseudo_child_pids[child];
@@ -1663,15 +1666,20 @@ win32_waitpid(int pid, int *status, int flags)
        long child = find_pid(pid);
        if (child >= 0) {
            HANDLE hProcess = w32_child_handles[child];
-           DWORD waitcode = WaitForSingleObject(hProcess, INFINITE);
-           if (waitcode != WAIT_FAILED) {
-               if (GetExitCodeProcess(hProcess, &waitcode)) {
-                   *status = (int)((waitcode & 0xff) << 8);
-                   retval = (int)w32_child_pids[child];
-                   remove_dead_process(child);
-                   return retval;
-               }
-           }
+                DWORD timeout=(flags&WNOHANG)?0:INFINITE;
+                DWORD waitcode = WaitForSingleObject(hProcess, timeout);
+                if (waitcode == WAIT_TIMEOUT) {
+                {
+                       return 0;
+                }
+                else if (waitcode != WAIT_FAILED) {
+                    if (GetExitCodeProcess(hProcess, &waitcode)) {
+                        *status = (int)((waitcode & 0xff) << 8);
+                        retval = (int)w32_child_pids[child];
+                        remove_dead_process(child);
+                        return retval;
+                    }
+                }
            else
                errno = ECHILD;
        }
index 2e5b074..eb5ecd2 100644 (file)
@@ -492,5 +492,9 @@ struct interp_intern {
  */
 #include "win32iop.h"
 
+#ifndef WNOHANG
+#  define WNOHANG 1
+#endif
+
 #endif /* _INC_WIN32_PERL5 */