diff --git a/src/java/com/threerings/util/unsafe/FreeBSD/Makefile b/src/java/com/threerings/util/unsafe/FreeBSD/Makefile index cb6f5c42..d45b291b 100644 --- a/src/java/com/threerings/util/unsafe/FreeBSD/Makefile +++ b/src/java/com/threerings/util/unsafe/FreeBSD/Makefile @@ -22,32 +22,24 @@ INSTALL_PATH=${ROOT}/dist/lib/i386-FreeBSD INCLUDES=-I.. -I${JAVA_HOME}/include -I${JAVA_HOME}/include/${OSINCDIR} LIBRARIES= -TARGETS=libunsafe.so libunsafegc.so +TARGET=libunsafe.so # # Target definitions -all: ${TARGETS} +all: ${TARGET} install: @${MKDIR} -p ${INSTALL_PATH} - cp ${TARGETS} ${INSTALL_PATH} + cp ${TARGET} ${INSTALL_PATH} -libunsafe.so: com_threerings_util_unsafe_Unsafe.c - @echo "Compiling $<" +${TARGET}: com_threerings_util_unsafe_Unsafe.c + @echo "Compiling Unsafe.c" @${CC} ${INCLUDES} -c com_threerings_util_unsafe_Unsafe.c \ -o com_threerings_util_unsafe_Unsafe.o - @echo "Creating $@" - @${CC} -o libunsafe.so com_threerings_util_unsafe_Unsafe.o \ - ${LIBRARIES} ${LIBRARIES_PATH} -shared - -libunsafegc.so: com_threerings_util_unsafe_UnsafeGC.c - @echo "Compiling $<" - @${CC} ${INCLUDES} -c com_threerings_util_unsafe_UnsafeGC.c \ - -o com_threerings_util_unsafe_Unsafe.o - @echo "Creating $@" - @${CC} -o libunsafegc.so com_threerings_util_unsafe_Unsafegc.o \ + @echo "Creating libunsafe.so" + @${CC} -o ${TARGET} com_threerings_util_unsafe_Unsafe.o \ ${LIBRARIES} ${LIBRARIES_PATH} -shared clean: - -${RM} -f *.o ${TARGETS} + -${RM} -f *.o ${TARGET} diff --git a/src/java/com/threerings/util/unsafe/FreeBSD/com_threerings_util_unsafe_Unsafe.c b/src/java/com/threerings/util/unsafe/FreeBSD/com_threerings_util_unsafe_Unsafe.c index 4f87315f..abe1fd3e 100644 --- a/src/java/com/threerings/util/unsafe/FreeBSD/com_threerings_util_unsafe_Unsafe.c +++ b/src/java/com/threerings/util/unsafe/FreeBSD/com_threerings_util_unsafe_Unsafe.c @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -12,6 +13,9 @@ #include "com_threerings_util_unsafe_Unsafe.h" +/* global jvmpi interface pointer */ +static JVMPI_Interface* jvmpi; + /** A sleep method that uses select(). This seems to have about 10ms * granularity where nanosleep() has about 20ms. Sigh. */ static int select_sleep (int millisecs) @@ -24,6 +28,18 @@ static int select_sleep (int millisecs) return select(0, &dummy, NULL, NULL, &toWait); } +JNIEXPORT void JNICALL +Java_com_threerings_util_unsafe_Unsafe_enableGC (JNIEnv* env, jclass clazz) +{ + jvmpi->EnableGC(); +} + +JNIEXPORT void JNICALL +Java_com_threerings_util_unsafe_Unsafe_disableGC (JNIEnv* env, jclass clazz) +{ + jvmpi->DisableGC(); +} + JNIEXPORT void JNICALL Java_com_threerings_util_unsafe_Unsafe_nativeSleep ( JNIEnv* env, jclass clazz, jint millis) @@ -84,5 +100,18 @@ JNIEXPORT jboolean JNICALL Java_com_threerings_util_unsafe_Unsafe_nativeSetegid JNIEXPORT jboolean JNICALL Java_com_threerings_util_unsafe_Unsafe_init (JNIEnv* env, jclass clazz) { + JavaVM* jvm; + + if ((*env)->GetJavaVM(env, &jvm) > 0) { + fprintf(stderr, "Failed to get JavaVM from env.\n"); + return JNI_FALSE; + } + + /* get jvmpi interface pointer */ + if (((*jvm)->GetEnv(jvm, (void**)&jvmpi, JVMPI_VERSION_1)) < 0) { + fprintf(stderr, "Failed to get JVMPI from JavaVM.\n"); + return JNI_FALSE; + } + return JNI_TRUE; } diff --git a/src/java/com/threerings/util/unsafe/FreeBSD/com_threerings_util_unsafe_UnsafeGC.c b/src/java/com/threerings/util/unsafe/FreeBSD/com_threerings_util_unsafe_UnsafeGC.c deleted file mode 100644 index f278d11f..00000000 --- a/src/java/com/threerings/util/unsafe/FreeBSD/com_threerings_util_unsafe_UnsafeGC.c +++ /dev/null @@ -1,49 +0,0 @@ -/* - * $Id$ - */ - -#include -#include -#include - -#include -#include -#include -#include - -#include "com_threerings_util_unsafe_UnsafeGC.h" - -/* global jvmpi interface pointer */ -static JVMPI_Interface* jvmpi; - -JNIEXPORT void JNICALL -Java_com_threerings_util_unsafe_UnsafeGC_enableGC (JNIEnv* env, jclass clazz) -{ - jvmpi->EnableGC(); -} - -JNIEXPORT void JNICALL -Java_com_threerings_util_unsafe_UnsafeGC_disableGC (JNIEnv* env, jclass clazz) -{ - jvmpi->DisableGC(); -} - - -JNIEXPORT jboolean JNICALL -Java_com_threerings_util_unsafe_UnsafeGC_init (JNIEnv* env, jclass clazz) -{ - JavaVM* jvm; - - if ((*env)->GetJavaVM(env, &jvm) > 0) { - fprintf(stderr, "Failed to get JavaVM from env.\n"); - return JNI_FALSE; - } - - /* get jvmpi interface pointer */ - if (((*jvm)->GetEnv(jvm, (void**)&jvmpi, JVMPI_VERSION_1)) < 0) { - fprintf(stderr, "Failed to get JVMPI from JavaVM.\n"); - return JNI_FALSE; - } - - return JNI_TRUE; -} diff --git a/src/java/com/threerings/util/unsafe/Linux/Makefile b/src/java/com/threerings/util/unsafe/Linux/Makefile index 6d2f8fb0..7d593545 100644 --- a/src/java/com/threerings/util/unsafe/Linux/Makefile +++ b/src/java/com/threerings/util/unsafe/Linux/Makefile @@ -21,32 +21,24 @@ INSTALL_PATH=${ROOT}/dist/lib/i686-Linux INCLUDES=-I.. -I${JAVA_HOME}/include -I${JAVA_HOME}/include/linux LIBRARIES=-lX11 -TARGETS=libunsafe.so libunsafegc.so +TARGET=libunsafe.so # # Target definitions -all: ${TARGETS} +all: ${TARGET} install: @${MKDIR} -p ${INSTALL_PATH} - cp ${TARGETS} ${INSTALL_PATH} + cp ${TARGET} ${INSTALL_PATH} -libunsafe.so: com_threerings_util_unsafe_Unsafe.c - @echo "Compiling $<" +${TARGET}: com_threerings_util_unsafe_Unsafe.c + @echo Compiling Unsafe.c @${CC} ${INCLUDES} -c com_threerings_util_unsafe_Unsafe.c \ -o com_threerings_util_unsafe_Unsafe.o - @echo "Creating $@" - @${CC} -o libunsafe.so com_threerings_util_unsafe_Unsafe.o \ - ${LIBRARIES} ${LIBRARIES_PATH} -shared - -libunsafegc.so: com_threerings_util_unsafe_UnsafeGC.c - @echo "Compiling $<" - @${CC} ${INCLUDES} -c com_threerings_util_unsafe_UnsafeGC.c \ - -o com_threerings_util_unsafe_UnsafeGC.o - @echo "Creating $@" - @${CC} -o libunsafegc.so com_threerings_util_unsafe_UnsafeGC.o \ + @echo Creating ${TARGET} + @${CC} -o ${TARGET} com_threerings_util_unsafe_Unsafe.o \ ${LIBRARIES} ${LIBRARIES_PATH} -shared clean: - -${RM} -f *.o ${TARGETS} + -${RM} -f *.o ${TARGET} diff --git a/src/java/com/threerings/util/unsafe/Linux/com_threerings_util_unsafe_UnsafeGC.c b/src/java/com/threerings/util/unsafe/Linux/com_threerings_util_unsafe_UnsafeGC.c deleted file mode 120000 index 766b3cf3..00000000 --- a/src/java/com/threerings/util/unsafe/Linux/com_threerings_util_unsafe_UnsafeGC.c +++ /dev/null @@ -1 +0,0 @@ -../FreeBSD/com_threerings_util_unsafe_UnsafeGC.c \ No newline at end of file diff --git a/src/java/com/threerings/util/unsafe/Linux/libunsafe.so b/src/java/com/threerings/util/unsafe/Linux/libunsafe.so index d9f3972e..59305842 100755 Binary files a/src/java/com/threerings/util/unsafe/Linux/libunsafe.so and b/src/java/com/threerings/util/unsafe/Linux/libunsafe.so differ diff --git a/src/java/com/threerings/util/unsafe/Linux/libunsafegc.so b/src/java/com/threerings/util/unsafe/Linux/libunsafegc.so deleted file mode 100755 index 583905f6..00000000 Binary files a/src/java/com/threerings/util/unsafe/Linux/libunsafegc.so and /dev/null differ diff --git a/src/java/com/threerings/util/unsafe/Mac OS X/Makefile b/src/java/com/threerings/util/unsafe/Mac OS X/Makefile index 942a310d..cc38f7af 100644 --- a/src/java/com/threerings/util/unsafe/Mac OS X/Makefile +++ b/src/java/com/threerings/util/unsafe/Mac OS X/Makefile @@ -22,32 +22,24 @@ INSTALL_PATH=${ROOT}/dist/lib/`uname -m`-Darwin INCLUDES=-I.. -I/System/Library/Frameworks/JavaVM.framework/Headers LIBRARIES= -TARGETS=libunsafe.jnilib libunsafegc.jnilib +TARGET=libunsafe.jnilib # # Target definitions -all: ${TARGETS} +all: ${TARGET} install: @${MKDIR} -p ${INSTALL_PATH} - cp ${TARGETS} ${INSTALL_PATH} + cp ${TARGET} ${INSTALL_PATH} -libunsafe.jnilib: com_threerings_util_unsafe_Unsafe.c +${TARGET}: com_threerings_util_unsafe_Unsafe.c @echo "Compiling $<" @${CC} ${INCLUDES} -c com_threerings_util_unsafe_Unsafe.c \ -o com_threerings_util_unsafe_Unsafe.o @echo "Creating $@" - @${CC} -o libunsafe.jnilib com_threerings_util_unsafe_Unsafe.o \ - ${LIBRARIES} ${LIBRARIES_PATH} -dynamiclib - -libunsafegc.jnilib: com_threerings_util_unsafe_UnsafeGC.c - @echo "Compiling $<" - @${CC} ${INCLUDES} -c com_threerings_util_unsafe_UnsafeGC.c \ - -o com_threerings_util_unsafe_UnsafeGC.o - @echo "Creating $@" - @${CC} -o libunsafegc.jnilib com_threerings_util_unsafe_UnsafeGC.o \ + @${CC} -o ${TARGET} com_threerings_util_unsafe_Unsafe.o \ ${LIBRARIES} ${LIBRARIES_PATH} -dynamiclib clean: - -${RM} -f *.o ${TARGETS} + -${RM} -f *.o ${TARGET} diff --git a/src/java/com/threerings/util/unsafe/Mac OS X/com_threerings_util_unsafe_UnsafeGC.c b/src/java/com/threerings/util/unsafe/Mac OS X/com_threerings_util_unsafe_UnsafeGC.c deleted file mode 120000 index 766b3cf3..00000000 --- a/src/java/com/threerings/util/unsafe/Mac OS X/com_threerings_util_unsafe_UnsafeGC.c +++ /dev/null @@ -1 +0,0 @@ -../FreeBSD/com_threerings_util_unsafe_UnsafeGC.c \ No newline at end of file diff --git a/src/java/com/threerings/util/unsafe/Unsafe.java b/src/java/com/threerings/util/unsafe/Unsafe.java index 99edee4c..88d8014f 100644 --- a/src/java/com/threerings/util/unsafe/Unsafe.java +++ b/src/java/com/threerings/util/unsafe/Unsafe.java @@ -32,6 +32,34 @@ import static com.threerings.NenyaLog.log; */ public class Unsafe { + /** + * Enables or disables garbage collection. Warning: you will + * be fucked if you leave it disabled for too long. Do not do this + * unless you are dang sure about what you're doing and are prepared + * to test your code on every platform this side of Nantucket. + * + *

Calls to this method do not nest. Regardless of how many times + * you disable GC, only one call is required to reenable it. + */ + public static void setGCEnabled (boolean enabled) + { + // we don't support nesting, NOOP if the state doesn't change + if (_loaded) { + if (!_initialized && !(_initialized = init())) { + // no joy initializing things + return; + } + + if (_initialized && enabled != _gcEnabled) { + if (_gcEnabled = enabled) { + enableGC(); + } else { + disableGC(); + } + } + } + } + /** * Causes the current thread to block for the specified number of * milliseconds. This exists primarily to work around the fact that on @@ -146,14 +174,16 @@ public class Unsafe /** The current state of GC enablement. */ protected static boolean _gcEnabled = true; - /** Whether or not we were able to load and initialize our library. */ + /** Whether or not we were able to load our library. */ protected static boolean _loaded; + /** Whether or not we were able to initialize our library (i.e. get access to jvmpi) */ + protected static boolean _initialized; + static { try { - System.out.println("Tring to load unsafe"); System.loadLibrary("unsafe"); - _loaded = init(); + _loaded = true; } catch (SecurityException se) { log.warning("Failed to load 'unsafe' library: " + se + "."); } catch (UnsatisfiedLinkError e) { diff --git a/src/java/com/threerings/util/unsafe/UnsafeGC.java b/src/java/com/threerings/util/unsafe/UnsafeGC.java deleted file mode 100644 index 35a6b17c..00000000 --- a/src/java/com/threerings/util/unsafe/UnsafeGC.java +++ /dev/null @@ -1,88 +0,0 @@ -// -// $Id$ -// -// Nenya library - tools for developing networked games -// Copyright (C) 2002-2007 Three Rings Design, Inc., All Rights Reserved -// http://www.threerings.net/code/nenya/ -// -// This library is free software; you can redistribute it and/or modify it -// under the terms of the GNU Lesser General Public License as published -// by the Free Software Foundation; either version 2.1 of the License, or -// (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public -// License along with this library; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - -package com.threerings.util.unsafe; - -import com.samskivert.util.RunAnywhere; - -import static com.threerings.NenyaLog.log; - -/** - * A native library for doing unsafe things involving garbage collection. - * Don't use this library. If you must ignore that warning, then be sure you use it - * sparingly and only in very well considered cases. - */ -public class UnsafeGC -{ - /** - * Enables or disables garbage collection. Warning: you will - * be fucked if you leave it disabled for too long. Do not do this - * unless you are dang sure about what you're doing and are prepared - * to test your code on every platform this side of Nantucket. - * - *

Calls to this method do not nest. Regardless of how many times - * you disable GC, only one call is required to reenable it. - */ - public static void setGCEnabled (boolean enabled) - { - // we don't support nesting, NOOP if the state doesn't change - if (_loaded && enabled != _gcEnabled) { - if (_gcEnabled = enabled) { - enableGC(); - } else { - disableGC(); - } - } - } - - - /** - * Reenable garbage collection after a call to {@link #disableGC}. - */ - protected static native void enableGC (); - - /** - * Disables garbage collection. - */ - protected static native void disableGC (); - - /** - * Called to initialize our library. - */ - protected static native boolean init (); - - /** The current state of GC enablement. */ - protected static boolean _gcEnabled = true; - - /** Whether or not we were able to load and initialize our library. */ - protected static boolean _loaded; - - static { - try { - System.loadLibrary("unsafegc"); - _loaded = init(); - } catch (SecurityException se) { - log.warning("Failed to load 'unsafegc' library: " + se + "."); - } catch (UnsatisfiedLinkError e) { - log.warning("Failed to load 'unsafegc' library: " + e + "."); - } - } -} diff --git a/src/java/com/threerings/util/unsafe/Windows/com_threerings_util_unsafe_Unsafe.c b/src/java/com/threerings/util/unsafe/Windows/com_threerings_util_unsafe_Unsafe.c index 8fdd77d1..82622f94 100644 --- a/src/java/com/threerings/util/unsafe/Windows/com_threerings_util_unsafe_Unsafe.c +++ b/src/java/com/threerings/util/unsafe/Windows/com_threerings_util_unsafe_Unsafe.c @@ -4,9 +4,27 @@ #include #include +#include #include "com_threerings_util_unsafe_Unsafe.h" +/* global jvmpi interface pointer */ +static JVMPI_Interface* jvmpi; + +JNIEXPORT void JNICALL +Java_com_threerings_util_unsafe_Unsafe_enableGC (JNIEnv* env, jclass clazz) +{ + fprintf(stderr, "Reenabling GC.\n"); + jvmpi->EnableGC(); +} + +JNIEXPORT void JNICALL +Java_com_threerings_util_unsafe_Unsafe_disableGC (JNIEnv* env, jclass clazz) +{ + fprintf(stderr, "Disabling GC.\n"); + jvmpi->DisableGC(); +} + JNIEXPORT void JNICALL Java_com_threerings_util_unsafe_Unsafe_nativeSleep ( JNIEnv* env, jclass clazz, jint millis) @@ -45,5 +63,18 @@ JNIEXPORT jboolean JNICALL Java_com_threerings_util_unsafe_Unsafe_nativeSetegid JNIEXPORT jboolean JNICALL Java_com_threerings_util_unsafe_Unsafe_init (JNIEnv* env, jclass clazz) { + JavaVM* jvm; + + if ((*env)->GetJavaVM(env, &jvm) > 0) { + fprintf(stderr, "Failed to get JavaVM from env.\n"); + return JNI_FALSE; + } + + /* get jvmpi interface pointer */ + if (((*jvm)->GetEnv(jvm, (void**)&jvmpi, JVMPI_VERSION_1)) < 0) { + fprintf(stderr, "Failed to get JVMPI from JavaVM.\n"); + return JNI_FALSE; + } + return JNI_TRUE; } diff --git a/src/java/com/threerings/util/unsafe/Windows/com_threerings_util_unsafe_UnsafeGC.c b/src/java/com/threerings/util/unsafe/Windows/com_threerings_util_unsafe_UnsafeGC.c deleted file mode 100644 index 54c79c29..00000000 --- a/src/java/com/threerings/util/unsafe/Windows/com_threerings_util_unsafe_UnsafeGC.c +++ /dev/null @@ -1,45 +0,0 @@ -/* - * $Id: com_threerings_util_unsafe_Unsafe.c 3331 2005-02-03 01:25:21Z mdb $ - */ - -#include -#include -#include - -#include "com_threerings_util_unsafe_UnsafeGC.h" - -/* global jvmpi interface pointer */ -static JVMPI_Interface* jvmpi; - -JNIEXPORT void JNICALL -Java_com_threerings_util_unsafe_Unsafe_enableGC (JNIEnv* env, jclass clazz) -{ - fprintf(stderr, "Reenabling GC.\n"); - jvmpi->EnableGC(); -} - -JNIEXPORT void JNICALL -Java_com_threerings_util_unsafe_Unsafe_disableGC (JNIEnv* env, jclass clazz) -{ - fprintf(stderr, "Disabling GC.\n"); - jvmpi->DisableGC(); -} - -JNIEXPORT jboolean JNICALL -Java_com_threerings_util_unsafe_Unsafe_init (JNIEnv* env, jclass clazz) -{ - JavaVM* jvm; - - if ((*env)->GetJavaVM(env, &jvm) > 0) { - fprintf(stderr, "Failed to get JavaVM from env.\n"); - return JNI_FALSE; - } - - /* get jvmpi interface pointer */ - if (((*jvm)->GetEnv(jvm, (void**)&jvmpi, JVMPI_VERSION_1)) < 0) { - fprintf(stderr, "Failed to get JVMPI from JavaVM.\n"); - return JNI_FALSE; - } - - return JNI_TRUE; -} diff --git a/src/java/com/threerings/util/unsafe/com_threerings_util_unsafe_Unsafe.h b/src/java/com/threerings/util/unsafe/com_threerings_util_unsafe_Unsafe.h index cc94f17c..bae9f5aa 100644 --- a/src/java/com/threerings/util/unsafe/com_threerings_util_unsafe_Unsafe.h +++ b/src/java/com/threerings/util/unsafe/com_threerings_util_unsafe_Unsafe.h @@ -7,6 +7,22 @@ #ifdef __cplusplus extern "C" { #endif +/* + * Class: com_threerings_util_unsafe_Unsafe + * Method: enableGC + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_com_threerings_util_unsafe_Unsafe_enableGC + (JNIEnv *, jclass); + +/* + * Class: com_threerings_util_unsafe_Unsafe + * Method: disableGC + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_com_threerings_util_unsafe_Unsafe_disableGC + (JNIEnv *, jclass); + /* * Class: com_threerings_util_unsafe_Unsafe * Method: nativeSleep diff --git a/src/java/com/threerings/util/unsafe/com_threerings_util_unsafe_UnsafeGC.h b/src/java/com/threerings/util/unsafe/com_threerings_util_unsafe_UnsafeGC.h deleted file mode 100644 index ff281c9a..00000000 --- a/src/java/com/threerings/util/unsafe/com_threerings_util_unsafe_UnsafeGC.h +++ /dev/null @@ -1,29 +0,0 @@ -/* DO NOT EDIT THIS FILE - it is machine generated */ -#include -/* Header for class com_threerings_util_unsafe_Unsafe */ - -#ifndef _Included_com_threerings_util_unsafe_Unsafe -#define _Included_com_threerings_util_unsafe_Unsafe -#ifdef __cplusplus -extern "C" { -#endif -/* - * Class: com_threerings_util_unsafe_Unsafe - * Method: enableGC - * Signature: ()V - */ -JNIEXPORT void JNICALL Java_com_threerings_util_unsafe_Unsafe_enableGC - (JNIEnv *, jclass); - -/* - * Class: com_threerings_util_unsafe_Unsafe - * Method: disableGC - * Signature: ()V - */ -JNIEXPORT void JNICALL Java_com_threerings_util_unsafe_Unsafe_disableGC - (JNIEnv *, jclass); - -#ifdef __cplusplus -} -#endif -#endif