This is the mail archive of the
java-prs@gcc.gnu.org
mailing list for the Java project.
[Bug libgcj/14751] [win32] thread creation leaks system handle resources
- From: "ovidr at users dot sourceforge dot net" <gcc-bugzilla at gcc dot gnu dot org>
- To: java-prs at gcc dot gnu dot org
- Date: 10 Sep 2004 16:15:30 -0000
- Subject: [Bug libgcj/14751] [win32] thread creation leaks system handle resources
- References: <20040327125736.14751.alessandro.pepi@email.it>
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
------- Additional Comments From ovidr at users dot sourceforge dot net 2004-09-10 16:15 -------
I think the problem is more than just thread creation. I have ripped out the
syncExec code from SWT (Display.syncExec()), and only use three threads in a
loop.
It requires swt-win32-3062.dll (SWT 3 final) in the directory for
PostThreadMessage calls.
Handles seem to increase ad infinitum, and memory seems to grow slowly as well
(unlike sun's java) but I don't know if it is ever collected.
gcj -o threadswitch.exe -fjni --main=org.eclipse.swt.internal.win32.OS OS.java
I am using gcj 3.4.1 final (with or without Mohan's patch
http://gcc.gnu.org/ml/java/2004-09/msg00048.html) as well as:
gcc version 3.5.0 20040730 (experimental)
---
package org.eclipse.swt.internal.win32;
class OS implements Runnable {
public static final native int GetCurrentProcessId();
public static final native int GetCurrentThreadId();
public static final native boolean PostThreadMessageW(int idThread, int Msg,
int wParam, int lParam);
public static final native boolean PostThreadMessageA(int idThread, int Msg,
int wParam, int lParam);
public static final int WM_NULL = 0x0;
static {
System.loadLibrary("swt-win32-3062");
}
public static void main(String[] args) {
final OS t1 = new OS();
Thread t3 = new Thread() {
public void run() {
while (true) {
try {
t1.runAsyncMessages();
Thread.sleep(1);
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
t3.start();
Thread t2 = new Thread() {
public void run() {
while (true) {
try {
System.out.println("Thread: " + Thread.currentThread());
t1.syncExec(t1);
Thread.sleep(1);
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
t2.start();
}
Thread thread;
Object messageLock = new Object();
RunnableLock[] messages;
int messageCount;
int threadId;
int processId;
Thread syncThread;
public OS() {
threadId = GetCurrentThreadId();
processId = GetCurrentProcessId();
thread = Thread.currentThread();
System.out.println("Thread: " + Thread.currentThread());
}
public static final boolean PostThreadMessage(int idThread, int Msg, int
wParam, int lParam) {
// IsUnicode = !IsWin32s && !IsWin95;
// if (IsUnicode) return PostThreadMessageW (idThread, Msg, wParam,
lParam);
return PostThreadMessageA(idThread, Msg, wParam, lParam);
}
public void run() {
System.out.println("TS: " + Thread.currentThread());
}
boolean isValidThread() {
return thread == Thread.currentThread();
}
void wakeThread() {
PostThreadMessage(threadId, WM_NULL, 0, 0);
}
void addLast(RunnableLock lock) {
synchronized (messageLock) {
if (messages == null)
messages = new RunnableLock[4];
if (messageCount == messages.length) {
RunnableLock[] newMessages = new RunnableLock[messageCount + 4];
System.arraycopy(messages, 0, newMessages, 0, messageCount);
messages = newMessages;
}
messages[messageCount++] = lock;
if (messageCount == 1)
this.wakeThread();
}
}
protected void syncExec(Runnable runnable) {
if (this.isValidThread()) {
if (runnable != null)
runnable.run();
return;
}
if (runnable == null) {
// display.wake();
return;
}
RunnableLock lock = new RunnableLock(runnable);
/*
* Only remember the syncThread for syncExec.
*/
lock.thread = Thread.currentThread();
synchronized (lock) {
addLast(lock);
boolean interrupted = false;
while (!lock.done()) {
try {
lock.wait();
} catch (InterruptedException e) {
interrupted = true;
}
}
if (interrupted) {
Thread.currentThread().interrupt();
}
if (lock.throwable != null) {
System.err.println("Throwable != null");
}
}
}
boolean runAsyncMessages() {
if (messageCount == 0)
return false;
RunnableLock lock = removeFirst();
if (lock == null)
return true;
synchronized (lock) {
syncThread = lock.thread;
try {
lock.run();
} catch (Throwable t) {
lock.throwable = t;
System.err.println("FAILED EXEC");
} finally {
syncThread = null;
lock.notifyAll();
}
}
return true;
}
RunnableLock removeFirst() {
synchronized (messageLock) {
if (messageCount == 0)
return null;
RunnableLock lock = messages[0];
System.arraycopy(messages, 1, messages, 0, --messageCount);
messages[messageCount] = null;
if (messageCount == 0)
messages = null;
return lock;
}
}
class RunnableLock {
Runnable runnable;
Thread thread;
Throwable throwable;
RunnableLock(Runnable runnable) {
this.runnable = runnable;
}
boolean done() {
return runnable == null || throwable != null;
}
void run() {
if (runnable != null)
runnable.run();
runnable = null;
}
}
}
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14751