Moved AWTSurfaceLock retry-loop to java to avoid non-standard sleep functions

This commit is contained in:
Elias Naur 2005-04-27 20:30:14 +00:00
parent e492cd240b
commit c8d5fe296b
2 changed files with 39 additions and 43 deletions

View File

@ -35,6 +35,7 @@ import java.nio.ByteBuffer;
import org.lwjgl.BufferUtils; import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException; import org.lwjgl.LWJGLException;
import org.lwjgl.LWJGLUtil;
import org.lwjgl.Sys; import org.lwjgl.Sys;
import java.awt.Canvas; import java.awt.Canvas;
@ -46,6 +47,8 @@ import java.awt.Canvas;
* @version $Revision$ * @version $Revision$
*/ */
final class AWTSurfaceLock { final class AWTSurfaceLock {
private final static int WAIT_DELAY_MILLIS = 100;
private final ByteBuffer lock_buffer; private final ByteBuffer lock_buffer;
public AWTSurfaceLock() { public AWTSurfaceLock() {
@ -54,10 +57,17 @@ final class AWTSurfaceLock {
private static native ByteBuffer createHandle(); private static native ByteBuffer createHandle();
public ByteBuffer lockAndGetHandle(Canvas canvas) throws LWJGLException { public ByteBuffer lockAndGetHandle(Canvas canvas) throws LWJGLException {
lockAndInitHandle(lock_buffer, canvas); while (!lockAndInitHandle(lock_buffer, canvas)) {
LWJGLUtil.log("Could not get drawing surface info, retrying...");
try {
Thread.sleep(WAIT_DELAY_MILLIS);
} catch (InterruptedException e) {
LWJGLUtil.log("Interrupted while retrying: " + e);
}
}
return lock_buffer; return lock_buffer;
} }
private static native void lockAndInitHandle(ByteBuffer lock_buffer, Canvas canvas) throws LWJGLException; private static native boolean lockAndInitHandle(ByteBuffer lock_buffer, Canvas canvas) throws LWJGLException;
protected void unlock() throws LWJGLException { protected void unlock() throws LWJGLException {
nUnlock(lock_buffer); nUnlock(lock_buffer);

View File

@ -39,63 +39,49 @@
#include <jni.h> #include <jni.h>
#include <jawt.h> #include <jawt.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif
#include "org_lwjgl_opengl_AWTSurfaceLock.h" #include "org_lwjgl_opengl_AWTSurfaceLock.h"
#include "awt_tools.h" #include "awt_tools.h"
#include "common_tools.h" #include "common_tools.h"
#define WAIT_DELAY 100
JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_AWTSurfaceLock_createHandle JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_AWTSurfaceLock_createHandle
(JNIEnv *env, jclass clazz) { (JNIEnv *env, jclass clazz) {
return newJavaManagedByteBuffer(env, sizeof(AWTSurfaceLock)); return newJavaManagedByteBuffer(env, sizeof(AWTSurfaceLock));
} }
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_AWTSurfaceLock_lockAndInitHandle JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_AWTSurfaceLock_lockAndInitHandle
(JNIEnv *env, jclass clazz, jobject lock_buffer_handle, jobject canvas) { (JNIEnv *env, jclass clazz, jobject lock_buffer_handle, jobject canvas) {
JAWT awt; JAWT awt;
JAWT_DrawingSurface* ds; JAWT_DrawingSurface* ds;
JAWT_DrawingSurfaceInfo *dsi; JAWT_DrawingSurfaceInfo *dsi;
AWTSurfaceLock *awt_lock = (AWTSurfaceLock *)(*env)->GetDirectBufferAddress(env, lock_buffer_handle); AWTSurfaceLock *awt_lock = (AWTSurfaceLock *)(*env)->GetDirectBufferAddress(env, lock_buffer_handle);
awt.version = JAWT_VERSION_1_4; awt.version = JAWT_VERSION_1_4;
while (true) {
if (JAWT_GetAWT(env, &awt) == JNI_FALSE) { if (JAWT_GetAWT(env, &awt) == JNI_FALSE) {
throwException(env, "Could not get the JAWT interface"); throwException(env, "Could not get the JAWT interface");
return; return JNI_FALSE;
} }
ds = awt.GetDrawingSurface(env, canvas); ds = awt.GetDrawingSurface(env, canvas);
if (ds == NULL) { if (ds == NULL) {
throwException(env, "Could not get the drawing surface"); throwException(env, "Could not get the drawing surface");
return; return JNI_FALSE;
} }
if((ds->Lock(ds) & JAWT_LOCK_ERROR) != 0) { if((ds->Lock(ds) & JAWT_LOCK_ERROR) != 0) {
awt.FreeDrawingSurface(ds); awt.FreeDrawingSurface(ds);
throwException(env, "Could not lock the drawing surface"); throwException(env, "Could not lock the drawing surface");
return; return JNI_FALSE;
} }
dsi = ds->GetDrawingSurfaceInfo(ds); dsi = ds->GetDrawingSurfaceInfo(ds);
if (dsi != NULL) if (dsi != NULL) {
break;
printfDebug("Could not get drawing surface info, retrying... \n");
ds->Unlock(ds);
awt.FreeDrawingSurface(ds);
#ifdef _WIN32
Sleep(WAIT_DELAY);
#else
usleep(WAIT_DELAY*1000);
#endif
}
awt_lock->awt = awt; awt_lock->awt = awt;
awt_lock->ds = ds; awt_lock->ds = ds;
awt_lock->dsi = dsi; awt_lock->dsi = dsi;
return JNI_TRUE;
}
ds->Unlock(ds);
awt.FreeDrawingSurface(ds);
return JNI_FALSE;
} }
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_AWTSurfaceLock_nUnlock JNIEXPORT void JNICALL Java_org_lwjgl_opengl_AWTSurfaceLock_nUnlock