Author: mturk
Date: Tue Aug 18 06:40:41 2009
New Revision: 805289

URL: http://svn.apache.org/viewvc?rev=805289&view=rev
Log:
Add windows semaphore implementation

Added:
    commons/sandbox/runtime/trunk/src/main/native/os/win32/psema.c   (with 
props)
Modified:
    commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in
    commons/sandbox/runtime/trunk/src/main/native/os/win32/pmutex.c

Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in?rev=805289&r1=805288&r2=805289&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in Tue Aug 18 
06:40:41 2009
@@ -103,6 +103,7 @@
        $(SRCDIR)/os/win32/os.$(OBJ) \
        $(SRCDIR)/os/win32/ios.$(OBJ) \
        $(SRCDIR)/os/win32/pmutex.$(OBJ) \
+       $(SRCDIR)/os/win32/psema.$(OBJ) \
        $(SRCDIR)/os/win32/shm.$(OBJ) \
        $(SRCDIR)/os/win32/signals.$(OBJ) \
        $(SRCDIR)/os/win32/syslog.$(OBJ) \

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/pmutex.c
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/pmutex.c?rev=805289&r1=805288&r2=805289&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/pmutex.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/pmutex.c Tue Aug 18 
06:40:41 2009
@@ -96,7 +96,7 @@
     int rc;
     DWORD  ws;
     HANDLE wh[2];
-    
+
     wh[0] = (HANDLE)ACR_IOH(mutex);
     wh[1] = dll_psig_handle;
     if (IS_INVALID_HANDLE(wh[0]) || ACR_IOH_TYPE(mutex) != ACR_DT_MUTEX) {
@@ -165,6 +165,11 @@
 ACR_DECLARE(int) ACR_ProcMutexPermSet(JNIEnv *_E, int mutex, int perms,
                                       acr_uid_t uid, acr_uid_t gid)
 {
+    HANDLE m = (HANDLE)ACR_IOH(mutex);
+
+    if (IS_INVALID_HANDLE(m) || ACR_IOH_TYPE(mutex) != ACR_DT_MUTEX) {
+        return ACR_EINVAL;
+    }
     return ACR_ENOTIMPL;
 }
 

Added: commons/sandbox/runtime/trunk/src/main/native/os/win32/psema.c
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/psema.c?rev=805289&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/psema.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/psema.c Tue Aug 18 
06:40:41 2009
@@ -0,0 +1,192 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "acr.h"
+#include "acr_private.h"
+#include "acr_arch.h"
+#include "acr_error.h"
+#include "acr_memory.h"
+#include "acr_string.h"
+#include "acr_descriptor.h"
+#include "acr_semaphore.h"
+
+static volatile unsigned int _sem_counter = 1;
+
+static int semaphore_cleanup(void *sema, int type, unsigned int flags)
+{
+    if (type != ACR_DT_SEMAPHORE) {
+        if (!IS_INVALID_HANDLE(sema))
+            CloseHandle(sema);
+        return ACR_SUCCESS;
+    }
+    return ACR_EBADF;
+}
+
+ACR_DECLARE(int) ACR_SemaphoreCreate(JNIEnv *_E, const acr_pchar_t *name,
+                                     int value)
+{
+    HANDLE s;
+    SECURITY_ATTRIBUTES sa;
+    wchar_t *reskey;
+    wchar_t  keybuf[128];
+
+    if (name == NULL) {
+        reskey = NULL;
+    }
+    /* Name-based shared memory */
+    else {
+        /* res_name_from_filename turns file into a pseudo-name
+         * without slashes or backslashes, and prepends the \global
+         * prefix on Win2K and later
+         */
+        reskey = res_name_from_filenamew(ACR_DT_MUTEX, keybuf, name);
+    }
+    /* Mark the semaphore as non inheritable.
+     */
+    sa.nLength = sizeof(SECURITY_ATTRIBUTES);
+    sa.lpSecurityDescriptor = NULL;
+    sa.bInheritHandle = FALSE;
+
+    s = CreateSemaphoreW(&sa, 1, 32767, reskey);
+    if (!s)
+        return -1;
+
+    rc = acr_ioh_open(s, ACR_DT_SEMAPHORE, 0, semaphore_cleanup);
+    return rc;
+}
+
+ACR_DECLARE(int) ACR_SemaphoreAttach(JNIEnv *_E, const acr_pchar_t *name)
+{
+    HANDLE s;
+    wchar_t *reskey;
+    wchar_t  keybuf[128];
+
+    if (name == NULL) {
+        /* We cannot attach to unnamed semaphore */
+        return ACR_EINVAL;
+    }
+    /* Name-based shared memory */
+    else {
+        /* res_name_from_filename turns file into a pseudo-name
+         * without slashes or backslashes, and prepends the \global
+         * prefix on Win2K and later
+         */
+        reskey = res_name_from_filenamew(ACR_DT_MUTEX, keybuf, name);
+    }
+
+    s = OpenSemaphoreW(READ_CONTROL | SEMAPHORE_MODIFY_STATE, FALSE, reskey);
+    if (!s)
+        return -1;
+
+    rc = acr_ioh_open(s, ACR_DT_SEMAPHORE, 0, semaphore_cleanup);
+    return rc;
+}
+
+ACR_DECLARE(int) ACR_SemaphoreClose(JNIEnv *_E, int sema)
+{
+
+    /* Close will call the cleanup function
+     */
+    return acr_ioh_close(sema);
+}
+
+ACR_DECLARE(int) ACR_SemaphorePermSet(JNIEnv *_E, int sema, int perms,
+                                      acr_uid_t uid, acr_uid_t gid)
+{
+    int rc = 0;
+    HANDLE s = (HANDLE)ACR_IOH(sema);
+
+    if (IS_INVALID_HANDLE(s) || ACR_IOH_TYPE(sema) != ACR_DT_SEMAPHORE) {
+        return ACR_EINVAL;
+    }
+    return ACR_ENOTIMPL;
+}
+
+ACR_DECLARE(int) ACR_SemaphoreWait(JNIEnv *_E, int sema)
+{
+    int rc;
+    DWORD  ws;
+    HANDLE wh[2];
+    HANDLE s = (HANDLE)ACR_IOH(sema);
+
+    if (ACR_IOH_TYPE(sema) != ACR_DT_SEMAPHORE || IS_INVALID_HANDLE(s)) {
+        return ACR_EINVAL;
+    }
+
+    wh[0] = s;
+    wh[1] = dll_psig_handle;
+    do {
+        rc = 0;
+        ws = WaitForMultipleObjectsEx(2, wh, FALSE, INFINITE, TRUE);
+
+        if (ws == WAIT_OBJECT_0 || ws == WAIT_ABANDONED) {
+            /* We got the lock */
+            return 0;
+        }
+        else if (ws == WAIT_OBJECT_0 + 1) {
+            /* Signal event is set
+             * TODO: Deliver a signal 
+             */
+            rc = ACR_EINTR;
+        }
+        else if (ws == WAIT_IO_COMPLETION) {
+            /* APC queued to this thread
+             * TODO: Deliver a signal 
+             */
+            rc = ACR_TIMEUP;
+        }
+        else
+            rc = ACR_GET_OS_ERROR();
+    } while (rc == ACR_EINTR);
+
+    return rc;
+}
+
+ACR_DECLARE(int) ACR_SemaphoreTryWait(JNIEnv *_E, int sema)
+{
+    int rc;
+    HANDLE s = (HANDLE)ACR_IOH(sema);
+
+    if (IS_INVALID_HANDLE(s) || ACR_IOH_TYPE(sema) != ACR_DT_SEMAPHORE) {
+        return ACR_EINVAL;
+    }
+    rc = WaitForSingleObject(s, 0);
+
+    if (rc == WAIT_OBJECT_0 || rc == WAIT_ABANDONED) {
+        return 0;
+    }
+    else if (rc == WAIT_TIMEOUT) {
+        return ACR_EBUSY;
+    }
+    return ACR_GET_OS_ERROR();
+}
+
+ACR_DECLARE(int) ACR_SemaphoreRelease(JNIEnv *env, int sema)
+{
+    int rc = 0;
+    HANDLE s = (HANDLE)ACR_IOH(sema);
+
+    if (IS_INVALID_HANDLE(s) || ACR_IOH_TYPE(sema) != ACR_DT_SEMAPHORE) {
+        return ACR_EINVAL;
+    }
+
+    if (!ReleaseSemaphore(s, 1, NULL))
+        return ACR_GET_OS_ERROR();
+    else
+        return ACR_SUCCESS;
+
+}
+

Propchange: commons/sandbox/runtime/trunk/src/main/native/os/win32/psema.c
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to