summaryrefslogtreecommitdiff
path: root/TAO/local/bin/Event_Service/Dispatching_Modules.h
blob: 31ce297788b814f718806a4f9c828cde85c87c1a (plain)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/* -*- C++ -*- */
// $Id$
//
// ============================================================================
//
// = LIBRARY
//    orbsvcs
// 
// = FILENAME
//    Dispatching_Module
//
// = AUTHOR
//    Tim Harrison (harrison@cs.wustl.edu)
//
// = DESCRIPTION
//    This file holds the different Event Service dispatching
//    mechanisms.  These include null-dispatching (EFD),
//    single-threaded with (RTU) and without preemption (LAME), and a
//    multithreaded implementation.
//
// ============================================================================

#ifndef ACE_DISPATCHING_MODULES_H
#define ACE_DISPATCHING_MODULES_H

#include "ReactorTask.h"
#include "Event_Channel.h"

// ************************************************************

// Forward declarations.
class ACE_ES_Dispatch_Queue;
class ACE_ES_Dispatch_Request;

// ************************************************************

// Forward declarations.
class ACE_ES_Consumer_Module;

class ACE_ES_Dispatching_Base : public ACE_Event_Handler
// = TITLE
//    Event Service Dispatch Module base class
//
// = DESCRIPTION
//    We inherit from ACE_Event_Handler so that we can be called back
//    by the ReactorEx when requests are queued.  The virtual
//    dispatch_event method allows ACE_ES_Dispatch_Requests to call
//    back the dispatching module when acting as command objects. When
//    this implementation is used by the Event Channel it forwards all
//    dispatch calls without any queuing.  Therefore, it can be
//    used to build an EFD.  It is also inherited by the Priority
//    Dispatching module.
{
public:
  ACE_ES_Dispatching_Base (ACE_EventChannel *channel);
  // Default construction.

  virtual void open (ACE_ES_Consumer_Module *up,
		     ACE_ES_Correlation_Module *down);
  // Link to adjacent modules.

  virtual void connected (ACE_Push_Consumer_Proxy *consumer,
			  CORBA::Environment &);
  // Forward down_.

  virtual void disconnecting (ACE_Push_Consumer_Proxy *consumer,
			      CORBA::Environment &);
  // Forward down_.

  virtual void disconnected (ACE_Push_Consumer_Proxy *consumer);
  // Release any unneeded dispatching resources.

  // = Not needed.
  // void connected (ACE_Push_Supplier_Proxy *supplier);
  // void disconnecting (ACE_Push_Supplier_Proxy *supplier);

  virtual void push (ACE_ES_Dispatch_Request *request,
		     CORBA::Environment &) = 0;
  // Forward up_.

  virtual int dispatch_event (ACE_ES_Dispatch_Request *request,
			      u_long &command_action);
  // Called by ACE_ES_Dispatch_Requests when dequeued by RT_Tasks.

  virtual void dispatch_queue_closed (ACE_ES_Dispatch_Queue *q);
  // Called when all the threads of a <q> have exited.

  virtual void shutdown (void);
  // This is called by the Event Channel.  This will attempt to shut
  // down all of its threads gracefully.  Wish it luck.

protected:
  ACE_EventChannel *channel_;
  // Dat der channel.

  ACE_ES_MUTEX lock_;
  // To synchronize thr_count_.

  int thr_count_;
  // The total number of threads in the Dispatching Module.  This will
  // be the sum of all the Dispatch Queue threads.

  ACE_ES_Consumer_Module *up_;
  // Next module up.

  ACE_ES_Correlation_Module *down_;
  // Next module down.
};

// ************************************************************

class ACE_ES_Dispatch_Request : public ACE_RT_Task_Command
// = TITLE
//    ACE Event Service Dispatch Request
//
// = DESCRIPTION
//    Encapsulates a consumer and the events that will be sent to the
//    consumer.  Right now, this class keeps a single_event_ that can
//    be used when only one event is sent to the consumer.  Since this
//    is frequently the case (except for correlations), this
//    optimization reduces the amount of dynamic memory allocation is
//    necessary.  This class is also a GOF Command object since it can
//    be dequeued by an RT_Task to call back the dispatching module
//    for request dispatching.
{
public:
  typedef ACE_CORBA_Sequence<ACE_ES_Event_Container_var> Event_Set;

  ACE_ES_Dispatch_Request (void);
  // Default construction.
  
  virtual ~ACE_ES_Dispatch_Request (void);
  // Default destruction.
  
  ACE_ES_Dispatch_Request (ACE_Push_Consumer_Proxy *consumer, 
			   RtecScheduler::handle_t rt_info);
  // All the events must be added after construction to the
  // event_set.

  ACE_ES_Dispatch_Request (ACE_Push_Consumer_Proxy *consumer, 
			   const Event_Set &event_set,
			   RtecScheduler::handle_t rt_info);
  // Set consumer_ to <consumer> and copy <event_set> to event_set_.
  // <rt_info> describes the method receiving this dispatch.

  ACE_ES_Dispatch_Request (ACE_Push_Consumer_Proxy *consumer,
			   const RtecEventComm::Time &time,
			   RtecScheduler::handle_t rt_info);
  // Set consumer_ to <consumer> and sets single_event_.time_ to
  // <time>.  Sets use_single_event_ to 1.  <rt_info> describes the
  // method receiving this dispatch.

  ACE_ES_Dispatch_Request (ACE_Push_Consumer_Proxy *consumer,
			   ACE_ES_Event_Container *event,
			   RtecScheduler::handle_t rt_info);
  // Sets consumer_ and the first slot of event_set_.  We use the
  // event_set_ instead of the single_event_ so that we can just carry
  // around the pointer to <event>.  <rt_info> describes the method
  // receiving this dispatch.

  RtecScheduler::handle_t rt_info (void);
  // Description of the method receiving this request.

  void set (ACE_ES_Dispatching_Base *dispatching_module,
	    RtecScheduler::OS_Priority priority,
	    RtecScheduler::Sub_Priority sub_priority);
  // For multi-threaded implementations, <dispatching_module> is
  // called back when a request is dequeued.  <priority> is the
  // dispatch priority of the event.  <sub_priority> is the enqueue
  // priority of the event and will be forwarded to
  // ACE_Message_Block.

  ACE_Push_Consumer_Proxy *consumer (void) const;
  // Consumer accessor.

  const Event_Set &event_set (void) const;
  // If accessed, make_copy will use event_set_.

  Event_Set &event_set (void);
  // If accessed, make_copy will use event_set_.

  CORBA::ULong number_of_events (void) const;
  // Returns 1 if we're using single_event, or event_set_.size ().

  void make_copy (RtecEventComm::EventSet &dest) const;
  // Copy single_event or event_set into <dest>.

  virtual int execute (u_long &command_action);
  // Calls dispatching_module_->dispatch_event.

  RtecScheduler::OS_Priority priority (void);
  // Priority accessor.

#if 0
  // @@ This cannot be done: the object would be allocated using this
  // class operator new, but it will be removed using the
  // ACE_Message_Block operator delete!
  void *operator new (size_t nbytes);
  // Allocates memory from a thread-specific memory pool.

  void operator delete (void *);
  // Returns memory to a thread-specific memory pool.
#endif

protected:
  RtecScheduler::OS_Priority priority_;

  RtecScheduler::handle_t rt_info_;
  // Describes the method receiving this dispatch.

  ACE_ES_Dispatching_Base *dispatching_module_;
  // The dispatching module called back when we're dequeued by a
  // thread.

  int use_single_event_;
  // Is true if we're using a single event.  Is 0 is we're using
  // event_set_.

  ACE_Push_Consumer_Proxy *consumer_;
  // The final destination for single_event_ or event_set_.

  ACE_ES_Event_Container single_event_;
  // This is used for single event dispatches.

  Event_Set event_set_;
  // This is used for event sets that need to be dispatched.
};

// ************************************************************

#if defined (ACE_WIN32)
class ACE_ES_ReactorEx_NS : public ACE_Notification_Strategy
// = TITLE 
//     Event Service ReactorEx Notification Strategy
//
// = DESCRIPTION
//     Integrates the ACE_Message_Queue notification to signal a
//     handle that will wake up the ACE_ES_Priority_Dispatching
//     module.  This is used in place of the
//     ACE_ReactorEx_Notification_Strategy to avoid any queueing by
//     the ReactorEx::notify mechanism.
{
public:
  ACE_ES_ReactorEx_NS (ACE_Event_Handler *eh);
  // Stores away <eh> for when this->open is called.

  int open (void);
  // Registers eh_ with the ReactorEx to be notified when this->notify
  // is called.

  void shutdown (void);
  // Removes self from the reactor.

  // = These result in eh_->handle_signal getting called.  eh_ should
  // point to a dispatching module.
  virtual int notify (void);
  virtual int notify (ACE_Event_Handler *,
		      ACE_Reactor_Mask mask);

  //  ACE_HANDLE get_handle (void);
  // Returns event_.handle ().

private:
  ACE_Auto_Event event_;
  // Registered with the ReactorEx.
};

typedef ACE_ES_ReactorEx_NS ACE_ES_Notification_Strategy;

#else // *******************************************************

class ACE_ES_Reactor_NS : public ACE_Reactor_Notification_Strategy
// = TITLE 
//     Event Service Reactor Notification Strategy
//
// = DESCRIPTION
//     Maps to the ACE_Reactor_Notification_Strategy interface.  This
//     version is for non WIN32 platforms.
{
public:
  ACE_ES_Reactor_NS (ACE_Event_Handler *eh);
  // Calls ACE_Reactor_Notification_Strategy with the ORB's reactor
  // and signal mask.

  int open (void);
  // Does nothing.

  void shutdown (void);
  // Does nothing.
};

typedef ACE_ES_Reactor_NS ACE_ES_Notification_Strategy;

#endif /* ACE_WIN32 */

// ************************************************************

class ACE_ES_MQ : public ACE_ES_QUEUE
// = TITLE
//    Event Service Message Queue
{
  virtual int notify (void) { return 0;}
  // Does nothing.
};

// ************************************************************

class ACE_ES_Dispatch_Queue : public ACE_RT_Task
// = TITLE
//    Event Service Dispatch Queue
//
// = DESCRIPTION
//    An encapsulation of a dispatch queue.  By inheriting from
//    ACE_RT_Task, we can make this zero-threaded or multi-threaded.
{
public:
  ACE_ES_Dispatch_Queue (ACE_ES_Dispatching_Base *dispatching_module,
			 ACE_ES_Notification_Strategy *notification_strategy);
  // Stores <dispatching_module> for this->threads_closed.  Stores
  // away <notification_strategy> for this->synch_threads.

  int open_queue (RtecScheduler::Period &period,
		  int threads);
  // This is a hack to get the channel to work with the new
  // scheduler.

#if 0
  int open_queue (RtecScheduler::OS_Priority priority,
		  int threads);
  // Creates a name from the <priority> and tries to find a scheduling
  // qos structure.  If one is not found, but created, qos_ is set
  // with default values.  Either way, if qos_->thread_ > 0, it calls
  // this->synch_threads.  Otherwise, our message queue will use
  // notification_strategy_.  This will cause the ReactorEx to call
  // back the dispatching_module_ when requests are queued on our
  // message queue.  Returns 0 on success, -1 on failure.
#endif

  virtual void threads_closed (void);
  // Called when every thread has exited.  This will call
  // dispatching_module_->dispatch_queue_closed.

private:
  ACE_ES_Dispatching_Base *dispatching_module_;
  // Used in threads_closed.

  ACE_ES_Notification_Strategy *notification_strategy_;
  // Notifies the Dispatching Module when a request has been queued on
  // our message queue.
};

// ************************************************************

class ACE_ES_Priority_Dispatching : public ACE_ES_Dispatching_Base
// = TITLE
//    Event Service Priority Dispatching Module
//
// = DESCRIPTION
//    Inherits from ACE_Event_Handler to utilitize the
//    ACE_Message_Queue notification strategy.  This implementation
//    does priority dispatching without preemption.
{
public:
  ACE_ES_Priority_Dispatching (ACE_EventChannel *channel,
			       int threads_per_queue);
  // Store <channel>.  Spawns <threads_per_queue> thread for each
  // dispatch queue.  If != 0, then the channel is an MT_CHANNEL.  If
  // == 0, then the channel is an ST_CHANNEL.

  ~ACE_ES_Priority_Dispatching (void);
  // Delete queues.

  void connected (ACE_Push_Consumer_Proxy *consumer,
		  CORBA::Environment &);
  // Allocate any needed dispatching resources for this consumers
  // priority.

  void disconnected (ACE_Push_Consumer_Proxy *consumer);
  // Release unneeded dispatch queues.

  // = Not needed.
  // void connected (ACE_Push_Supplier_Proxy *supplier);
  // void disconnecting (ACE_Push_Supplier_Proxy *supplier);

  virtual void push (ACE_ES_Dispatch_Request *request,
		     CORBA::Environment &);
  // Enqueues the request on the appropriate Dispatch Queue.

  virtual void shutdown (void);
  // Closes all queues "asynchronously."  When all queues are closed,
  // deletes them all and then deletes itself.

  virtual void dispatch_queue_closed (ACE_ES_Dispatch_Queue *q);
  // Called when all the threads of a <q> have exited.  Deletes <q>.

  //  virtual ACE_HANDLE get_handle (void) const;
  // Get the I/O handle.

protected:
  virtual int handle_signal (int signum, siginfo_t * = 0, ucontext_t * = 0);
  // Called when input has arrived on a message queue.  This is used
  // for single-threaded implementations.

  virtual int handle_input (ACE_HANDLE);
  // For single-threaded implementations on non-win32 platforms that
  // use the ACE_Reactor_Notification_Strategy.  This just forwards
  // all calls to this->handle_signal ().

  ACE_ES_Notification_Strategy notification_strategy_;
  // Shared between all dispatch queues.

  void initialize_queues (void);
  // This is a hack to create a queue for each of the 4 rate groups.

  ACE_ES_Dispatch_Queue *queues_[ACE_Scheduler_MAX_PRIORITIES];
  // Pointers to dispatch queues.

  ACE_ES_Dispatch_Queue *delete_me_queues_[ACE_Scheduler_MAX_PRIORITIES];
  // Pointers to dispatch queues that want to die.

  int queue_count_[ACE_Scheduler_MAX_PRIORITIES];
  // The number of consumers using each queue.

  int highest_priority_;
  // The highest priority queue in queues_.  This allows us to
  // optimize the handle_signal method.

  int shutdown_;
  // Make sure to only shutdown the dispatching module once.

  int threads_per_queue_;
  // The number of threads to spawn for each dispatch queue.
};

// ************************************************************

class ACE_ES_EFD_Dispatching : public ACE_ES_Dispatching_Base
// = TITLE
//    Event Service EFD Dispatching Module
//
// = DESCRIPTION
//    Implements a zero-threaded dispatcher with no preemption.
{
public:
  ACE_ES_EFD_Dispatching (ACE_EventChannel *channel);
  // Acquires the proper qos structure and passes <channel> onto to
  // the dispatching base constructor.

  virtual void push (ACE_ES_Dispatch_Request *request,
		     CORBA::Environment &);
  // Forward up_.
};

// ************************************************************

class ACE_ES_RTU_Dispatching : public ACE_ES_Priority_Dispatching
// = TITLE
//    Event Service RTU Dispatching Module
//
// = DESCRIPTION
//    Implements a single-threaded dispatcher with delayed preemption.
{
public:
  ACE_ES_RTU_Dispatching (ACE_EventChannel *channel);
  // Store <channel>.  

  virtual int dispatch_event (ACE_ES_Dispatch_Request *request,
			      u_long &command_action);
  // Called by ACE_Dispatch_Queues and handle_signal when an event
  // needs to be dequeued.  Implements an RTU-like delayed preemption
  // policy.

  virtual void push (ACE_ES_Dispatch_Request *request,
		     CORBA::Environment &);
  // Calls ACE_ES_Priority_Dispatching::push and then checks if
  // preemption is necessary.
};

#if defined (__ACE_INLINE__)
#include "Dispatching_Modules.i"
#endif /* __ACE_INLINE__ */
#endif /* ACE_DISPATCHING_MODULES_H */