summaryrefslogtreecommitdiff
path: root/ace/Message_Queue.cpp
blob: bb3980bffdb14054d5064bfa2d7e3aa8c550417f (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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
// Message_Queue.cpp
// $Id$

#if !defined (ACE_MESSAGE_QUEUE_C)
#define ACE_MESSAGE_QUEUE_C

#define ACE_BUILD_DLL
#include "ace/Message_Queue.h"

#if !defined (__ACE_INLINE__)
#include "ace/Message_Queue.i"
#endif /* __ACE_INLINE__ */

ACE_ALLOC_HOOK_DEFINE(ACE_Message_Queue)

template <ACE_SYNCH_1> void
ACE_Message_Queue<ACE_SYNCH_2>::dump (void) const
{
  ACE_TRACE ("ACE_Message_Queue<ACE_SYNCH_2>::dump");
  ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
  ACE_DEBUG ((LM_DEBUG,
	      "deactivated = %d\n"
	      "low_water_mark = %d\n"
	      "high_water_mark = %d\n"	     
	      "cur_bytes = %d\n"	     
	      "cur_count = %d\n",
	      this->deactivated_,
	      this->low_water_mark_, 
	      this->high_water_mark_,
	      this->cur_bytes_, 
	      this->cur_count_));
  ACE_DEBUG ((LM_DEBUG,"notfull_cond: \n"));
  notfull_cond_.dump();
  ACE_DEBUG ((LM_DEBUG,"notempty_cond: \n"));
  notempty_cond_.dump();
  ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
}

template <ACE_SYNCH_1>
ACE_Message_Queue<ACE_SYNCH_2>::ACE_Message_Queue (size_t hwm, 
						   size_t lwm,
						   ACE_Notification_Strategy *ns)
  : notempty_cond_ (this->lock_),
    notfull_cond_ (this->lock_)
{
  ACE_TRACE ("ACE_Message_Queue<ACE_SYNCH_2>::ACE_Message_Queue");

  if (this->open (hwm, lwm, ns) == -1)
    ACE_ERROR ((LM_ERROR, "open"));
}

template <ACE_SYNCH_1>
ACE_Message_Queue<ACE_SYNCH_2>::~ACE_Message_Queue (void)
{
  ACE_TRACE ("ACE_Message_Queue<ACE_SYNCH_2>::~ACE_Message_Queue");
  if (this->head_ != 0 && this->close () == -1)
    ACE_ERROR ((LM_ERROR, "close"));
}
  
// Don't bother locking since if someone calls this function more than
// once for the same queue, we're in bigger trouble than just
// concurrency control!

template <ACE_SYNCH_1> int 
ACE_Message_Queue<ACE_SYNCH_2>::open (size_t hwm, 
				      size_t lwm,
				      ACE_Notification_Strategy *ns)
{
  ACE_TRACE ("ACE_Message_Queue<ACE_SYNCH_2>::open");
  this->high_water_mark_ = hwm;
  this->low_water_mark_  = lwm;
  this->deactivated_ = 0;
  this->cur_bytes_ = 0;
  this->cur_count_ = 0;
  this->tail_ = 0;
  this->head_ = 0;

  this->notification_strategy_ = ns;
  return 0;
}

// Implementation of the public deactivate() method
// (assumes locks are held).

template <ACE_SYNCH_1> int
ACE_Message_Queue<ACE_SYNCH_2>::deactivate_i (void)
{
  ACE_TRACE ("ACE_Message_Queue<ACE_SYNCH_2>::deactivate_i");
  int current_status = 
    this->deactivated_ ? WAS_INACTIVE : WAS_ACTIVE;

  // Wakeup all waiters.
  this->notempty_cond_.broadcast ();
  this->notfull_cond_.broadcast ();

  this->deactivated_ = 1;
  return current_status;
}

template <ACE_SYNCH_1> int
ACE_Message_Queue<ACE_SYNCH_2>::activate_i (void)
{
  ACE_TRACE ("ACE_Message_Queue<ACE_SYNCH_2>::activate_i");
  int current_status = 
    this->deactivated_ ? WAS_INACTIVE : WAS_ACTIVE;
  this->deactivated_ = 0;
  return current_status;
}

// Clean up the queue if we have not already done so! 

template <ACE_SYNCH_1> int
ACE_Message_Queue<ACE_SYNCH_2>::close (void)
{
  ACE_TRACE ("ACE_Message_Queue<ACE_SYNCH_2>::close");
  ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, ace_mon, this->lock_, -1);

  int res = this->deactivate_i ();

  // Free up the remaining message on the list 

  for (this->tail_ = 0; this->head_ != 0; )
    {
      ACE_Message_Block *temp;

      // Make sure we decrement all the counts.
      for (temp = this->head_;
	   temp != 0; 
	   temp = temp->cont ())
	this->cur_bytes_ -= temp->size ();

      this->cur_count_--;

      this->head_ = this->head_->next ();
      delete temp;
    }

  return res;
}

// Actually put the node at the end (no locking so must be called with
// locks held).

template <ACE_SYNCH_1> int
ACE_Message_Queue<ACE_SYNCH_2>::enqueue_tail_i (ACE_Message_Block *new_item)
{
  ACE_TRACE ("ACE_Message_Queue<ACE_SYNCH_2>::enqueue_tail_i");

  if (new_item == 0)
    return -1;

  // List was empty, so build a new one.
  if (this->tail_ == 0)
    {
      this->head_ = new_item;
      this->tail_ = new_item;
      new_item->next (0);
      new_item->prev (0);
    }
  // Link at the end.
  else 
    {
      
      new_item->next (0);
      this->tail_->next (new_item);
      new_item->prev (this->tail_);
      this->tail_ = new_item;
    }

  // Make sure to count *all* the bytes in a composite message!!! 

  for (ACE_Message_Block *temp = new_item; 
       temp != 0; 
       temp = temp->cont ())
    this->cur_bytes_ += temp->size ();

  this->cur_count_++;

  // Tell any blocked threads that the queue has a new item!
  if (this->notempty_cond_.signal () != 0)
    return -1;
  else
    return this->cur_count_;
}

// Actually put the node at the head (no locking) 

template <ACE_SYNCH_1> int
ACE_Message_Queue<ACE_SYNCH_2>::enqueue_head_i (ACE_Message_Block *new_item)
{
  ACE_TRACE ("ACE_Message_Queue<ACE_SYNCH_2>::enqueue_head_i");

  if (new_item == 0)
    return -1;

  new_item->prev (0);
  new_item->next (this->head_);

  if (this->head_ != 0)
      this->head_->prev (new_item);
  else
    this->tail_ = new_item;

  this->head_ = new_item;

  // Make sure to count *all* the bytes in a composite message!!!

  for (ACE_Message_Block *temp = new_item; 
       temp != 0; 
       temp = temp->cont ())
    this->cur_bytes_ += temp->size ();

  this->cur_count_++;

  // Tell any blocked threads that the queue has a new item! 
  if (this->notempty_cond_.signal () != 0)
    return -1;
  else
    return this->cur_count_;
}

// Actually put the node at its proper position relative to its
// priority.

template <ACE_SYNCH_1> int
ACE_Message_Queue<ACE_SYNCH_2>::enqueue_i (ACE_Message_Block *new_item)
{
  ACE_TRACE ("ACE_Message_Queue<ACE_SYNCH_2>::enqueue_i");

  if (new_item == 0)
    return -1;

  if (this->head_ == 0)
    // Check for simple case of an empty queue, where all we need to
    // do is insert <new_item> into the head.
    return this->enqueue_head_i (new_item);
  else
    {
      ACE_Message_Block *temp;

      // Figure out where the new item goes relative to its priority.
      // We start looking from the highest priority to the lowest
      // priority.

      for (temp = this->tail_;
	   temp != 0;
	   temp = temp->prev ())
	{
	  if (temp->msg_priority () >= new_item->msg_priority ())
	    // Break out when we've located an item that has higher
	    // priority that <new_item>.
	    break;
	}

      if (temp == 0)
	// Check for simple case of inserting at the head of the queue,
	// where all we need to do is insert <new_item> before the
	// current head.
	return this->enqueue_head_i (new_item);
      else if (temp->next () == 0)
	// Check for simple case of inserting at the end of the
	// queue, where all we need to do is insert <new_item> after
	// the current tail.
	return this->enqueue_tail_i (new_item);
      else
	{
	  // Insert the message right before the item of equal or
	  // higher priority.  This ensures that FIFO order is
	  // maintained when messages of the same priority are
	  // inserted consecutively.
	  new_item->prev (temp);
	  new_item->next (temp->next ());
	  temp->next ()->prev (new_item);
	  temp->next (new_item);
	}
    }

  // Make sure to count *all* the bytes in a composite message!!!

  for (ACE_Message_Block *temp = new_item; 
       temp != 0; 
       temp = temp->cont ())
    this->cur_bytes_ += temp->size ();

  this->cur_count_++;

  // Tell any blocked threads that the queue has a new item! 
  if (this->notempty_cond_.signal () != 0)
    return -1;
  else
    return this->cur_count_;
}

// Actually get the first ACE_Message_Block (no locking, so must be called
// with locks held).  This method assumes that the queue has at least
// one item in it when it is called. 

template <ACE_SYNCH_1> int
ACE_Message_Queue<ACE_SYNCH_2>::dequeue_head_i (ACE_Message_Block *&first_item)
{
  ACE_TRACE ("ACE_Message_Queue<ACE_SYNCH_2>::dequeue_head_i");
  first_item = this->head_;
  this->head_ = this->head_->next ();

  if (this->head_ == 0)
    this->tail_ = 0;
  else
    // The prev pointer of the first message block has to point to
    // NULL...
    this->head_->prev (0);

  // Make sure to subtract off all of the bytes associated with this
  // message.
  for (ACE_Message_Block *temp = first_item; 
       temp != 0; 
       temp = temp->cont ())
    this->cur_bytes_ -= temp->size ();

  this->cur_count_--;

#if 0
  if (this->cur_bytes_ <= this->low_water_mark_)
  // If queue is no longer full signal any waiting threads. 
#endif /* 0 */

  if (this->notfull_cond_.signal () != 0)
    return -1;
  else
    return this->cur_count_;
}

// Take a look at the first item without removing it. 

template <ACE_SYNCH_1> int
ACE_Message_Queue<ACE_SYNCH_2>::peek_dequeue_head (ACE_Message_Block *&first_item, 
						   ACE_Time_Value *tv)
{
  ACE_TRACE ("ACE_Message_Queue<ACE_SYNCH_2>::peek_dequeue_head");
  ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, ace_mon, this->lock_, -1);

  if (this->deactivated_)
    {
      errno = ESHUTDOWN;
      return -1;
    }

  // Wait for at least one item to become available 

  while (this->is_empty_i ())
    {
      if (this->notempty_cond_.wait (tv) == -1)
	{
	  if (errno == ETIME)
	    errno = EWOULDBLOCK;
	  return -1;
	}
      if (this->deactivated_)
	{
	  errno = ESHUTDOWN;
	  return -1;
	}
    }

  first_item = this->head_;
  return this->cur_count_;
}

// Block indefinitely waiting for an item to arrive,
// does not ignore alerts (e.g., signals). 

template <ACE_SYNCH_1> int 
ACE_Message_Queue<ACE_SYNCH_2>::enqueue_head (ACE_Message_Block *new_item, 
					      ACE_Time_Value *tv)
{
  ACE_TRACE ("ACE_Message_Queue<ACE_SYNCH_2>::enqueue_head");
  ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, ace_mon, this->lock_, -1);

  int queue_count;
  {
    if (this->deactivated_)
      {
	errno = ESHUTDOWN;
	return -1;
      }

    // Wait while the queue is full 

    while (this->is_full_i ())
      {
	if (this->notfull_cond_.wait (tv) == -1)
	  {
	    if (errno == ETIME)
	      errno = EWOULDBLOCK;
	    return -1;
	  }
	if (this->deactivated_)
	  {
	    errno = ESHUTDOWN;
	    return -1;
	  }
      }

    queue_count = this->enqueue_head_i (new_item);
  }
  if (queue_count == -1)
    return -1;
  else
    {
      this->notify ();
      return queue_count;
    }
}

// Enqueue an <ACE_Message_Block *> into the <Message_Queue> in
// accordance with its <msg_priority> (0 is lowest priority).  Returns
// -1 on failure, else the number of items still on the queue.

template <ACE_SYNCH_1> int 
ACE_Message_Queue<ACE_SYNCH_2>::enqueue_prio (ACE_Message_Block *new_item, 
					      ACE_Time_Value *tv)
{
  ACE_TRACE ("ACE_Message_Queue<ACE_SYNCH_2>::enqueue_prio");

  int queue_count;

  {
    ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, ace_mon, this->lock_, -1);

    if (this->deactivated_)
      {
	errno = ESHUTDOWN;
	return -1;
      }

    // Wait while the queue is full 

    while (this->is_full_i ())
      {
	if (this->notfull_cond_.wait (tv) == -1)
	  {
	    if (errno == ETIME)
	      errno = EWOULDBLOCK;
	    return -1;
	  }
	if (this->deactivated_)
	  {
	    errno = ESHUTDOWN;
	    return -1;
	  }
      }

    queue_count = this->enqueue_i (new_item);
  }
  if (queue_count == -1)
    return -1;
  else
    {
      this->notify ();
      return queue_count;
    }
}

template <ACE_SYNCH_1> int 
ACE_Message_Queue<ACE_SYNCH_2>::enqueue (ACE_Message_Block *new_item, 
					 ACE_Time_Value *tv)
{
  ACE_TRACE ("ACE_Message_Queue<ACE_SYNCH_2>::enqueue");
  return this->enqueue_prio (new_item, tv);
}

// Block indefinitely waiting for an item to arrive,
// does not ignore alerts (e.g., signals). 

template <ACE_SYNCH_1> int 
ACE_Message_Queue<ACE_SYNCH_2>::enqueue_tail (ACE_Message_Block *new_item, 
					      ACE_Time_Value *tv)
{
  ACE_TRACE ("ACE_Message_Queue<ACE_SYNCH_2>::enqueue_tail");

  int queue_count;
  {
    ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, ace_mon, this->lock_, -1);

    if (this->deactivated_)
      {
	errno = ESHUTDOWN;
	return -1;
      }

    // Wait while the queue is full 

    while (this->is_full_i ())
      {
	if (this->notfull_cond_.wait (tv) == -1)
	  {
	    if (errno == ETIME)
	      errno = EWOULDBLOCK;
	    return -1;
	  }
	if (this->deactivated_)
	  {
	    errno = ESHUTDOWN;
	    return -1;
	  }
      }
    queue_count = this->enqueue_tail_i (new_item);
  }
  if (queue_count == -1)
    return -1;
  else
    {
      this->notify ();
      return queue_count;
    }
}

// Remove an item from the front of the queue.  If TV == 0 block 
// indefinitely (or until an alert occurs).  Otherwise, block for upto
// the amount of time specified by TV. 

template <ACE_SYNCH_1> int 
ACE_Message_Queue<ACE_SYNCH_2>::dequeue_head (ACE_Message_Block *&first_item, 
					      ACE_Time_Value *tv)
{
  ACE_TRACE ("ACE_Message_Queue<ACE_SYNCH_2>::dequeue_head");
  ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, ace_mon, this->lock_, -1);

  if (this->deactivated_)
    {
      errno = ESHUTDOWN;
      return -1;
    }

  // Wait while the queue is empty.

  while (this->is_empty_i ())
    {
      if (this->notempty_cond_.wait (tv) == -1)
	{
	  if (errno == ETIME)
	    errno = EWOULDBLOCK;
	  return -1;
	}
      if (this->deactivated_)
	{
	  errno = ESHUTDOWN;
	  return -1;
	}
    }

  return this->dequeue_head_i (first_item);
}

template <ACE_SYNCH_1> int
ACE_Message_Queue<ACE_SYNCH_2>::notify (void)
{
  ACE_TRACE ("ACE_Message_Queue<ACE_SYNCH_2>::dequeue_head");

  // By default, don't do anything.
  if (this->notification_strategy_ == 0)
    return 0;
  else
    return this->notification_strategy_->notify ();
}

#endif /* ACE_MESSAGE_QUEUE_C */