Async behaviour in a thread

T

Tom Vandeplas

Hi all,

When using an asynchronous API call (in my case WriteFileEx) in a thread it
seems to become blocking instead of non-blocking (see details below). Can
somebody explain me what is happening here...

Details:
I wrote a simple program that is able to do very fast async writes to disk.
For that I used the WriteFileEx API call which is by nature asynchronous. At
first I wrote a simple console app to verify the concept, it worked fine.
Once integrated it in my main app, it became a lot slower. After
investigating a little, it seemed that the call became blocking instead of
non-blocking. The main difference between the console app and my main app is
that I put the calling method inside a worker thread. Does anybody know how
to fix this, make it async again ?

Kindest regards,

Tom

PS: My API call goes like this:

[DllImport("kernel32.dll", SetLastError=true)]
public unsafe static extern int WriteFileEx ( IntPtr hFile,
byte[] lpBuffer,
int nNumberOfBytesToWrite,
NativeOverlapped * lpOverlapped,
IOCompletionCallback lpCompletionRoutine);
 
W

Willy Denoyette [MVP]

Any reason why you aren't using FileStream.BeginRead for asynchronous IO?
I guess you are expecting an asynchronous WriteFile to be faster than a
synchronous one, however, I'm afraid this is not the case and it's not the
purpose of the API. This API is useful whenever you need to execute some
code after the call returns, but before the IO completes.
Anyway, without more details and some code that illustrates the problem,
it's hard to tell you why you see this different behavior.

Willy.
 
G

Guest

Hi Willy

('t is toch altijd weer plezant om hier landgenoten terug te vinden ;-) )

I tried implementing the class using the different solutions .NET offers,
but in the end the one using WriteFileEx was really the fastest (according to
my tests, please convince me that I'm wrong, cause I don't like it
either...). The speed I'm aiming at is btw 150Mb/s on a SCSI Raid System...

The way I implemented it:

1) The File handle:
mFHandle = SystemAPI.CreateFile(FileName,
FileAccess.Write,
FileShare.Write,
0,
FileMode.Create,
FILE_FLAG_OVERLAPPED|FILE_FLAG_SEQUENTIAL_SCAN |
FILE_FLAG_NO_BUFFERING,
IntPtr.Zero);

As you can see I use the NO_BUFFERING option, this resulted in a significant
speed improvement... but it requires you to take sector sizes into account
(that's why I don't like it...)

2) Using the FilePointer I give the file it's final size (rounded to the
next sector size), to prevent the system resizing after every write...

3) The actual write routine:
internal unsafe void WriteBufferToDisk(int Length)
{
IOCompletionCallback _IOComp = new IOCompletionCallback(IOComplete);

Overlapped _ovl = new Overlapped();

_ovl.OffsetLow=mWritePtr;
_ovl.AsyncResult = new myAsyncResult(mIndx);

mOvrlBuf[mIndx]=_ovl;

SystemAPI.WriteFileEx(mFHandle,mStreamingBuffers[mIndx].Bytes,Length,mOvrlBuf[mIndx].Pack(_IOComp),_IOComp);

mIndx++;
mActiveWrites++;

if(mIndx==8)
mIndx=0;

mWritePtr+=Length;
}

You'll see something weird with the mOvrlBuf[], I agree... basically I keep
an array of Overlapped close, so I can do upto 8 writes "at the same time"
without having to look for the overlapped when finished... the array is
organized as a FIFO... This way I make sure the overlapped is never destroyed
before it's not needed anymore

4) The completion routine

private unsafe void IOComplete(uint Lo, uint Hi, NativeOverlapped * NatOvrl)
{
Overlapped _ovl = Overlapped.Unpack(NatOvrl);

//int _indx = (int)_ovl.AsyncResult.AsyncState;

//I don't unpin, since my Streaming buffer handles this...
// UnPinBuffer(mGCHandles[_indx]);

Overlapped.Free(NatOvrl);
mActiveWrites--;
}

5) The pacing of the System happens using:

internal bool ReadyForData
{
get
{
return mActiveWrites<4;
}
}

I use a Async reset event at first.... but this "don't do it like this!"
method proved to be faster...

Anyway, as I said in my first post... once you put this in a thread it's not
async anymore. Any ideas ??

Kindest regards,

Tom

Willy Denoyette said:
Any reason why you aren't using FileStream.BeginRead for asynchronous IO?
I guess you are expecting an asynchronous WriteFile to be faster than a
synchronous one, however, I'm afraid this is not the case and it's not the
purpose of the API. This API is useful whenever you need to execute some
code after the call returns, but before the IO completes.
Anyway, without more details and some code that illustrates the problem,
it's hard to tell you why you see this different behavior.

Willy.

Tom Vandeplas said:
Hi all,

When using an asynchronous API call (in my case WriteFileEx) in a thread
it
seems to become blocking instead of non-blocking (see details below). Can
somebody explain me what is happening here...

Details:
I wrote a simple program that is able to do very fast async writes to
disk.
For that I used the WriteFileEx API call which is by nature asynchronous.
At
first I wrote a simple console app to verify the concept, it worked fine.
Once integrated it in my main app, it became a lot slower. After
investigating a little, it seemed that the call became blocking instead of
non-blocking. The main difference between the console app and my main app
is
that I put the calling method inside a worker thread. Does anybody know
how
to fix this, make it async again ?

Kindest regards,

Tom

PS: My API call goes like this:

[DllImport("kernel32.dll", SetLastError=true)]
public unsafe static extern int WriteFileEx ( IntPtr hFile,
byte[] lpBuffer,
int nNumberOfBytesToWrite,
NativeOverlapped * lpOverlapped,
IOCompletionCallback lpCompletionRoutine);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top