SequentialTableReaderBackgroundImpl< Holder > Class Template Reference

#include <kaldi-table-inl.h>

Inheritance diagram for SequentialTableReaderBackgroundImpl< Holder >:
Collaboration diagram for SequentialTableReaderBackgroundImpl< Holder >:

Public Types

typedef Holder::T T
 
- Public Types inherited from SequentialTableReaderImplBase< Holder >
typedef Holder::T T
 

Public Member Functions

 SequentialTableReaderBackgroundImpl (SequentialTableReaderImplBase< Holder > *base_reader)
 
virtual bool Open (const std::string &rxfilename)
 
virtual bool IsOpen () const
 
void RunInBackground ()
 
virtual bool Done () const
 
virtual std::string Key ()
 
virtual TValue ()
 
void SwapHolder (Holder *other_holder)
 
virtual void FreeCurrent ()
 
virtual void Next ()
 
virtual bool Close ()
 
 ~SequentialTableReaderBackgroundImpl ()
 
- Public Member Functions inherited from SequentialTableReaderImplBase< Holder >
 SequentialTableReaderImplBase ()
 
virtual ~SequentialTableReaderImplBase ()
 

Static Public Member Functions

static void run (SequentialTableReaderBackgroundImpl< Holder > *object)
 

Private Attributes

std::string key_
 
Holder holder_
 
Semaphore consumer_sem_
 
Semaphore producer_sem_
 
std::thread thread_
 
SequentialTableReaderImplBase< Holder > * base_reader_
 

Detailed Description

template<class Holder>
class kaldi::SequentialTableReaderBackgroundImpl< Holder >

Definition at line 702 of file kaldi-table-inl.h.

Member Typedef Documentation

◆ T

typedef Holder::T T

Definition at line 705 of file kaldi-table-inl.h.

Constructor & Destructor Documentation

◆ SequentialTableReaderBackgroundImpl()

Definition at line 707 of file kaldi-table-inl.h.

708  :
709  base_reader_(base_reader) {}
SequentialTableReaderImplBase< Holder > * base_reader_

◆ ~SequentialTableReaderBackgroundImpl()

Definition at line 835 of file kaldi-table-inl.h.

References SequentialTableReaderImplBase< Holder >::Close(), and KALDI_ERR.

835  {
836  if (base_reader_) {
837  if (!Close()) {
838  KALDI_ERR << "Error detected closing background reader "
839  << "(relates to ',bg' modifier)";
840  }
841  }
842  }
SequentialTableReaderImplBase< Holder > * base_reader_
#define KALDI_ERR
Definition: kaldi-error.h:147

Member Function Documentation

◆ Close()

virtual bool Close ( )
inlinevirtual

Implements SequentialTableReaderImplBase< Holder >.

Definition at line 816 of file kaldi-table-inl.h.

References KALDI_ASSERT.

816  {
817  KALDI_ASSERT(base_reader_ != NULL && thread_.joinable());
818  // wait until the producer thread is idle.
820  bool ans = true;
821  try {
822  ans = base_reader_->Close();
823  } catch (...) {
824  ans = false;
825  }
826  delete base_reader_;
827  // setting base_reader_ to NULL will cause the loop in the producer thread
828  // to exit.
829  base_reader_ = NULL;
831 
832  thread_.join();
833  return ans;
834  }
SequentialTableReaderImplBase< Holder > * base_reader_
void Signal()
increase the counter
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
void Wait()
decrease the counter

◆ Done()

virtual bool Done ( ) const
inlinevirtual

Implements SequentialTableReaderImplBase< Holder >.

Definition at line 771 of file kaldi-table-inl.h.

771  {
772  return key_.empty();
773  }

◆ FreeCurrent()

virtual void FreeCurrent ( )
inlinevirtual

Implements SequentialTableReaderImplBase< Holder >.

Definition at line 787 of file kaldi-table-inl.h.

References KALDI_ERR.

787  {
788  if (key_.empty())
789  KALDI_ERR << "Calling FreeCurrent() at the wrong time.";
790  // note: ideally a call to Value() should crash if you have just called
791  // FreeCurrent(). For typical holders such as KaldiObjectHolder this will
792  // happen inside the holder_.Value() call. This won't be the case for all
793  // holders, but it's not a great loss (just a missed opportunity to spot a
794  // code error).
795  holder_.Clear();
796  }
#define KALDI_ERR
Definition: kaldi-error.h:147

◆ IsOpen()

virtual bool IsOpen ( ) const
inlinevirtual

Implements SequentialTableReaderImplBase< Holder >.

Definition at line 727 of file kaldi-table-inl.h.

727  {
728  // Close() sets base_reader_ to NULL, and we never initialize this object
729  // with a non-open base_reader_, so no need to check if it's open.
730  return base_reader_ != NULL;
731  }
SequentialTableReaderImplBase< Holder > * base_reader_

◆ Key()

virtual std::string Key ( )
inlinevirtual

Implements SequentialTableReaderImplBase< Holder >.

Definition at line 774 of file kaldi-table-inl.h.

References KALDI_ERR.

774  {
775  if (key_.empty())
776  KALDI_ERR << "Calling Key() at the wrong time.";
777  return key_;
778  }
#define KALDI_ERR
Definition: kaldi-error.h:147

◆ Next()

virtual void Next ( )
inlinevirtual

Implements SequentialTableReaderImplBase< Holder >.

Definition at line 797 of file kaldi-table-inl.h.

References KALDI_ERR.

797  {
799  if (base_reader_ == NULL || !base_reader_->IsOpen())
800  KALDI_ERR << "Error detected (likely code error) in background "
801  << "reader (',bg' option)";
802  if (base_reader_->Done()) {
803  // there is nothing else to read.
804  key_ = "";
805  } else {
806  key_ = base_reader_->Key();
807  base_reader_->SwapHolder(&holder_);
808  }
809  // this Signal() tells the producer thread, in the background,
810  // that it's now safe to read the next value.
812  }
SequentialTableReaderImplBase< Holder > * base_reader_
void Signal()
increase the counter
#define KALDI_ERR
Definition: kaldi-error.h:147
void Wait()
decrease the counter

◆ Open()

virtual bool Open ( const std::string &  rxfilename)
inlinevirtual

Implements SequentialTableReaderImplBase< Holder >.

Definition at line 714 of file kaldi-table-inl.h.

References KALDI_ASSERT, and SequentialTableReaderImplBase< Holder >::Next().

714  {
715  KALDI_ASSERT(base_reader_ != NULL &&
716  base_reader_->IsOpen()); // or code error.
717  {
719  this);
720  }
721 
722  if (!base_reader_->Done())
723  Next();
724  return true;
725  }
SequentialTableReaderImplBase< Holder > * base_reader_
static void run(SequentialTableReaderBackgroundImpl< Holder > *object)
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ run()

static void run ( SequentialTableReaderBackgroundImpl< Holder > *  object)
inlinestatic

Definition at line 768 of file kaldi-table-inl.h.

768  {
769  object->RunInBackground();
770  }

◆ RunInBackground()

void RunInBackground ( )
inline

Definition at line 733 of file kaldi-table-inl.h.

733  {
734  try {
735  // This function is called in the background thread. The whole point of
736  // the background thread is that we don't want to do the actual reading
737  // (inside Next()) in the foreground.
738  while (base_reader_ != NULL && !base_reader_->Done()) {
740  // Here is where the consumer process (parent thread) gets to do its
741  // stuff. Principally it calls SwapHolder()-- a shallow swap that is
742  // cheap.
744  // we check that base_reader_ is not NULL in case Close() was
745  // called in the main thread.
746  if (base_reader_ != NULL)
747  base_reader_->Next(); // here is where the work happens.
748  }
749  // this signal will be waited on in the Next() function of the foreground
750  // thread if it is still running, or Close() otherwise.
752  // this signal may be waited on in Close().
754  } catch (...) {
755  // There is nothing we called above that could potentially throw due to
756  // user data. So we treat reaching this point as a code-error condition.
757  // Closing base_reader_ will trigger an exception in Next() in the main
758  // thread when it checks that base_reader_->IsOpen().
759  if (base_reader_->IsOpen()) {
760  base_reader_->Close();
761  delete base_reader_;
762  base_reader_ = NULL;
763  }
765  return;
766  }
767  }
SequentialTableReaderImplBase< Holder > * base_reader_
void Signal()
increase the counter
void Wait()
decrease the counter

◆ SwapHolder()

void SwapHolder ( Holder *  other_holder)
inlinevirtual

Implements SequentialTableReaderImplBase< Holder >.

Definition at line 784 of file kaldi-table-inl.h.

References KALDI_ERR.

784  {
785  KALDI_ERR << "SwapHolder() should not be called on this class.";
786  }
#define KALDI_ERR
Definition: kaldi-error.h:147

◆ Value()

virtual T& Value ( )
inlinevirtual

Implements SequentialTableReaderImplBase< Holder >.

Definition at line 779 of file kaldi-table-inl.h.

References KALDI_ERR.

779  {
780  if (key_.empty())
781  KALDI_ERR << "Calling Value() at the wrong time.";
782  return holder_.Value();
783  }
#define KALDI_ERR
Definition: kaldi-error.h:147

Member Data Documentation

◆ base_reader_

SequentialTableReaderImplBase<Holder>* base_reader_
private

Definition at line 852 of file kaldi-table-inl.h.

◆ consumer_sem_

Semaphore consumer_sem_
private

Definition at line 849 of file kaldi-table-inl.h.

◆ holder_

Holder holder_
private

Definition at line 845 of file kaldi-table-inl.h.

◆ key_

std::string key_
private

Definition at line 844 of file kaldi-table-inl.h.

◆ producer_sem_

Semaphore producer_sem_
private

Definition at line 850 of file kaldi-table-inl.h.

◆ thread_

std::thread thread_
private

Definition at line 851 of file kaldi-table-inl.h.


The documentation for this class was generated from the following file: