2 //=============================================================================
4 * @file test_aiocb_ace.cpp
6 * This program helps you to test the <aio_*> calls on a
9 * Before running this test, make sure the platform can
10 * support POSIX <aio_> calls, using
11 * ACE_ROOT/tests/Aio_Platform_Test.
13 * This program tests the AIOCB (AIO Control Blocks) based
14 * completion approach which uses <aio_suspend> for completion
17 * If this test is successful, ACE_POSIX_AIOCB_PROACTOR
18 * can be used on this platform.
24 * @author Alexander Babu Arulanthu <alex@cs.wustl.edu>
26 //=============================================================================
30 #include "ace/Log_Msg.h"
31 #include "ace/os_include/os_aio.h"
32 #include "ace/OS_NS_string.h"
37 /// Default constructor.
40 /// Initting the output file and the buffer.
43 /// Doing the testing stuff.
49 /// Output file descriptor.
52 /// For writing to the file.
53 struct aiocb
*aiocb_write_
;
55 /// Reading stuff from the file.
56 struct aiocb
*aiocb_read_
;
58 /// The buffer to be written to the out_fd.
61 /// The buffer to be read back from the file.
71 ACE_NEW (this->aiocb_write_
,
73 ACE_NEW (this->aiocb_read_
,
77 Test_Aio::~Test_Aio ()
85 // Init the output file and init the buffer.
89 // Open the output file.
90 this->out_fd_
= ACE_OS::open ("test_aio.log",
91 O_RDWR
| O_CREAT
| O_TRUNC
,
93 if (this->out_fd_
== 0)
94 ACE_ERROR_RETURN ((LM_ERROR
,
95 "Error: Opening file\n"),
99 this->buffer_write_
= ACE::strnew ("Welcome to the world of AIO... AIO Rules !!!");
100 ACE_DEBUG ((LM_DEBUG
,
102 this->buffer_write_
));
104 // Allocate memory for the read buffer.
105 ACE_NEW_RETURN (this->buffer_read_
,
106 char [ACE_OS::strlen (this->buffer_write_
)],
112 // Set the necessary things for the AIO stuff.
113 // Write the buffer asynchly.hmm Disable signals.
114 // Go on aio_suspend. Wait for completion.
115 // Print out the result.
119 // = Write to the file.
122 this->aiocb_write_
->aio_fildes
= this->out_fd_
;
123 this->aiocb_write_
->aio_offset
= 0;
124 this->aiocb_write_
->aio_buf
= this->buffer_write_
;
125 this->aiocb_write_
->aio_nbytes
= ACE_OS::strlen (this->buffer_write_
);
126 this->aiocb_write_
->aio_reqprio
= 0;
127 this->aiocb_write_
->aio_sigevent
.sigev_notify
= SIGEV_NONE
;
128 //this->this->aiocb_.aio_sigevent.sigev_signo = SIGRTMAX;
129 this->aiocb_write_
->aio_sigevent
.sigev_value
.sival_ptr
=
130 (void *) this->aiocb_write_
;
132 // Fire off the aio write.
133 if (aio_write (this->aiocb_write_
) != 0)
134 ACE_ERROR_RETURN ((LM_ERROR
,
139 // = Read from that file.
142 this->aiocb_read_
->aio_fildes
= this->out_fd_
;
143 this->aiocb_read_
->aio_offset
= 0;
144 this->aiocb_read_
->aio_buf
= this->buffer_read_
;
145 this->aiocb_read_
->aio_nbytes
= ACE_OS::strlen (this->buffer_write_
);
146 this->aiocb_read_
->aio_reqprio
= 0;
147 this->aiocb_read_
->aio_sigevent
.sigev_notify
= SIGEV_NONE
;
148 //this->this->aiocb_.aio_sigevent.sigev_signo = SIGRTMAX;
149 this->aiocb_read_
->aio_sigevent
.sigev_value
.sival_ptr
=
150 (void *) this->aiocb_read_
;
152 // Fire off the aio write. If it doesnt get queued, carry on to get
153 // the completion for the first one.
154 if (aio_read (this->aiocb_read_
) < 0)
155 ACE_ERROR_RETURN ((LM_ERROR
,
160 // Wait for the completion on aio_suspend.
161 struct aiocb
*list_aiocb
[2];
162 list_aiocb
[0] = this->aiocb_write_
;
163 list_aiocb
[1] = this->aiocb_read_
;
165 // Do suspend till all the aiocbs in the list are done.
168 while (to_finish
> 0)
170 return_val
= aio_suspend (list_aiocb
,
173 ACE_DEBUG ((LM_DEBUG
,
174 "Result of <aio_suspend> : %d\n",
177 // Analyze return and error values.
180 if (aio_error (list_aiocb
[1]) != EINPROGRESS
)
182 if (aio_return (list_aiocb
[1]) == -1)
183 ACE_ERROR_RETURN ((LM_ERROR
,
185 "aio_return, item 1"),
189 // Successful. Remember we have one less thing to finish.
195 ACE_DEBUG ((LM_DEBUG
,
196 "aio_error says aio 1 is in progress\n"));
199 if (aio_error (list_aiocb
[0]) != EINPROGRESS
)
201 if (aio_return (list_aiocb
[0]) == -1)
202 ACE_ERROR_RETURN ((LM_ERROR
,
204 "aio_return, item 0"),
208 // Successful. Store the pointer somewhere and bump the
209 // read entry up to the front, if it is still not done.
211 list_aiocb
[0] = this->aiocb_read_
;
215 ACE_DEBUG ((LM_DEBUG
,
216 "aio_error says aio 0 is in progress\n"));
219 ACE_DEBUG ((LM_DEBUG
,
220 "Both the AIO operations done.\n"
221 "The buffer is : %s\n",
222 this->buffer_read_
));
228 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
230 ACE_UNUSED_ARG (argc
);
231 ACE_UNUSED_ARG (argv
);
235 if (test_aio
.init () != 0)
236 ACE_ERROR_RETURN ((LM_ERROR
,
237 "AIOCB test failed:\n"
238 "ACE_POSIX_AIOCB_PROACTOR may not work in this platform\n"),
241 if (test_aio
.do_aio () != 0)
242 ACE_ERROR_RETURN ((LM_ERROR
,
243 "AIOCB test failed:\n"
244 "ACE_POSIX_AIOCB_PROACTOR may not work in this platform\n"),
247 ACE_DEBUG ((LM_DEBUG
,
248 "AIOCB test successful:\n"
249 "ACE_POSIX_AIOCB_PROACTOR should work in this platform\n"));