Simultaneous Read and a Write to a device

Hi All,

Is it possible to issue a ReadFile and a WriteFile on the same device handle but from different threads simultaneously?

The device handle was returned by CreateFile function which reads like-

devHandle = CreateFile("\\.\USB_DEV, // device to open
GENERIC_READ | GENERIC_WRITE, // open for reading & writing
FILE_SHARE_READ | FILE_SHARE_WRITE, // // share for reading & writing
NULL, // default security
OPEN_EXISTING, // open // existing devices only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attributes template

One of my threads issued a ReadFile (synchronous i/o) on devHandle and is pending in the device driver for data to become available at (say) READ_BUFFER. One of the other thread then tries to write data (WriteFile again synchronous i/o) on devHandle to a different buffer (say) WRITE_BUFFER in the device driver. This sequence is hanging the system to hang.

So, i am wondering if a simulaneous ReadFile and a WriteFile to the same devHandle is a NO-NO condition.

Inside the device driver, i am making sure that such read and write i/o requests are serialized.

Any pointers are highly welcome.

Thanks in advance.

Open it overlapped, then this will work.

D

-----Original Message-----
From: “xxxxx@gmail.com
To: “Windows System Software Devs Interest List”
Sent: 04/05/07 6:48 PM
Subject: [ntdev] Simultaneous Read and a Write to a device

Hi All,

Is it possible to issue a ReadFile and a WriteFile on the same device handle but from different threads simultaneously?

The device handle was returned by CreateFile function which reads like-

devHandle = CreateFile("\\.\USB_DEV, // device to open
GENERIC_READ | GENERIC_WRITE, // open for reading & writing
FILE_SHARE_READ | FILE_SHARE_WRITE, // // share for reading & writing
NULL, // default security
OPEN_EXISTING, // open // existing devices only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attributes template

One of my threads issued a ReadFile (synchronous i/o) on devHandle and is pending in the device driver for data to become available at (say) READ_BUFFER. One of the other thread then tries to write data (WriteFile again synchronous i/o) on devHandle to a different buffer (say) WRITE_BUFFER in the device driver. This sequence is hanging the system to hang.

So, i am wondering if a simulaneous ReadFile and a WriteFile to the same devHandle is a NO-NO condition.

Inside the device driver, i am making sure that such read and write i/o requests are serialized.

Any pointers are highly welcome.

Thanks in advance.


Questions? First check the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

Dear Holan,

But isn’t overlapped flag for asynchronous I/O process? Does waiting for the event in the overlapped staructure make it synchronous?

Also, is there any limit on how many such simulaneous Reads/Writes i can issue if i ended up using overlapped flag?

Thanks.

You need to open the handle for OVERLAPPED I/O or the I/O manager will serialize requests for you. You’ll then need to handle ERROR_IO_PENDING coming back from ReadFile or WriteFile and use an event to wait while the request completes.

-p

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@gmail.com
Sent: Thursday, April 05, 2007 6:44 PM
To: Windows System Software Devs Interest List
Subject: [ntdev] Simultaneous Read and a Write to a device

Hi All,

Is it possible to issue a ReadFile and a WriteFile on the same device handle but from different threads simultaneously?

The device handle was returned by CreateFile function which reads like-

devHandle = CreateFile("\\.\USB_DEV, // device to open
GENERIC_READ | GENERIC_WRITE, // open for reading & writing
FILE_SHARE_READ | FILE_SHARE_WRITE, // // share for reading & writing
NULL, // default security
OPEN_EXISTING, // open // existing devices only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attributes template

One of my threads issued a ReadFile (synchronous i/o) on devHandle and is pending in the device driver for data to become available at (say) READ_BUFFER. One of the other thread then tries to write data (WriteFile again synchronous i/o) on devHandle to a different buffer (say) WRITE_BUFFER in the device driver. This sequence is hanging the system to hang.

So, i am wondering if a simulaneous ReadFile and a WriteFile to the same devHandle is a NO-NO condition.

Inside the device driver, i am making sure that such read and write i/o requests are serialized.

Any pointers are highly welcome.

Thanks in advance.


Questions? First check the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

> Is it possible to issue a ReadFile and a WriteFile on the same device handle

but from different threads simultaneously?

Only if the handle is opened as overlapped. Otherwise, the internal mutex in
the file object will serialize all operations on it (to
protect ->CurrentByteOffset, for instance).


Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.com

> But isn’t overlapped flag for asynchronous I/O process?

“Asynchronous IO process” is just some abstract words, whose meaning depend on
the talker, and thus they are next to meaningless.

Here I will describe exactly what overlapped flag means:

  1. no serialization of several threads doing IO on the same handle.
  2. OVERLAPPED structure pointer is mandatory for IO calls.
  3. ->CurrentByteOffset is not used at all. Instead, the offset from OVERLAPPED
    is used. This makes lseek()/SetFilePointer meaningless on the overlapped
    handle. Also, read/write do not update ->CurrentByteOffset.
  4. IO calls can return with ERROR_IO_PENDING, with the competion notification
    delivered later by one of 3 means - a) event in OVERLAPPED b) IO completion
    port associated with the file object c) for Read/WriteFileEx - the user APC
    provided.

Some comments on this:

  • OVERLAPPED structure can be used for non-overlapped handle too. At least the
    byte offset will be taken from it, and IIRC the event will also be signaled.
  • from what I remember, all 3 completion notifications will work for
    non-overlapped handle too. The difference here is that the thread will be
    blocked inside the kernel till the IO will complete, not in the completion
    notifications being not delivered.

Also, is there any limit on how many such simulaneous Reads/Writes i can
issue if i ended up using overlapped flag?

No limits, unless the driver imposes some.


Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.com