~ubuntu-branches/ubuntu/wily/cxxtools/wily-proposed

« back to all changes in this revision

Viewing changes to include/cxxtools/pool.h

  • Committer: Bazaar Package Importer
  • Author(s): Kari Pahula
  • Date: 2008-06-16 12:24:28 UTC
  • mfrom: (3.1.3 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080616122428-7bllgyt1358u779r
Tags: 1.4.8-2
Made libcxxtools-dev depend on libcxxtools6, not libcxxtools5.

Show diffs side-by-side

added added

removed removed

Lines of Context:
84
84
              MutexLock lock(mypool->mutex);
85
85
              if (next == this)
86
86
              {
87
 
                if (reuse)
88
 
                {
 
87
                if (reuse && (mypool->maxCount == 0 || mypool->currentCount <= mypool->maxCount))
89
88
                  mypool->freePool.push(ptr);
90
 
                  if (mypool->maxcount > 0)
91
 
                    mypool->sem.post();
92
 
                }
93
89
                else
 
90
                {
94
91
                  delete ptr;
 
92
                  --mypool->currentCount;
 
93
                }
 
94
                mypool->objectAvailable.signal();
95
95
              }
96
96
              else
97
97
              {
183
183
 
184
184
  private:
185
185
    typedef std::stack<T*> objectcontainer_type;
186
 
    unsigned maxcount;
187
 
    Semaphore sem;
 
186
    unsigned currentCount;
 
187
    unsigned maxCount;
188
188
    objectcontainer_type freePool;
189
189
    mutable Mutex mutex;
 
190
    mutable Condition objectAvailable;
190
191
    CreatorType creator;
191
192
 
192
193
    // make non-copyable
196
197
  public:
197
198
    /// Create a Pool with a maximum count.
198
199
    /// If count is 0, no limit is used
199
 
    explicit Pool(unsigned _maxcount = 0, CreatorType _creator = CreatorType())
200
 
      : maxcount(_maxcount),
201
 
        sem(_maxcount),
 
200
    explicit Pool(unsigned _maxCount = 0, CreatorType _creator = CreatorType())
 
201
      : currentCount(0),
 
202
        maxCount(_maxCount),
202
203
        creator(_creator)
203
204
    { }
204
205
 
205
206
    /// create a Pool without limit and a special creator
206
207
    explicit Pool(CreatorType _creator)
207
 
      : maxcount(0),
208
 
        sem(0),
 
208
      : currentCount(0),
 
209
        maxCount(0),
209
210
        creator(_creator)
210
211
    { }
211
212
 
227
228
     */
228
229
    objectptr_type get()
229
230
    {
230
 
      if (maxcount > 0)
231
 
        sem.wait();
 
231
      MutexLock lock(mutex);
 
232
 
 
233
      if (maxCount > 0)
 
234
        while (freePool.empty() && currentCount >= maxCount)
 
235
          objectAvailable.wait(lock);
232
236
 
233
237
      T* obj;
234
238
 
235
 
      {
236
 
        MutexLock lock(mutex);
 
239
      if (freePool.empty())
 
240
      {
 
241
        obj = creator();
 
242
        ++currentCount;
 
243
      }
 
244
      else
 
245
      {
 
246
        obj = freePool.top();
 
247
        freePool.pop();
 
248
      }
237
249
 
238
 
        if (freePool.empty())
239
 
          obj = creator();
240
 
        else
241
 
        {
242
 
          obj = freePool.top();
243
 
          freePool.pop();
244
 
        }
245
 
      }
 
250
      lock.unlock();
246
251
 
247
252
      objectptr_type po(obj, *this);
248
253
      return po;
260
265
      while (freePool.size() > keep)
261
266
      {
262
267
        delete freePool.top();
 
268
        --currentCount;
263
269
        freePool.pop();
264
270
      }
 
271
      objectAvailable.signal();
265
272
    }
266
273
 
267
274
    unsigned getCurrentSize() const
268
275
    {
269
276
      MutexLock lock(mutex);
270
 
      return freePool.size();
271
 
    }
 
277
      return currentCount;
 
278
    }
 
279
 
 
280
    unsigned getMaximumSize() const
 
281
    { return maxCount; }
 
282
 
 
283
    void setMaximumSize(unsigned s)
 
284
    {
 
285
      MutexLock lock(mutex);
 
286
      maxCount = s;
 
287
      while (!freePool.empty() && currentCount > maxCount)
 
288
      {
 
289
        delete freePool.top();
 
290
        --currentCount;
 
291
        freePool.pop();
 
292
      }
 
293
 
 
294
    }
 
295
 
 
296
    CreatorType& getCreator()               { return creator; }
 
297
    const CreatorType& getCreator() const   { return creator; }
272
298
};
273
299
 
274
300
}