Merge pull request #2254 from esohns/fstat_size_64_bits
[ACE_TAO.git] / ACE / examples / Reactor / WFMO_Reactor / APC.cpp
blob00d21aa59c55bbd78e698218dd61b9e6e4f9ee95
2 //=============================================================================
3 /**
4 * @file APC.cpp
6 * Tests the WFMO_Reactor's ability to handle regular APC
7 * notifications.
9 * @author Irfan Pyarali <irfan@cs.wustl.edu>
11 //=============================================================================
13 #include "ace/OS_main.h"
15 #if defined (ACE_WIN32) && _WIN32_WINNT >= 0x400
17 #include "ace/Reactor.h"
18 #include "ace/Auto_Event.h"
19 #include "ace/Log_Msg.h"
21 class Event_Handler : public ACE_Event_Handler
23 public:
24 int handle_signal (int signum, siginfo_t * = 0, ucontext_t * = 0);
26 int handle_timeout (const ACE_Time_Value &tv, const void *arg = 0);
28 ACE_Auto_Event handle_;
29 int iterations_;
32 static Event_Handler *global_event_handler;
34 static void WINAPI
35 apc_callback (DWORD)
37 ACE_DEBUG ((LM_DEBUG,
38 "(%t) apc occurred @ %T\n"));
40 global_event_handler->handle_.signal ();
43 void
44 queue_apc ()
46 DWORD result = ::QueueUserAPC (reinterpret_cast<PAPCFUNC> (&apc_callback),
47 // pointer to APC function
48 ::GetCurrentThread (), // handle to the thread
49 0); // argument for the APC function
50 if (result == FALSE)
51 ACE_OS::exit (-1);
54 int
55 Event_Handler::handle_signal (int, siginfo_t *, ucontext_t *)
57 --this->iterations_;
59 if (this->iterations_ == 0)
61 ACE_Reactor::instance ()->remove_handler (this->handle_.handle (),
62 ACE_Event_Handler::DONT_CALL);
63 ACE_Reactor::end_event_loop ();
66 return 0;
69 int
70 Event_Handler::handle_timeout (const ACE_Time_Value &,
71 const void *)
73 ACE_DEBUG ((LM_DEBUG,
74 "(%t) timeout occurred @ %T\n"));
75 queue_apc ();
76 return 0;
79 int
80 ACE_TMAIN (int, ACE_TCHAR *[])
82 Event_Handler event_handler;
83 event_handler.iterations_ = 5;
84 global_event_handler = &event_handler;
86 int result = ACE_Reactor::instance ()->register_handler (&event_handler,
87 event_handler.handle_.handle ());
88 ACE_TEST_ASSERT (result == 0);
90 ACE_Time_Value timeout (2);
91 result = ACE_Reactor::instance ()->schedule_timer (&event_handler,
93 timeout,
94 timeout);
95 ACE_TEST_ASSERT (result != -1);
97 ACE_Reactor::run_alertable_event_loop ();
99 ACE_Reactor::instance ()->cancel_timer(&event_handler);
101 return 0;
103 #else /* !ACE_WIN32 */
105 ACE_TMAIN (int, ACE_TCHAR *[])
107 return 0;
109 #endif /* ACE_WIN32 */