~ps10gel/ubuntu/xenial/trafficserver/6.2.0

« back to all changes in this revision

Viewing changes to iocore/eventsystem/I_Event.h

  • Committer: Bazaar Package Importer
  • Author(s): Arno Toell
  • Date: 2011-01-13 11:49:18 UTC
  • Revision ID: james.westby@ubuntu.com-20110113114918-vu422h8dknrgkj15
Tags: upstream-2.1.5-unstable
ImportĀ upstreamĀ versionĀ 2.1.5-unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** @file
 
2
 
 
3
  A brief file description
 
4
 
 
5
  @section license License
 
6
 
 
7
  Licensed to the Apache Software Foundation (ASF) under one
 
8
  or more contributor license agreements.  See the NOTICE file
 
9
  distributed with this work for additional information
 
10
  regarding copyright ownership.  The ASF licenses this file
 
11
  to you under the Apache License, Version 2.0 (the
 
12
  "License"); you may not use this file except in compliance
 
13
  with the License.  You may obtain a copy of the License at
 
14
 
 
15
      http://www.apache.org/licenses/LICENSE-2.0
 
16
 
 
17
  Unless required by applicable law or agreed to in writing, software
 
18
  distributed under the License is distributed on an "AS IS" BASIS,
 
19
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
20
  See the License for the specific language governing permissions and
 
21
  limitations under the License.
 
22
 
 
23
 */
 
24
 
 
25
#ifndef _Event_h_
 
26
#define _Event_h_
 
27
 
 
28
#include "libts.h"
 
29
#include "I_Action.h"
 
30
 
 
31
//
 
32
//  Defines
 
33
//
 
34
 
 
35
#define MAX_EVENTS_PER_THREAD     100000
 
36
 
 
37
// Events
 
38
 
 
39
#define EVENT_NONE                CONTINUATION_EVENT_NONE       // 0
 
40
#define EVENT_IMMEDIATE           1
 
41
#define EVENT_INTERVAL            2
 
42
#define EVENT_ERROR               3
 
43
#define EVENT_CALL                4     // used internally in state machines
 
44
#define EVENT_POLL                5     // negative event; activated on poll or epoll
 
45
 
 
46
// Event callback return functions
 
47
 
 
48
#define EVENT_DONE                CONTINUATION_DONE     // 0
 
49
#define EVENT_CONT                CONTINUATION_CONT     // 1
 
50
#define EVENT_RETURN              5
 
51
#define EVENT_RESTART             6
 
52
#define EVENT_RESTART_DELAYED     7
 
53
 
 
54
// Event numbers block allocation
 
55
// ** ALL NEW EVENT TYPES SHOULD BE ALLOCATED FROM BLOCKS LISTED HERE! **
 
56
 
 
57
#define VC_EVENT_EVENTS_START                     100
 
58
#define NET_EVENT_EVENTS_START                    200
 
59
#define DISK_EVENT_EVENTS_START                   300
 
60
#define CLUSTER_EVENT_EVENTS_START                400
 
61
#define HOSTDB_EVENT_EVENTS_START                 500
 
62
#define DNS_EVENT_EVENTS_START                    600
 
63
#define CONFIG_EVENT_EVENTS_START                 800
 
64
#define LOG_EVENT_EVENTS_START                    900
 
65
#define MULTI_CACHE_EVENT_EVENTS_START            1000
 
66
#define CACHE_EVENT_EVENTS_START                  1100
 
67
#define CACHE_DIRECTORY_EVENT_EVENTS_START        1200
 
68
#define CACHE_DB_EVENT_EVENTS_START               1300
 
69
#define HTTP_NET_CONNECTION_EVENT_EVENTS_START    1400
 
70
#define HTTP_NET_VCONNECTION_EVENT_EVENTS_START   1500
 
71
#define GC_EVENT_EVENTS_START                     1600
 
72
#define ICP_EVENT_EVENTS_START                    1800
 
73
#define TRANSFORM_EVENTS_START                    2000
 
74
#define STAT_PAGES_EVENTS_START                   2100
 
75
#define HTTP_SESSION_EVENTS_START                 2200
 
76
#define HTTP_TUNNEL_EVENTS_START                  2300
 
77
#define HTTP_SCH_UPDATE_EVENTS_START              2400
 
78
#define NT_ASYNC_CONNECT_EVENT_EVENTS_START       3000
 
79
#define NT_ASYNC_IO_EVENT_EVENTS_START            3100
 
80
#define RAFT_EVENT_EVENTS_START                   3200
 
81
#define SIMPLE_EVENT_EVENTS_START                 3300
 
82
#define UPDATE_EVENT_EVENTS_START                 3500
 
83
#define LOG_COLLATION_EVENT_EVENTS_START          3800
 
84
#define AIO_EVENT_EVENTS_START                    3900
 
85
#define BLOCK_CACHE_EVENT_EVENTS_START            4000
 
86
#define UTILS_EVENT_EVENTS_START                  5000
 
87
#define CONGESTION_EVENT_EVENTS_START             5100
 
88
#define INK_API_EVENT_EVENTS_START                60000
 
89
#define SRV_EVENT_EVENTS_START                    62000
 
90
#define REMAP_EVENT_EVENTS_START                  63000
 
91
 
 
92
//define misc events here
 
93
#define ONE_WAY_TUNNEL_EVENT_PEER_CLOSE (SIMPLE_EVENT_EVENTS_START+1)
 
94
#define PREFETCH_EVENT_SEND_URL         (SIMPLE_EVENT_EVENTS_START+2)
 
95
 
 
96
typedef int EventType;
 
97
const int ET_CALL = 0;
 
98
const int MAX_EVENT_TYPES = 8; // conservative, these are dynamically allocated
 
99
 
 
100
class EThread;
 
101
 
 
102
/**
 
103
  A type of Action returned by the EventProcessor. The Event class
 
104
  is the type of Action returned by the EventProcessor as a result
 
105
  of scheduling an operation. Unlike asynchronous operations
 
106
  represented by actions, events never call reentrantly.
 
107
 
 
108
  Besides being able to cancel an event (because it is an action),
 
109
  you can also reschedule it once received.
 
110
 
 
111
  <b>Remarks</b>
 
112
 
 
113
  When reschedulling an event through any of the Event class
 
114
  schedulling fuctions, state machines must not make these calls
 
115
  in other thread other than the one that called them back. They
 
116
  also must have acquired the continuation's lock before calling
 
117
  any of the schedulling functions.
 
118
 
 
119
  The rules for cancelling an event are the same as those for
 
120
  actions:
 
121
 
 
122
  The canceller of an event must be the state machine that will be
 
123
  called back by the task and that state machine's lock must be
 
124
  held while calling cancel. Any reference to that event object
 
125
  (ie. pointer) held by the state machine must not be used after
 
126
  the cancellation.
 
127
 
 
128
  Event Codes:
 
129
 
 
130
  At the completion of an event, state machines use the event code
 
131
  passed in through the Continuation's handler function to distinguish
 
132
  the type of event and handle the data parameter accordingly. State
 
133
  machine implementers should be careful when defining the event
 
134
  codes since they can impact on other state machines presents. For
 
135
  this reason, this numbers are usually allocated from a common
 
136
  pool.
 
137
 
 
138
  Time values:
 
139
 
 
140
  The schedulling functions use a time parameter typed as ink_hrtime
 
141
  for specifying the timeouts or periods. This is a nanosecond value
 
142
  supported by libts and you should use the time functions and
 
143
  macros defined in ink_hrtime.h.
 
144
 
 
145
  The difference between the timeout specified for schedule_at and
 
146
  schedule_in is that in the former it is an absolute value of time
 
147
  that is expected to be in the future where in the latter it is
 
148
  an amount of time to add to the current time (obtained with
 
149
  ink_get_hrtime).
 
150
 
 
151
*/
 
152
class Event:public Action
 
153
{
 
154
public:
 
155
 
 
156
  ///////////////////////////////////////////////////////////
 
157
  // Common Interface                                      //
 
158
  ///////////////////////////////////////////////////////////
 
159
 
 
160
  /**
 
161
     Reschedules this event immediately. Instructs the event object
 
162
     to reschedule itself as soon as possible in the EventProcessor.
 
163
 
 
164
     @param callback_event Event code to return at the completion
 
165
      of this event. See the Remarks section.
 
166
 
 
167
  */
 
168
  void schedule_imm(int callback_event = EVENT_IMMEDIATE);
 
169
 
 
170
  /**
 
171
     Reschedules this event to callback at time 'atimeout_at'.
 
172
     Instructs the event object to reschedule itself at the time
 
173
     specified in atimeout_at on the EventProcessor.
 
174
 
 
175
     @param atimeout_at Time at which to callcallback. See the Remarks section.
 
176
     @param callback_event Event code to return at the completion of this event. See the Remarks section.
 
177
 
 
178
  */
 
179
  void schedule_at(ink_hrtime atimeout_at, int callback_event = EVENT_INTERVAL);
 
180
 
 
181
  /**
 
182
     Reschedules this event to callback at time 'atimeout_at'.
 
183
     Instructs the event object to reschedule itself at the time
 
184
     specified in atimeout_at on the EventProcessor.
 
185
 
 
186
     @param atimeout_in Time at which to callcallback. See the Remarks section.
 
187
     @param callback_event Event code to return at the completion of this event. See the Remarks section.
 
188
 
 
189
  */
 
190
  void schedule_in(ink_hrtime atimeout_in, int callback_event = EVENT_INTERVAL);
 
191
 
 
192
  /**
 
193
     Reschedules this event to callback every 'aperiod'. Instructs
 
194
     the event object to reschedule itself to callback every 'aperiod'
 
195
     from now.
 
196
 
 
197
     @param aperiod Time period at which to callcallback. See the Remarks section.
 
198
     @param callback_event Event code to return at the completion of this event. See the Remarks section.
 
199
 
 
200
  */
 
201
  void schedule_every(ink_hrtime aperiod, int callback_event = EVENT_INTERVAL);
 
202
 
 
203
  // inherited from Action::cancel
 
204
  // virtual void cancel(Continuation * c = NULL);
 
205
 
 
206
  void free();
 
207
 
 
208
  EThread *ethread;
 
209
 
 
210
  unsigned int in_the_prot_queue:1;
 
211
  unsigned int in_the_priority_queue:1;
 
212
  unsigned int immediate:1;
 
213
  unsigned int globally_allocated:1;
 
214
  unsigned int in_heap:4;
 
215
  int callback_event;
 
216
 
 
217
  ink_hrtime timeout_at;
 
218
  ink_hrtime period;
 
219
 
 
220
  /**
 
221
    This field can be set when an event is created. It is returned
 
222
    as part of the Event structure to the continuation when handleEvent
 
223
    is called.
 
224
 
 
225
  */
 
226
  void *cookie;
 
227
 
 
228
  // Private
 
229
 
 
230
  Event();
 
231
 
 
232
 
 
233
  Event *init(Continuation * c, ink_hrtime atimeout_at = 0, ink_hrtime aperiod = 0);
 
234
 
 
235
#ifdef ENABLE_TIME_TRACE
 
236
  ink_hrtime start_time;
 
237
#endif
 
238
 
 
239
private:
 
240
  void *operator  new(size_t size);     // use the fast allocators
 
241
 
 
242
private:
 
243
  // prevent unauthorized copies (Not implemented)
 
244
    Event(const Event &);
 
245
    Event & operator =(const Event &);
 
246
 
 
247
public:
 
248
  LINK(Event, link);
 
249
 
 
250
  /*-------------------------------------------------------*\
 
251
  | UNIX/non-NT Interface                                   |
 
252
  \*-------------------------------------------------------*/
 
253
 
 
254
#ifdef ONLY_USED_FOR_FIB_AND_BIN_HEAP
 
255
  void *node_pointer;
 
256
  void set_node_pointer(void *x)
 
257
  {
 
258
    node_pointer = x;
 
259
  }
 
260
  void *get_node_pointer()
 
261
  {
 
262
    return node_pointer;
 
263
  }
 
264
#endif
 
265
 
 
266
#if defined(__GNUC__)
 
267
  virtual ~ Event() {
 
268
  }
 
269
#endif
 
270
};
 
271
 
 
272
//
 
273
// Event Allocator
 
274
//
 
275
extern ClassAllocator<Event> eventAllocator;
 
276
 
 
277
#define EVENT_ALLOC(_a, _t) THREAD_ALLOC(_a, _t)
 
278
#define EVENT_FREE(_p, _a, _t)               \
 
279
  _p->mutex = NULL;                          \
 
280
  if (_p->globally_allocated) ::_a.free(_p); \
 
281
  else THREAD_FREE_TO(_p, _a, _t, MAX_EVENTS_PER_THREAD)
 
282
 
 
283
#endif /*_Event_h_*/