1 //=============================================================================
3 * @file post_completions.cpp
5 * This program demonstrates how to post fake completions to The
6 * Proactor. It also shows the how to specify the particular
7 * real-time signals to post completions. The Real-time signal
8 * based completion strategy is implemented with
9 * ACE_POSIX_SIG_PROACTOR.
10 * (So, it can be used only if both ACE_HAS_AIO_CALLS and
11 * ACE_HAS_POSIX_REALTIME_SIGNALS are defined.)
12 * Since it is faking results, you have to pay by knowing and
13 * using platform-specific implementation objects for Asynchronous
15 * This example shows using an arbitrary result class for faking
16 * completions. You can also use the predefined Result classes for
17 * faking. The factory methods in the Proactor class create the
22 * @author Alexander Babu Arulanthu <alex@cs.wustl.edu>
24 //=============================================================================
27 #include "ace/OS_NS_unistd.h"
28 #include "ace/OS_main.h"
29 #include "ace/Proactor.h"
31 #include "ace/WIN32_Proactor.h"
32 #include "ace/POSIX_Proactor.h"
33 #include "ace/Atomic_Op.h"
34 #include "ace/Thread_Mutex.h"
36 // Keep track of how many completions are still expected.
37 static ACE_Atomic_Op
<ACE_SYNCH_MUTEX
, size_t> Completions_To_Go
;
40 #if defined (ACE_HAS_WIN32_OVERLAPPED_IO) || defined (ACE_HAS_AIO_CALLS)
41 // This only works on Win32 platforms and on Unix platforms supporting
44 #if defined (ACE_HAS_AIO_CALLS)
45 #define RESULT_CLASS ACE_POSIX_Asynch_Result
46 #elif defined (ACE_HAS_WIN32_OVERLAPPED_IO)
47 #define RESULT_CLASS ACE_WIN32_Asynch_Result
48 #endif /* ACE_HAS_AIO_CALLS */
53 * @brief Result Object that we will post to the Proactor.
55 class My_Result
: public RESULT_CLASS
58 My_Result (ACE_Handler
&handler
,
61 size_t sequence_number
)
62 : RESULT_CLASS (handler
.proxy (),
69 sequence_number_ (sequence_number
)
78 * This is the method that will be called by the Proactor for
79 * dispatching the completion. This method generally calls one of
80 * the call back hood methods defined in the ACE_Handler
81 * class. But, we will just handle the completions here.
83 void complete (size_t,
85 const void *completion_key
,
88 this->success_
= success
;
89 this->completion_key_
= completion_key
;
92 size_t to_go
= --Completions_To_Go
;
94 // Print the completion details.
96 "(%t) Completion sequence number %d, success : %d, error : %d, signal_number : %d, %u more to go\n",
97 this->sequence_number_
,
100 this->signal_number (),
103 // Sleep for a while.
108 /// Sequence number for the result object.
109 size_t sequence_number_
;
115 * @brief Handler class for faked completions.
117 class My_Handler
: public ACE_Handler
124 virtual ~My_Handler () {}
130 * @brief Contains thread functions which execute event loops. Each
131 * thread waits for a different signal.
133 class My_Task
: public ACE_Task
<ACE_NULL_SYNCH
>
140 virtual ~My_Task () {}
142 //FUZZ: disable check_for_lack_ACE_OS
143 int open (void *proactor
)
145 //FUZZ: enable check_for_lack_ACE_OS
146 // Store the proactor.
147 this->proactor_
= (ACE_Proactor
*) proactor
;
149 // Activate the Task.
150 this->activate (THR_NEW_LWP
, 5);
156 // Handle events for 13 seconds.
157 ACE_Time_Value
run_time (13);
159 ACE_DEBUG ((LM_DEBUG
, "(%t):Starting svc routine\n"));
161 if (this->proactor_
->handle_events (run_time
) == -1)
162 ACE_ERROR_RETURN ((LM_ERROR
, "(%t):%p.\n", "Worker::svc"), -1);
164 ACE_DEBUG ((LM_DEBUG
, "(%t) work complete\n"));
170 /// Proactor for this task.
171 ACE_Proactor
*proactor_
;
175 ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
177 ACE_UNUSED_ARG (argc
);
178 ACE_UNUSED_ARG (argv
);
180 ACE_DEBUG ((LM_DEBUG
,
181 "(%P | %t):Test starts\n"));
183 // = Get two POSIX_SIG_Proactors, one with SIGRTMIN and one with
186 ACE_Proactor proactor1
;
187 // Proactor1. SIGRTMIN Proactor. (default).
189 // = Proactor2. SIGRTMAX Proactor.
190 #if defined (ACE_HAS_AIO_CALLS) && defined (ACE_HAS_POSIX_REALTIME_SIGNALS)
192 ACE_DEBUG ((LM_DEBUG
, "Using ACE_POSIX_SIG_Proactor\n"));
195 // Signal set that we want to mask.
197 // Clear the signal set.
198 if (ACE_OS::sigemptyset (&signal_set
) == -1)
199 ACE_ERROR_RETURN ((LM_ERROR
,
201 "sigemptyset failed"),
204 // Add the SIGRTMAX to the signal set.
205 if (ACE_OS::sigaddset (&signal_set
, ACE_SIGRTMAX
) == -1)
206 ACE_ERROR_RETURN ((LM_ERROR
,
211 // Make the POSIX Proactor.
212 ACE_POSIX_SIG_Proactor
posix_proactor (signal_set
);
213 // Get the Proactor interface out of it.
214 ACE_Proactor
proactor2 (&posix_proactor
);
215 #else /* ACE_HAS_AIO_CALLS && ACE_HAS_POSIX_REALTIME_SIGNALS */
216 ACE_Proactor proactor2
;
217 #endif /* ACE_HAS_AIO_CALLS && ACE_HAS_POSIX_REALTIME_SIGNALS */
219 // = Create Tasks. One pool of threads to handle completions on
220 // SIGRTMIN and the other one to handle completions on SIGRTMAX.
221 My_Task task1
, task2
;
222 task1
.open (&proactor1
);
223 task2
.open (&proactor2
);
225 // Handler for completions.
228 // = Create a few MyResult objects and post them to Proactor.
229 const size_t NrCompletions (10);
230 My_Result
*result_objects
[NrCompletions
];
231 int signal_number
= ACE_SIGRTMAX
;
234 Completions_To_Go
= NrCompletions
;
237 for (ri
= 0; ri
< NrCompletions
; ri
++)
239 // Use RTMIN and RTMAX proactor alternatively, to post
242 signal_number
= ACE_SIGRTMIN
;
244 signal_number
= ACE_SIGRTMAX
;
245 // Create the result.
246 ACE_NEW_RETURN (result_objects
[ri
],
254 // Post all the result objects.
255 ACE_Proactor
*proactor
;
256 for (ri
= 0; ri
< NrCompletions
; ri
++)
258 // Use RTMIN and RTMAX Proactor alternatively, to post
261 proactor
= &proactor1
;
263 proactor
= &proactor2
;
264 if (result_objects
[ri
]->post_completion (proactor
->implementation ())
266 ACE_ERROR_RETURN ((LM_ERROR
,
271 ACE_Thread_Manager::instance ()->wait ();
274 size_t to_go
= Completions_To_Go
.value ();
275 if (size_t (0) != to_go
)
277 ACE_ERROR ((LM_ERROR
,
278 "Fail! Expected all completions to finish but %u to go\n",
283 ACE_DEBUG ((LM_DEBUG
,
284 "(%P | %t):Test ends\n"));
288 #else /* ACE_HAS_WIN32_OVERLAPPED_IO || ACE_HAS_AIO_CALLS */
291 ACE_TMAIN (int, ACE_TCHAR
*[])
293 ACE_DEBUG ((LM_DEBUG
,
294 "This example cannot work with AIOCB_Proactor.\n"));
298 #endif /* ACE_HAS_WIN32_OVERLAPPED_IO || ACE_HAS_AIO_CALLS */