delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2006/10/06/22:52:52

X-Spam-Check-By: sourceware.org
Date: Sat, 7 Oct 2006 04:52:33 +0200
From: Peter Ekberg <peda AT lysator DOT liu DOT se>
To: cygwin AT cygwin DOT com
Subject: How to select on a win32 handle?
Message-ID: <20061007025233.GA24180@sellafield.lysator.liu.se>
MIME-Version: 1.0
User-Agent: Mutt/1.5.12-2006-07-14
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com

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/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019