This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

How to select on a win32 handle?


Hello!

I'm finding that I have a need to select on a win32 handle. I found the
function cygwin_attach_handle_to_fd which sounded promising, but I do
not have any luck when I try to use it.

I did look through the sources in winsup, but came to the perhaps
misguided conclusion that there needs to be a new fhandler in order to
support this.

I would not mind to be proved wrong though. :-)

I was hoping that the following program would print "tick" with one
second intervals, but the fd is selectable all the time. If I
uncomment the call to WaitForSingleObject, the output is as I hoped,
so I believe the Win32 parts are correct in the sample.

I have tried various arguments instead of NULL for
cygwin_attach_handle_to_fd, in the hopes of finding a workable
fhandler, but to no avail.

Is there some other approach to select on Win32 handles? Or should
I declare defeat and use get_osfhandle on the fds and WaitFor...
the combined set of fds/objects that way instead?

Cheers,
Peter

-------------8<------------

#include <windows.h>

#include <stdio.h>
#include <unistd.h>
#include <sys/cygwin.h>

int
main(void)
{
	HANDLE timer;
	LARGE_INTEGER due;
	int fd;
	fd_set fds;
	int i;
	int res = 0;

	timer = CreateWaitableTimer(NULL, TRUE, NULL);
	due.QuadPart = -10000000;
	if (!SetWaitableTimer(timer, &due, 0, NULL, NULL, FALSE))
		return -1;

	fd = cygwin_attach_handle_to_fd(NULL, -1, timer, 1, GENERIC_READ);

	if (fd < 0) {
		CloseHandle(timer);
		return -1;
	}

	FD_ZERO(&fds);
	FD_SET(fd, &fds);

	for (i = 0; i < 5; ++i) {
		res = select(fd + 1, &fds, NULL, NULL, NULL);
		if (res < 0)
			break;
		printf("tick\n");
		/* WaitForSingleObject(timer, INFINITE); */
		if (!SetWaitableTimer(timer, &due, 0, NULL, NULL, FALSE)) {
			res = -1;
			break;
		}
	}

	close(fd);

	return res < 0 ? res : 0;
}

-------------8<------------

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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]