Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Multithreading in X11 Window implementation [patch]  (Read 3332 times)

0 Members and 1 Guest are viewing this topic.

egastal

  • Newbie
  • *
  • Posts: 1
    • View Profile
Multithreading in X11 Window implementation [patch]
« on: April 17, 2009, 04:42:01 am »
Hi there,

I've started using SFML for window and GL context creation, as it's nice to have a OO API for this stuff. Recently I've found a problem when trying to execute something like this pseudocode:

Code: [Select]

Thread:
    new sf::Window(...);
    /* ... */

Main:
    t0 = new Thread();
    t1 = new Thread();

    t0.join();
    t1.join();


Sometimes the code works, sometimes it terminates with an assertion inside Xlib. The problem is that all sf::Window's share a connection to the X server (the display), and concurrent use of the same connection is not supported by the X11 protocol. I don't have much experience with X programming, but I don't see any advantage in having a global connection for all windows. If the user wants more than one window, he should accept the overhead of more than one connection to the X server (which I would guess is not significant).

OpenGL contexts can still share resources, but the user must treat window creation as a critical section (due to the global context inside SFML) or else contexts won't be shared properly. Thus:

Code: [Select]

Thread:
    window_creation_mutex.lock();
    new sf::Window(...);
    window_creation_mutex.unlock();
    /* ... */

Main:
    t0 = new Thread();
    t1 = new Thread();

    t0.join();
    t1.join();


I'm posting this in the forums as it seems the sourceforge tracker is not being followed. I've attached a patch to the sourceforge page that gives all windows a unique connection to the X server ( https://sourceforge.net/support/tracker.php?aid=2769876 ). The patch is against the current trunk revision 1076. (Note: the patch also implements the fix to this artifact: http://sourceforge.net/support/tracker.php?aid=2719693 )

Looking at the sourceforge page it seems development is happening only on the sfml2 branch. Looking at sfml2 X11 Window implementation there is heavy work being done (DisplayRef) to make the X server connection unique, causing the same threading problems. I'm willing to write a patch against the sfml2 branch (removing the global state DisplayRef), if this branch is stable enough for use.

Sorry for the long post :]

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Multithreading in X11 Window implementation [patch]
« Reply #1 on: April 17, 2009, 08:51:45 am »
Hi

Thanks a lot for your patches :)

Quote
I don't have much experience with X programming, but I don't see any advantage in having a global connection for all windows

I'm not an Xlib expert neither, and I don't know the consequences of having one connection per window. What you're saying makes sense, I'll try your solution and do some tests.

Quote
OpenGL contexts can still share resources, but the user must treat window creation as a critical section (due to the global context inside SFML) or else contexts won't be shared properly

This issue has already been fixed in the sfml2 branch (the context to share is always the same one, and is explicitely passed to the other contexts' constructor).

Quote
Looking at the sourceforge page it seems development is happening only on the sfml2 branch

True, SFML 1.5 will just be a "bug fixes" release.

Quote
I'm willing to write a patch against the sfml2 branch (removing the global state DisplayRef), if this branch is stable enough for use

It is. But don't bother writing a patch, I'll do it myself ;)
Laurent Gomila - SFML developer

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Multithreading in X11 Window implementation [patch]
« Reply #2 on: July 24, 2009, 03:26:26 pm »
Hi

I hope you're still there :lol:

I rencently tried to apply your idea (not directly your patch because many things have changed), and I got errors from the X server on context creation. I thought it was because my contexts are shared and thus need the same display, but I'm not sure, especially when you say that "OpenGL contexts can still share resources". Are you sure of this point? Did your patch really work 100%?
Laurent Gomila - SFML developer

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Multithreading in X11 Window implementation [patch]
« Reply #3 on: July 24, 2009, 04:16:32 pm »
Nevermind, I found my error. Everything seems to be ok now.
Laurent Gomila - SFML developer

 

anything