class ThreadSynchronizer acts to guard an arbitrary type of buffer between a producing and a consuming thread (note: it's all symmetric between the two thread types). More...
#include <online-nnet2-decoding-threaded.h>
Public Types | |
enum | ThreadType { kProducer, kConsumer } |
Public Member Functions | |
ThreadSynchronizer () | |
bool | Lock (ThreadType t) |
bool | UnlockSuccess (ThreadType t) |
bool | UnlockFailure (ThreadType t) |
void | SetAbort () |
~ThreadSynchronizer () | |
Private Member Functions | |
KALDI_DISALLOW_COPY_AND_ASSIGN (ThreadSynchronizer) | |
Private Attributes | |
bool | abort_ |
bool | producer_waiting_ |
bool | consumer_waiting_ |
std::mutex | mutex_ |
ThreadType | held_by_ |
Semaphore | producer_semaphore_ |
Semaphore | consumer_semaphore_ |
int32 | num_errors_ |
class ThreadSynchronizer acts to guard an arbitrary type of buffer between a producing and a consuming thread (note: it's all symmetric between the two thread types).
It has a similar interface to a mutex, except that instead of just Lock and Unlock, it has Lock, UnlockSuccess and UnlockFailure, and each function takes an argument kProducer or kConsumer to identify whether the producing or consuming thread is waiting.
The basic concept is that you lock the object; and if you discover the you're blocked because you're either trying to read an empty buffer or trying to write to a full buffer, you unlock with UnlockFailure; and this will cause your next call to Lock to block until the *other* thread has called Lock and then UnlockSuccess. However, if at that point the other thread calls Lock and then UnlockFailure, it is an error because you can't have both producing and consuming threads claiming that the buffer is full/empty. If you lock the object and were successful you call UnlockSuccess; and you call UnlockSuccess even if, for your own reasons, you ended up not changing the state of the buffer.
Definition at line 65 of file online-nnet2-decoding-threaded.h.
enum ThreadType |
Enumerator | |
---|---|
kProducer | |
kConsumer |
Definition at line 72 of file online-nnet2-decoding-threaded.h.
Definition at line 28 of file online-nnet2-decoding-threaded.cc.
References ThreadSynchronizer::consumer_semaphore_, ThreadSynchronizer::producer_semaphore_, and Semaphore::Signal().
~ThreadSynchronizer | ( | ) |
Definition at line 99 of file online-nnet2-decoding-threaded.cc.
|
private |
bool Lock | ( | ThreadType | t | ) |
Definition at line 37 of file online-nnet2-decoding-threaded.cc.
References ThreadSynchronizer::abort_, ThreadSynchronizer::consumer_semaphore_, ThreadSynchronizer::held_by_, ThreadSynchronizer::kProducer, ThreadSynchronizer::mutex_, ThreadSynchronizer::producer_semaphore_, and Semaphore::Wait().
Referenced by SingleUtteranceNnet2DecoderThreaded::AcceptWaveform(), SingleUtteranceNnet2DecoderThreaded::FeatureComputation(), SingleUtteranceNnet2DecoderThreaded::InputFinished(), SingleUtteranceNnet2DecoderThreaded::NumWaveformPiecesPending(), SingleUtteranceNnet2DecoderThreaded::RunDecoderSearchInternal(), and SingleUtteranceNnet2DecoderThreaded::RunNnetEvaluationInternal().
void SetAbort | ( | ) |
Definition at line 91 of file online-nnet2-decoding-threaded.cc.
References ThreadSynchronizer::abort_, ThreadSynchronizer::consumer_semaphore_, ThreadSynchronizer::producer_semaphore_, and Semaphore::Signal().
Referenced by SingleUtteranceNnet2DecoderThreaded::AbortAllThreads().
bool UnlockFailure | ( | ThreadType | t | ) |
Definition at line 76 of file online-nnet2-decoding-threaded.cc.
References ThreadSynchronizer::abort_, ThreadSynchronizer::consumer_waiting_, ThreadSynchronizer::held_by_, KALDI_ASSERT, ThreadSynchronizer::kProducer, ThreadSynchronizer::mutex_, and ThreadSynchronizer::producer_waiting_.
Referenced by SingleUtteranceNnet2DecoderThreaded::FeatureComputation(), SingleUtteranceNnet2DecoderThreaded::RunDecoderSearchInternal(), and SingleUtteranceNnet2DecoderThreaded::RunNnetEvaluationInternal().
bool UnlockSuccess | ( | ThreadType | t | ) |
Definition at line 57 of file online-nnet2-decoding-threaded.cc.
References ThreadSynchronizer::abort_, ThreadSynchronizer::consumer_semaphore_, ThreadSynchronizer::consumer_waiting_, ThreadSynchronizer::kProducer, ThreadSynchronizer::mutex_, ThreadSynchronizer::producer_semaphore_, ThreadSynchronizer::producer_waiting_, and Semaphore::Signal().
Referenced by SingleUtteranceNnet2DecoderThreaded::AcceptWaveform(), SingleUtteranceNnet2DecoderThreaded::FeatureComputation(), SingleUtteranceNnet2DecoderThreaded::InputFinished(), SingleUtteranceNnet2DecoderThreaded::NumWaveformPiecesPending(), SingleUtteranceNnet2DecoderThreaded::RunDecoderSearchInternal(), and SingleUtteranceNnet2DecoderThreaded::RunNnetEvaluationInternal().
|
private |
Definition at line 99 of file online-nnet2-decoding-threaded.h.
Referenced by ThreadSynchronizer::Lock(), ThreadSynchronizer::SetAbort(), ThreadSynchronizer::UnlockFailure(), and ThreadSynchronizer::UnlockSuccess().
|
private |
Definition at line 106 of file online-nnet2-decoding-threaded.h.
Referenced by ThreadSynchronizer::Lock(), ThreadSynchronizer::SetAbort(), ThreadSynchronizer::ThreadSynchronizer(), and ThreadSynchronizer::UnlockSuccess().
|
private |
Definition at line 101 of file online-nnet2-decoding-threaded.h.
Referenced by ThreadSynchronizer::UnlockFailure(), and ThreadSynchronizer::UnlockSuccess().
|
private |
Definition at line 103 of file online-nnet2-decoding-threaded.h.
Referenced by ThreadSynchronizer::Lock(), and ThreadSynchronizer::UnlockFailure().
|
private |
Definition at line 102 of file online-nnet2-decoding-threaded.h.
Referenced by ThreadSynchronizer::Lock(), ThreadSynchronizer::UnlockFailure(), and ThreadSynchronizer::UnlockSuccess().
|
private |
Definition at line 107 of file online-nnet2-decoding-threaded.h.
|
private |
Definition at line 105 of file online-nnet2-decoding-threaded.h.
Referenced by ThreadSynchronizer::Lock(), ThreadSynchronizer::SetAbort(), ThreadSynchronizer::ThreadSynchronizer(), and ThreadSynchronizer::UnlockSuccess().
|
private |
Definition at line 100 of file online-nnet2-decoding-threaded.h.
Referenced by ThreadSynchronizer::UnlockFailure(), and ThreadSynchronizer::UnlockSuccess().