home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Misc / CBMDevKit3.dms / CBMDevKit3.adf / AsynchIO / async.txt < prev    next >
Encoding:
Text File  |  1993-11-17  |  3.0 KB  |  72 lines

  1.  
  2. Fast AmigaDOS IO
  3. ----------------
  4.  
  5. by Martin Taillefer
  6.  
  7. Reading and writing data is crucial to most applications and is in many cases a
  8. major bottleneck. Using the AmigaDOS' sophisticated file system architecture
  9. can help reduce, and sometimes eliminate, the time spent waiting for IO to
  10. complete. This article presents six small routines that can greatly improve an
  11. application's IO performance.
  12.  
  13. Normally, an application processes a file in a manner similar to the following:
  14.  
  15.   1 - Open the file
  16.  
  17.   2 - Read some data
  18.  
  19.   3 - Process data just read
  20.  
  21.   4 - Repeat steps 2 and 3 until all data is processed
  22.  
  23.   5 - Close file
  24.  
  25. Although the above sequence works fine, it doesn't make full use of the Amiga's
  26. multitasking abilities. Step 2 in the above can become a serious bottleneck.
  27. Whenever the application needs some data by using the DOS Read() function,
  28. AmigaDOS has to put that task to sleep, and initiate a request to the file
  29. system to have it fetch the data. The file system then starts up the disk
  30. hardware and reads the data. Once the data is read, the application is woken
  31. up and can start processing the data just read.
  32.  
  33. The point to note in the above paragraph is that when the file system is
  34. reading data from disk, the application is asleep. Wouldn't it be nice if
  35. the application could keep running while data is being fetched for it?
  36.  
  37. Most Amiga hard drives make use of DMA (Direct Memory Access). DMA enables
  38. a hard drive to transfer data to memory _at the same time_ as the CPU does
  39. some work. This parallelism is what makes the set of accompanying routines
  40. so efficient. They exploit the fact that data can be transfered to memory while
  41. the application is busy processing other data.
  42.  
  43. Using the asynchronous IO routines, an application's IO happens like this:
  44.  
  45.   1 - Open the file, ask the file system to start reading ahead
  46.  
  47.   2 - Read some data, ask the file system to read more data
  48.  
  49.   3 - Process data
  50.  
  51.   4 - Repeat steps 2 and 3 until all data is processed
  52.  
  53.   5 - Close file
  54.  
  55. Immediately after opening the file, a request is sent to the file system to get
  56. it reading data in the background. By the time the application gets around to
  57. reading the first byte of data, it is likely already in memory. That means the
  58. application doesn't need to wait and can start processing the data. As soon as
  59. the application starts processing data from the file, a second request is sent
  60. out to the file system to fill up a second buffer. Once the application is done
  61. processing the first buffer, it starts processing the second one. When this
  62. happens, the file system starts filling up the first buffer again with new
  63. data. This process continues until all data has been read.
  64.  
  65. The whole technique is known as "double-buffered asynchronous IO" since it uses
  66. two buffers, and happens in the background (asynchronously).
  67.  
  68. The set of functions presented below offer high-performance IO using the
  69. technique described above. The interface is very similar to standard
  70. AmigaDOS files. These routines enable full asynchronous read/write of
  71. any file.
  72.