M
Morgan Cheng
Asynchronous I/O is said to be using native completion port. However,
i checked rotor2.0 code, it seems not
in stream.cs. BeginRead is defined in below way. In my understanding,
If BeginInvoke is used, the task is queued; and when there is free
worker thread in ThreadPool, the task is performed in specified worker
thread. Then, Asynchronous I/O doesn't save thread in ThreadPool. Is
my understanding correct?
[HostProtection(ExternalThreading=true)]
public virtual IAsyncResult BeginRead(byte[] buffer, int
offset, int count, AsyncCallback callback, Object state)
{
if (!CanRead) __Error.ReadNotSupported();
// Increment the count to account for this async operation
BCLDebug.Assert(_asyncActiveCount >= 1, "ref counting
mismatch, possible race in the code");
Interlocked.Increment(ref _asyncActiveCount);
ReadDelegate d = new ReadDelegate(Read);
// To avoid a race with a stream's position pointer &
generating race
// conditions with internal buffer indexes in our own
streams that
// don't natively support async IO operations when there
are multiple
// async requests outstanding, we will block the
application's main
// thread if it does a second IO request until the first
one completes.
if (_asyncActiveEvent == null) {
lock(this) {
if (_asyncActiveEvent == null)
_asyncActiveEvent = new AutoResetEvent(true);
}
}
bool r = _asyncActiveEvent.WaitOne();
BCLDebug.Assert(r, "AutoResetEvent didn't get a signal
when we called WaitOne!");
BCLDebug.Assert(_readDelegate == null && _writeDelegate ==
null, "Expected no other readers or writers!");
// Set delegate before we call BeginInvoke, to avoid a
race
_readDelegate = d;
IAsyncResult asyncResult = d.BeginInvoke(buffer, offset,
count, callback, state);
return asyncResult;
}
i checked rotor2.0 code, it seems not
in stream.cs. BeginRead is defined in below way. In my understanding,
If BeginInvoke is used, the task is queued; and when there is free
worker thread in ThreadPool, the task is performed in specified worker
thread. Then, Asynchronous I/O doesn't save thread in ThreadPool. Is
my understanding correct?
[HostProtection(ExternalThreading=true)]
public virtual IAsyncResult BeginRead(byte[] buffer, int
offset, int count, AsyncCallback callback, Object state)
{
if (!CanRead) __Error.ReadNotSupported();
// Increment the count to account for this async operation
BCLDebug.Assert(_asyncActiveCount >= 1, "ref counting
mismatch, possible race in the code");
Interlocked.Increment(ref _asyncActiveCount);
ReadDelegate d = new ReadDelegate(Read);
// To avoid a race with a stream's position pointer &
generating race
// conditions with internal buffer indexes in our own
streams that
// don't natively support async IO operations when there
are multiple
// async requests outstanding, we will block the
application's main
// thread if it does a second IO request until the first
one completes.
if (_asyncActiveEvent == null) {
lock(this) {
if (_asyncActiveEvent == null)
_asyncActiveEvent = new AutoResetEvent(true);
}
}
bool r = _asyncActiveEvent.WaitOne();
BCLDebug.Assert(r, "AutoResetEvent didn't get a signal
when we called WaitOne!");
BCLDebug.Assert(_readDelegate == null && _writeDelegate ==
null, "Expected no other readers or writers!");
// Set delegate before we call BeginInvoke, to avoid a
race
_readDelegate = d;
IAsyncResult asyncResult = d.BeginInvoke(buffer, offset,
count, callback, state);
return asyncResult;
}