commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r1094619 - in /commons/sandbox/runtime/trunk/src/main/native: Makefile.unx.in os/unix/procmutex.c os/unix/shmem.c
Date Mon, 18 Apr 2011 15:28:39 GMT
Author: mturk
Date: Mon Apr 18 15:28:38 2011
New Revision: 1094619

URL: http://svn.apache.org/viewvc?rev=1094619&view=rev
Log:
Add posix shared memory support

Added:
    commons/sandbox/runtime/trunk/src/main/native/os/unix/shmem.c   (with props)
Modified:
    commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in
    commons/sandbox/runtime/trunk/src/main/native/os/unix/procmutex.c

Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in?rev=1094619&r1=1094618&r2=1094619&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in Mon Apr 18 15:28:38 2011
@@ -65,6 +65,7 @@ UNIX_SOURCES=\
 	$(TOPDIR)/os/unix/platform.c \
 	$(TOPDIR)/os/unix/posixapi.c \
 	$(TOPDIR)/os/unix/procmutex.c \
+	$(TOPDIR)/os/unix/shmem.c \
 	$(TOPDIR)/os/unix/semaphore.c \
 	$(TOPDIR)/os/unix/time.c \
 	$(TOPDIR)/os/unix/util.c

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=1094619&r1=1094618&r2=1094619&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 Mon Apr 18 15:28:38
2011
@@ -144,17 +144,19 @@ ACR_UNX_EXPORT(jint, SysVMutex, create0)
         }
 
 cleanup:
-        if (fd != -1)
+        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;
+            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);
+                    pd = -1;
+                }
             }
+            if (pd == -1)
+                unlink(J2S(name));
         }
     } DONE_WITH_STR(name);
 

Added: commons/sandbox/runtime/trunk/src/main/native/os/unix/shmem.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/shmem.c?rev=1094619&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/shmem.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/shmem.c Mon Apr 18 15:28:38 2011
@@ -0,0 +1,231 @@
+/* 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/string.h"
+#include "acr/clazz.h"
+#include "acr/jniapi.h"
+#include "acr/port.h"
+#include "arch_opts.h"
+
+#include <sys/mman.h>
+#include <sys/ipc.h>
+#include <sys/shm.h>
+#include <sys/file.h>
+
+#if !defined(SHM_R)
+#define SHM_R 0400
+#endif
+#if !defined(SHM_W)
+#define SHM_W 0200
+#endif
+
+typedef struct shmblock_t {
+    acr_uint32_t   magic;       /* Is this our memory      */
+    pid_t          creator;     /* Creator's process ID    */
+    acr_uint64_t   length;      /* Shared memory size      */
+} shmblock_t;
+
+J_DECLARE_CLAZZ = {
+    INVALID_FIELD_OFFSET,
+    0,
+    0,
+    0,
+    ACR_UNX_CP "PosixShmImpl"
+};
+
+J_DECLARE_M_ID(0000) = {
+    0,
+    "<init>",
+    "()V"
+};
+
+ACR_JNI_EXPORT(jobject, ShmImpl, init0)(JNI_STDARGS)
+{
+    if (_clazzn.u == 1)
+        return (*env)->NewObject(env, _clazzn.i, J4MID(0000));
+    if (AcrLoadClass(env, &_clazzn, 0) != 0) {
+        ACR_THROW_MSG(ACR_EX_EINSTANCE, "PosixShmemImpl not initialized");
+        return 0;
+    }
+    R_LOAD_METHOD(0000, 0);
+    _clazzn.u = 1;
+    return (*env)->NewObject(env, _clazzn.i, J4MID(0000));
+}
+
+ACR_UNX_EXPORT(jint, PosixShm, create0)(JNI_STDARGS, jstring name, jlong size)
+{
+    int sd = -1;
+    key_t skey;
+
+    WITH_CSTR(name) {
+        size_t     nbytes;
+        size_t     realsize = (size_t)size;
+        shmblock_t hdr;
+        int fd = -1;
+
+        fd = open(J2S(name), O_WRONLY | O_CREAT | O_EXCL, 0660);
+        if (fd == -1) {
+            ACR_THROW_BY_ERRNO();
+            goto cleanup;
+        }
+        skey = ftok(J2S(name), 'a');
+        if (skey == (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(shmblock_t);
+        hdr.creator = getpid();
+        hdr.magic   = ACR_SHM_MAGIC;
+        hdr.length  = realsize;
+        if (r_write(fd, (const void *)&hdr, nbytes) == -1) {
+            ACR_THROW_SYS_ERROR();
+            goto cleanup;
+        }
+        sd = shmget(skey, realsize, SHM_R | SHM_W | IPC_CREAT | IPC_EXCL);
+        if (sd == -1)
+            ACR_THROW_BY_ERRNO();
+
+cleanup:
+        if (fd != -1) {
+            close(fd);
+            if (sd == -1)
+                unlink(J2S(name));
+        }
+    } DONE_WITH_STR(name);
+
+    return sd;
+}
+
+ACR_UNX_EXPORT(jint, PosixShm, open0)(JNI_STDARGS, jstring name)
+{
+    int sd = -1;
+    key_t skey;
+
+    WITH_CSTR(name) {
+        int fd = -1;
+        shmblock_t hdr;
+        ssize_t    rd, nbytes;
+
+        fd = open(J2S(name), O_RDONLY);
+        if (fd == -1) {
+            ACR_THROW_BY_ERRNO();
+            goto cleanup;
+        }
+        nbytes = sizeof(shmblock_t);
+
+        rd = r_read(fd, (void *)&hdr, nbytes);
+        if (rd != nbytes) {
+            /* Wrong format */
+            ACR_THROW(ACR_EX_ESYS, EBADF);
+            goto cleanup;
+        }
+        if (hdr.magic != ACR_SHM_MAGIC) {
+            /* Not created by us or corrupted */
+            ACR_THROW(ACR_EX_ESYS, EBADF);
+            goto cleanup;
+        }        
+        skey = ftok(J2S(name), 'a');
+        if (skey == (key_t)-1) {
+            ACR_THROW_BY_ERRNO();
+            goto cleanup;
+        }
+        sd = shmget(skey, 0, SHM_R | SHM_W);
+        if (sd == -1)
+            ACR_THROW_BY_ERRNO();
+
+cleanup:
+        if (fd != -1)
+            close(fd);
+    } DONE_WITH_STR(name);
+
+    return sd;
+}
+
+ACR_UNX_EXPORT(jlong, PosixShm, ssize0)(JNI_STDARGS, jstring name)
+{
+    jlong sz = 0;
+
+    WITH_CSTR(name) {
+        int fd = -1;
+        shmblock_t hdr;
+        ssize_t    rd, nbytes;
+
+        fd = open(J2S(name), O_RDONLY);
+        if (fd == -1) {
+            ACR_THROW_BY_ERRNO();
+            goto cleanup;
+        }
+        nbytes = sizeof(shmblock_t);
+
+        rd = r_read(fd, (void *)&hdr, nbytes);
+        if (rd != nbytes) {
+            /* Wrong format */
+            ACR_THROW(ACR_EX_ESYS, EBADF);
+            goto cleanup;
+        }
+        if (hdr.magic != ACR_SHM_MAGIC) {
+            /* Not created by us or corrupted */
+            ACR_THROW(ACR_EX_ESYS, EBADF);
+            goto cleanup;
+        }
+        sz = (jlong)hdr.length;
+cleanup:
+        if (fd != -1)
+            close(fd);
+    } DONE_WITH_STR(name);
+
+    return sz;
+}
+
+ACR_UNX_EXPORT(jlong, PosixShm, shmat0)(JNI_STDARGS, jlong addr, jint fd, jint flags)
+{
+    void *sa = J2P(addr, void *);
+    void *sm;
+
+    if ((sm = shmat(fd, sa, flags)) == (void *)-1) {
+        ACR_SAVE_OS_ERROR();
+        sm = 0;
+    }
+    return P2J(sm);
+}
+
+ACR_UNX_EXPORT(jint, PosixShm, shmdt0)(JNI_STDARGS, jlong addr)
+{
+    void *sa = J2P(addr, void *);
+
+    if (shmdt(sa) == 0)
+        return 0;
+    else
+        return errno;
+}
+
+ACR_UNX_EXPORT(jint, PosixShm, unlink0)(JNI_STDARGS, jint fd, jstring name)
+{
+    int rc = 0;
+
+    WITH_CSTR(name) {
+        if (shmctl(fd, IPC_RMID, 0) == -1)
+            rc = ACR_GET_OS_ERROR();
+        if (unlink(J2S(name)) == -1 && rc == 0)
+            rc = ACR_GET_OS_ERROR();
+    } DONE_WITH_STR(name);
+
+    return rc;
+}

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



Mime
View raw message