mirror of
https://github.com/mat007/turtle.git
synced 2026-06-22 12:13:43 +00:00
Document mechanism used in the functor constructor
This commit is contained in:
parent
9e2223d4be
commit
68700d4c3a
1 changed files with 13 additions and 4 deletions
|
|
@ -27,16 +27,25 @@ namespace mock { namespace detail {
|
|||
functor()
|
||||
{
|
||||
scoped_lock _(functor_mutex);
|
||||
static functor* f = 0;
|
||||
// MOCK_FUNCTOR creates 2 functor objects:
|
||||
// The user-usable one with the passed name and a 2nd used by MOCK_EXPECT with a suffixed name
|
||||
// We need the 2nd to be a copy of the first and use a static variable for storing a pointer to the first
|
||||
static functor* f = nullptr;
|
||||
if(f)
|
||||
{
|
||||
*this = *f;
|
||||
f = 0;
|
||||
// Release the lock from the first call (see below) so other threads can create functors again
|
||||
// after the function exits (the scoped_lock still holds the mutex)
|
||||
functor_mutex.unlock();
|
||||
// Copy the first functor to the current (2nd) one
|
||||
*this = *f;
|
||||
f = nullptr;
|
||||
} else
|
||||
{
|
||||
functor_mutex.lock();
|
||||
// This is the first object, store its pointer
|
||||
f = this;
|
||||
// Lock the mutex again so only this thread can create new instances of a functor
|
||||
// making sure that we copy the right instance above and not one from a concurrent thread
|
||||
functor_mutex.lock();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue