bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 06/11] pipe_filter_ii_execute: port to OS/2 kLIBC


From: KO Myung-Hun
Subject: [PATCH 06/11] pipe_filter_ii_execute: port to OS/2 kLIBC
Date: Sat, 7 Nov 2015 11:45:51 +0900

Pipes on kLIBC does not support O_NONBLOCK like Win32.

* lib/pipe-filter-ii.c (start_wrapper, _beginthreadex, CloseHandle,
WaiForSingleObject, WaitForMultipleObjects): New on OS/2 kLIBC.
Reuse Win32 codes on OS/2 kLIBC.
* lib/spawn-pipe.c: Reuse Win32 codes on OS/2 kLIBC.
* lib/w32spawn.h: Do not include windows.h on OS/2 kLIBC.
---
 ChangeLog            |  11 +++++
 lib/pipe-filter-ii.c | 133 +++++++++++++++++++++++++++++++++++++++++++++++++--
 lib/spawn-pipe.c     |   6 ++-
 lib/w32spawn.h       |   6 ++-
 4 files changed, 148 insertions(+), 8 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 602ce34..205ed29 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,16 @@
 2015-11-07  KO Myung-Hun  <address@hidden>
 
+    pipe_filter_ii_execute: port to OS/2 kLIBC
+    Pipes on kLIBC does not support O_NONBLOCK like Win32.
+
+    * lib/pipe-filter-ii.c (start_wrapper, _beginthreadex, CloseHandle,
+    WaiForSingleObject, WaitForMultipleObjects): New on OS/2 kLIBC.
+    Reuse Win32 codes on OS/2 kLIBC.
+    * lib/spawn-pipe.c: Reuse Win32 codes on OS/2 kLIBC.
+    * lib/w32spawn.h: Do not include windows.h on OS/2 kLIBC.
+
+2015-11-07  KO Myung-Hun  <address@hidden>
+
     wchar: fix 'conflicting types' error for `__wcwidth' on OS/2 kLIBC
     On OS/2 kLIBC, wcwidth is a macro to static inline function.
     Implementation of wcwidth in wcwidth.c causes 'conflicting types'
diff --git a/lib/pipe-filter-ii.c b/lib/pipe-filter-ii.c
index 88b0d0d..fff96b4 100644
--- a/lib/pipe-filter-ii.c
+++ b/lib/pipe-filter-ii.c
@@ -27,6 +27,127 @@
 #include <unistd.h>
 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
 # include <windows.h>
+#elif defined __KLIBC__
+# define INCL_DOS
+# include <os2.h>
+
+/* Simple implementation of Win32 APIs */
+
+# define WINAPI
+
+typedef struct _HANDLE
+{
+    TID tid;
+    HEV hevDone;
+    unsigned int WINAPI (*start) (void *);
+    void *arg;
+} *HANDLE;
+
+typedef ULONG DWORD;
+
+static void
+start_wrapper (void *arg)
+{
+  HANDLE h = (HANDLE) arg;
+
+  h->start (h->arg);
+
+  DosPostEventSem (h->hevDone);
+  _endthread ();
+}
+
+static HANDLE
+_beginthreadex (void *s, unsigned n, unsigned int WINAPI (*start) (void *),
+                void *arg, unsigned fl, unsigned *th)
+{
+  HANDLE h;
+
+  h = malloc (sizeof (*h));
+  if (!h)
+    return NULL;
+
+  if (DosCreateEventSem (NULL, &h->hevDone, 0, FALSE ))
+    goto exit_free;
+
+  h->start = start;
+  h->arg   = arg;
+
+  h->tid = _beginthread (start_wrapper, NULL, n, (void *) h);
+  if (h->tid == -1)
+    goto exit_close_event_sem;
+
+  return h;
+
+exit_close_event_sem:
+  DosCloseEventSem (h->hevDone);
+
+exit_free:
+  free (h);
+
+  return NULL;
+}
+
+static BOOL
+CloseHandle (HANDLE h)
+{
+  DosCloseEventSem (h->hevDone);
+  free (h);
+}
+
+# define _endthreadex(x) return (x)
+# define TerminateThread(h, e) DosKillThread (h->tid)
+
+# define GetLastError()  -1
+
+# ifndef ERROR_NO_DATA
+#  define ERROR_NO_DATA 232
+# endif
+
+# define INFINITE       SEM_INDEFINITE_WAIT
+# define WAIT_OBJECT_0  0
+
+static DWORD
+WaitForSingleObject (HANDLE h, DWORD ms)
+{
+  return DosWaitEventSem (h->hevDone, ms) == 0 ? WAIT_OBJECT_0 : (DWORD) -1;
+}
+
+static DWORD
+WaitForMultipleObjects (DWORD nCount, const HANDLE *pHandles, BOOL bWaitAll,
+                        DWORD ms)
+{
+    HMUX       hmux;
+    PSEMRECORD psr;
+    ULONG      ulUser;
+    ULONG      rc = (ULONG) -1;
+    DWORD      i;
+
+    psr = malloc (sizeof (*psr) * nCount);
+    if (!psr)
+      goto exit_return;
+
+    for (i = 0; i < nCount; ++i)
+      {
+        psr[i].hsemCur = (HSEM) pHandles[i]->hevDone;
+        psr[i].ulUser  = WAIT_OBJECT_0 + i;
+      }
+
+    if (DosCreateMuxWaitSem (NULL, &hmux, nCount, psr,
+                             bWaitAll ? DCMW_WAIT_ALL : DCMW_WAIT_ANY))
+      goto exit_free;
+
+    rc = DosWaitMuxWaitSem (hmux, ms, &ulUser);
+    DosCloseMuxWaitSem (hmux);
+
+exit_free:
+    free (psr);
+
+exit_return:
+    if (rc)
+        return (DWORD) -1;
+
+    return ulUser;
+}
 #else
 # include <signal.h>
 # include <sys/select.h>
@@ -41,7 +162,8 @@
 
 #include "pipe-filter-aux.h"
 
-#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
+#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ \
+    || defined __KLIBC__
 
 struct locals
 {
@@ -143,7 +265,8 @@ pipe_filter_ii_execute (const char *progname,
 {
   pid_t child;
   int fd[2];
-#if !((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__)
+#if !((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ \
+      || defined __KLIBC__)
   struct sigaction orig_sigpipe_action;
 #endif
 
@@ -154,7 +277,8 @@ pipe_filter_ii_execute (const char *progname,
   if (child == -1)
     return -1;
 
-#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
+#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ \
+    || defined __KLIBC__
   /* Native Windows API.  */
   /* Pipes have a non-blocking mode, see function SetNamedPipeHandleState and
      the article "Named Pipe Type, Read, and Wait Modes", but Microsoft's
@@ -462,7 +586,8 @@ pipe_filter_ii_execute (const char *progname,
   {
     int saved_errno = errno;
     close (fd[1]);
-#if !((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__)
+#if !((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ \
+      || defined __KLIBC__)
     if (sigaction (SIGPIPE, &orig_sigpipe_action, NULL) < 0)
       abort ();
 #endif
diff --git a/lib/spawn-pipe.c b/lib/spawn-pipe.c
index 2c2270e..00b9b0d 100644
--- a/lib/spawn-pipe.c
+++ b/lib/spawn-pipe.c
@@ -35,7 +35,8 @@
 
 #define _(str) gettext (str)
 
-#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
+#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ \
+    || defined __KLIBC__
 
 /* Native Windows API.  */
 # include <process.h>
@@ -109,7 +110,8 @@ create_pipe (const char *progname,
              bool slave_process, bool exit_on_error,
              int fd[2])
 {
-#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
+#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ \
+    || defined __KLIBC__
 
   /* Native Windows API.
      This uses _pipe(), dup2(), and spawnv().  It could also be implemented
diff --git a/lib/w32spawn.h b/lib/w32spawn.h
index dbab2f8..fef7f6b 100644
--- a/lib/w32spawn.h
+++ b/lib/w32spawn.h
@@ -15,9 +15,11 @@
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
+#ifndef __KLIBC__
 /* Get declarations of the native Windows API functions.  */
-#define WIN32_LEAN_AND_MEAN
-#include <windows.h>
+# define WIN32_LEAN_AND_MEAN
+# include <windows.h>
+#endif
 
 /* Get _open_osfhandle().  */
 #include <io.h>
-- 
2.6.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]