summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/Notify/Routing_Slip_Queue.cpp
blob: 93cde74a63e76b1e2cfe683bdac6dd6629af60e1 (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
// $Id$

#include "Routing_Slip_Queue.h"

#if ! defined (__ACE_INLINE__)
#include "Routing_Slip_Queue.inl"
#endif /* __ACE_INLINE__ */

#include "tao/debug.h"
#include "ace/Dynamic_Service.h"

//#define DEBUG_LEVEL 9
#define DEBUG_LEVEL TAO_debug_level

namespace TAO_Notify
{
  Routing_Slip_Queue::Routing_Slip_Queue (size_t allowed)
    : allowed_ (allowed)
    , active_ (0)
  {
  }

  Routing_Slip_Queue::~Routing_Slip_Queue ()
  {
  }

  void
  Routing_Slip_Queue::add (const Routing_Slip_Ptr & routing_slip)
  {
    Guard guard (internals_);
    ACE_ASSERT (guard.locked()); // check recursion
    if (this->allowed_ == 0)
    {
      ++this->active_;
      guard.release ();
      routing_slip->at_front_of_persist_queue ();
//      guard.acquire ();
    }
    else
    {
      this->queue_.enqueue_tail (routing_slip);
      dispatch (guard);
    }
  }

  void Routing_Slip_Queue::complete ()
  {
    Guard guard (internals_);
    ACE_ASSERT (guard.locked()); // check recursion
    ACE_ASSERT (this->active_ > 0);
    --this->active_;
    dispatch (guard);
  }

  void
  Routing_Slip_Queue::dispatch (Guard & guard)
  {
    // we start out pretty nice,
    // but the more work we do for other people
    // the less nice we get.
    size_t nice = this->allowed_ + 1;
    while (nice > 0 && (this->active_ < this->allowed_))
    {
      if (dispatch_one (guard))
      {
        --nice;
      }
      else
      {
        // that's about as nice as I get.
        nice = 0;
      }
    }
  }

  bool
  Routing_Slip_Queue::dispatch_one (Guard & guard)
  {
    bool ok = false;
    Routing_Slip_Ptr routing_slip;
    if (this->queue_.dequeue_head (routing_slip) == 0)
    {
      ++this->active_;
      guard.release ();
      routing_slip->at_front_of_persist_queue ();
      guard.acquire ();
    }
    return ok;
  }

  void
  Routing_Slip_Queue::set_allowed (size_t allowed)
  {
    Guard guard (internals_);
    size_t allowed_was = this->allowed_;
    this->allowed_ = allowed;
    if (allowed == 0 && allowed_was != 0)
    {
      while (dispatch_one (guard))
      {
        ; // work happens in dispatc_one
      }
    }
    else
    {
      dispatch (guard);
    }
  }
} // namespace

#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
template class ACE_Node<TAO_Notify::Routing_Slip_Ptr>;
template class ACE_Unbounded_Queue<TAO_Notify::Routing_Slip_Ptr>;
template class ACE_Unbounded_Queue_Iterator<TAO_Notify::Routing_Slip_Ptr>;
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#pragma instantiate ACE_Node<TAO_Notify::Routing_Slip_Ptr>
#pragma instantiate ACE_Unbounded_Queue<TAO_Notify::Routing_Slip_Ptr>
#pragma instantiate ACE_Unbounded_Queue_Iterator<TAO_Notify::Routing_Slip_Ptr>
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */