commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r819266 - in /commons/sandbox/runtime/trunk/src: main/native/os/darwin/ main/native/os/unix/ test/org/apache/commons/runtime/
Date Sun, 27 Sep 2009 07:28:21 GMT
Author: mturk
Date: Sun Sep 27 07:28:20 2009
New Revision: 819266

URL: http://svn.apache.org/viewvc?rev=819266&view=rev
Log:
Few fixes and test suite tuning

Modified:
    commons/sandbox/runtime/trunk/src/main/native/os/darwin/mutex.c
    commons/sandbox/runtime/trunk/src/main/native/os/unix/mutex.c
    commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestParallel.java
    commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestPrivate.java
    commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestSignal.java

Modified: commons/sandbox/runtime/trunk/src/main/native/os/darwin/mutex.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/darwin/mutex.c?rev=819266&r1=819265&r2=819266&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/darwin/mutex.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/darwin/mutex.c Sun Sep 27 07:28:20 2009
@@ -14,11 +14,7 @@
  * limitations under the License.
  */
 
-/* File lock based mutex implementation.
- * XXX: Darwin uses posix semaphores so they could
- *      probably be used for mutexes as well.
- *      Of course they don't have a descent API for
- *      setting the permissions afterwards.
+/* System V mutex implementation.
  */
 
 #include "acr.h"
@@ -61,7 +57,245 @@
     acr_uint32_t    value;       /* Maximum semaphore value */
 } semblock_t;
 
-#if defined(ACR_USE_SYSV_MUTEX)
+#if defined(ACR_USE_FCNTL_MUTEX)
+
+static int mutex_cleanup(void *mutex, int type, unsigned int flags)
+{
+    if (type == ACR_DT_MUTEX) {
+        int rc = ACR_EBADF;
+        acr_pmutex_t *m = (acr_pmutex_t *)mutex;
+        if (m->filedes > 0) {
+            if (m->locked) {
+                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(m->filedes, F_SETLKW, &op);
+                } while (rc < 0 && errno == EINTR);
+
+            }
+            close(m->filedes);
+            rc = ACR_SUCCESS;
+        }
+        if (m->filename) {
+            if (access(m->filename, F_OK) == 0) {
+                /* File exists. Remove it */
+                if (unlink(m->filename))
+                    rc = ACR_GET_OS_ERROR();
+            }
+            x_free((void *)(m->filename));
+        }
+        x_free(m);
+        return rc;
+    }
+    return ACR_EFTYPE;
+}
+
+ACR_DECLARE(int) ACR_ProcMutexCreate(JNIEnv *_E, const acr_pchar_t *fname)
+{
+    int rc = 0;
+    int anon = 0;
+    acr_pmutex_t *m;
+
+    m = ACR_Calloc(_E, THROW_FMARK, sizeof(acr_pmutex_t));
+    if (!m)
+        return -1;
+    if (!fname) {
+        anon  = 1;
+        fname = "/tmp/apmXXXXXX";
+    }
+    m->filename = ACR_StrdupA(_E, THROW_FMARK, fname);
+    if (!m->filename) {
+        rc =  ACR_GET_OS_ERROR();
+        free(m);
+        ACR_SET_OS_ERROR(rc);
+        return -1;
+    }
+    if (anon)
+        m->filedes = mkstemp((char *)m->filename);
+    else
+        m->filedes = open(m->filename, O_CREAT | O_WRONLY | O_EXCL, 0660);
+
+    if (m->filedes < 0) {
+        rc =  ACR_GET_OS_ERROR();
+        goto finally;
+    }
+    m->locked = 0;
+
+finally:
+    if (rc) {
+        x_free((void *)(m->filename));
+        x_free(m);
+        ACR_THROW_IO_IF_ERR(rc);
+        return -1;
+    }
+    else {
+        rc = acr_ioh_open(m, ACR_DT_MUTEX, 0, mutex_cleanup);
+        return rc;
+    }
+}
+
+ACR_DECLARE(int) ACR_ProcMutexAttach(JNIEnv *_E, const acr_pchar_t *fname)
+{
+    int rc = 0;
+    acr_pmutex_t *m;
+
+    if (!fname) {
+        /* Cannot attach to unnamed mutex */
+        ACR_THROW_IO_IF_ERR(ACR_EINVAL);
+        return -1;
+    }
+    m = ACR_Calloc(_E, THROW_FMARK, sizeof(acr_pmutex_t));
+    if (!m)
+        return -1;
+    m->filedes = open(fname, O_WRONLY);
+    if (m->filedes < 0) {
+        rc =  ACR_GET_OS_ERROR();
+        goto finally;
+    }
+    m->locked = 0;
+
+finally:
+    if (rc) {
+        x_free((void *)(m->filename));
+        x_free(m);
+        ACR_THROW_IO_IF_ERR(rc);
+        return -1;
+    }
+    else {
+        rc = acr_ioh_open(m, ACR_DT_MUTEX, 1, mutex_cleanup);
+        return rc;
+    }
+    return -1;
+}
+
+ACR_DECLARE(int) ACR_ProcMutexLock(JNIEnv *_E, int mutex)
+{
+    int rc;
+    struct flock op;
+    acr_pmutex_t *m = (acr_pmutex_t *)ACR_IOH_FDATA(mutex);
+
+    if (ACR_IOH_FTYPE(mutex) != ACR_DT_MUTEX) {
+        return ACR_EFTYPE;
+    }
+    if (IS_INVALID_HANDLE(m)) {
+        return ACR_EBADF;
+    }
+    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(m->filedes, F_SETLKW, &op);
+    } while (rc < 0 && errno == EINTR);
+
+    if (rc < 0)
+        return ACR_GET_OS_ERROR();
+    m->locked = 1;
+
+    return ACR_SUCCESS;
+}
+
+ACR_DECLARE(int) ACR_ProcMutexTryLock(JNIEnv *_E, int mutex)
+{
+    int rc;
+    struct flock op;
+    acr_pmutex_t *m = (acr_pmutex_t *)ACR_IOH_FDATA(mutex);
+
+    if (ACR_IOH_FTYPE(mutex) != ACR_DT_MUTEX) {
+        return ACR_EFTYPE;
+    }
+    if (IS_INVALID_HANDLE(m)) {
+        return ACR_EBADF;
+    }
+    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(m->filedes, F_SETLK, &op);
+    } while (rc < 0 && errno == EINTR);
+
+    if (rc < 0) {
+        if (errno == EAGAIN) {
+            return ACR_EBUSY;
+        }
+    }
+    else
+        m->locked = 1;
+
+    return ACR_SUCCESS;;
+}
+
+ACR_DECLARE(int) ACR_ProcMutexRelease(JNIEnv *_E, int mutex)
+{
+    int rc;
+    struct flock op;
+    acr_pmutex_t *m = (acr_pmutex_t *)ACR_IOH_FDATA(mutex);
+
+    if (ACR_IOH_FTYPE(mutex) != ACR_DT_MUTEX) {
+        return ACR_EFTYPE;
+    }
+    if (IS_INVALID_HANDLE(m)) {
+        return ACR_EBADF;
+    }
+    m->locked   = 0;
+    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(m->filedes, F_SETLKW, &op);
+    } while (rc < 0 && errno == EINTR);
+
+    if (rc < 0)
+        return ACR_GET_OS_ERROR();
+    else
+        return ACR_SUCCESS;
+}
+
+ACR_DECLARE(int) ACR_ProcMutexPermSet(JNIEnv *_E, int mutex, int perms,
+                                      acr_uid_t uid, acr_uid_t gid)
+{
+    acr_pmutex_t *m = (acr_pmutex_t *)ACR_IOH_FDATA(mutex);
+
+    if (ACR_IOH_FTYPE(mutex) != ACR_DT_MUTEX) {
+        return ACR_EFTYPE;
+    }
+    if (IS_INVALID_HANDLE(m)) {
+        return ACR_EBADF;
+    }
+    if (!(perms & ACR_FPROT_GSETID))
+        gid = -1;
+    if (fchown(m->filedes, uid, gid) < 0) {
+        return ACR_GET_OS_ERROR();
+    }
+    else
+        return ACR_SUCCESS;
+}
+
+ACR_DECLARE(int) ACR_ProcMutexRemove(JNIEnv *_E, const acr_pchar_t *filename)
+{
+
+    if (unlink(filename)) {
+        ACR_THROW_IO_ERRNO();
+        return ACR_GET_OS_ERROR();
+    }
+    else
+        return ACR_SUCCESS;
+}
+
+#else   /* ACR_USE_FCNTL_MUTEX */
 
 static int mutex_owner_cleanup(void *mutex, int type, unsigned int flags)
 {
@@ -419,244 +653,6 @@
     return rc;
 }
 
-#else   /* ACR_USE_SYSV_MUTEX */
-
-static int mutex_cleanup(void *mutex, int type, unsigned int flags)
-{
-    if (type == ACR_DT_MUTEX) {
-        int rc = ACR_EBADF;
-        acr_pmutex_t *m = (acr_pmutex_t *)mutex;
-        if (m->filedes > 0) {
-            if (m->locked) {
-                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(m->filedes, F_SETLKW, &op);
-                } while (rc < 0 && errno == EINTR);
-
-            }
-            close(m->filedes);
-            rc = ACR_SUCCESS;
-        }
-        if (m->filename) {
-            if (access(m->filename, F_OK) == 0) {
-                /* File exists. Remove it */
-                if (unlink(m->filename))
-                    rc = ACR_GET_OS_ERROR();
-            }
-            x_free((void *)(m->filename));
-        }
-        x_free(m);
-        return rc;
-    }
-    return ACR_EFTYPE;
-}
-
-ACR_DECLARE(int) ACR_ProcMutexCreate(JNIEnv *_E, const acr_pchar_t *fname)
-{
-    int rc = 0;
-    int anon = 0;
-    acr_pmutex_t *m;
-
-    m = ACR_Calloc(_E, THROW_FMARK, sizeof(acr_pmutex_t));
-    if (!m)
-        return -1;
-    if (!fname) {
-        anon  = 1;
-        fname = "/tmp/apmXXXXXX";
-    }
-    m->filename = ACR_StrdupA(_E, THROW_FMARK, fname);
-    if (!m->filename) {
-        rc =  ACR_GET_OS_ERROR();
-        free(m);
-        ACR_SET_OS_ERROR(rc);
-        return -1;
-    }
-    if (anon)
-        m->filedes = mkstemp((char *)m->filename);
-    else
-        m->filedes = open(m->filename, O_CREAT | O_WRONLY | O_EXCL, 0660);
-
-    if (m->filedes < 0) {
-        rc =  ACR_GET_OS_ERROR();
-        goto finally;
-    }
-    m->locked = 0;
-
-finally:
-    if (rc) {
-        x_free((void *)(m->filename));
-        x_free(m);
-        ACR_THROW_IO_IF_ERR(rc);
-        return -1;
-    }
-    else {
-        rc = acr_ioh_open(m, ACR_DT_MUTEX, 0, mutex_cleanup);
-        return rc;
-    }
-}
-
-ACR_DECLARE(int) ACR_ProcMutexAttach(JNIEnv *_E, const acr_pchar_t *fname)
-{
-    int rc = 0;
-    acr_pmutex_t *m;
-
-    if (!fname) {
-        /* Cannot attach to unnamed mutex */
-        ACR_THROW_IO_IF_ERR(ACR_EINVAL);
-        return -1;
-    }
-    m = ACR_Calloc(_E, THROW_FMARK, sizeof(acr_pmutex_t));
-    if (!m)
-        return -1;
-    m->filedes = open(fname, O_WRONLY);
-    if (m->filedes < 0) {
-        rc =  ACR_GET_OS_ERROR();
-        goto finally;
-    }
-    m->locked = 0;
-
-finally:
-    if (rc) {
-        x_free((void *)(m->filename));
-        x_free(m);
-        ACR_THROW_IO_IF_ERR(rc);
-        return -1;
-    }
-    else {
-        rc = acr_ioh_open(m, ACR_DT_MUTEX, 1, mutex_cleanup);
-        return rc;
-    }
-    return -1;
-}
-
-ACR_DECLARE(int) ACR_ProcMutexLock(JNIEnv *_E, int mutex)
-{
-    int rc;
-    struct flock op;
-    acr_pmutex_t *m = (acr_pmutex_t *)ACR_IOH_FDATA(mutex);
-
-    if (ACR_IOH_FTYPE(mutex) != ACR_DT_MUTEX) {
-        return ACR_EFTYPE;
-    }
-    if (IS_INVALID_HANDLE(m)) {
-        return ACR_EBADF;
-    }
-    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(m->filedes, F_SETLKW, &op);
-    } while (rc < 0 && errno == EINTR);
-
-    if (rc < 0)
-        return ACR_GET_OS_ERROR();
-    m->locked = 1;
-
-    return ACR_SUCCESS;
-}
-
-ACR_DECLARE(int) ACR_ProcMutexTryLock(JNIEnv *_E, int mutex)
-{
-    int rc;
-    struct flock op;
-    acr_pmutex_t *m = (acr_pmutex_t *)ACR_IOH_FDATA(mutex);
-
-    if (ACR_IOH_FTYPE(mutex) != ACR_DT_MUTEX) {
-        return ACR_EFTYPE;
-    }
-    if (IS_INVALID_HANDLE(m)) {
-        return ACR_EBADF;
-    }
-    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(m->filedes, F_SETLK, &op);
-    } while (rc < 0 && errno == EINTR);
-
-    if (rc < 0) {
-        if (errno == EAGAIN) {
-            return ACR_EBUSY;
-        }
-    }
-    else
-        m->locked = 1;
-
-    return ACR_SUCCESS;;
-}
-
-ACR_DECLARE(int) ACR_ProcMutexRelease(JNIEnv *_E, int mutex)
-{
-    int rc;
-    struct flock op;
-    acr_pmutex_t *m = (acr_pmutex_t *)ACR_IOH_FDATA(mutex);
-
-    if (ACR_IOH_FTYPE(mutex) != ACR_DT_MUTEX) {
-        return ACR_EFTYPE;
-    }
-    if (IS_INVALID_HANDLE(m)) {
-        return ACR_EBADF;
-    }
-    m->locked   = 0;
-    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(m->filedes, F_SETLKW, &op);
-    } while (rc < 0 && errno == EINTR);
-
-    if (rc < 0)
-        return ACR_GET_OS_ERROR();
-    else
-        return ACR_SUCCESS;
-}
-
-ACR_DECLARE(int) ACR_ProcMutexPermSet(JNIEnv *_E, int mutex, int perms,
-                                      acr_uid_t uid, acr_uid_t gid)
-{
-    acr_pmutex_t *m = (acr_pmutex_t *)ACR_IOH_FDATA(mutex);
-
-    if (ACR_IOH_FTYPE(mutex) != ACR_DT_MUTEX) {
-        return ACR_EFTYPE;
-    }
-    if (IS_INVALID_HANDLE(m)) {
-        return ACR_EBADF;
-    }
-    if (!(perms & ACR_FPROT_GSETID))
-        gid = -1;
-    if (fchown(m->filedes, uid, gid) < 0) {
-        return ACR_GET_OS_ERROR();
-    }
-    else
-        return ACR_SUCCESS;
-}
-
-ACR_DECLARE(int) ACR_ProcMutexRemove(JNIEnv *_E, const acr_pchar_t *filename)
-{
-
-    if (unlink(filename)) {
-        ACR_THROW_IO_ERRNO();
-        return ACR_GET_OS_ERROR();
-    }
-    else
-        return ACR_SUCCESS;
-}
-
 #endif  /* ACR_USE_SYSV_MUTEX */
 
 ACR_DECLARE(int) ACR_ProcMutexClose(JNIEnv *_E, int mutex)

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/mutex.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/mutex.c?rev=819266&r1=819265&r2=819266&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/mutex.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/mutex.c Sun Sep 27 07:28:20 2009
@@ -15,7 +15,8 @@
  */
 
 /*
- * Posix semaphore implementation.
+ * System V semaphore implementation.
+ * define --enable-posix-mutex for Posix mutexes
  */
 
 #include "acr.h"

Modified: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestParallel.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestParallel.java?rev=819266&r1=819265&r2=819266&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestParallel.java (original)
+++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestParallel.java Sun
Sep 27 07:28:20 2009
@@ -69,6 +69,11 @@
         for (i = 0; i < workers.length; i++) {
             workers[i].start();
         }
+        try {
+            Thread.sleep(2000);
+        } catch (Throwable t) {
+            // Ignore
+        }
         // Wait for all threads to finish
         for (i = 0; i < workers.length; i++) {
             try {

Modified: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestPrivate.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestPrivate.java?rev=819266&r1=819265&r2=819266&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestPrivate.java (original)
+++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestPrivate.java Sun
Sep 27 07:28:20 2009
@@ -741,7 +741,7 @@
             }
             for (i = 0; i < 100; i++)
                 test053(fd[i]);
-            synchronized (IohParallelWorker.class) {
+            synchronized (this) {
                ((IohParallelWorker)o).numCalls++;
             }
             for (i = 0; i < 100; i++) {

Modified: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestSignal.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestSignal.java?rev=819266&r1=819265&r2=819266&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestSignal.java (original)
+++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestSignal.java Sun
Sep 27 07:28:20 2009
@@ -26,6 +26,7 @@
 public class TestSignal extends TestCase
 {
 
+    private static Object lock = new Object();
     private static volatile int counter = 0;
     public static Test suite() {
         TestSuite suite = new TestSuite(TestSignal.class);
@@ -47,7 +48,7 @@
         public void handler(Signal signo)
             throws Exception
         {
-            synchronized(TestSignal.class) {
+            synchronized(lock) {
                 counter++;
             }
             System.out.println("Received signal(" + signo + ") in " +
@@ -88,7 +89,7 @@
             System.out.println("Handler initialized in " + Thread.currentThread().getName());
             for (int i = 0; i < 2; i++) {
                 try {
-                    Thread.sleep(100);
+                    Thread.sleep((int)(System.currentTimeMillis() % 100));
                 } catch (Throwable e) {
                 // Ignore
                 }
@@ -105,9 +106,16 @@
         MySignalTester h2 = new MySignalTester(Signal.SIGHUP);
         h1.start();
         h2.start();
+        try {
+            Thread.sleep(500);
+        } catch (Throwable t) {
+            // Ignore
+        }
         h1.join();
         h2.join();
-        assertEquals("Signaled" , 4, counter);
+        synchronized(lock) {
+            assertEquals("Signaled" , 4, counter);
+        }
     }
 
     /*



Mime
View raw message