SLikeNet
0.1.3
|
#include <slikenet/ThreadPool.h>
Public Member Functions | |
ThreadPool () | |
~ThreadPool () | |
bool | StartThreads (int numThreads, int stackSize, void *(*_perThreadInit)()=0, void(*_perThreadDeinit)(void *)=0) |
void | SetThreadDataInterface (ThreadDataInterface *tdi, void *context) |
void | StopThreads (void) |
Stops all threads. | |
void | AddInput (OutputType(*workerThreadCallback)(InputType, bool *returnOutput, void *perThreadData), InputType inputData) |
void | AddOutput (OutputType outputData) |
bool | HasOutput (void) |
bool | HasOutputFast (void) |
bool | HasInput (void) |
bool | HasInputFast (void) |
OutputType | GetOutput (void) |
void | Clear (void) |
Clears internal buffers. | |
void | LockInput (void) |
void | UnlockInput (void) |
Unlock the input buffer after you are done with the functions InputSize, GetInputAtIndex, and RemoveInputAtIndex. | |
unsigned | InputSize (void) |
Length of the input queue. | |
InputType | GetInputAtIndex (unsigned index) |
Get the input at a specified index. | |
void | RemoveInputAtIndex (unsigned index) |
Remove input from a specific index. This does NOT do memory deallocation - it only removes the item from the queue. | |
void | LockOutput (void) |
void | UnlockOutput (void) |
Unlock the output buffer after you are done with the functions OutputSize, GetOutputAtIndex, and RemoveOutputAtIndex. | |
unsigned | OutputSize (void) |
Length of the output queue. | |
OutputType | GetOutputAtIndex (unsigned index) |
Get the output at a specified index. | |
void | RemoveOutputAtIndex (unsigned index) |
Remove output from a specific index. This does NOT do memory deallocation - it only removes the item from the queue. | |
void | ClearInput (void) |
Removes all items from the input queue. | |
void | ClearOutput (void) |
Removes all items from the output queue. | |
bool | IsWorking (void) |
Are any of the threads working, or is input or output available? | |
int | NumThreadsWorking (void) |
The number of currently active threads. | |
bool | WasStarted (void) |
Did we call Start? | |
bool | Pause (void) |
void | Resume (void) |
Protected Member Functions | |
template<class ThreadInputType , class ThreadOutputType > | |
friend | RAK_THREAD_DECLARATION (WorkerThread) |
Protected Attributes | |
SLNet::SimpleMutex | inputQueueMutex |
SLNet::SimpleMutex | outputQueueMutex |
SLNet::SimpleMutex | workingThreadCountMutex |
SLNet::SimpleMutex | runThreadsMutex |
void *(* | perThreadDataFactory )() |
void(* | perThreadDataDestructor )(void *) |
DataStructures::Queue < OutputType(*)(InputType, bool *, void *)> | inputFunctionQueue |
DataStructures::Queue< InputType > | inputQueue |
DataStructures::Queue< OutputType > | outputQueue |
ThreadDataInterface * | threadDataInterface |
void * | tdiContext |
bool | runThreads |
int | numThreadsRunning |
int | numThreadsWorking |
SLNet::SimpleMutex | numThreadsRunningMutex |
SLNet::SignaledEvent | quitAndIncomingDataEvents |
A simple class to create worker threads that processes a queue of functions with data. This class does not allocate or deallocate memory. It is up to the user to handle memory management. InputType and OutputType are stored directly in a queue. For large structures, if you plan to delete from the middle of the queue, you might wish to store pointers rather than the structures themselves so the array can shift efficiently.
Definition at line 40 of file ThreadPool.h.
ThreadPool< InputType, OutputType >::ThreadPool | ( | ) |
Definition at line 307 of file ThreadPool.h.
References numThreadsRunning.
ThreadPool< InputType, OutputType >::~ThreadPool | ( | ) |
Definition at line 317 of file ThreadPool.h.
void ThreadPool< InputType, OutputType >::AddInput | ( | OutputType(*)(InputType, bool *returnOutput, void *perThreadData) | workerThreadCallback, |
InputType | inputData | ||
) |
Adds a function to a queue with data to pass to that function. This function will be called from the thread Memory management is your responsibility! This class does not allocate or deallocate memory. The best way to deallocate inputData is in userCallback. If you call EndThreads such that callbacks were not called, you can iterate through the inputQueue and deallocate all pending input data there The best way to deallocate output is as it is returned to you from GetOutput. Similarly, if you end the threads such that not all output was returned, you can iterate through outputQueue and deallocate it there.
[in] | workerThreadCallback | The function to call from the thread |
[in] | inputData | The parameter to pass to userCallback |
Definition at line 422 of file ThreadPool.h.
References _FILE_AND_LINE_.
void ThreadPool< InputType, OutputType >::AddOutput | ( | OutputType | outputData | ) |
Adds to the output queue Use it if you want to inject output into the same queue that the system uses. Normally you would not use this. Consider it a convenience function.
[in] | outputData | The output to inject |
Definition at line 432 of file ThreadPool.h.
References _FILE_AND_LINE_.
void ThreadPool< InputType, OutputType >::Clear | ( | void | ) |
void ThreadPool< InputType, OutputType >::ClearInput | ( | void | ) |
Removes all items from the input queue.
Definition at line 551 of file ThreadPool.h.
References _FILE_AND_LINE_.
void ThreadPool< InputType, OutputType >::ClearOutput | ( | void | ) |
Removes all items from the output queue.
Definition at line 558 of file ThreadPool.h.
References _FILE_AND_LINE_.
InputType ThreadPool< InputType, OutputType >::GetInputAtIndex | ( | unsigned | index | ) |
Get the input at a specified index.
Definition at line 515 of file ThreadPool.h.
OutputType ThreadPool< InputType, OutputType >::GetOutput | ( | void | ) |
Gets the output of a call to userCallback HasOutput must return true before you call this function. Otherwise it will assert.
Definition at line 467 of file ThreadPool.h.
OutputType ThreadPool< InputType, OutputType >::GetOutputAtIndex | ( | unsigned | index | ) |
Get the output at a specified index.
Definition at line 541 of file ThreadPool.h.
bool ThreadPool< InputType, OutputType >::HasInput | ( | void | ) |
Returns true if input from GetInput is waiting.
Definition at line 458 of file ThreadPool.h.
bool ThreadPool< InputType, OutputType >::HasInputFast | ( | void | ) |
Inaccurate but fast version of HasInput. If this returns true, you should still check HasInput for the real value.
Definition at line 453 of file ThreadPool.h.
bool ThreadPool< InputType, OutputType >::HasOutput | ( | void | ) |
Returns true if output from GetOutput is waiting.
Definition at line 444 of file ThreadPool.h.
bool ThreadPool< InputType, OutputType >::HasOutputFast | ( | void | ) |
Inaccurate but fast version of HasOutput. If this returns true, you should still check HasOutput for the real value.
Definition at line 439 of file ThreadPool.h.
unsigned ThreadPool< InputType, OutputType >::InputSize | ( | void | ) |
Length of the input queue.
Definition at line 510 of file ThreadPool.h.
bool ThreadPool< InputType, OutputType >::IsWorking | ( | void | ) |
Are any of the threads working, or is input or output available?
Definition at line 563 of file ThreadPool.h.
void ThreadPool< InputType, OutputType >::LockInput | ( | void | ) |
Lock the input buffer before calling the functions InputSize, InputAtIndex, and RemoveInputAtIndex It is only necessary to lock the input or output while the threads are running
Definition at line 500 of file ThreadPool.h.
void ThreadPool< InputType, OutputType >::LockOutput | ( | void | ) |
Lock the output buffer before calling the functions OutputSize, OutputAtIndex, and RemoveOutputAtIndex It is only necessary to lock the input or output while the threads are running
Definition at line 526 of file ThreadPool.h.
int ThreadPool< InputType, OutputType >::NumThreadsWorking | ( | void | ) |
The number of currently active threads.
Definition at line 591 of file ThreadPool.h.
unsigned ThreadPool< InputType, OutputType >::OutputSize | ( | void | ) |
Length of the output queue.
Definition at line 536 of file ThreadPool.h.
bool ThreadPool< InputType, OutputType >::Pause | ( | void | ) |
Definition at line 606 of file ThreadPool.h.
References RakSleep().
|
protected |
void ThreadPool< InputType, OutputType >::RemoveInputAtIndex | ( | unsigned | index | ) |
Remove input from a specific index. This does NOT do memory deallocation - it only removes the item from the queue.
Definition at line 520 of file ThreadPool.h.
void ThreadPool< InputType, OutputType >::RemoveOutputAtIndex | ( | unsigned | index | ) |
Remove output from a specific index. This does NOT do memory deallocation - it only removes the item from the queue.
Definition at line 546 of file ThreadPool.h.
void ThreadPool< InputType, OutputType >::Resume | ( | void | ) |
Definition at line 619 of file ThreadPool.h.
void ThreadPool< InputType, OutputType >::SetThreadDataInterface | ( | ThreadDataInterface * | tdi, |
void * | context | ||
) |
Definition at line 382 of file ThreadPool.h.
bool ThreadPool< InputType, OutputType >::StartThreads | ( | int | numThreads, |
int | stackSize, | ||
void *(*)() | _perThreadInit = 0 , |
||
void(*)(void *) | _perThreadDeinit = 0 |
||
) |
Start the specified number of threads.
[in] | numThreads | The number of threads to start |
[in] | stackSize | 0 for default (except on consoles). |
[in] | _perThreadInit | User callback to return data stored per thread. Pass 0 if not needed. |
[in] | _perThreadDeinit | User callback to destroy data stored per thread, created by _perThreadInit. Pass 0 if not needed. |
Definition at line 323 of file ThreadPool.h.
References SLNet::RakThread::Create(), numThreadsRunning, and RakSleep().
void ThreadPool< InputType, OutputType >::StopThreads | ( | void | ) |
Stops all threads.
Definition at line 388 of file ThreadPool.h.
References numThreadsRunning, and RakSleep().
void ThreadPool< InputType, OutputType >::UnlockInput | ( | void | ) |
Unlock the input buffer after you are done with the functions InputSize, GetInputAtIndex, and RemoveInputAtIndex.
Definition at line 505 of file ThreadPool.h.
void ThreadPool< InputType, OutputType >::UnlockOutput | ( | void | ) |
Unlock the output buffer after you are done with the functions OutputSize, GetOutputAtIndex, and RemoveOutputAtIndex.
Definition at line 531 of file ThreadPool.h.
bool ThreadPool< InputType, OutputType >::WasStarted | ( | void | ) |
Did we call Start?
Definition at line 597 of file ThreadPool.h.
|
protected |
Definition at line 161 of file ThreadPool.h.
Referenced by for().
|
protected |
Definition at line 162 of file ThreadPool.h.
Referenced by for().
|
protected |
Definition at line 154 of file ThreadPool.h.
Referenced by for().
|
protected |
Definition at line 183 of file ThreadPool.h.
|
protected |
Definition at line 187 of file ThreadPool.h.
|
protected |
Definition at line 185 of file ThreadPool.h.
Referenced by for().
|
protected |
Definition at line 163 of file ThreadPool.h.
Referenced by for().
|
protected |
Definition at line 154 of file ThreadPool.h.
Referenced by for().
|
protected |
Definition at line 157 of file ThreadPool.h.
|
protected |
Definition at line 156 of file ThreadPool.h.
|
protected |
Definition at line 189 of file ThreadPool.h.
Referenced by for().
|
protected |
Definition at line 181 of file ThreadPool.h.
Referenced by for().
|
protected |
Definition at line 154 of file ThreadPool.h.
Referenced by for().
|
protected |
Definition at line 166 of file ThreadPool.h.
|
protected |
Definition at line 165 of file ThreadPool.h.
|
protected |
Definition at line 154 of file ThreadPool.h.
Referenced by for().