You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2009/09/17 07:45:29 UTC

svn commit: r816062 - in /commons/sandbox/runtime/trunk/src/main/native: include/acr_signals.h os/win32/env.c os/win32/signals.c os/win32/time.c

Author: mturk
Date: Thu Sep 17 05:45:28 2009
New Revision: 816062

URL: http://svn.apache.org/viewvc?rev=816062&view=rev
Log:
Implement ACR_Signal and ACR_Sleep

Modified:
    commons/sandbox/runtime/trunk/src/main/native/include/acr_signals.h
    commons/sandbox/runtime/trunk/src/main/native/os/win32/env.c
    commons/sandbox/runtime/trunk/src/main/native/os/win32/signals.c
    commons/sandbox/runtime/trunk/src/main/native/os/win32/time.c

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_signals.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_signals.h?rev=816062&r1=816061&r2=816062&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr_signals.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr_signals.h Thu Sep 17 05:45:28 2009
@@ -31,6 +31,8 @@
  *
  */
 
+typedef void (acr_sigfunc_t)(int);
+
 /**
  * Get the description for a specific signal number
  * @param signum The signal number
@@ -57,6 +59,15 @@
  */
 ACR_DECLARE(int) ACR_SignalSetKey(const acr_pchar_t *key);
 
+/**
+ * Set the signal handler function for a given signal
+ * @param signo The signal (eg... SIGHUP)
+ * @param function The function to get called on signal.
+ * @return previous signal handler function
+ */
+ACR_DECLARE(acr_sigfunc_t *) ACR_Signal(int signo, acr_sigfunc_t *func);
+
+
 #ifdef __cplusplus
 }
 #endif

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/env.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/env.c?rev=816062&r1=816061&r2=816062&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/env.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/env.c Thu Sep 17 05:45:28 2009
@@ -189,7 +189,9 @@
 
 ACR_DECLARE(int) ACR_EnvDelete(const char *var)
 {
-    char *estr;
+    char   bstk[128];
+    char  *estr;
+    size_t elen;
 
     if (!var || !*var) {
         errno = EINVAL;
@@ -197,7 +199,11 @@
     }
     if (getenv(var) == NULL)
         return 0;             /* no work */
-    estr = (char *)x_malloc(strlen(var) + 2);
+    elen = strlen(var) + 2;
+    if (elen > 128)
+        estr = (char *)x_malloc(elen);
+    else
+        estr = bstk;
     if (!estr) {
         /* not much we can do if no memory */
         ACR_SET_OS_ERROR(ACR_ENOMEM);
@@ -208,12 +214,14 @@
     sprintf(estr, "%s=", var);
     if (_msvcrt_putenv(estr)) {
         int se = errno;
-        x_free(estr);
+        if (estr != bstk)
+            x_free(estr);
         errno = se;
         return -1;
     }
 
-    x_free(estr);
+    if (estr != bstk)
+        x_free(estr);
     return 0;
 }
 
@@ -279,6 +287,7 @@
 
 int acr_EnvDeleteW(const wchar_t *var)
 {
+    wchar_t  bstk[128];
     wchar_t *estr;
     size_t   elen;
 
@@ -289,7 +298,10 @@
     if (_wgetenv(var) == NULL)
         return 0;             /* no work */
     elen = (wcslen(var) + 2);
-    estr = s_malloc(wchar_t, elen);
+    if (elen > 128)
+        estr = s_malloc(wchar_t, elen);
+    else
+        estr = bstk;
     if (!estr) {
         /* not much we can do if no memory */
         ACR_SET_OS_ERROR(ACR_ENOMEM);
@@ -299,12 +311,12 @@
     _snwprintf(estr, elen, L"%s=", var);
     if (_msvcrt_wputenv(estr)) {
         int se = errno;
-        x_free(estr);
+        if (estr != bstk)
+            x_free(estr);
         errno = se;
         return -1;
     }
-
-    x_free(estr);
+    if (estr != bstk)
+        x_free(estr);
     return 0;
 }
-

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/signals.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/signals.c?rev=816062&r1=816061&r2=816062&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/signals.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/signals.c Thu Sep 17 05:45:28 2009
@@ -305,9 +305,8 @@
 }
 
 
-static void WINAPI completion_write(DWORD, DWORD, LPOVERLAPPED);
-static void WINAPI completion_sread(DWORD err, DWORD nread,
-                                    LPOVERLAPPED lpo)
+static void WINAPI completion_wr(DWORD, DWORD, LPOVERLAPPED);
+static void WINAPI completion_rd(DWORD err, DWORD nread, LPOVERLAPPED lpo)
 {
     sig_pipe_data_t *pd = (sig_pipe_data_t *)lpo;
     BOOL ws = FALSE;
@@ -357,7 +356,7 @@
                          &pd->msg,
                          (DWORD)sizeof(acr_sig_msg_t),
                          (LPOVERLAPPED)pd,
-                         (LPOVERLAPPED_COMPLETION_ROUTINE)completion_write);
+                         (LPOVERLAPPED_COMPLETION_ROUTINE)completion_wr);
 #if defined(DEBUG)
         if (!ws) {
             fprintf(stderr, "WriteFileEx failed (%d)\n", GetLastError());
@@ -374,8 +373,7 @@
     }
 }
 
-static void WINAPI completion_write(DWORD err, DWORD nsend,
-                                    LPOVERLAPPED lpo)
+static void WINAPI completion_wr(DWORD err, DWORD nsend, LPOVERLAPPED lpo)
 {
     sig_pipe_data_t *pd = (sig_pipe_data_t *)lpo;
     BOOL rs = FALSE;
@@ -385,7 +383,7 @@
                         &pd->msg,
                         (DWORD)sizeof(acr_sig_msg_t),
                         (LPOVERLAPPED)pd,
-                        (LPOVERLAPPED_COMPLETION_ROUTINE)completion_sread);
+                        (LPOVERLAPPED_COMPLETION_ROUTINE)completion_rd);
 #if defined(DEBUG)
         if (!rs) {
             fprintf(stderr, "ReadFileEx failed (%d)\n", GetLastError());
@@ -514,7 +512,7 @@
                         break;
                     }
                     pd->pipe = sig_pipe_handle;
-                    completion_write(0, 0, (LPOVERLAPPED)pd);
+                    completion_wr(0, 0, (LPOVERLAPPED)pd);
                 }
                 cs = sig_pipe_connect(&sync);
             break;
@@ -767,6 +765,25 @@
     }
 }
 
+ACR_DECLARE(acr_sigfunc_t *) ACR_Signal(int signo, acr_sigfunc_t *func)
+{
+    acr_sigfunc_t *orghandler;
+
+    if (signo <= 0 || signo >= _NSIG) {
+        SetLastError(ERROR_INVALID_PARAMETER);
+        return SIG_ERR;
+    }
+    if (func == SIG_ACK || func == SIG_SGE) {
+        SetLastError(ERROR_NOT_SUPPORTED);
+        return SIG_ERR;
+    }
+    orghandler = signal_handlers[signo];
+    if (func != SIG_GET)
+        signal_handlers[signo] = func;
+
+    return orghandler;
+}
+
 ACR_DECLARE(int) ACR_SignalSetKey(const wchar_t *key)
 {
     acr_sha1_ctx_t sha;
@@ -797,4 +814,3 @@
     }
     return ACR_SUCCESS;
 }
-

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/time.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/time.c?rev=816062&r1=816061&r2=816062&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/time.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/time.c Thu Sep 17 05:45:28 2009
@@ -69,3 +69,17 @@
             return 0x00210000;
     }
 }
+
+ACR_DECLARE(void) ACR_Sleep(acr_time_t t)
+{
+    DWORD ms = (DWORD)(t < 500 ? 1 : (t + 500) / 1000);
+
+    /* Wait on signal event if not already signaled.
+     * Otherwise use alertable SleepEx
+     */
+    DWORD rc = WaitForSingleObject(sig_raised_event, 0);
+    if (rc == WAIT_TIMEOUT)
+        WaitForSingleObject(sig_raised_event, ms);
+    else
+        SleepEx(ms, TRUE);
+}