[Box Backup-dev] 0.11rc1 win32 code review
Charles Lecklider
boxbackup-dev at fluffy.co.uk
Mon Jan 28 01:18:10 GMT 2008
Chris Wilson wrote:
> I'd really like to remove overlapped I/O completely. But I have a bit of a
> problem with that too, which is why I introduced it in the first place.
Overlapped IO is actually very simple; it's just explained amazingly
poorly in the SDK. Before you rip it out, try using it more as it was
intended to be used ;-)
1) Make sure you're using it everywhere; if you open a pipe with the
overlapped flag then everything has to be overlapped.
2) Make sure you're not sharing overlapped structs. It's fine to re-use
them after each request, but attempting to use one that's already in use
will send you straight to deadlock, do not pass Go, do not collect £200.
3) Always use GetOverlappedResult(). Even when you want to wait for only
a certain time before giving up: wait on the overlapped event, if it
gets signalled then call GetOverlappedResult(), otherwise clean up and exit.
4) Let Windows reset the overlapped event when it wants to - don't try
to babysit.
5) If you really really want to poll the pipe for a new message (and I
don't understand why you'd want to - this is what threads are for),
issue the overlapped read as usual, then call GetOverlappedResult()
passing FALSE for bWait. That will always return immediately, with a
message if there is one, or ERROR_IO_INCOMPLETE if there isn't.
6) Use message mode - it makes life much easier.
> Second question (easier I hope): I noticed that Win32 has a Timer API
> (CreateTimerQueueTimer) and I tried to use this to replace the horrible
> RunTimer thread, but the events seem to be delivered very late or not at
> all. I can post sample code if it helps (I don't want to commit it because
> it's badly broken), but do you have any recommendations about it or ideas
> why it might not be working properly? I think you recommended using
> something different in the past, was it Waitable Timer objects? Do they
> work better?
Have a look at RegisterWaitForSingleObject(). With a little care and
attention you can use the overlapped event as the handle to wait for,
and end up with some very simple and short code....
-C
More information about the Boxbackup-dev
mailing list