Skip to content

Commit

Permalink
Replacing use of unique_ptr<T> with T* in ObjectPool.
Browse files Browse the repository at this point in the history
This is done to enable building without std full supporting C++11.
PROBLEM: AutoFilterTest fails first test and hangs when exiting test.
  • Loading branch information
Gabriel Hare committed Aug 16, 2014
1 parent e66baf9 commit 94fd45b
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions autowiring/ObjectPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class ObjectPool
// time the ClearCachedEntities method is called, and causes entities which might be trying
// to return to the pool to instead free themselves.
size_t m_poolVersion;
std::vector<std::unique_ptr<T>> m_objs;
std::vector<T*> m_objs;

size_t m_maxPooled;
size_t m_limit;
Expand Down Expand Up @@ -129,19 +129,18 @@ class ObjectPool
// Finalize object before destruction or return to pool
final(*ptr);

// Default behavior will be to destroy the pointer
std::unique_ptr<T> unique(ptr);

// Hold the lock next, in order to ensure that destruction happens after the monitor
// lock is released.
std::lock_guard<std::mutex> lk(*monitor);

if(monitor->IsAbandoned())
if(monitor->IsAbandoned()) {
// Nothing we can do, monitor object abandoned already, just destroy the object
delete ptr;
return;
}

// Pass control to the Return method
static_cast<ObjectPool<T>*>(monitor->GetOwner())->Return(poolVersion, unique);
static_cast<ObjectPool<T>*>(monitor->GetOwner())->Return(poolVersion, ptr);
}
);

Expand All @@ -152,7 +151,7 @@ class ObjectPool
return retVal;
}

void Return(size_t poolVersion, std::unique_ptr<T>& unique) {
void Return(size_t poolVersion, T* ptr) {
// ASSERT: Object has already been finalized
// Always decrement the count when an object is no longer outstanding
assert(m_outstanding);
Expand All @@ -164,10 +163,9 @@ class ObjectPool

// Object pool needs to be capable of accepting another object as an input
m_objs.size() < m_maxPooled
) {
)
// Return the object to the pool:
m_objs.emplace_back(std::move(unique));
}
m_objs.push_back(ptr);

// If the new outstanding count is less than or equal to the limit, wake up any waiters:
if(m_outstanding <= m_limit)
Expand Down Expand Up @@ -196,8 +194,8 @@ class ObjectPool
}

// Transition from pooled to issued:
std::shared_ptr<T> iObj = Wrap(m_objs.back().release()); // Takes ownership
m_objs.pop_back(); // Removes non-referencing object
std::shared_ptr<T> iObj = Wrap(m_objs.back()); // Takes ownership
m_objs.pop_back(); // Remove unsafe reference
return iObj;
}

Expand Down Expand Up @@ -228,14 +226,11 @@ class ObjectPool
/// prior to this call.
/// </remarks>
void ClearCachedEntities(void) {
// Declare this first, so it's freed last:
std::vector<std::unique_ptr<T>> objs;

// Default destructor is using in object pool, so it is safe to destroy all
// in pool while holding lock.
std::lock_guard<std::mutex> lk(*m_monitor);
m_poolVersion++;
for (T* obj : m_objs)
delete obj;
m_objs.clear();
m_poolVersion++;
}

/// <summary>
Expand All @@ -257,6 +252,7 @@ class ObjectPool
return;

// Remove unique pointer
delete m_objs.back();
m_objs.pop_back();
}
}
Expand Down

0 comments on commit 94fd45b

Please sign in to comment.