Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The library leaves some bytes at X11 socket unread. Can not wait for epoll events without busy loop. #2267

Closed
yvwmash opened this issue Feb 4, 2023 · 3 comments
Assignees
Labels
input Keyboard, joystick or mouse support X11
Milestone

Comments

@yvwmash
Copy link

yvwmash commented Feb 4, 2023

I took the example mentioned in a tutorial as a baseline of my code. What I want to achieve is to get the X11 socket and wait on it with epoll. As soon as I get socket ready for reading, I do glfwPollEvents() to drain the event queue. My expectations is that, that the program enters wait state. But I get busy loop with 72 bytes left for read each iteration. These bytes are not belong to the X11 event loop, as the type of XEvent contains garbage.

My setup follows.

#define GLFW_INCLUDE_NONE
#define GLFW_EXPOSE_NATIVE_X11
#include <GLFW/glfw3.h>
#include <GLFW/glfw3native.h>
...
/* initialize the library */
  x11_display = glfwGetX11Display();
  sd_x11  = ConnectionNumber(x11_display);
/* add sd_x11 to epoll */
/* create window set calbacks */

 while (!glfwWindowShouldClose(window)) {
  /* wait for events to be ready */
  epoll_wait(...)
  /* here is the issue 
     printing bytes avaible shows 72 bytes.
     sd_x11 seems to have read state every iteration
  */
  glfwPollEvents();
  glfwSwapBuffers(window);  
}

RELEASE=21.1
CODENAME=vera
EDITION="Xfce"
DESCRIPTION="Linux Mint 21.1 Vera"
DESKTOP=Gnome
TOOLKIT=GTK
NEW_FEATURES_URL=https://www.linuxmint.com/rel_vera_xfce_whatsnew.php
RELEASE_NOTES_URL=https://www.linuxmint.com/rel_vera_xfce.php
USER_GUIDE_URL=https://www.linuxmint.com/documentation.php
GRUB_TITLE=Linux Mint 21.1 Xfce

@yvwmash
Copy link
Author

yvwmash commented Feb 5, 2023

The event that wake-ups the epoll is a generic X11 event. From the frequency of wake-ups I think that the event is related to monitor vertical synchronization. The issue is resolved.

@elmindreda elmindreda self-assigned this Feb 6, 2023
@elmindreda elmindreda added the X11 label Feb 6, 2023
@ad8e
Copy link

ad8e commented Feb 9, 2023

I tried replacing glfw's poll() with epoll(), since according to https://man7.org/linux/man-pages/man7/epoll.7.html, "when used as a level-triggered interface (the default, when EPOLLET is not specified), epoll is simply a faster poll(2)"

The event you're seeing is caused by buffer swap. I guess the X11 fd supplies this event and nothing can be done about it from glfw's side. If I comment out glfwSwapBuffers, or use single buffer + glFlush(), this periodic signal disappears from epoll(). I didn't check, but poll() will likely see the same.

On X11, any performance difference between poll() vs epoll() is currently overwhelmed by latency from rendering, which is causing waits of 2-200 ms with both. No idea why the render thread is clobbering poll() on a different thread, but it's not a glfw-caused problem. I have been unsuccessful in testing Wayland.

@ad8e
Copy link

ad8e commented Feb 14, 2023

GLFW avoids the buffer swap event in the beginning by using XPending, seen here: https://github.com/glfw/glfw/blob/master/src/x11_window.c#L69

So if you copy GLFW's code, you will avoid this issue.

@elmindreda elmindreda added bug Bug reports and bugfix pull requests input Keyboard, joystick or mouse labels Feb 27, 2023
@elmindreda elmindreda added support and removed bug Bug reports and bugfix pull requests labels Mar 31, 2023
@elmindreda elmindreda added this to the None milestone Mar 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
input Keyboard, joystick or mouse support X11
Projects
None yet
Development

No branches or pull requests

3 participants