On 08/08/2010 12:04, Andy Koppe wrote:
On 7 August 2010 23:07, Jon TURNEY wrote:
Hmmm, looking again at the implementation of select(), I don't immediately
see that when waiting on /dev/windows, it checks that the message queue has
old messages on it before waiting.  The MSDN documentation for
MsgWaitForMultipleObjects() seems to says that messages which had arrived
before the last PeekMessage() etc. aren't considered new and so don't end
the wait?

I think you're right, a call to PeekMessage is needed for proper
select() semantics: it shouldn't block if data is available for
reading.

Attached is a small test-case which seems to demonstrate this problem.

Run ./dev-windows-select-test and observe select() blocks for the full timeout, despite the fact that the /dev/windows fd is ready for reading (and it reported as such as the end of the timeout)

If you run './dev-windows-select-test -skip' to skip the PeekMessage(), select() returns immediately, indicating the /dev/windows fd is ready for reading.
#include <stdio.h>
#include <fcntl.h>
#include <sys/select.h>
#include <errno.h>
#include <windows.h>

// gcc -o dev-windows-select-test.exe dev-windows-select-test.c -Wall -mwindows

int main(int argc, char *argv[])
{
  int fd = open("/dev/windows", O_RDONLY);

  if (PostMessage(NULL, WM_USER, 0, 0) != 0)
    printf("PostMessage succeeded\n");
  else
    printf("PostMessage failed\n");

  if (argc <= 1)
    {
      MSG msg;
      if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
        printf("PeekMessage reports a message available\n");
    }

  struct timeval timeout;
  timeout.tv_sec = 5;
  timeout.tv_usec = 0;

  fd_set readfds;
  FD_ZERO(&readfds);
  FD_SET(fd, &readfds);

  int rc = select(fd+1, &readfds, NULL, NULL, &timeout);
  printf("select returned %d %s\n", rc, strerror(errno));

  return 0;
}
--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

Reply via email to