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 2011/04/15 19:43:58 UTC

svn commit: r1092775 - in /commons/sandbox/runtime/trunk/src/main/native/os: unix/arch_opts.h unix/posixapi.c unix/procmutex.c unix/semaphore.c win32/arch_opts.h

Author: mturk
Date: Fri Apr 15 17:43:58 2011
New Revision: 1092775

URL: http://svn.apache.org/viewvc?rev=1092775&view=rev
Log:
Implement native mutex methods

Added:
    commons/sandbox/runtime/trunk/src/main/native/os/unix/arch_opts.h   (with props)
Modified:
    commons/sandbox/runtime/trunk/src/main/native/os/unix/posixapi.c
    commons/sandbox/runtime/trunk/src/main/native/os/unix/procmutex.c
    commons/sandbox/runtime/trunk/src/main/native/os/unix/semaphore.c
    commons/sandbox/runtime/trunk/src/main/native/os/win32/arch_opts.h

Added: commons/sandbox/runtime/trunk/src/main/native/os/unix/arch_opts.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/arch_opts.h?rev=1092775&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/arch_opts.h (added)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/arch_opts.h Fri Apr 15 17:43:58 2011
@@ -0,0 +1,87 @@
+/* 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.
+ */
+
+#ifndef _ACR_ARCH_OPTS_H_
+#define _ACR_ARCH_OPTS_H_
+
+#include "acr/stddefs.h"
+
+#define THROW_STD_ERRORS()              \
+    if (errno == EEXIST)                \
+        ACR_THROW(ACR_EX_EEXIST, 0);    \
+    else if (errno == ENOENT)           \
+        ACR_THROW(ACR_EX_ENOENT, 0);    \
+    else if (errno == EACCES)           \
+        ACR_THROW(ACR_EX_EACCES, 0);    \
+    else                                \
+        ACR_THROW_SYS_ERROR();      
+
+/* restartable close() */
+ACR_INLINE(int) r_close(int fd)
+{
+    int rc;
+    do {
+        rc = close(fd);
+    } while (rc == -1 && errno == EINTR);
+
+    return rc;
+}
+
+/* safe close() */
+ACR_INLINE(int) s_close(int fd)
+{
+    if (fd == -1) {
+        errno = EBADF;
+        return -1;
+    }
+    else
+        return r_close(fd);
+}
+
+/* restartable dup2() */
+ACR_INLINE(int) r_dup2(int oldfd, int newfd)
+{
+    int rc;
+    do {
+        rc = dup2(oldfd, newfd);
+    } while (rc == -1 && errno == EINTR);
+
+    return rc;
+}
+
+/* restartable read() */
+ACR_INLINE(ssize_t) r_read(int fd, void *buf, size_t count)
+{
+    ssize_t r;
+    do {
+        r = read(fd, buf, count);
+    } while (r == -1 && errno == EINTR);
+
+    return r;
+}
+
+/* restartable write() */
+ACR_INLINE(ssize_t) r_write(int fd, const void *buf, size_t count)
+{
+    ssize_t w;
+    do {
+        w = write(fd, buf, count);
+    } while (w == -1 && errno == EINTR);
+
+    return w;
+}
+
+#endif /* _ACR_ARCH_OPTS_H_ */

Propchange: commons/sandbox/runtime/trunk/src/main/native/os/unix/arch_opts.h
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/posixapi.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/posixapi.c?rev=1092775&r1=1092774&r2=1092775&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/posixapi.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/posixapi.c Fri Apr 15 17:43:58 2011
@@ -16,17 +16,14 @@
 
 #include "acr/string.h"
 #include "acr/port.h"
+#include "arch_opts.h"
 
 ACR_JNI_EXPORT(jint, Posix, close)(JNI_STDARGS, jint fd)
 {
-    int rc;
-    do {
-        rc = close(fd);
-    } while (rc == -1 && errno == EINTR);
-    if (rc == -1)
-        return errno;
-    else
+    if (r_close(fd) == 0)
         return 0;
+    else
+        return errno;
 }
 
 ACR_JNI_EXPORT(jint, Posix, unlink)(JNI_STDARGS, jstring name)
@@ -87,26 +84,26 @@ ACR_JNI_EXPORT(jint, Posix, chdir)(JNI_S
 
 ACR_JNI_EXPORT(jint, Posix, fchdir)(JNI_STDARGS, jint fd)
 {
-    if (fchdir(fd) == -1)
-        return errno;
-    else
+    if (fchdir(fd) == 0)
         return 0;
+    else
+        return errno;
 }
 
 ACR_JNI_EXPORT(jint, Posix, fchown)(JNI_STDARGS, jint fd, jint uid, jint gid)
 {
-    if (fchown(fd, (uid_t)uid, (gid_t)gid) == -1)
-        return errno;
-    else
+    if (fchown(fd, (uid_t)uid, (gid_t)gid) == 0)
         return 0;
+    else
+        return errno;
 }
 
 ACR_JNI_EXPORT(jint, Posix, fchmod)(JNI_STDARGS, jint fd, jint mode)
 {
-    if (fchmod(fd, (mode_t)mode) == -1)
-        return errno;
-    else
+    if (fchmod(fd, (mode_t)mode) == 0)
         return 0;
+    else
+        return errno;
 }
 
 ACR_JNI_EXPORT(jstring, Posix, getcwd)(JNI_STDARGS)

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/procmutex.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/procmutex.c?rev=1092775&r1=1092774&r2=1092775&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/procmutex.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/procmutex.c Fri Apr 15 17:43:58 2011
@@ -17,6 +17,7 @@
 #include "acr/string.h"
 #include "acr/clazz.h"
 #include "acr/port.h"
+#include "arch_opts.h"
 
 #include <sys/ipc.h>
 #include <sys/sem.h>
@@ -60,6 +61,8 @@
 # error "Mutex type not configured"
 #endif
 
+#define MUTEX_MAGIC     0x23036401
+
 #if !HAVE_UNION_SEMUN
 union semun {
     int val;
@@ -79,3 +82,263 @@ ACR_UNX_EXPORT(jint, PosixMutexImpl, dt0
     return _DEFAULT_MUTEX_TYPE;
 }
 
+ACR_UNX_EXPORT(jint, SysVMutex, create0)(JNI_STDARGS, jstring name)
+{
+    union semun ick;
+    int pd = -1;
+    key_t mkey;
+    int flags  = IPC_CREAT;
+
+    WITH_CSTR(name) {
+        size_t     nbytes;
+        semblock_t hdr;
+        int fd = -1;
+
+        fd = open(J2S(name), O_WRONLY | O_CREAT | O_EXCL, 0660);
+        if (fd == -1) {
+            THROW_STD_ERRORS();
+            goto cleanup;
+        }
+        mkey = ftok(J2S(name), 'a');
+        if (mkey == (key_t)-1) {
+            ACR_THROW_SYS_ERROR();                
+            goto cleanup;
+        }
+        /* Write our header to shadow file
+         * Not needed, but might be used in the
+         * future to pass some data along with the mutex
+         */
+        nbytes = sizeof(semblock_t);
+        hdr.creator = getpid();
+        hdr.magic   = MUTEX_MAGIC;
+        hdr.value   = 1;
+        if (r_write(fd,(const void *)&hdr, nbytes) == -1) {
+            ACR_THROW_SYS_ERROR();                
+            goto cleanup;
+        }
+        flags |= IPC_EXCL;
+        pd = semget(mkey, 1, flags | 0660);
+        if (pd == -1) {
+            THROW_STD_ERRORS();
+            goto cleanup;
+        }            
+
+cleanup:
+        if (fd != -1)
+            close(fd);
+        if (pd != -1) {
+            ick.val = 1;
+            if (semctl(pd, 0, SETVAL, ick) == -1) {
+                ACR_THROW_SYS_ERROR();
+                ick.val = 0;
+                semctl(pd, 0, IPC_RMID, ick);
+                unlink(J2S(name));
+                pd = -1;
+            }
+        }
+    } DONE_WITH_STR(name);    
+    
+    return pd;
+}
+
+ACR_UNX_EXPORT(jint, SysVMutex, open0)(JNI_STDARGS, jstring name)
+{
+    int pd = -1;
+    key_t mkey;
+
+    WITH_CSTR(name) {
+        int fd = -1;
+
+        fd = open(J2S(name), O_RDONLY);
+        if (fd == -1) {
+            THROW_STD_ERRORS();
+            goto cleanup;
+        }
+        mkey = ftok(J2S(name), 'a');
+        if (mkey == (key_t)-1) {
+            ACR_THROW_SYS_ERROR();                
+            close(fd);
+            goto cleanup;
+        }
+        pd = semget(mkey, 1, 0);
+        if (pd == -1) {
+            THROW_STD_ERRORS();
+            goto cleanup;
+        }            
+
+cleanup:
+        if (fd != -1)
+            close(fd);
+    } DONE_WITH_STR(name);    
+    
+    return pd;
+}
+
+ACR_UNX_EXPORT(jint, SysVMutex, unlink0)(JNI_STDARGS, jint fd, jstring name)
+{
+    int rc = 0;
+
+    WITH_CSTR(name) {
+        union semun ick;
+
+        ick.val = 0;
+        if (semctl(fd, 0, IPC_RMID, ick) == -1)
+            rc = ACR_GET_OS_ERROR();
+        if (unlink(J2S(name)) == -1 && rc == 0)
+            rc = ACR_GET_OS_ERROR();
+    } DONE_WITH_STR(name);    
+    
+    return rc;
+}
+
+ACR_UNX_EXPORT(jint, SysVMutex, wait0)(JNI_STDARGS, jint fd)
+{
+    int rc;
+    struct sembuf op;
+
+    op.sem_num = 0;
+    op.sem_op  = -1;
+    op.sem_flg = SEM_UNDO;
+    do {
+        rc = semop(fd, &op, 1);
+    } while (rc == -1 && errno == EINTR);
+
+    if (rc == -1)
+        return ACR_GET_OS_ERROR();
+    else
+        return 0;
+}
+
+ACR_UNX_EXPORT(jint, SysVMutex, try0)(JNI_STDARGS, jint fd)
+{
+    int rc;
+    struct sembuf op;
+
+    op.sem_num = 0;
+    op.sem_op  = -1;
+    op.sem_flg = SEM_UNDO | IPC_NOWAIT;
+    do {
+        rc = semop(fd, &op, 1);
+    } while (rc == -1 && errno == EINTR);
+
+    if (rc == -1) {
+        if (errno == EAGAIN)
+            return ACR_EBUSY;
+        else
+            return ACR_GET_OS_ERROR();
+    }
+    else
+        return 0;
+}
+
+ACR_UNX_EXPORT(jint, SysVMutex, release0)(JNI_STDARGS, jint fd)
+{
+    int rc;
+    struct sembuf op;
+
+    op.sem_num = 0;
+    op.sem_op  = 1;
+    op.sem_flg = SEM_UNDO;
+    do {
+        rc = semop(fd, &op, 1);
+    } while (rc == -1 && errno == EINTR);
+
+    if (rc == -1)
+        return ACR_GET_OS_ERROR();
+    else
+        return 0;
+}
+
+ACR_UNX_EXPORT(jint, FcntlMutex, create0)(JNI_STDARGS, jstring name)
+{
+    int fd = -1;
+
+    WITH_CSTR(name) {
+        fd = open(J2S(name), O_WRONLY | O_CREAT | O_EXCL, 0660);
+        if (fd == -1) {
+            THROW_STD_ERRORS();
+        }
+    } DONE_WITH_STR(name);    
+    
+    return fd;
+}
+
+ACR_UNX_EXPORT(jint, FcntlMutex, open0)(JNI_STDARGS, jstring name)
+{
+    int fd = -1;
+
+    WITH_CSTR(name) {
+        fd = open(J2S(name), O_RDONLY);
+        if (fd == -1) {
+            THROW_STD_ERRORS();
+        }
+    } DONE_WITH_STR(name);    
+    
+    return fd;
+}
+
+ACR_UNX_EXPORT(jint, FcntlMutex, wait0)(JNI_STDARGS, jint fd)
+{
+    int rc;
+    struct flock op;
+
+    op.l_whence = SEEK_SET;   /* from current point */
+    op.l_start  = 0;          /* -"- */
+    op.l_len    = 0;          /* until end of file */
+    op.l_type   = F_WRLCK;    /* set exclusive/write lock */
+    op.l_pid    = 0;          /* pid not actually interesting */
+
+    do {
+        rc = fcntl(fd, F_SETLKW, &op);
+    } while (rc == -1 && errno == EINTR);
+
+    if (rc == -1)
+        return ACR_GET_OS_ERROR();
+    else
+        return 0;
+}
+
+ACR_UNX_EXPORT(jint, FcntlMutex, try0)(JNI_STDARGS, jint fd)
+{
+    int rc;
+    struct flock op;
+
+    op.l_whence = SEEK_SET;   /* from current point */
+    op.l_start  = 0;          /* -"- */
+    op.l_len    = 0;          /* until end of file */
+    op.l_type   = F_WRLCK;    /* set exclusive/write lock */
+    op.l_pid    = 0;          /* pid not actually interesting */
+
+    do {
+        rc = fcntl(fd, F_SETLK, &op);
+    } while (rc == -1 && errno == EINTR);
+
+    if (rc == -1) {
+        if (errno == EAGAIN)
+            return ACR_EBUSY;
+        else
+            return ACR_GET_OS_ERROR();
+    }
+    else
+        return 0;
+}
+
+ACR_UNX_EXPORT(jint, FcntlMutex, release0)(JNI_STDARGS, jint fd)
+{
+    int rc;
+    struct flock op;
+
+    op.l_whence = SEEK_SET;   /* from current point */
+    op.l_start  = 0;          /* -"- */
+    op.l_len    = 0;          /* until end of file */
+    op.l_type   = F_UNLCK;    /* unlock */
+    op.l_pid    = 0;          /* pid not actually interesting */
+    do {
+        rc = fcntl(fd, F_SETLKW, &op);
+    } while (rc == -1 && errno == EINTR);
+
+    if (rc == -1)
+        return ACR_GET_OS_ERROR();
+    else
+        return 0;
+}

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/semaphore.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/semaphore.c?rev=1092775&r1=1092774&r2=1092775&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/semaphore.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/semaphore.c Fri Apr 15 17:43:58 2011
@@ -17,7 +17,7 @@
 #include "acr/string.h"
 #include "acr/clazz.h"
 #include "acr/port.h"
-#include "arch_defs.h"
+#include "arch_opts.h"
 
 #if HAVE_POSIX_SEMAPHORE
 #if HAVE_SEMAPHORE_H
@@ -97,18 +97,10 @@ ACR_UNX_EXPORT(jlong, PosixSemaphore, cr
     WITH_CSTR(name) {
         sp = sem_open(J2S(name), O_CREAT | O_EXCL, 0660, value);       
         if (sp == SEM_FAILED) {
-            if (errno == ENAMETOOLONG) {
+            if (errno == ENAMETOOLONG)
                 ACR_THROW(ACR_EX_EINVAL, 0);
-            }
-            else if (errno == EEXIST) {
-                ACR_THROW(ACR_EX_EEXIST, 0);
-            }
-            else if (errno == EACCES) {
-                ACR_THROW(ACR_EX_EACCES, 0);
-            }
-            else {
-                ACR_THROW_SYS_ERROR();                
-            }
+            else
+                THROW_STD_ERRORS();
         }
     } DONE_WITH_STR(name);    
     
@@ -123,18 +115,10 @@ ACR_UNX_EXPORT(jlong, PosixSemaphore, op
     WITH_CSTR(name) {
         sp = sem_open(J2S(name), O_RDWR, 0, 0);       
         if (sp == SEM_FAILED) {
-            if (errno == ENAMETOOLONG) {
+            if (errno == ENAMETOOLONG)
                 ACR_THROW(ACR_EX_EINVAL, 0);
-            }
-            else if (errno == ENOENT) {
-                ACR_THROW(ACR_EX_ENOENT, 0);
-            }
-            else if (errno == EACCES) {
-                ACR_THROW(ACR_EX_EACCES, 0);
-            }
-            else {
-                ACR_THROW_SYS_ERROR();                
-            }
+            else
+                THROW_STD_ERRORS();
         }
     } DONE_WITH_STR(name);    
     

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/arch_opts.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/arch_opts.h?rev=1092775&r1=1092774&r2=1092775&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/arch_opts.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/arch_opts.h Fri Apr 15 17:43:58 2011
@@ -119,7 +119,7 @@ ACR_DECLARE_LATE_DLL_FUNC1(SYSDLL_KERNEL
 extern HANDLE acr_raised_event;
 extern volatile LONG acr_signal_waiters;
 
-static __inline DWORD AcrWaitForObjectOrSignal(HANDLE handle, DWORD dwTimeout)
+ACR_INLINE(DWORD) AcrWaitForObjectOrSignal(HANDLE handle, DWORD dwTimeout)
 {
     DWORD  rc;
     HANDLE wh[2];
@@ -139,9 +139,9 @@ static __inline DWORD AcrWaitForObjectOr
         return rc;
 }
 
-static __inline DWORD AcrWaitForMultipleObjectsOrSignal(DWORD nCount,
-                                                        const HANDLE *handles,
-                                                        DWORD dwTimeout)
+ACR_INLINE(DWORD) AcrWaitForMultipleObjectsOrSignal(DWORD nCount,
+                                                    const HANDLE *handles,
+                                                    DWORD dwTimeout)
 {
     DWORD  rc;
     DWORD  wn;
@@ -166,7 +166,7 @@ static __inline DWORD AcrWaitForMultiple
         return rc;
 }
 
-static __inline acr_time_t FileTimeToUsecTime(LPFILETIME input)
+ACR_INLINE(acr_time_t) FileTimeToUsecTime(LPFILETIME input)
 {
     acr_time_t result;
     /* Convert FILETIME one 64 bit number so we can work with it. */
@@ -178,7 +178,7 @@ static __inline acr_time_t FileTimeToUse
     return result;
 }
 
-static __inline acr_time_t FileTimeToMsecTime(LPFILETIME input)
+ACR_INLINE(acr_time_t) FileTimeToMsecTime(LPFILETIME input)
 {
     acr_time_t result;
     /* Convert FILETIME one 64 bit number so we can work with it. */
@@ -191,13 +191,13 @@ static __inline acr_time_t FileTimeToMse
 }
 
 
-static __inline void UsecTimeToFileTime(LPFILETIME result, acr_time_t t)
+ACR_INLINE(void) UsecTimeToFileTime(LPFILETIME result, acr_time_t t)
 {
     ((LARGE_INTEGER *)result)->QuadPart = (t + ACR_DELTA_EPOCH_IN_USEC) * 10;
     return;
 }
 
-static __inline void MsecTimeToFileTime(LPFILETIME result, acr_time_t t)
+ACR_INLINE(void) MsecTimeToFileTime(LPFILETIME result, acr_time_t t)
 {
     ((LARGE_INTEGER *)result)->QuadPart = (t + ACR_DELTA_EPOCH_IN_USEC) * 10000;
     return;