summaryrefslogtreecommitdiff
path: root/ACE/ace/Monitor_Point_Registry.cpp
blob: b448ab77e89d4b6b3b45acba396f3de658c53a81 (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
#include "ace/Monitor_Point_Registry.h"

#if defined (ACE_HAS_MONITOR_FRAMEWORK) && (ACE_HAS_MONITOR_FRAMEWORK == 1)

#include "ace/Monitor_Base.h"
#include "ace/Guard_T.h"

ACE_BEGIN_VERSIONED_NAMESPACE_DECL

namespace ACE
{
  namespace Monitor_Control
  {
    Monitor_Point_Registry*
    Monitor_Point_Registry::instance ()
    {
      return
        ACE_Singleton<Monitor_Point_Registry, ACE_SYNCH_MUTEX>::instance ();
    }

    bool
    Monitor_Point_Registry::add (Monitor_Base* type)
    {
      if (type == 0)
        {
          ACELIB_ERROR_RETURN ((LM_ERROR,
                             "registry add: null type\n"),
                            false);
        }

      int status = 0;

      {
        ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, guard, this->mutex_, false);

        type->add_ref ();

        status = this->map_.bind (type->name (), type);
      }

      if (status == -1)
        {
          ACELIB_ERROR_RETURN ((LM_ERROR,
                             "registry add: map bind failed\n"),
                            false);
        }

      return (status == 0);
    }

    bool
    Monitor_Point_Registry::remove (const char* name)
    {
      if (name == 0)
        {
          ACELIB_ERROR_RETURN ((LM_ERROR,
                             "registry remove: null name\n"),
                            false);
        }

      int status = 0;
      Map::data_type mp = 0;

      {
        ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, guard, this->mutex_, false);

        ACE_CString name_str (name, 0, false);
        status = this->map_.unbind (name_str, mp);
      }

      if (status == -1)
        {
// (JP) There is a problem with this failing on a single ACE_Message_Queue
//      monitor per process. I think it is the message queue associated
//      with the default reactor, maybe because at that low level, ACE
//      is using malloc with placement, then free, which may bypass the
//      normal destructors. In any case, it happens only at shutdown
//      and there seems to be no memory leak.
//          ACELIB_ERROR_RETURN ((LM_ERROR,
//                             "registry remove: unbind failed for %s\n",
//                             name),
//                            false);
        }
      else
        {
          mp->remove_ref ();
        }

      return (status == 0);
    }

    Monitor_Control_Types::NameList
    Monitor_Point_Registry::names ()
    {
      Monitor_Control_Types::NameList name_holder_;

      {
        ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, guard, this->mutex_, 0);

        for (Map::CONST_ITERATOR i (this->map_); !i.done (); i.advance ())
          {
            name_holder_.push_back (i->key ());
          }
      }

      return name_holder_;
    }

    Monitor_Base*
    Monitor_Point_Registry::get (const ACE_CString& name) const
    {
      Map::data_type mp = 0;

      {
        ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, guard, this->mutex_, 0);

        this->map_.find (name, mp);
      }

      if (mp != 0)
        {
          mp->add_ref ();
        }

      return mp;
    }

    long
    Monitor_Point_Registry::constraint_id ()
    {
      long retval = 0;

      {
        ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, guard, this->mutex_, -1);

        retval = this->constraint_id_++;
      }

      return retval;
    }

    void
    Monitor_Point_Registry::cleanup ()
    {
      for (Map::ITERATOR i = this->map_.begin ();
           i != this->map_.end ();
           i.advance ())
        {
          Map::ENTRY* entry = 0;
          i.next (entry);
          entry->int_id_->remove_ref ();
        }
    }
  }
}

ACE_END_VERSIONED_NAMESPACE_DECL

#endif /* ACE_HAS_MONITOR_FRAMEWORK==1 */