Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / examples / APG / Timers / Upcall.cpp
blob4a783112d10512d70d2820876b0f0929cec90167
1 #include "ace/OS_NS_sys_time.h"
2 #include "ace/Log_Msg.h"
3 #include "Upcall.h"
4 #include "PTimerDispatcher.h"
6 // Listing 2 code/ch20
7 // The signature of this method changed at ACE 5.4. The 'recurring_timer'
8 // parameter was added.
9 int
10 UpcallHandler::timeout (PTimerQueue &,
11 PCB *handler,
12 const void *arg,
13 int /* recurring_timer */,
14 const ACE_Time_Value &)
16 ACE_TRACE ("UpcallHandler::timeout");
18 return (*handler).handleEvent (arg);
21 #if 0
22 // This method was removed at ACE 5.4. Replaced by cancel_type() and
23 // cancel_timer().
24 int
25 UpcallHandler::cancellation (PTimerQueue &,
26 PCB *handler)
28 ACE_TRACE ("UpcallHandler::cancellation");
30 ACE_DEBUG ((LM_DEBUG,
31 ACE_TEXT ("Handler %d has been cancelled\n"),
32 handler->getID ()));
34 return handler->handleCancel ();
36 #endif /* 0 */
38 // This method is called when the timer is canceled
39 int
40 UpcallHandler::deletion (PTimerQueue &,
41 PCB *handler,
42 const void *)
44 ACE_TRACE ("UpcallHandler::deletion");
46 ACE_DEBUG ((LM_DEBUG,
47 ACE_TEXT ("Handler %d has been deleted\n"),
48 handler->getID ()));
50 return handler->handleClose ();
52 // Listing 2
54 // *** The rest of the UpcallHandler methods were added for ACE 5.4 ***
56 // This method is called when a timer is registered.
57 int
58 UpcallHandler::registration (PTimerQueue &,
59 PCB *handler,
60 const void *)
62 ACE_TRACE ("UpcallHandler::registration");
64 ACE_DEBUG ((LM_DEBUG,
65 ACE_TEXT ("Handler %d has been registered.\n"),
66 handler->getID ()));
67 return 0;
70 // This method is called at expiration time, before the actual upcall
71 // to the handler is made. ACE uses this to adjust reference counts
72 // when needed.
73 int
74 UpcallHandler::preinvoke (PTimerQueue &,
75 PCB *handler,
76 const void *,
77 int,
78 const ACE_Time_Value &,
79 const void *&)
81 ACE_TRACE ("UpcallHandler::preinvoke");
83 ACE_DEBUG ((LM_DEBUG,
84 ACE_TEXT ("Handler %d is about to upcalled.\n"),
85 handler->getID ()));
86 return 0;
89 // This method is called at expiration time, after the actual upcall
90 // to the handler returns. ACE uses this to adjust reference counts
91 // when needed.
92 int
93 UpcallHandler::postinvoke (PTimerQueue &,
94 PCB *handler,
95 const void *,
96 int,
97 const ACE_Time_Value &,
98 const void *)
100 ACE_TRACE ("UpcallHandler::postinvoke");
102 ACE_DEBUG ((LM_DEBUG,
103 ACE_TEXT ("Handler %d returned from upcall.\n"),
104 handler->getID ()));
105 return 0;
108 // This method is called when a handler is cancelled
110 UpcallHandler::cancel_type (PTimerQueue &,
111 PCB *handler,
112 int dont_call,
113 int &)
115 ACE_TRACE ("UpcallHandler::cancel_type");
117 ACE_DEBUG ((LM_DEBUG,
118 ACE_TEXT ("Handler %d has been cancelled\n"),
119 handler->getID ()));
120 if (!dont_call)
121 return handler->handleCancel ();
122 return 0;
125 // This method is called when a timer is cancelled
127 UpcallHandler::cancel_timer (PTimerQueue &,
128 PCB *handler,
129 int dont_call,
130 int)
132 ACE_TRACE ("UpcallHandler::cancel_timer");
134 ACE_DEBUG ((LM_DEBUG,
135 ACE_TEXT ("Handler %d has been cancelled\n"),
136 handler->getID ()));
137 if (!dont_call)
138 return handler->handleCancel ();
139 return 0;
143 // Listing 3 code/ch20
144 int ACE_TMAIN (int, ACE_TCHAR *[])
146 PCB cb1, cb2;
147 cb1.setID (1);
148 cb2.setID (2);
149 int arg1 = 1, arg2 = 2;
151 PTimerQueue *timerQueue;
153 ACE_NEW_RETURN (timerQueue, PTimerHeap (), -1);
155 PTimer::instance ()->set (timerQueue);
157 ACE_Time_Value tv = ACE_OS::gettimeofday ();
158 tv += 20L;
160 // Schedule two different timers to go off.
161 PTimer::instance ()->schedule (&cb1, &arg1, tv, ACE_Time_Value (1));
162 PTimer::instance ()->schedule (&cb2, &arg2, tv, ACE_Time_Value (2));
164 // Run the timer event loop forever.
165 PTimer::instance ()->wait_for_event ();
167 return 0;
169 // Listing 3