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

#if defined (ACE_HAS_TOKENS_LIBRARY)

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

ACE_BEGIN_VERSIONED_NAMESPACE_DECL

ACE_Token_Collection::ACE_Token_Collection (bool debug,
                                            const ACE_TCHAR *name)
: debug_ (debug)
{
  ACE_TRACE ("ACE_Token_Collection::ACE_Token_Collection");

  if (name == 0)
    name = ACE_TEXT ("no name");

  ACE_OS::strsncpy (this->name_,
                    const_cast<ACE_TCHAR *> (name),
                    ACE_MAXTOKENNAMELEN);
}

int
ACE_Token_Collection::insert (ACE_Token_Proxy &new_token)
{
  ACE_TRACE ("ACE_Token_Collection::insert");

  TOKEN_NAME name (new_token.name ());

  // Check if the new_proxy is already in the list.
  if (collection_.find (name) == 1)
    // One already exists, so fail.
    return -1;

  // Clone the new token.
  ACE_Token_Proxy *temp = new_token.clone ();

  if (collection_.bind (name, temp) == -1)
    ACELIB_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("bind failed\n")), -1);
  return 0;
}

int
ACE_Token_Collection::extract (const ACE_TCHAR *token_name, ACE_Token_Proxy *&proxy)
{
  ACE_TRACE ("ACE_Token_Collection::extract");
  TOKEN_NAME name (token_name);
  return collection_.unbind (token_name, proxy);
}

ACE_Token_Proxy *
ACE_Token_Collection::is_member (const ACE_TCHAR *token_name)
{
  ACE_TRACE ("ACE_Token_Collection::is_member");
  TOKEN_NAME name (token_name);
  ACE_Token_Proxy *temp;
  // Get the token from the collection.
  return collection_.find (name, temp) == -1 ? 0 : temp;
}

int
ACE_Token_Collection::is_member (const ACE_Token_Proxy &token)
{
  ACE_TRACE ("ACE_Token_Collection::is_member");
  TOKEN_NAME token_name (token.name ());
  return collection_.find (token_name) == 0;
}

int
ACE_Token_Collection::acquire (int notify,
                               void (*sleep_hook)(void *),
                               ACE_Synch_Options &options)
{
  ACE_TRACE ("ACE_Token_Collection::acquire");

  COLLECTION::ITERATOR iterator (collection_);

  for (COLLECTION::ENTRY *temp = 0;
       iterator.next (temp) != 0;
       iterator.advance ())
    {
      if (debug_)
        ACELIB_DEBUG ((LM_DEBUG,
                    ACE_TEXT ("collection acquiring %s\n"),
                    temp->int_id_->name ()));
      if (temp->int_id_->acquire (notify,
                                  sleep_hook,
                                  options) == -1)
        {
          // Save/restore errno.
          ACE_Errno_Guard error (errno);
          this->release ();
          ACE_RETURN (-1);
        }
    }

  return 0;
}

int
ACE_Token_Collection::acquire (const ACE_TCHAR *token_name,
                               int notify,
                               void (*sleep_hook)(void *),
                               ACE_Synch_Options &options)
{
  ACE_TRACE ("ACE_Token_Collection::acquire");
  TOKEN_NAME name (token_name);
  ACE_Token_Proxy *temp;
  // Get the token from the collection.
  int result = collection_.find (name, temp);
  // did we find it?
  if (result == -1)
    return result;
  // perform the operation
  return temp->acquire (notify, sleep_hook, options);
}


int
ACE_Token_Collection::tryacquire (const ACE_TCHAR *token_name,
                                  void (*sleep_hook)(void *))
{
  ACE_TRACE ("ACE_Token_Collection::tryacquire");
  TOKEN_NAME name (token_name);
  ACE_Token_Proxy *temp;
  // Get the token from the collection.
  int result = collection_.find (name, temp);
  // did we find it?
  if (result == -1)
    return result;

  // perform the operation
  return temp->tryacquire (sleep_hook);
}

int
ACE_Token_Collection::tryacquire (void (*sleep_hook)(void *))
{
  ACE_TRACE ("ACE_Token_Collection::tryacquire");

  COLLECTION::ITERATOR iterator (collection_);

  for (COLLECTION::ENTRY *temp = 0;
       iterator.next (temp) != 0;
       iterator.advance ())
    {
      if (debug_)
        ACELIB_DEBUG ((LM_DEBUG,  ACE_TEXT ("collection acquiring %s\n"),
                    temp->int_id_->name ()));
      // We will fail if _any_ token is not free.
      if (temp->int_id_->tryacquire (sleep_hook) == -1)
        return -1;
    }

  return 0;
}

int
ACE_Token_Collection::renew (int requeue_position,
                             ACE_Synch_Options &options)
{
  ACE_TRACE ("ACE_Token_Collection::renew");

  COLLECTION::ITERATOR iterator (collection_);

  for (COLLECTION::ENTRY *temp = 0;
       iterator.next (temp) != 0;
       iterator.advance ())
    {
      if (debug_)
        ACELIB_DEBUG ((LM_DEBUG,  ACE_TEXT ("collection renewing %s\n"),
                    temp->int_id_->name ()));
      if (temp->int_id_->renew (requeue_position, options) == -1)
        return -1;
    }

  return 0;
}

int
ACE_Token_Collection::renew (const ACE_TCHAR *token_name,
                             int requeue_position,
                             ACE_Synch_Options &options)
{
  ACE_TRACE ("ACE_Token_Collection::renew");
  TOKEN_NAME name (token_name);
  ACE_Token_Proxy *temp;

  // Get the token from the collection.
  int result = collection_.find (name, temp);

  // Did we find it?
  if (result == -1)
    ACELIB_ERROR_RETURN ((LM_DEBUG, ACE_TEXT ("%p %s\n"),
                       ACE_TEXT ("not in collection "),
                       token_name), -1);
  // perform the operation
  return temp->renew (requeue_position, options);
}

int
ACE_Token_Collection::release (ACE_Synch_Options &)

{
  ACE_TRACE ("ACE_Token_Collection::release");
  COLLECTION::ITERATOR iterator (collection_);

  for (COLLECTION::ENTRY *temp = 0;
       iterator.next (temp) != 0;
       iterator.advance ())
    {
      if (debug_)
        ACELIB_DEBUG ((LM_DEBUG,  ACE_TEXT ("collection releasing %s\n"),
                    temp->int_id_->name ()));
      temp->int_id_->release ();
    }

  return 0;
}

int
ACE_Token_Collection::release (const ACE_TCHAR *token_name,
                               ACE_Synch_Options &options)
{
  ACE_TRACE ("ACE_Token_Collection::release");
  TOKEN_NAME name (token_name);
  ACE_Token_Proxy *temp;
  // get the token from the collection
  int result = collection_.find (name, temp);
  // did we find it?
  if (result != 0)
    return result;
  // perform the operation
  return temp->release (options);
}

ACE_Token_Collection::~ACE_Token_Collection (void)
{
  ACE_TRACE ("ACE_Token_Collection::~ACE_Token_Collection");
  COLLECTION::ITERATOR iterator (collection_);

  for (COLLECTION::ENTRY *temp = 0;
       iterator.next (temp) != 0;
       iterator.advance ())
    {
      delete temp->int_id_;
      // The ext_id_'s delete themselves when the array of
      // COLLECTION::ENTRYs goes away.
    }
}


// This method doesn't mean anything for a collection.
ACE_Token_Proxy *
ACE_Token_Collection::clone () const
{
  ACE_TRACE ("ACE_Token_Collection::clone");
  return (ACE_Token_Proxy *) 0;
}

// This method doesn't mean anything for a collection.
ACE_Tokens *
ACE_Token_Collection::create_token (const ACE_TCHAR *)
{
  ACE_TRACE ("ACE_Token_Collection::create_token");
  return (ACE_Tokens *) 0;
}

void
ACE_Token_Collection::dump () const
{
#if defined (ACE_HAS_DUMP)
  ACE_TRACE ("ACE_Token_Collection::dump");
  ACELIB_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
  ACELIB_DEBUG ((LM_DEBUG,  ACE_TEXT ("ACE_Token_Collection::dump:\n")
              ACE_TEXT (" debug_ = %d\n"), debug_));
  ACELIB_DEBUG ((LM_DEBUG,  ACE_TEXT ("collection_\n")));
  collection_.dump ();
  ACELIB_DEBUG ((LM_DEBUG,  ACE_TEXT ("base:\n")));
  ACE_Token_Proxy::dump ();
  ACELIB_DEBUG ((LM_DEBUG, ACE_END_DUMP));
#endif /* ACE_HAS_DUMP */
}

ACE_END_VERSIONED_NAMESPACE_DECL

#endif /* ACE_HAS_TOKENS_LIBRARY */