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/08/20 20:17:49 UTC

svn commit: r806287 - /commons/sandbox/runtime/trunk/src/main/native/os/unix/pmutex.c

Author: mturk
Date: Thu Aug 20 18:17:49 2009
New Revision: 806287

URL: http://svn.apache.org/viewvc?rev=806287&view=rev
Log:
Allow named mutexes

Modified:
    commons/sandbox/runtime/trunk/src/main/native/os/unix/pmutex.c

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/pmutex.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/pmutex.c?rev=806287&r1=806286&r2=806287&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/pmutex.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/pmutex.c Thu Aug 20 18:17:49 2009
@@ -72,13 +72,27 @@
     union semun ick;
     int rc = 0;
     acr_pmutex_t *m;
+    key_t mkey = IPC_PRIVATE;
+    int flags  = IPC_CREAT;
 
+    if (fname) {
+        mkey = ftok(fname, 1);
+        if (mkey == (key_t)-1) {
+            ACR_THROW_IO_ERRNO();
+            return -1;
+        }
+        flags |= IPC_EXCL;
+    }
     m = ACR_Calloc(_E, THROW_FMARK, sizeof(acr_pmutex_t));
     if (!m)
         return -1;
-    m->filedes = semget(IPC_PRIVATE, 1, IPC_CREAT | 0600);
+    m->filedes = semget(mkey, 1, flags | 0600);
 
     if (m->filedes < 0) {
+        if (fname && errno == EEXIST) {
+            /* XXX: Should we throw separate exception here?
+             */
+        }
         rc =  ACR_GET_OS_ERROR();
         goto finally;
     }
@@ -107,8 +121,47 @@
 
 ACR_DECLARE(int) ACR_ProcMutexAttach(JNIEnv *_E, const acr_pchar_t *fname)
 {
-    ACR_SET_OS_ERROR(ACR_ENOTIMPL);
-    return -1;
+    union semun ick;
+    int rc = 0;
+    acr_pmutex_t *m;
+    key_t mkey;
+
+    if (!fname) {
+        /* Cannot attach to unnamed mutex */
+        ACR_THROW_IO_IF_ERR(ACR_EINVAL);
+        return -1;
+    }
+
+    mkey = ftok(fname, 1);
+    if (mkey == (key_t)-1) {
+        ACR_THROW_IO_ERRNO();
+        return -1;
+    }
+    m = ACR_Calloc(_E, THROW_FMARK, sizeof(acr_pmutex_t));
+    if (!m)
+        return -1;
+    m->filedes = semget(mkey, 1, 0);
+
+    if (m->filedes < 0) {
+        rc =  ACR_GET_OS_ERROR();
+        goto finally;
+    }
+    m->locked = 0;
+
+finally:
+    if (rc) {
+        if (m->filedes > 0) {
+            ick.val = 0;
+            semctl(m->filedes, 0, IPC_RMID, ick);
+        }
+        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_ProcMutexLock(JNIEnv *_E, int mutex)