![]() |
KSquare Utilities
|
A simple/ light-weight implementation of critical section blocking. More...
#include <GoalKeeperSimple.h>
Public Types | |
| typedef GoalKeeperSimple * | GoalKeeperSimplePtr |
Public Member Functions | |
| bool | Blocked () |
| Will return true if any thread lock on this instance of "GoalKeeperSimple". More... | |
| bool | BlockedByAnotherThread () |
| Returns true if a different thread has this instance of "GoalKeeperSimple" locked. More... | |
| kkint32 | BlockerThreadId () |
| ThreadId of thread that currently holds the Block -1 indicates no Block. More... | |
| void | EndBlock () |
| Ends the block and allows other threads to pass through StatBlock. More... | |
| kkint32 | MemoryConsumedEstimated () const |
| const KKStr & | Name () const |
| void | StartBlock () |
| Initiates a Block as long as another thread has not already locked this object. More... | |
Static Public Member Functions | |
| static void | Create (const KKStr &_name, volatile GoalKeeperSimplePtr &_newGoalKeeper) |
| Create a GoalKeeperSimple object and avoid a race condition doing it. More... | |
| static void | CreateAndStartBlock (const KKStr &_name, volatile GoalKeeperSimplePtr &_newGoalKeeper, bool &_didNotExistYet) |
| Create a GoalKeeperSimple object and avoid a race condition doing it. More... | |
| static void | Destroy (volatile GoalKeeperSimplePtr &_goalKeeperInstance) |
| Destroys an existing instance of GoalKeeperSimple. More... | |
| static void | FinalCleanUp () |
| Will be registered with 'atexit' so that it will be called when program is unloaded from memory. More... | |
Friends | |
| class | KKQueue< GoalKeeperSimple > |
A simple/ light-weight implementation of critical section blocking.
Definition at line 29 of file GoalKeeperSimple.h.
Definition at line 32 of file GoalKeeperSimple.h.
| bool GoalKeeperSimple::Blocked | ( | ) |
Will return true if any thread lock on this instance of "GoalKeeperSimple".
Definition at line 76 of file GoalKeeperSimple.cpp.
| bool GoalKeeperSimple::BlockedByAnotherThread | ( | ) |
Returns true if a different thread has this instance of "GoalKeeperSimple" locked.
GoalKeeperSimple keeps track of which thread has a lock on this instance of 'GoalKeeperSimple'. This way we know if the calling thread is not the one to have a lock on the thread.
Definition at line 83 of file GoalKeeperSimple.cpp.
References KKB::osGetThreadId().
| kkint32 GoalKeeperSimple::BlockerThreadId | ( | ) |
ThreadId of thread that currently holds the Block -1 indicates no Block.
Definition at line 404 of file GoalKeeperSimple.cpp.
|
static |
Create a GoalKeeperSimple object and avoid a race condition doing it.
In case two different threads try to create the same GoalKeeperSimple at the same time you only want one of them to succeed and the other to use same GaolKeeper object.
| [in] | _name | Name of Goal Keeper object that you want to create. |
| [in,out] | _newGoalKeeper | You pass this in. Create will block out the critical region that creates the GoalKeeperSimple object. If it was already created, that is != NULL will just return not changing its value. If it is still NULL in the critical section it will create a new instance and set this parameter to its address. |
< default security attributes
< initially not owned
Definition at line 160 of file GoalKeeperSimple.cpp.
References KKB::GoalKeeperSimpleList::GoalKeeperSimpleList().
Referenced by KKB::MsgQueue::MsgQueue().
|
static |
Create a GoalKeeperSimple object and avoid a race condition doing it.
Create a new instance of a GoalKeeperSimple object if it has not already been done and locks it if we create it.
Similar to 'Create' except it will also call the StartBlock method. There is also an additional parameter that will let you know if your call was responsible for creating it.
In case two different threads try to create the same GoalKeeperSimple at the same time you only want one of them to succeed and the other to use same GaolKeeper object.
| [in] | _name | Name of Goal Keeper object that you want to create. |
| [in,out] | _newGoalKeeper | You pass this in. Create will block out the critical region that creates the GoalKeeperSimple object. If it was already created, that is != NULL will just return not changing its value. If it is still NULL in the critical section it will create a new instance and set this parameter to its address. |
| [out] | _didNotExistYet | Indicates if this call had to create the GoalKeeperSimple instance; if it already existed will return as false. |
| [in] | _name | Name to be assigned to GoalKeeperSimple object. |
| [in,out] | _newGoalKeeper | A pointer to the GoalKeeperSimple that already exists or to new one that got created. |
| [out] | _didNotExistYet | Indicates if the call to 'CreateAndStartBlock' had to create a new instance. |
< default security attributes.
< initially not owned.
Definition at line 231 of file GoalKeeperSimple.cpp.
References KKB::GoalKeeperSimpleList::GoalKeeperSimpleList(), and StartBlock().
|
static |
Destroys an existing instance of GoalKeeperSimple.
Use this method rather than calling the destructor directly. This way the 'existingGoalKeepers' data member can be kept up to date. If for some reason two different threads managed to call this method for the same GoalKeeperSimple instance only one of them will actually destroy the instance.
| [in,out] | _goalKeeperInstance | Instance of GoalKeeperSimple that is to be destroyed. Upon return it will be set to NULL. |
< default security attributes
< initially not owned
Definition at line 313 of file GoalKeeperSimple.cpp.
Referenced by KKB::MsgQueue::~MsgQueue().
| void GoalKeeperSimple::EndBlock | ( | ) |
Ends the block and allows other threads to pass through StatBlock.
Decrements the variable 'blockerDepth' by one. Once 'blockerDepth' is equal zero the Block on this instance is removed. The idea is that for each time in a row a Thread calls StartBlock it has to call EndBlock the same number of times.
Definition at line 139 of file GoalKeeperSimple.cpp.
References KKB::KKException::KKException(), KKB::KKStr::operator+(), KKB::operator+(), and KKB::osGetThreadId().
Referenced by KKB::MsgQueue::AddMsg(), KKB::MsgQueue::AddMsgs(), KKB::MsgQueue::GetAllMsgs(), KKB::MsgQueue::GetCopyOfLastMsg(), KKB::MsgQueue::GetNextMsg(), and KKB::MsgQueue::MemoryConsumedEstimated().
|
static |
Will be registered with 'atexit' so that it will be called when program is unloaded from memory.
Definition at line 392 of file GoalKeeperSimple.cpp.
| kkint32 GoalKeeperSimple::MemoryConsumedEstimated | ( | ) | const |
Definition at line 69 of file GoalKeeperSimple.cpp.
References KKB::KKStr::MemoryConsumedEstimated().
Referenced by KKB::MsgQueue::MsgQueue().
|
inline |
Definition at line 122 of file GoalKeeperSimple.h.
| void GoalKeeperSimple::StartBlock | ( | ) |
Initiates a Block as long as another thread has not already locked this object.
If it is already blocked processor will sleep and then try again. As long as the variable 'blockerDepth'is greater than zero this instance will be considered blocked. Once a thread has the instance blocked it will increment 'blockerDepth' and return to caller.
Definition at line 117 of file GoalKeeperSimple.cpp.
References KKB::osGetThreadId().
Referenced by KKB::MsgQueue::AddMsg(), KKB::MsgQueue::AddMsgs(), CreateAndStartBlock(), KKB::MsgQueue::GetAllMsgs(), KKB::MsgQueue::GetCopyOfLastMsg(), KKB::MsgQueue::GetNextMsg(), and KKB::MsgQueue::MemoryConsumedEstimated().
|
friend |
Definition at line 41 of file GoalKeeperSimple.h.