Merge branch 'master' into jwi-bcc64xsingletonwarning
[ACE_TAO.git] / ACE / examples / Reactor / Proactor / test_multiple_loops.cpp
blobf22cc5d6b57d3e4aeeffa138de2ce00a4be888e0
2 //=============================================================================
3 /**
4 * @file test_multiple_loops.cpp
6 * This example application shows how to write programs that
7 * combine the Proactor and Reactor event loops. This is possible
8 * only on WIN32 platform.
10 * @author Irfan Pyarali
12 //=============================================================================
15 #include "ace/Task.h"
16 #include "ace/Proactor.h"
17 #include "ace/WIN32_Proactor.h"
18 #include "ace/Atomic_Op.h"
19 #include "ace/OS_NS_unistd.h"
22 #if defined (ACE_HAS_WIN32_OVERLAPPED_IO)
24 /**
25 * @class Timeout_Handler
27 * @brief Generic timeout handler.
29 class Timeout_Handler : public ACE_Handler, public ACE_Event_Handler
31 public:
32 Timeout_Handler ()
36 // This is called by the Proactor. This is declared in ACE_Handler.
37 virtual void handle_time_out (const ACE_Time_Value &tv,
38 const void *arg)
40 // Print out when timeouts occur.
41 ACE_DEBUG ((LM_DEBUG, "(%t) %d timeout occurred for %s @ %d.\n",
42 ++count_,
43 (char *) arg,
44 tv.sec ()));
46 // Since there is only one thread that can do the timeouts in
47 // Reactor, lets keep the handle_timeout short for that
48 // thread.
49 if (ACE_OS::strcmp ((char *) arg, "Proactor") == 0)
50 // Sleep for a while
51 ACE_OS::sleep (1);
54 // This method is declared in ACE_Event_Handler.
55 virtual int handle_timeout (const ACE_Time_Value &tv,
56 const void *arg)
58 this->handle_time_out (tv, arg);
59 return 0;
62 private:
63 ACE_Atomic_Op <ACE_Thread_Mutex, int> count_;
66 class Worker : public ACE_Task <ACE_NULL_SYNCH>
68 public:
69 // Thread fuction.
70 int svc ()
72 ACE_DEBUG ((LM_DEBUG, "(%t) Worker started\n"));
74 // Handle events for 13 seconds.
75 ACE_Time_Value run_time (13);
77 // Try to become the owner
78 ACE_Reactor::instance ()->owner (ACE_Thread::self ());
80 if (ACE_Reactor::run_event_loop (run_time) == -1)
81 ACE_ERROR_RETURN ((LM_ERROR, "%p.\n", "Worker::svc"), -1);
82 else
83 ACE_DEBUG ((LM_DEBUG, "(%t) work complete\n"));
85 return 0;
89 int
90 ACE_TMAIN (int, ACE_TCHAR *[])
92 Timeout_Handler handler;
93 ACE_WIN32_Proactor win32_proactor (0, 1);
94 ACE_Proactor proactor (&win32_proactor, 0, 0);
96 ACE_Reactor::instance ()->register_handler (proactor.implementation ());
98 // Register a 2 second timer.
99 ACE_Time_Value foo_tv (2);
100 if (proactor.schedule_timer (handler,
101 (void *) "Proactor",
102 ACE_Time_Value::zero,
103 foo_tv) == -1)
104 ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "schedule_timer"), -1);
106 // Register a 3 second timer.
107 ACE_Time_Value bar_tv (3);
108 if (ACE_Reactor::instance ()->schedule_timer (&handler,
109 (void *) "Reactor",
110 ACE_Time_Value::zero,
111 bar_tv) == -1)
112 ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "schedule_timer"), -1);
114 Worker worker;
116 if (worker.activate (THR_NEW_LWP, 10) == -1)
117 ACE_ERROR_RETURN ((LM_ERROR, "%p.\n", "main"), -1);
119 ACE_Thread_Manager::instance ()->wait ();
121 // Remove from reactor
122 ACE_Reactor::instance ()->remove_handler (&proactor,
123 ACE_Event_Handler::DONT_CALL);
125 return 0;
127 #else
129 ACE_TMAIN (int, ACE_TCHAR *[])
131 return 0;
133 #endif /* ACE_HAS_WIN32_OVERLAPPED_IO */