RE: When do I need to pin?



Hello Nick

The part where you say 'CLR may do
it for us' is a bit disturbing. Will it pin for us or not?

It's a very sharp and acute observation. Thank you for reminding me. I
should have explained it in the first reply.

I said 'may' because it depends on whether it is a synchronous or a
asynchronous I/O operation:

Synchronous and Asynchronous I/O
http://msdn.microsoft.com/en-us/library/aa365683(VS.85).aspx

When FILE_FLAG_OVERLAPPED is not specified in the dwFlagsAndAttributes
parameter of the CreateFile call, it's a synchronous IO. CLR will help us
to pin the buffer.

But when we have to do asynchronous IO operations (FILE_FLAG_OVERLAPPED is
specified in dwFlagsAndAttributes), we "MAY" need to manually pin the
buffer because you pass the address to the buffer down and the API returns
before it has completed using the buffer. Then, when the I/O completes, you
unpin the buffer manually. I say "MAY" because CLR provides basic interop
with the native async IO including automatic pinning the user buffers. The
related CLR class is called Overlapped. For the said interop to work you
have to invoke Overlapped.Pack (or UnsafePack) method. The
Overlapped.Pack(IoCompletionCallback iocb, Object data) overload expects
one to pass "data" as either byte[] or object[] type. If it's byte[] then
the Pack() method will pin that byte array. Otherwise it will pin all
members of the object[] array. The data gets un-pinned when IO completes:

<quote>
Overlapped.Pack Method (IOCompletionCallback)
http://msdn.microsoft.com/en-us/library/1d949965.aspx
The caller is responsible for pinning the buffer. If the application domain
is unloaded, however, the handle to the pinned buffer is destroyed and the
buffer is released, leaving the I/O operation to write to the freed
address. For this reason, it is better to use the
Pack(IOCompletionCallback, Object) method overload, in which the runtime
pins the buffer.

Overlapped.Pack Method (IOCompletionCallback, Object)
http://msdn.microsoft.com/en-us/library/a059s071.aspx
The runtime pins the buffer or buffers specified in userData for the
duration of the I/O operation. If the application domain is unloaded, the
runtime keeps the memory pinned until the I/O operation completes.
</quote>

If you use the built-in BeginXxx/EndXxx methods to do async I/O, then these
methods do the pinning/unpinning for you automatically.

For more readings:

Asynchronous Device Operations
http://msdn.microsoft.com/en-us/magazine/cc163415.aspx

Pop Quiz Pins: Answers by Greg Young [MVP]
http://codebetter.com/blogs/gregyoung/archive/2007/08/16/pop-quiz-pins-answe
rs.aspx

Another situation in which manual pinning an object is required is that we
may want to continue pinning the object after the native function returns:

(quoted from http://msdn.microsoft.com/en-us/magazine/cc163910.aspx)
When the runtime marshaler sees that your code is passing to native code a
reference to a managed reference object, it automatically pins the object.
When the native function returns, the marshaled object is automatically
unpinned. Automatic pinning is very convenient, but it raises another
question. What happens if the native function caches the pointer for use
later on? When the function returns, won't the collector be free to move
the object? The answer is yes, and the solution for your code in such a
situation is to manually pin the object using the
System.Runtime.InteropServices.GCHandle type.

I hope that the above info is easy to understand. If you find anything
unclear, let me know and I will give more explanation or examples.

Regards,
Jialiang Ge (jialge@xxxxxxxxxxxxxxxxxxxx, remove 'online.')
Microsoft Online Community Support

=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@xxxxxxxxxxxxxx

This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================

.



Relevant Pages

  • Re: mixing analog and digital in SoC
    ... analog behaviour can be modelled ... // an unknown value is being conveyed. ... module analog_out(value, pin); ... buffer = }; ...
    (comp.lang.verilog)
  • Re: Making of an Audio TEE filter with Infinite number of input pins
    ... I calculate the buffer size each pin should fill with: ... I first calculate the output buffer size. ... input samples and determine the output sample time like this: ... pin, i deliver the BeginFlush call downstream, then I return S_FALSE ...
    (microsoft.public.win32.programmer.directx.audio)
  • Re: Making of an Audio TEE filter with Infinite number of input pins
    ... I'm filling this circular buffer on every pin, ... I keep the timings from the ... The DirectSound renderer defaults to 1000ms for it's buffer size, ...
    (microsoft.public.win32.programmer.directx.audio)
  • Re: Making of an Audio TEE filter with Infinite number of input pins
    ... The easy method is to introduce an additional circular buffer in the ... filter, one for each pin. ... It would probably make sense here to use a separate output thread yes. ...
    (microsoft.public.win32.programmer.directx.audio)
  • pin a generic collection
    ... pinning it would be something like: ... What I'm curious about, though, is whether or not it's possible to pin a generic collection in a similar manner. ... The app I'm working on will be moving REALLY big buffers between managed and unmanaged memory, and creating a copy of the collection ) would make a copy of the buffer, and I'd rather not double my data usage, if I can avoid it. ...
    (microsoft.public.dotnet.languages.vc)

Loading