Return-Path: Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: (qmail 27452 invoked from network); 20 Aug 2009 07:46:46 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 20 Aug 2009 07:46:46 -0000 Received: (qmail 87848 invoked by uid 500); 20 Aug 2009 07:47:05 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 87769 invoked by uid 500); 20 Aug 2009 07:47:05 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 87760 invoked by uid 99); 20 Aug 2009 07:47:05 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 20 Aug 2009 07:47:05 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 20 Aug 2009 07:46:34 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 94B4D23888C5; Thu, 20 Aug 2009 07:46:12 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r806073 - in /commons/sandbox/runtime/trunk/src: main/java/org/apache/commons/runtime/ main/java/org/apache/commons/runtime/io/ main/native/ main/native/include/ main/native/os/hpux/ main/native/os/unix/ main/native/os/win32/ main/native/sh... Date: Thu, 20 Aug 2009 07:46:12 -0000 To: commits@commons.apache.org From: mturk@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090820074612.94B4D23888C5@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: mturk Date: Thu Aug 20 07:46:09 2009 New Revision: 806073 URL: http://svn.apache.org/viewvc?rev=806073&view=rev Log: impplement SharedMemory API Added: commons/sandbox/runtime/trunk/src/main/native/os/hpux/pshm.c - copied, changed from r806059, commons/sandbox/runtime/trunk/src/main/native/os/hpux/shm.c commons/sandbox/runtime/trunk/src/main/native/os/unix/pshm.c - copied, changed from r806059, commons/sandbox/runtime/trunk/src/main/native/os/unix/shm.c commons/sandbox/runtime/trunk/src/main/native/os/win32/pshm.c - copied, changed from r806059, commons/sandbox/runtime/trunk/src/main/native/os/win32/shm.c commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestSharedMemory.java (with props) Removed: commons/sandbox/runtime/trunk/src/main/native/os/hpux/shm.c commons/sandbox/runtime/trunk/src/main/native/os/unix/shm.c commons/sandbox/runtime/trunk/src/main/native/os/win32/shm.c Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/SharedMemory.java commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Status.java commons/sandbox/runtime/trunk/src/main/native/Makefile.in commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in commons/sandbox/runtime/trunk/src/main/native/include/acr_descriptor.h commons/sandbox/runtime/trunk/src/main/native/include/acr_shm.h commons/sandbox/runtime/trunk/src/main/native/shared/descriptor.c commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/SharedMemory.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/SharedMemory.java?rev=806073&r1=806072&r2=806073&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/SharedMemory.java (original) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/SharedMemory.java Thu Aug 20 07:46:09 2009 @@ -17,6 +17,8 @@ import java.io.IOException; import org.apache.commons.runtime.io.File; +import org.apache.commons.runtime.io.Status; +import org.apache.commons.runtime.exception.ClosedDescriptorException; import org.apache.commons.runtime.exception.OperatingSystemException; import org.apache.commons.runtime.exception.UnsupportedOperatingSystemException; @@ -46,6 +48,9 @@ this.shm = shm; } + private static native SharedMemory create0(long size) + throws IOException, SecurityException, OutOfMemoryError; + /** * Create and make accessable anonymous shared memory segment. *

@@ -69,10 +74,13 @@ * require it. */ public static SharedMemory create(long size) + throws IOException, SecurityException, OutOfMemoryError { - return null; + return create0(size); } + private static native SharedMemory create1(String name, long size) + throws IOException, SecurityException, OutOfMemoryError; /** * Create and make accessable a shared memory segment. *

@@ -88,18 +96,21 @@ * about the segment within the actual segment. In order to supply * the caller with the requested size it may be necessary for the * implementation to request a slightly greater segment length - * from the subsystem. In all cases, the {@code get()} - * function will return the first usable byte of memory. + * from the subsystem. In all cases, the {@code map()} + * method will return the first usable byte of memory. *

* @param reqsize The desired size of the segment. * @param file The abstract file path to use for shared memory on platforms * that require it. */ public static SharedMemory create(File file, long size) + throws IOException, SecurityException, OutOfMemoryError { - return null; + return create1(file.getPath(), size); } + private static native SharedMemory create2(String name) + throws IOException, SecurityException, OutOfMemoryError; /** * Attach to a shared memory segment that was created * by another process. @@ -107,10 +118,13 @@ * shared memory block. */ public static SharedMemory attach(File file) + throws IOException, SecurityException, OutOfMemoryError { - return null; + return create2(file.getPath()); } + private static native int remove0(String name) + throws IOException, SecurityException; /** * Remove shared memory segment associated with a {@code file}. *

@@ -121,10 +135,19 @@ * segment which needs to be removed. */ public static boolean remove(File file) + throws IOException, SecurityException { - return true; + int rc; + + rc = remove0(file.getPath()); + if (rc == Status.OK) + return true; + else + return false; } + private static native int detach0(Descriptor shm) + throws IOException, SecurityException; /** * Detach the shared memory segment from the system. *

@@ -137,9 +160,18 @@ *

*/ public void detach() + throws IOException, SecurityException, ClosedDescriptorException { + if (shm.valid()) { + detach0(shm); + } + else { + throw new ClosedDescriptorException(); + } } + private static native int close0(Descriptor shm) + throws IOException, SecurityException; /** * Close and remove the shared memory segment from the system. *

@@ -154,17 +186,34 @@ *

*/ public void close() + throws IOException, SecurityException, ClosedDescriptorException { + if (shm.valid()) { + close0(shm); + } + else { + throw new ClosedDescriptorException(); + } } + private static native long size0(Descriptor shm) + throws IOException; /** * Retrieve the length of a shared memory segment in bytes. */ public long size() + throws IOException, ClosedDescriptorException { - return 0; + if (shm.valid()) { + return size0(shm); + } + else { + throw new ClosedDescriptorException(); + } } + private static native Pointer map0(Descriptor shm) + throws IOException; /** * Retrieve new {@code Pointer} base address of the shared memory segment. *

@@ -174,11 +223,19 @@ *

* @return Memory pointer. */ - public Pointer get() + public Pointer map() + throws IOException, ClosedDescriptorException { - return null; + if (shm.valid()) { + return map0(shm); + } + else { + throw new ClosedDescriptorException(); + } } + private static native Pointer map1(Descriptor shm, long off, long size) + throws IOException, IndexOutOfBoundsException; /** * Retrieve new @code Pointer}address of the shared memory segment. *

@@ -190,9 +247,15 @@ * @param size Size to map. * @return Memory pointer. */ - public Pointer get(long offset, long size) + public Pointer map(long offset, long size) + throws IOException, IndexOutOfBoundsException, ClosedDescriptorException { - return null; + if (shm.valid()) { + return map1(shm, offset, size); + } + else { + throw new ClosedDescriptorException(); + } } } Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Status.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Status.java?rev=806073&r1=806072&r2=806073&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Status.java (original) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Status.java Thu Aug 20 07:46:09 2009 @@ -106,6 +106,8 @@ private static final int OS_START_SYSERR = 720000; private static final int OS_ERRSPACE_SIZE = 50000; + /** Status is OK. */ + public static final int OK = 0; /** Unable to perform a stat on the file. */ public static final int ENOSTAT = OS_START_ERROR + 1; /** Pool from which to allocate the memory was not provided. */ Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.in URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.in?rev=806073&r1=806072&r2=806073&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/Makefile.in (original) +++ commons/sandbox/runtime/trunk/src/main/native/Makefile.in Thu Aug 20 07:46:09 2009 @@ -97,6 +97,7 @@ $(SRCDIR)/shared/sha1.$(OBJ) \ $(SRCDIR)/shared/sha2.$(OBJ) \ $(SRCDIR)/shared/sbuf.$(OBJ) \ + $(SRCDIR)/shared/shm.$(OBJ) \ $(SRCDIR)/shared/string.$(OBJ) \ $(SRCDIR)/shared/tables.$(OBJ) \ $(SRCDIR)/shared/xdr.$(OBJ) \ @@ -111,7 +112,7 @@ $(SRCDIR)/os/unix/user.$(OBJ) \ $(SRCDIR)/os/unix/pmutex.$(OBJ) \ $(SRCDIR)/os/unix/psema.$(OBJ) \ - $(SRCDIR)/os/unix/shm.$(OBJ) \ + $(SRCDIR)/os/unix/pshm.$(OBJ) \ $(SRCDIR)/os/unix/signals.$(OBJ) \ $(SRCDIR)/os/unix/syslog.$(OBJ) \ $(SRCDIR)/os/unix/time.$(OBJ) \ @@ -135,7 +136,7 @@ $(SRCDIR)/os/unix/user.$(OBJ) \ $(SRCDIR)/os/unix/pmutex.$(OBJ) \ $(SRCDIR)/os/unix/psema.$(OBJ) \ - $(SRCDIR)/os/unix/shm.$(OBJ) \ + $(SRCDIR)/os/unix/pshm.$(OBJ) \ $(SRCDIR)/os/unix/signals.$(OBJ) \ $(SRCDIR)/os/unix/syslog.$(OBJ) \ $(SRCDIR)/os/unix/time.$(OBJ) \ @@ -156,7 +157,7 @@ $(SRCDIR)/os/unix/group.$(OBJ) \ $(SRCDIR)/os/unix/user.$(OBJ) \ $(SRCDIR)/os/unix/psema.$(OBJ) \ - $(SRCDIR)/os/unix/shm.$(OBJ) \ + $(SRCDIR)/os/unix/pshm.$(OBJ) \ $(SRCDIR)/os/unix/signals.$(OBJ) \ $(SRCDIR)/os/unix/syslog.$(OBJ) \ $(SRCDIR)/os/unix/time.$(OBJ) \ @@ -188,7 +189,7 @@ $(SRCDIR)/os/hpux/platform.$(OBJ) \ $(SRCDIR)/os/hpux/pgroup.$(OBJ) \ $(SRCDIR)/os/hpux/puser.$(OBJ) \ - $(SRCDIR)/os/hpux/shm.$(OBJ) \ + $(SRCDIR)/os/hpux/pshm.$(OBJ) \ $(SRCDIR)/os/hpux/os.$(OBJ) PPORT_OBJS=\ 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=806073&r1=806072&r2=806073&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in (original) +++ commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in Thu Aug 20 07:46:09 2009 @@ -88,6 +88,7 @@ $(SRCDIR)/shared/sha1.$(OBJ) \ $(SRCDIR)/shared/sha2.$(OBJ) \ $(SRCDIR)/shared/sbuf.$(OBJ) \ + $(SRCDIR)/shared/shm.$(OBJ) \ $(SRCDIR)/shared/string.$(OBJ) \ $(SRCDIR)/shared/tables.$(OBJ) \ $(SRCDIR)/shared/xdr.$(OBJ) \ @@ -104,7 +105,7 @@ $(SRCDIR)/os/win32/ios.$(OBJ) \ $(SRCDIR)/os/win32/pmutex.$(OBJ) \ $(SRCDIR)/os/win32/psema.$(OBJ) \ - $(SRCDIR)/os/win32/shm.$(OBJ) \ + $(SRCDIR)/os/win32/pshm.$(OBJ) \ $(SRCDIR)/os/win32/signals.$(OBJ) \ $(SRCDIR)/os/win32/syslog.$(OBJ) \ $(SRCDIR)/os/win32/group.$(OBJ) \ Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_descriptor.h URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_descriptor.h?rev=806073&r1=806072&r2=806073&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/include/acr_descriptor.h (original) +++ commons/sandbox/runtime/trunk/src/main/native/include/acr_descriptor.h Thu Aug 20 07:46:09 2009 @@ -107,6 +107,13 @@ ACR_DECLARE(int) ACR_DescriptorCleanup(JNIEnv *env, jobject obj); /** + * Clear the Java object without calling cleanup. + * @param env Current JNI environment + * @param obj Java Descriptor object to clear. + */ +ACR_DECLARE(int) ACR_DescriptorClear(JNIEnv *env, jobject obj); + +/** * Get the native errno number from the Descriptor object. * @param env Current JNI environment * @param obj Java Descriptor object use. Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_shm.h URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_shm.h?rev=806073&r1=806072&r2=806073&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/include/acr_shm.h (original) +++ commons/sandbox/runtime/trunk/src/main/native/include/acr_shm.h Thu Aug 20 07:46:09 2009 @@ -141,6 +141,26 @@ ACR_DECLARE(int) ACR_ShmPermSet(JNIEnv *env, int shm, int perms, acr_uid_t uid, acr_uid_t gid); +/** + * Create shared memory object. + * @param env JNI environment to use. + * @param filename The file to use for shared memory on platforms that + * require it. + * @return The shared memory structure to create. + */ +ACR_DECLARE(jobject) ACR_SharedMemoryObjectCreate(JNIEnv *env, + const acr_pchar_t *filename, + size_t size); + +/** + * Create attached shared memory object. + * @param env JNI environment to use. + * @param filename The file used to create the original segment. + * (This MUST match the original filename.) + * @return The shared memory structure to create. + */ +ACR_DECLARE(jobject) ACR_SharedMemoryObjectAttach(JNIEnv *env, + const acr_pchar_t *filename); #ifdef __cplusplus } Copied: commons/sandbox/runtime/trunk/src/main/native/os/hpux/pshm.c (from r806059, commons/sandbox/runtime/trunk/src/main/native/os/hpux/shm.c) URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/hpux/pshm.c?p2=commons/sandbox/runtime/trunk/src/main/native/os/hpux/pshm.c&p1=commons/sandbox/runtime/trunk/src/main/native/os/hpux/shm.c&r1=806059&r2=806073&rev=806073&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/os/hpux/shm.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/os/hpux/pshm.c Thu Aug 20 07:46:09 2009 @@ -543,3 +543,109 @@ ACR_UnloadClass(_E, &_clazzn); } +static int shm_descriptor_cleanup(ACR_JNISTDARGS, + acr_descriptor_cb_type_e cm, + acr_descriptor_cb_t *dp) +{ + int rc = ACR_SUCCESS; + switch (cm) { + case ACR_DESC_CLOSE: + if (dp->di > 0) + rc = acr_ioh_close(dp->di); + else + rc = ACR_EBADF; + break; + default: + rc = ACR_ENOTIMPL; + break; + } + return rc; +} + +ACR_DECLARE(jobject) ACR_SharedMemoryObjectCreate(JNIEnv *_E, + const acr_pchar_t *name, + size_t size) +{ + jobject shmm; + jobject shmd; + int ishm; + + ishm = ACR_ShmCreate(_E, size, name); + if (ishm < 0) { + + return NULL; + } + /* Create Descriptor Object */ + shmd = ACR_DescriptorCreate(_E, ACR_DT_USER, ishm, NULL, + shm_descriptor_cleanup); + if (!shmd) { + + return NULL; + } + shmm = (*_E)->NewObject(_E, _clazzn.i, J4MID(0000), shmd); + return shmm; +} + +ACR_DECLARE(jobject) ACR_SharedMemoryObjectAttach(JNIEnv *_E, + const acr_pchar_t *name) +{ + jobject shmm; + jobject shmd; + int ishm; + + ishm = ACR_ShmAttach(_E, name); + if (ishm < 0) { + + return NULL; + } + /* Create Descriptor Object */ + shmd = ACR_DescriptorCreate(_E, ACR_DT_USER, ishm, NULL, + shm_descriptor_cleanup); + if (!shmd) { + + return NULL; + } + shmm = (*_E)->NewObject(_E, _clazzn.i, J4MID(0000), shmd); + return shmm; +} + +ACR_JNI_EXPORT_DECLARE(jobject, SharedMemory, create1)(ACR_JNISTDARGS, + jstring name, + jlong size) +{ + + jobject shm = NULL; + UNREFERENCED_O; + + WITH_CSTR(name) { + shm = ACR_SharedMemoryObjectCreate(_E, J2S(name), (size_t)size); + } END_WITH_CSTR(name); + return shm; +} + +ACR_JNI_EXPORT_DECLARE(jobject, SharedMemory, create2)(ACR_JNISTDARGS, + jstring name) +{ + + jobject shm = NULL; + UNREFERENCED_O; + + WITH_CSTR(name) { + shm = ACR_SharedMemoryObjectAttach(_E, J2S(name)); + } END_WITH_CSTR(name); + return shm; +} + +ACR_JNI_EXPORT_DECLARE(jint, SharedMemory, remove0)(ACR_JNISTDARGS, + jstring name) +{ + + int rc = ACR_SUCCESS; + UNREFERENCED_O; + + WITH_CSTR(name) { + rc = ACR_ShmRemove(_E, J2S(name)); + } END_WITH_CSTR(name); + return rc; +} + Copied: commons/sandbox/runtime/trunk/src/main/native/os/unix/pshm.c (from r806059, commons/sandbox/runtime/trunk/src/main/native/os/unix/shm.c) URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/pshm.c?p2=commons/sandbox/runtime/trunk/src/main/native/os/unix/pshm.c&p1=commons/sandbox/runtime/trunk/src/main/native/os/unix/shm.c&r1=806059&r2=806073&rev=806073&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/os/unix/shm.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/os/unix/pshm.c Thu Aug 20 07:46:09 2009 @@ -78,7 +78,7 @@ return ACR_EINVAL; } /* Anonymous shared memory or - * we are calling this from forked child. + * we are calling this from forked child. */ if (m->filename == NULL) { if (m->base && munmap(m->base, m->realsize) == -1) { @@ -227,9 +227,9 @@ return -1; /* Check if they want anonymous or name-based shared memory */ if (filename == NULL) { - shm->filename = NULL; + shm->filename = NULL; shm->reqsize = reqsize; - shm->realsize = reqsize + + shm->realsize = reqsize + ACR_ALIGN_DEFAULT(sizeof(acr_size_t)); /* room for metadata */ shm->base = mmap(NULL, shm->realsize, PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0); @@ -314,7 +314,7 @@ } while (rc == (acr_size_t)-1 && errno == EINTR); if (rc == -1) { rc = ACR_GET_OS_ERROR(); - goto finally; + goto finally; } finally: if (file > 0) @@ -462,7 +462,7 @@ if (IS_INVALID_HANDLE(m) || ACR_IOH_TYPE(shm) != ACR_DT_SHM) { rc = ACR_EINVAL; goto finally; - } + } if ((shmid = shmget(m->shmkey, 0, SHM_R | SHM_W)) == -1) { rc = ACR_GET_OS_ERROR(); goto finally; @@ -524,3 +524,109 @@ ACR_UnloadClass(_E, &_clazzn); } +static int shm_descriptor_cleanup(ACR_JNISTDARGS, + acr_descriptor_cb_type_e cm, + acr_descriptor_cb_t *dp) +{ + int rc = ACR_SUCCESS; + switch (cm) { + case ACR_DESC_CLOSE: + if (dp->di > 0) + rc = acr_ioh_close(dp->di); + else + rc = ACR_EBADF; + break; + default: + rc = ACR_ENOTIMPL; + break; + } + return rc; +} + +ACR_DECLARE(jobject) ACR_SharedMemoryObjectCreate(JNIEnv *_E, + const acr_pchar_t *name, + size_t size) +{ + jobject shmm; + jobject shmd; + int ishm; + + ishm = ACR_ShmCreate(_E, size, name); + if (ishm < 0) { + + return NULL; + } + /* Create Descriptor Object */ + shmd = ACR_DescriptorCreate(_E, ACR_DT_USER, ishm, NULL, + shm_descriptor_cleanup); + if (!shmd) { + + return NULL; + } + shmm = (*_E)->NewObject(_E, _clazzn.i, J4MID(0000), shmd); + return shmm; +} + +ACR_DECLARE(jobject) ACR_SharedMemoryObjectAttach(JNIEnv *_E, + const acr_pchar_t *name) +{ + jobject shmm; + jobject shmd; + int ishm; + + ishm = ACR_ShmAttach(_E, name); + if (ishm < 0) { + + return NULL; + } + /* Create Descriptor Object */ + shmd = ACR_DescriptorCreate(_E, ACR_DT_USER, ishm, NULL, + shm_descriptor_cleanup); + if (!shmd) { + + return NULL; + } + shmm = (*_E)->NewObject(_E, _clazzn.i, J4MID(0000), shmd); + return shmm; +} + +ACR_JNI_EXPORT_DECLARE(jobject, SharedMemory, create1)(ACR_JNISTDARGS, + jstring name, + jlong size) +{ + + jobject shm = NULL; + UNREFERENCED_O; + + WITH_CSTR(name) { + shm = ACR_SharedMemoryObjectCreate(_E, J2S(name), (size_t)size); + } END_WITH_CSTR(name); + return shm; +} + +ACR_JNI_EXPORT_DECLARE(jobject, SharedMemory, create2)(ACR_JNISTDARGS, + jstring name) +{ + + jobject shm = NULL; + UNREFERENCED_O; + + WITH_CSTR(name) { + shm = ACR_SharedMemoryObjectAttach(_E, J2S(name)); + } END_WITH_CSTR(name); + return shm; +} + +ACR_JNI_EXPORT_DECLARE(jint, SharedMemory, remove0)(ACR_JNISTDARGS, + jstring name) +{ + + int rc = ACR_SUCCESS; + UNREFERENCED_O; + + WITH_CSTR(name) { + rc = ACR_ShmRemove(_E, J2S(name)); + } END_WITH_CSTR(name); + return rc; +} + Copied: commons/sandbox/runtime/trunk/src/main/native/os/win32/pshm.c (from r806059, commons/sandbox/runtime/trunk/src/main/native/os/win32/shm.c) URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/pshm.c?p2=commons/sandbox/runtime/trunk/src/main/native/os/win32/pshm.c&p1=commons/sandbox/runtime/trunk/src/main/native/os/win32/shm.c&r1=806059&r2=806073&rev=806073&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/os/win32/shm.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/os/win32/pshm.c Thu Aug 20 07:46:09 2009 @@ -405,3 +405,109 @@ ACR_UnloadClass(_E, &_clazzn); } +static int shm_descriptor_cleanup(ACR_JNISTDARGS, + acr_descriptor_cb_type_e cm, + acr_descriptor_cb_t *dp) +{ + int rc = ACR_SUCCESS; + switch (cm) { + case ACR_DESC_CLOSE: + if (dp->di > 0) + rc = acr_ioh_close(dp->di); + else + rc = ACR_EBADF; + break; + default: + rc = ACR_ENOTIMPL; + break; + } + return rc; +} + +ACR_DECLARE(jobject) ACR_SharedMemoryObjectCreate(JNIEnv *_E, + const acr_pchar_t *name, + size_t size) +{ + jobject shmm; + jobject shmd; + int ishm; + + ishm = ACR_ShmCreate(_E, size, name); + if (ishm < 0) { + + return NULL; + } + /* Create Descriptor Object */ + shmd = ACR_DescriptorCreate(_E, ACR_DT_USER, ishm, NULL, + shm_descriptor_cleanup); + if (!shmd) { + + return NULL; + } + shmm = (*_E)->NewObject(_E, _clazzn.i, J4MID(0000), shmd); + return shmm; +} + +ACR_DECLARE(jobject) ACR_SharedMemoryObjectAttach(JNIEnv *_E, + const acr_pchar_t *name) +{ + jobject shmm; + jobject shmd; + int ishm; + + ishm = ACR_ShmAttach(_E, name); + if (ishm < 0) { + + return NULL; + } + /* Create Descriptor Object */ + shmd = ACR_DescriptorCreate(_E, ACR_DT_USER, ishm, NULL, + shm_descriptor_cleanup); + if (!shmd) { + + return NULL; + } + shmm = (*_E)->NewObject(_E, _clazzn.i, J4MID(0000), shmd); + return shmm; +} + +ACR_JNI_EXPORT_DECLARE(jobject, SharedMemory, create1)(ACR_JNISTDARGS, + jstring name, + jlong size) +{ + + jobject shm = NULL; + UNREFERENCED_O; + + WITH_WSTR(name) { + shm = ACR_SharedMemoryObjectCreate(_E, J2W(name), (size_t)size); + } END_WITH_WSTR(name); + return shm; +} + +ACR_JNI_EXPORT_DECLARE(jobject, SharedMemory, create2)(ACR_JNISTDARGS, + jstring name) +{ + + jobject shm = NULL; + UNREFERENCED_O; + + WITH_WSTR(name) { + shm = ACR_SharedMemoryObjectAttach(_E, J2W(name)); + } END_WITH_WSTR(name); + return shm; +} + +ACR_JNI_EXPORT_DECLARE(jint, SharedMemory, remove0)(ACR_JNISTDARGS, + jstring name) +{ + + int rc = ACR_SUCCESS; + UNREFERENCED_O; + + WITH_WSTR(name) { + rc = ACR_ShmRemove(_E, J2S(name)); + } END_WITH_WSTR(name); + return rc; +} + Modified: commons/sandbox/runtime/trunk/src/main/native/shared/descriptor.c URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/descriptor.c?rev=806073&r1=806072&r2=806073&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/shared/descriptor.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/shared/descriptor.c Thu Aug 20 07:46:09 2009 @@ -327,6 +327,27 @@ return ACR_ECLASSNOTFOUND; } +ACR_DECLARE(int) ACR_DescriptorClear(ACR_JNISTDARGS) +{ + if (_clazzn.i && J4MID(0000)) { + if ((*_E)->MonitorEnter(_E, _O)) { + /* Object locking failed */ + return ACR_ENOLOCK; + } + SET_IFIELD_I(0000, _O, -1); + SET_IFIELD_P(0001, _O, NULL); + SET_IFIELD_P(0002, _O, NULL); + SET_IFIELD_P(0003, _O, NULL); + SET_IFIELD_P(0004, _O, NULL); + SET_IFIELD_O(0005, _O, NULL); + + (*_E)->MonitorExit(_E, _O); + return ACR_SUCCESS; + } + else + return ACR_ECLASSNOTFOUND; +} + ACR_DECLARE(void *) ACR_DescriptorGetPtr(ACR_JNISTDARGS) { if (_clazzn.i && J4MID(0000)) { Modified: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java?rev=806073&r1=806072&r2=806073&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java (original) +++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java Thu Aug 20 07:46:09 2009 @@ -45,6 +45,7 @@ suite.addTest(TestFile.suite()); suite.addTest(TestStrings.suite()); suite.addTest(TestSemaphore.suite()); + suite.addTest(TestSharedMemory.suite()); return suite; } Added: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestSharedMemory.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestSharedMemory.java?rev=806073&view=auto ============================================================================== --- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestSharedMemory.java (added) +++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestSharedMemory.java Thu Aug 20 07:46:09 2009 @@ -0,0 +1,81 @@ +/* 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. + */ + +package org.apache.commons.runtime; + +import java.lang.System; +import junit.framework.*; +import org.apache.commons.runtime.io.*; +import org.apache.commons.runtime.exception.*; + +/** + * SharedMemory Test. + * + */ +public class TestSharedMemory extends TestCase +{ + + public static Test suite() { + TestSuite suite = new TestSuite(TestSharedMemory.class); + return suite; + } + + protected void setUp() + throws Exception + { + System.loadLibrary("acr"); + } + + public void testSharedMemoryCreate() + throws Throwable + { + + try { + SharedMemory.remove(new File("ACRSharedMemory")); + System.out.println("Removed previous instance of ACRSharedMemory"); + } catch (Throwable t) { + // Ignore. + } + SharedMemory shm; + shm = SharedMemory.create(new File("ACRSharedMemory"), 1024); + + long size = shm.size(); + System.out.println("ACRSharedMemory created with real size " + size); + assertTrue("Wrong Shared memory size", size >= 1024); + + shm.close(); + } + + public void testSharedMemoryDetach() + throws Throwable + { + SharedMemory shm; + + shm = SharedMemory.create(new File("ACRSharedMemory"), 1024); + + shm.detach(); + try { + shm.close(); + } catch (Throwable t) { + assertSame("Wrong Exception class", + ClosedDescriptorException.class, t.getClass()); + } + boolean rv = SharedMemory.remove(new File("ACRSharedMemory")); + assertTrue("Shared memory not removed", rv); + } + +} + Propchange: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestSharedMemory.java ------------------------------------------------------------------------------ svn:eol-style = native