2 //=============================================================================
4 * @file test_timeout.cpp
6 * This example application shows how to write event loops that
7 * handle events for some fixed amount of time. Note that any
8 * thread in the Proactor thread pool can call back the handler. On
9 * POSIX4 systems, this test works only with POSIX_SIG_Proactor,
10 * which can work with multiple threads.
12 * @author Irfan Pyarali and Alexander Babu Arulanthu
14 //=============================================================================
16 #include "ace/Proactor.h"
18 #include "ace/Atomic_Op.h"
19 #include "ace/OS_NS_sys_time.h"
20 #include "ace/OS_NS_unistd.h"
21 #include "ace/OS_main.h"
23 #if defined (ACE_HAS_WIN32_OVERLAPPED_IO) || defined (ACE_HAS_AIO_CALLS)
24 // This only works on Win32 platforms and on Unix platforms supporting
28 * @class Timeout_Handler
30 * @brief Generic timeout handler.
32 class Timeout_Handler
: public ACE_Handler
36 : start_time_ (ACE_OS::gettimeofday ())
40 virtual void handle_time_out (const ACE_Time_Value
&tv
,
43 // Print out when timeouts occur.
44 ACE_DEBUG ((LM_DEBUG
, "(%t) %d timeout occurred for %s @ %d.\n",
47 (tv
- this->start_time_
).sec ()));
54 /// Number of the timer event.
55 ACE_Atomic_Op
<ACE_SYNCH_MUTEX
, int> count_
;
57 /// Starting time of the test.
58 ACE_Time_Value start_time_
;
61 class Worker
: public ACE_Task
<ACE_NULL_SYNCH
>
66 // Handle events for 13 seconds.
67 ACE_Time_Value
run_time (13);
69 ACE_DEBUG ((LM_DEBUG
, "(%t):Starting svc routine\n"));
71 if (ACE_Proactor::run_event_loop(run_time
) == -1)
72 ACE_ERROR_RETURN ((LM_ERROR
, "(%t):%p.\n", "Worker::svc"), -1);
74 ACE_DEBUG ((LM_DEBUG
, "(%t) work complete\n"));
81 ACE_TMAIN (int, ACE_TCHAR
*[])
83 Timeout_Handler handler
;
85 // Register a 2 second timer.
86 ACE_Time_Value
foo_tv (2);
87 if (ACE_Proactor::instance ()->schedule_timer (handler
,
91 ACE_ERROR_RETURN ((LM_ERROR
, "%p\n", "schedule_timer"), -1);
93 // Register a 3 second timer.
94 ACE_Time_Value
bar_tv (3);
95 if (ACE_Proactor::instance ()->schedule_timer (handler
,
99 ACE_ERROR_RETURN ((LM_ERROR
, "%p\n", "schedule_timer"), -1);
103 if (worker
.activate (THR_NEW_LWP
, 10) == -1)
104 ACE_ERROR_RETURN ((LM_ERROR
, "%p.\n", "main"), -1);
106 ACE_Thread_Manager::instance ()->wait ();
111 #else /* ACE_HAS_WIN32_OVERLAPPED_IO || ACE_HAS_AIO_CALLS */
114 ACE_TMAIN (int, ACE_TCHAR
*[])
116 ACE_DEBUG ((LM_DEBUG
,
117 "This example is multithreaded version of test_timeout_st.cpp\n"
118 "This doesnt work on this platform !!!\n"));
122 #endif /* ACE_HAS_WIN32_OVERLAPPED_IO || ACE_HAS_AIO_CALLS */