[Contents] [Index] [Help] [Retrace] [Browse <] [Browse >]

You end device access by reversing the steps you took to access it.  This
means you close the device, deallocate the I/O request memory and delete
the message port.  In that order!

Closing a device is how you tell Exec that you are finished using a device
and any associated resources.  This can result in housecleaning being
performed by the device.  However, before you close a device, you might
have to do some housecleaning of your own.

A device is closed by calling the CloseDevice() function. The
CloseDevice() function does not return a value. It has this format:

  CloseDevice(IORequest)

where iorequest is the i/o request used to open the device.

You should not close a device while there are outstanding I/O requests,
otherwise you can cause major and minor problems.  Let's begin with the
minor problem: memory.  If an I/O request is outstanding at the time you
close a device, you won't be able to reclaim the memory you allocated for
it.

The major problem: the device will try to respond to the I/O request.  If
the device tries to respond to an I/O request, and you've deleted the
message port (which is covered below), you will probably crash the system.

One solution would be to wait until all I/O requests you sent to the
device return.  This is not always practical if you've sent a few requests
and the user wants to exit the application immediately.

In that case, the only solution is to abort and remove any outstanding I/O
requests. You do this with the functions abortio() and waitio().  they
must be used together for cleaning up. AbortIO() will abort an I/O
request, but will not prevent a reply message from being sent to the
application requesting the abort. waitio() will wait for an i/o request to
complete and remove it from the message port.  This is why they must be
used together.

   Be Careful With AbortIO().
   --------------------------
   Do not abortio() an i/o request which has not been sent to a device.
   If you do, you may crash the system.

After the device is closed, you must deallocate the I/O request memory.
The exact method you use depends on how you allocated the memory in the
first place.  For allocmem() you call freemem(), for createextio() you
call deleteextio(), and for createiorequest() you call deleteiorequest().
If you allocated the I/O request memory at compile time, you naturally
have nothing to free.

Finally, you must delete the message port you created.  You delete the
message port by calling deletemsgport() if you used createmsgport(), or
deleteport() if you used createport().

Here is the checklist for gracefully exiting:

   1. Abort any outstanding I/O requests with abortio()

   2. Wait for the completion of any outstanding or aborted I/O requests
      with waitio().

   3. Close the device with CloseDevice().

   4. Release the I/O request memory with either deleteiorequest(),
      deleteextio() or freemem() (as appropriate).

   5. Delete the message port with deletemsgport() or deleteport().