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