int do_test (int argc, char *argv[]) { struct aiocb64 cbs[10]; struct aiocb64 cbs_fsync; struct aiocb64 *cbp[10]; struct aiocb64 *cbp_fsync[1]; char buf[1000]; size_t cnt; int result = 0; /* Preparation. */ for (cnt = 0; cnt < 10; ++cnt) { cbs[cnt].aio_fildes = fd; cbs[cnt].aio_reqprio = 0; cbs[cnt].aio_buf = memset (&buf[cnt * 100], '0' + cnt, 100); cbs[cnt].aio_nbytes = 100; cbs[cnt].aio_offset = cnt * 100; cbs[cnt].aio_sigevent.sigev_notify = SIGEV_NONE; cbp[cnt] = &cbs[cnt]; } /* First a simple test. */ for (cnt = 10; cnt > 0; ) if (aio_write64 (cbp[--cnt]) < 0 && errno == ENOSYS) { error (0, 0, "no aio support in this configuration"); return 0; } /* Wait 'til the results are there. */ result |= do_wait (cbp, 10, 0); /* Test this. */ result |= test_file (buf, sizeof (buf), fd, "aio_write"); /* Read now as we've written it. */ memset (buf, '\0', sizeof (buf)); /* Issue the commands. */ for (cnt = 10; cnt > 0; ) { --cnt; cbp[cnt] = &cbs[cnt]; aio_read64 (cbp[cnt]); } /* Wait 'til the results are there. */ result |= do_wait (cbp, 10, 0); /* Test this. */ for (cnt = 0; cnt < 1000; ++cnt) if (buf[cnt] != '0' + (cnt / 100)) { result = 1; error (0, 0, "comparison failed for aio_read test"); break; } if (cnt == 1000) puts ("aio_read test ok"); /* Remove the test file contents. */ if (ftruncate64 (fd, 0) < 0) { error (0, errno, "ftruncate failed\n"); result = 1; } /* Test lio_listio. */ for (cnt = 0; cnt < 10; ++cnt) { cbs[cnt].aio_lio_opcode = LIO_WRITE; cbp[cnt] = &cbs[cnt]; } /* Issue the command. */ lio_listio64 (LIO_WAIT, cbp, 10, NULL); /* ...and immediately test it since we started it in wait mode. */ result |= test_file (buf, sizeof (buf), fd, "lio_listio (write)"); /* Test aio_fsync. */ cbs_fsync.aio_fildes = fd; cbs_fsync.aio_sigevent.sigev_notify = SIGEV_NONE; cbp_fsync[0] = &cbs_fsync; /* Remove the test file contents first. */ if (ftruncate64 (fd, 0) < 0) { error (0, errno, "ftruncate failed\n"); result = 1; } /* Write again. */ for (cnt = 10; cnt > 0; ) aio_write64 (cbp[--cnt]); if (aio_fsync64 (O_SYNC, &cbs_fsync) < 0) { error (0, errno, "aio_fsync failed\n"); result = 1; } result |= do_wait (cbp_fsync, 1, 0); /* ...and test since all data should be on disk now. */ result |= test_file (buf, sizeof (buf), fd, "aio_fsync (aio_write)"); /* Test aio_cancel. */ /* Remove the test file contents first. */ if (ftruncate64 (fd, 0) < 0) { error (0, errno, "ftruncate failed\n"); result = 1; } /* Write again. */ for (cnt = 10; cnt > 0; ) aio_write64 (cbp[--cnt]); /* Cancel all requests. */ if (aio_cancel64 (fd, NULL) == -1) printf ("aio_cancel64 (fd, NULL) cannot cancel anything\n"); result |= do_wait (cbp, 10, ECANCELED); /* Another test for aio_cancel. */ /* Remove the test file contents first. */ if (ftruncate64 (fd, 0) < 0) { error (0, errno, "ftruncate failed\n"); result = 1; } /* Write again. */ for (cnt = 10; cnt > 0; ) { --cnt; cbp[cnt] = &cbs[cnt]; aio_write64 (cbp[cnt]); } puts ("finished3"); /* Cancel all requests. */ for (cnt = 10; cnt > 0; ) if (aio_cancel64 (fd, cbp[--cnt]) == -1) /* This is not an error. The request can simply be finished. */ printf ("aio_cancel64 (fd, cbp[%Zd]) cannot be canceled\n", cnt); puts ("finished2"); result |= do_wait (cbp, 10, ECANCELED); puts ("finished"); return result; }
// // WriteFile() writes "bytes_to_write" bytes from the buffer into the file pointed to by // the file handle. // The call uses asynch I/O routine aio_write(). // // WriteFile() checks the Overlapped structure to determine the write offset in the file handle. // It also determines from the Overlapped structure if the I/O completion status should be // posted on the event queue or the completion queue associated with the file handle. // BOOL WriteFile(HANDLE file_handle, void *buffer, DWORD bytes_to_write, LPDWORD bytes_written, LPOVERLAPPED lpOverlapped) { struct File *filep; struct IOCQ *this_cq; struct aiocb64 *aiocbp; int i, free_index = -1; #ifdef IMMEDIATE_AIO_COMPLETION int aio_error_return; #endif filep = (struct File *)file_handle; // // At this point we have to decide whether to place this in the Completion queue // or the event queue. if ((ULONG_PTR) lpOverlapped->hEvent & 0x00000001) { // forcibly place this on the event queue even though a completion queue is associated // with the file. Well, thats what you asked for. this_cq = (IOCQ *) ((ULONG_PTR) lpOverlapped->hEvent ^ 0x1); } else this_cq = filep->iocq; if (this_cq == NULL) { cout << "event or completion queue not allocated " << endl; return (FALSE); } // First locate an empty slot in the queue. if (this_cq->last_freed != -1) { free_index = this_cq->last_freed; this_cq->last_freed = -1; // the slot is taken. Thanks } else // search for a free index. for (i = 0; i < this_cq->size; i++) { if (this_cq->aiocb_list[i] == NULL) { free_index = i; break; } } // either free_index holds the free index or there is no free space in the Q. if (free_index == -1) return (FALSE); aiocbp = &this_cq->element_list[free_index].aiocbp; aiocbp->aio_buf = buffer; aiocbp->aio_fildes = filep->fd; aiocbp->aio_nbytes = bytes_to_write; aiocbp->aio_offset = (off64_t) lpOverlapped->OffsetHigh; aiocbp->aio_offset = aiocbp->aio_offset << 32; aiocbp->aio_offset += lpOverlapped->Offset; aiocbp->aio_sigevent.sigev_notify = SIGEV_NONE; this_cq->element_list[free_index].data = lpOverlapped; this_cq->element_list[free_index].bytes_transferred = 0; this_cq->element_list[free_index].completion_key = filep->completion_key; *bytes_written = 0; #if defined(IOMTR_OS_LINUX) || defined(IOMTR_OS_OSX) || defined(IOMTR_OS_SOLARIS) if (aio_write64(&this_cq->element_list[free_index].aiocbp) < 0) #elif defined(IOMTR_OS_NETWARE) if (aio_write64(&this_cq->element_list[free_index].aiocbp, filep->type) < 0) #else #warning ===> WARNING: You have to do some coding here to get the port done! #endif { cout << "queuing for write failed with error " << errno << endl; // Note that we have not set aiocb_list[] with the correct pointers. // So, this slot will get grabbed in the next loop. SetLastError(errno); return (FALSE); } #ifdef IMMEDIATE_AIO_COMPLETION // Check if the aio_write completed successfully. if ((aio_error_return = aio_error64(&this_cq->element_list[free_index].aiocbp)) != EINPROGRESS) { *bytes_written = (DWORD) aio_return64(&this_cq->element_list[free_index].aiocbp); if ((long)*bytes_written < 0) { *bytes_written = 0; if (aio_error_return) SetLastError(aio_error_return); else if (errno) SetLastError(errno); return (FALSE); } else { SetLastError(0); return (TRUE); } } #endif else // aio_write is in progress. We have to set the aiocb_list[] to point correctly. this_cq->aiocb_list[free_index] = aiocbp; SetLastError(ERROR_IO_PENDING); return (FALSE); }