mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-27 11:06:50 +01:00
Added condition variable and timeout to ObjectPool::borrowObject/returnObject to block caller until an object is available.
This commit is contained in:
@@ -22,6 +22,7 @@
|
|||||||
|
|
||||||
#include "Poco/Poco.h"
|
#include "Poco/Poco.h"
|
||||||
#include "Poco/Mutex.h"
|
#include "Poco/Mutex.h"
|
||||||
|
#include "Poco/Condition.h"
|
||||||
#include "Poco/AutoPtr.h"
|
#include "Poco/AutoPtr.h"
|
||||||
#include "Poco/SharedPtr.h"
|
#include "Poco/SharedPtr.h"
|
||||||
#include <vector>
|
#include <vector>
|
||||||
@@ -157,7 +158,7 @@ class ObjectPool
|
|||||||
/// removed from the pool, activated (using the factory) and returned.
|
/// removed from the pool, activated (using the factory) and returned.
|
||||||
/// - Otherwise, if the peak capacity of the pool has not yet been reached,
|
/// - Otherwise, if the peak capacity of the pool has not yet been reached,
|
||||||
/// a new object is created and activated, using the object factory, and returned.
|
/// a new object is created and activated, using the object factory, and returned.
|
||||||
/// - If the peak capacity has already been reached, null is returned.
|
/// - If the peak capacity has already been reached, null is returned after timeout.
|
||||||
///
|
///
|
||||||
/// When an object is returned to the pool:
|
/// When an object is returned to the pool:
|
||||||
/// - If the object is valid (checked by calling validateObject()
|
/// - If the object is valid (checked by calling validateObject()
|
||||||
@@ -207,33 +208,47 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
P borrowObject()
|
P borrowObject(long timeoutMilliseconds = 0)
|
||||||
/// Obtains an object from the pool, or creates a new object if
|
/// Obtains an object from the pool, or creates a new object if
|
||||||
/// possible.
|
/// possible.
|
||||||
///
|
///
|
||||||
/// Returns null if no object is available.
|
/// Returns null if no object is available after timeout.
|
||||||
///
|
///
|
||||||
/// If activating the object fails, the object is destroyed and
|
/// If activating the object fails, the object is destroyed and
|
||||||
/// the exception is passed on to the caller.
|
/// the exception is passed on to the caller.
|
||||||
{
|
{
|
||||||
Poco::FastMutex::ScopedLock lock(_mutex);
|
Poco::FastMutex::ScopedLock lock(_mutex);
|
||||||
|
|
||||||
if (!_pool.empty())
|
if (!_pool.empty())
|
||||||
{
|
{
|
||||||
P pObject = _pool.back();
|
P pObject = _pool.back();
|
||||||
_pool.pop_back();
|
_pool.pop_back();
|
||||||
return activateObject(pObject);
|
return activateObject(pObject);
|
||||||
}
|
}
|
||||||
else if (_size < _peakCapacity)
|
|
||||||
|
if (_size >= _peakCapacity)
|
||||||
{
|
{
|
||||||
P pObject = _factory.createObject();
|
if (timeoutMilliseconds == 0)
|
||||||
activateObject(pObject);
|
{
|
||||||
_size++;
|
return 0;
|
||||||
return pObject;
|
}
|
||||||
|
while (_size >= _peakCapacity)
|
||||||
|
{
|
||||||
|
if ( !_availableCondition.tryWait(_mutex, timeoutMilliseconds))
|
||||||
|
{
|
||||||
|
// timeout
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else return 0;
|
|
||||||
|
// _size < _peakCapacity
|
||||||
|
P pObject = _factory.createObject();
|
||||||
|
activateObject(pObject);
|
||||||
|
_size++;
|
||||||
|
return pObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
void returnObject(P pObject)
|
void returnObject(P pObject)
|
||||||
/// Returns an object to the pool.
|
/// Returns an object to the pool.
|
||||||
{
|
{
|
||||||
@@ -250,6 +265,7 @@ public:
|
|||||||
{
|
{
|
||||||
_factory.destroyObject(pObject);
|
_factory.destroyObject(pObject);
|
||||||
_size--;
|
_size--;
|
||||||
|
_availableCondition.signal();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -308,6 +324,7 @@ private:
|
|||||||
std::size_t _size;
|
std::size_t _size;
|
||||||
std::vector<P> _pool;
|
std::vector<P> _pool;
|
||||||
mutable Poco::FastMutex _mutex;
|
mutable Poco::FastMutex _mutex;
|
||||||
|
Poco::Condition _availableCondition;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -77,9 +77,9 @@ class PooledConnection
|
|||||||
/// Helper class for borrowing and returning a connection automatically from a pool.
|
/// Helper class for borrowing and returning a connection automatically from a pool.
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
PooledConnection(Poco::ObjectPool<Connection, Connection::Ptr>& pool) : _pool(pool)
|
PooledConnection(Poco::ObjectPool<Connection, Connection::Ptr>& pool, long timeoutMilliseconds = 0) : _pool(pool)
|
||||||
{
|
{
|
||||||
_connection = _pool.borrowObject();
|
_connection = _pool.borrowObject(timeoutMilliseconds);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual ~PooledConnection()
|
virtual ~PooledConnection()
|
||||||
|
|||||||
Reference in New Issue
Block a user