Is a created Display really created...?

Started by EgonOlsen, January 08, 2007, 23:56:48

Previous topic - Next topic

EgonOlsen

Hi.

The situation is, that i have one thread that polls the keyboard and another one that does the rendering. This works fine except for a small problem that occurs from time to time: The polling thread is responsible for calling Keyboard.create(). Because it can't tell exactly, when the Display-thread will create the display and there is no interaction between the two at this stage, it simply polls the display for being created, i.e. it does something like this
if (!Keyboard.isCreated()) {
   if (Display.isCreated()) {
       Keyboard.create();
   }
}

It does that, until the display has been created and then it starts polling the keyboard. As said, this works fine...except for some rare (more or less) cases where it hangs in Keyboard.create() with 0% cpu usage and no exception. The problem goes away if i'm doing this:
if (!Keyboard.isCreated()) {
   if (Display.isCreated()) {
       Thread.sleed(100);
       Keyboard.create();
   }
}


My idea was, that even a Display that returns isCreated()==true may not have been fully created yet and that this causes the Keyboard.create() to hang somehow. Any other ideas? Is creating the keyboard and the display in different threads evil?

Matzon

....
		implementation = impl;
		implementation.createKeyboard();
		created = true;
		readBuffer = ByteBuffer.allocate(EVENT_SIZE*BUFFER_SIZE);
		reset();

so creation is being set prematurely - this is probably a bug. Can you reproduce it - if so, can you modify Keyboard.java and test when moving  created = true down 2 lines ?

EgonOlsen

I'm not sure if the problem lies within Keyboard but Display, because this doesn't fix the problem:

if (!Keyboard.isCreated()) {
   Thread.sleed(100);
   if (Display.isCreated()) {
       Keyboard.create();
   }
}

elias

Display, Keyboard and Mouse are not thread safe (but AWTGLCanvas, GL* and AL* functions are!), so it's illegal to call them from different threads at the same time. You can use several threads, but then you'd need some external synchronization so that you can guarantee no two threads are calling Display, Keyboard nor Mouse at the same time.

- elias

EgonOlsen

That's what i thought. Looking at the sources, i assume that the problem arises when Display-initialization calls the create-methods on keyboard and mouse while my other thread tries to do the same after getting a Display.isCreated()==true. The problem here is, that window_created=true; comes before actually initializing the controls, so that my own thread thinks it's save to do it too. Albeit i think that moving window_created=true; to the end of the method would be a good idea anyway, i'll take care of this in my code. Thank you.

elias

Moving window_created down below initControls will break Mouse.create and Keyboard.create which both check Display.isCreated(), which returns window_created.

- elias