nidas v1.2.3
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes | List of all members
nidas::core::SampleSorter Class Reference

A SampleClient that sorts its received samples, using an STL multiset, and then sends the sorted samples onto its SampleClients. More...

#include <SampleSorter.h>

Inheritance diagram for nidas::core::SampleSorter:
Inheritance graph
[legend]

Public Types

enum  runStatus { RUN_CANCELED = -1 , RUN_OK = 0 , NOT_RUNNING = 1 , RUN_EXCEPTION = 2 }
 Values that can be returned by run method. More...
 
enum  SchedPolicy { NU_THREAD_OTHER =SCHED_OTHER , NU_THREAD_FIFO =SCHED_FIFO , NU_THREAD_RR =SCHED_RR }
 

Public Member Functions

 SampleSorter (const std::string &name, bool raw)
 Constructor.
 
virtual ~SampleSorter ()
 
void setKeepStats (bool val)
 
bool getKeepStats () const
 
SampleSourcegetRawSampleSource ()
 Several objects in NIDAS can be both a SampleSource of raw Samples and processed Samples.
 
SampleSourcegetProcessedSampleSource ()
 Several objects in NIDAS can be both a SampleSource of raw Samples and processed Samples.
 
void addSampleTag (const SampleTag *tag) throw ()
 How to tell this SampleSorter what sample tags it will be sorting.
 
void removeSampleTag (const SampleTag *tag) throw ()
 
std::list< const SampleTag * > getSampleTags () const
 Implementation of SampleSource::getSampleTags().
 
SampleTagIterator getSampleTagIterator () const
 Implementation of SampleSource::getSampleTagIterator().
 
void addSampleClient (SampleClient *client) throw ()
 Implementation of SampleSource::addSampleClient().
 
void removeSampleClient (SampleClient *client) throw ()
 Remove a SampleClient from this SampleSource.
 
void addSampleClientForTag (SampleClient *client, const SampleTag *tag) throw ()
 Add a Client for a given SampleTag.
 
void removeSampleClientForTag (SampleClient *client, const SampleTag *tag) throw ()
 Remove a SampleClient for a given SampleTag from this SampleSource.
 
int getClientCount () const throw ()
 How many SampleClients are currently in my list.
 
const SampleStatsgetSampleStats () const
 
void flush () throw ()
 Implementation of SampleSource::flush().
 
void interrupt ()
 Interrupt sorting thread.
 
bool receive (const Sample *s) throw ()
 Implementation of SampleClient::receive().
 
size_t size () const
 Current number of samples in the sorter.
 
void setLengthSecs (float val)
 
float getLengthSecs () const
 
void setHeapMax (size_t val)
 Set the maximum amount of heap memory to use for sorting samples.
 
size_t getHeapMax () const
 
size_t getHeapSize () const
 Get the current amount of heap being used for sorting.
 
void setHeapBlock (bool val)
 
bool getHeapBlock () const
 
size_t getNumDiscardedSamples () const
 Number of samples discarded because of _heapSize > _heapMax and heapBlock == true.
 
size_t getNumFutureSamples () const
 Number of samples discarded because their timetags were in the future.
 
size_t getNumEarlySamples () const
 Number of early samples, which may not be sorted.
 
void setRealTime (bool val)
 Is this sorter running in real-time? If so then we can screen for bad time-tags by checking against the system clock, which is trusted.
 
bool getRealTime () const
 
void setLateSampleCacheSize (unsigned int val)
 When aging-off samples, cache this number of samples with the latest time tags.
 
unsigned int getLateSampleCacheSize () const
 
virtual void start ()
 Start the thread running, meaning execute the run method in a separate thread.
 
virtual int join ()
 The calling thread joins this thread, waiting until the thread finishes, which means either that the run() method returned, the thread called pthread_exit() or the thread was cancelled.
 
virtual void kill (int sig)
 Send a signal to this thread.
 
virtual void cancel ()
 Cancel this thread.
 
virtual bool isInterrupted () const
 Return true when this thread has been interrupted.
 
virtual bool isRunning () const
 Is this thread running?
 
virtual bool isJoined () const
 Has this thread been joined?
 
virtual bool isDetached () const
 Is this a detached thread.
 
bool isCancelEnabled () const
 Return true if the cancel state of this thread is PTHREAD_CANCEL_ENABLE.
 
bool isCancelDeferred () const
 Return true if the cancel type of this thread is PTHREAD_CANCEL_DEFERRED.
 
const std::string & getName () const throw ()
 Return the name of this thread.
 
const std::string & getFullName () throw ()
 Return a name with a bunch of descriptive fields, specifying whether it is detached, the real-time priority, etc.
 
bool setRealTimeRoundRobinPriority (int val)
 
bool setRealTimeFIFOPriority (int val)
 
bool setNonRealTimePriority ()
 
void setThreadScheduler (enum SchedPolicy policy, int priority)
 
void blockSignal (int)
 Block a signal in this thread.
 
void unblockSignal (int)
 Install a signal handler and unblock the signal.
 

Static Public Member Functions

static ThreadcurrentThread ()
 Return the thread object for the current thread.
 
static pthread_t currentThreadId ()
 
static ThreadlookupThread (pthread_t id)
 
static const std::string & currentName ()
 Convenience routine to return the name for the current thread, or a string indicating that the name of the thread is unknown.
 
static std::string getPolicyString (int policy)
 Convenience function to return a string for the given scheduler policy: "Non-RT", "RT:FIFO", "RT:RR" or "RT:Unknown".
 
static int test (int argc, char **argv)
 a test method.
 

Protected Member Functions

void setCancelEnabled (bool val)
 Set the cancel state for this thread - false means cancel requests are ignored.
 
void setCancelDeferred (bool val)
 Set the cancel type for this thread.
 
pthread_t getId () const
 
void testCancel () const
 Check if we have been cancelled.
 
virtual bool amInterrupted () const
 Call testCancel, and return true when this thread has been interrupted.
 

Private Member Functions

int run ()
 Thread run function.
 
void heapDecrement (size_t bytes)
 Utility function to decrement the heap size after writing one or more samples.
 
 SampleSorter (const SampleSorter &)
 No copy.
 
SampleSorteroperator= (const SampleSorter &)
 No assignment.
 
virtual void signalHandler (int, siginfo_t *)
 Signal handler function for this thread.
 
virtual int pRun ()
 
void setThreadSchedulerNolock (enum SchedPolicy policy, int priority)
 Set the scheduler policy and priority for this thread, then call setThreadSchedulerNolock() to effect the settings if the thread is running.
 
void setThreadSchedulerNolock ()
 If the thread is running, set the scheduling policy according to the current policy and priority attributes in this Thread.
 
void makeFullName ()
 
void registerThread ()
 Register this current thread with a static registry of threads by id.
 
void unregisterThread ()
 
void unsetId ()
 

Static Private Member Functions

static voidthr_run (void *me)
 
static voidthr_run_detached (void *me)
 
static void thr_cleanup (void *me)
 
static void thr_cleanup_delete (void *me)
 
static void thr_add_sig (int sig)
 
static void sigAction (int sig, siginfo_t *si, void *vptr)
 

Private Attributes

SampleSourceSupport _source
 
unsigned int _sorterLengthUsec
 Length of SampleSorter, in micro-seconds.
 
SortedSampleSet _samples
 
nidas::util::Cond _sampleSetCond
 
nidas::util::Cond _flushCond
 
size_t _heapMax
 Limit on the maximum size of memory to use while buffering samples.
 
size_t _heapSize
 Current heap size, in bytes.
 
bool _heapBlock
 _heapBlock controls what happens when the number of bytes in _samples exceeds _heapMax.
 
nidas::util::Cond _heapCond
 
bool _heapExceeded
 
unsigned int _discardedSamples
 Number of samples discarded because of _heapSize > _heapMax and heapBlock == true.
 
unsigned int _realTimeFutureSamples
 Number of samples discarded because getRealTime() is true and the samples have timetags later than the system clock.
 
unsigned int _earlySamples
 Samples which are earlier than the current latest sample in the sorter minus the sorter length.
 
int _discardWarningCount
 How often to log warnings about discardedSamples.
 
int _earlyWarningCount
 How often to log warnings about early samples.
 
bool _doFlush
 
bool _flushed
 
SampleT< char_dummy
 
bool _realTime
 Is this sorter running in real-time? If so then we can screen for bad time-tags by checking against the system clock, which is trusted.
 
long long _maxSorterLengthUsec
 
unsigned int _lateSampleCacheSize
 
Mutex _mutex
 Mutex for accessing _id.
 
std::string _name
 
std::string _fullname
 
pthread_t _id
 
bool _running
 
std::atomic< bool_interrupted
 
bool _cancel_enabled
 
bool _cancel_deferred
 
Exception * _exception
 Exception thrown by run method.
 
bool _detached
 
SchedPolicy _policy
 
int _priority
 
sigset_t _blockedSignals
 
sigset_t _unblockedSignals
 

Static Private Attributes

static std::string unknownName = std::string("unknown")
 

Detailed Description

A SampleClient that sorts its received samples, using an STL multiset, and then sends the sorted samples onto its SampleClients.

The time period of the sorting is specified with setLengthSecs(). Samples whose time-tags are previous to the time-tag of the latest sample received minus the sorter length, are sent on to the SampleClients. This is implemented as a Thread, which must be started, otherwise the sorter will grow and no samples will be sent to clients. This can be a client of multiple SampleSources, so that the distributed samples are sorted in time.

Member Enumeration Documentation

◆ runStatus

Values that can be returned by run method.

User can define other values greater than RUN_EXCEPTION. These values are then returned by int join(). Note that PTHREAD_CANCELLED is -1

Enumerator
RUN_CANCELED 
RUN_OK 
NOT_RUNNING 
RUN_EXCEPTION 

◆ SchedPolicy

Enumerator
NU_THREAD_OTHER 
NU_THREAD_FIFO 
NU_THREAD_RR 

Constructor & Destructor Documentation

◆ SampleSorter() [1/2]

SampleSorter::SampleSorter ( const std::string & name,
bool raw )

Constructor.

@ param raw: boolean indicating whether this SampleSorter is for raw or processed samples. Clients can query this value, and it controls what is returned by getRawSampleSource() and getProcessedSampleSource().

References _discardWarningCount, and _earlyWarningCount.

◆ ~SampleSorter()

SampleSorter::~SampleSorter ( )
virtual

◆ SampleSorter() [2/2]

nidas::core::SampleSorter::SampleSorter ( const SampleSorter & )
private

No copy.

Member Function Documentation

◆ addSampleClient()

void nidas::core::SampleSorter::addSampleClient ( SampleClient * client)
throw ( )
inlinevirtual

◆ addSampleClientForTag()

void nidas::core::SampleSorter::addSampleClientForTag ( SampleClient * client,
const SampleTag * tag )
throw ( )
inlinevirtual

◆ addSampleTag()

void nidas::core::SampleSorter::addSampleTag ( const SampleTag * tag)
throw ( )
inlinevirtual

How to tell this SampleSorter what sample tags it will be sorting.

SampleClients can then query it.

Implements nidas::core::SampleSource.

References _source, and nidas::core::SampleSourceSupport::addSampleTag().

◆ amInterrupted()

bool Runnable::amInterrupted ( ) const
protectedvirtualinherited

Call testCancel, and return true when this thread has been interrupted.

This is protected because it should only called within the run method of the thread.

References nidas::util::Runnable::isInterrupted(), and nidas::util::Runnable::testCancel().

Referenced by nidas::core::Looper::run(), nidas::core::SampleOutputRequestThread::run(), and nidas::core::DSMServerStat::run().

◆ blockSignal()

void Thread::blockSignal ( int sig)
inherited

◆ cancel()

void Thread::cancel ( )
virtualinherited

◆ currentName()

static const std::string & nidas::util::Thread::currentName ( )
inlinestaticinherited

Convenience routine to return the name for the current thread, or a string indicating that the name of the thread is unknown.

References nidas::util::Thread::currentThread(), and nidas::util::Thread::unknownName.

Referenced by nidas::util::Logger::msg().

◆ currentThread()

Thread * Thread::currentThread ( )
staticinherited

◆ currentThreadId()

pthread_t Thread::currentThreadId ( )
staticinherited

◆ flush()

void SampleSorter::flush ( )
throw ( )
virtual

Implementation of SampleSource::flush().

distribute all buffered samples to SampleClients.

flush all samples from buffer, distributing them to SampleClients. The first caller will block until the buffer is empty.

Implements nidas::core::SampleThread.

References _doFlush, _flushCond, _flushed, _samples, _sampleSetCond, _source, DLOG, GET_DSM_ID, GET_SPS_ID, nidas::core::SampleSourceSupport::getRawSampleSource(), nidas::core::Sample::getTimeTag(), nidas::util::Thread::isInterrupted(), nidas::util::Cond::lock(), nidas::util::Cond::signal(), nidas::util::Cond::unlock(), VLOG, nidas::util::Cond::wait(), and WLOG.

◆ getClientCount()

int nidas::core::SampleSorter::getClientCount ( ) const
throw ( )
inlinevirtual

How many SampleClients are currently in my list.

Exceptions

)

Implements nidas::core::SampleSource.

References _source, and nidas::core::SampleSourceSupport::getClientCount().

◆ getFullName()

const std::string & Thread::getFullName ( )
throw ( )
inherited

Return a name with a bunch of descriptive fields, specifying whether it is detached, the real-time priority, etc.

References nidas::util::Thread::_fullname.

Referenced by nidas::util::Thread::pRun().

◆ getHeapBlock()

bool nidas::core::SampleSorter::getHeapBlock ( ) const
inlinevirtual

Implements nidas::core::SampleThread.

References _heapBlock.

◆ getHeapMax()

size_t nidas::core::SampleSorter::getHeapMax ( ) const
inlinevirtual

Implements nidas::core::SampleThread.

References _heapMax.

◆ getHeapSize()

size_t nidas::core::SampleSorter::getHeapSize ( ) const
inlinevirtual

Get the current amount of heap being used for sorting.

Implements nidas::core::SampleThread.

References _heapSize.

◆ getId()

pthread_t nidas::util::Thread::getId ( ) const
inlineprotectedinherited

◆ getKeepStats()

bool nidas::core::SampleSorter::getKeepStats ( ) const
inlinevirtual

◆ getLateSampleCacheSize()

unsigned int nidas::core::SampleSorter::getLateSampleCacheSize ( ) const
inlinevirtual

◆ getLengthSecs()

float nidas::core::SampleSorter::getLengthSecs ( ) const
inlinevirtual

◆ getName()

const std::string & Thread::getName ( ) const
throw ( )
inherited

◆ getNumDiscardedSamples()

size_t nidas::core::SampleSorter::getNumDiscardedSamples ( ) const
inlinevirtual

Number of samples discarded because of _heapSize > _heapMax and heapBlock == true.

Implements nidas::core::SampleThread.

References _discardedSamples.

◆ getNumEarlySamples()

size_t nidas::core::SampleSorter::getNumEarlySamples ( ) const
inline

Number of early samples, which may not be sorted.

References _earlySamples.

◆ getNumFutureSamples()

size_t nidas::core::SampleSorter::getNumFutureSamples ( ) const
inlinevirtual

Number of samples discarded because their timetags were in the future.

Implements nidas::core::SampleThread.

References _realTimeFutureSamples.

◆ getPolicyString()

string Thread::getPolicyString ( int policy)
staticinherited

Convenience function to return a string for the given scheduler policy: "Non-RT", "RT:FIFO", "RT:RR" or "RT:Unknown".

References nidas::util::Thread::NU_THREAD_FIFO, nidas::util::Thread::NU_THREAD_OTHER, and nidas::util::Thread::NU_THREAD_RR.

Referenced by nidas::util::Thread::makeFullName().

◆ getProcessedSampleSource()

SampleSource * nidas::core::SampleSorter::getProcessedSampleSource ( )
inlinevirtual

Several objects in NIDAS can be both a SampleSource of raw Samples and processed Samples.

SampleClients use this method to get a pointer to whatever sample source they are interested in. Derived classes can return NULL if they are not a SampleSource of processed samples.

Implements nidas::core::SampleSource.

References _source, and nidas::core::SampleSourceSupport::getProcessedSampleSource().

◆ getRawSampleSource()

SampleSource * nidas::core::SampleSorter::getRawSampleSource ( )
inlinevirtual

Several objects in NIDAS can be both a SampleSource of raw Samples and processed Samples.

SampleClients use this method to get a pointer to whatever sample source they are interested in. Derived classes can return NULL if they are not a SampleSource of raw samples.

Implements nidas::core::SampleSource.

References _source, and nidas::core::SampleSourceSupport::getRawSampleSource().

◆ getRealTime()

bool nidas::core::SampleSorter::getRealTime ( ) const
inlinevirtual

Implements nidas::core::SampleThread.

References _realTime.

◆ getSampleStats()

const SampleStats & nidas::core::SampleSorter::getSampleStats ( ) const
inlinevirtual

◆ getSampleTagIterator()

SampleTagIterator nidas::core::SampleSorter::getSampleTagIterator ( ) const
inlinevirtual

◆ getSampleTags()

std::list< const SampleTag * > nidas::core::SampleSorter::getSampleTags ( ) const
inlinevirtual

◆ heapDecrement()

void SampleSorter::heapDecrement ( size_t bytes)
private

Utility function to decrement the heap size after writing one or more samples.

If the heapSize has has shrunk below heapMax then signal any threads waiting on heapCond.

References _heapBlock, _heapCond, _heapExceeded, _heapMax, _heapSize, DLOG, nidas::util::Thread::getName(), nidas::util::Cond::lock(), nidas::util::Cond::signal(), and nidas::util::Cond::unlock().

Referenced by run().

◆ interrupt()

void SampleSorter::interrupt ( )
virtual

◆ isCancelDeferred()

bool Thread::isCancelDeferred ( ) const
inherited

Return true if the cancel type of this thread is PTHREAD_CANCEL_DEFERRED.

References nidas::util::Thread::_cancel_deferred.

◆ isCancelEnabled()

bool Thread::isCancelEnabled ( ) const
inherited

Return true if the cancel state of this thread is PTHREAD_CANCEL_ENABLE.

References nidas::util::Thread::_cancel_enabled.

◆ isDetached()

virtual bool nidas::util::Thread::isDetached ( ) const
inlinevirtualinherited

Is this a detached thread.

References nidas::util::Thread::_detached.

◆ isInterrupted()

virtual bool nidas::util::Thread::isInterrupted ( ) const
inlinevirtualinherited

◆ isJoined()

virtual bool nidas::util::Thread::isJoined ( ) const
inlinevirtualinherited

◆ isRunning()

virtual bool nidas::util::Thread::isRunning ( ) const
inlinevirtualinherited

◆ join()

int Thread::join ( )
virtualinherited

◆ kill()

void Thread::kill ( int sig)
virtualinherited

◆ lookupThread()

Thread * Thread::lookupThread ( pthread_t id)
staticinherited

◆ makeFullName()

void Thread::makeFullName ( )
privateinherited

◆ operator=()

SampleSorter & nidas::core::SampleSorter::operator= ( const SampleSorter & )
private

No assignment.

◆ pRun()

int Thread::pRun ( )
privatevirtualinherited

◆ receive()

bool SampleSorter::receive ( const Sample * s)
throw ( )
virtual

◆ registerThread()

void Thread::registerThread ( )
privateinherited

Register this current thread with a static registry of threads by id.

Thread::registerThread (new Thread ("Main"));

References nidas::util::Thread::_id.

Referenced by nidas::util::Thread::start().

◆ removeSampleClient()

void nidas::core::SampleSorter::removeSampleClient ( SampleClient * c)
throw ( )
inlinevirtual

◆ removeSampleClientForTag()

void nidas::core::SampleSorter::removeSampleClientForTag ( SampleClient * c,
const SampleTag *  )
throw ( )
inlinevirtual

Remove a SampleClient for a given SampleTag from this SampleSource.

The pointer to the SampleClient must remain valid, until after it is removed.

Exceptions

)

Implements nidas::core::SampleSource.

References _source, and nidas::core::SampleSourceSupport::removeSampleClientForTag().

◆ removeSampleTag()

void nidas::core::SampleSorter::removeSampleTag ( const SampleTag * )
throw ( )
inlinevirtual

◆ run()

int SampleSorter::run ( )
privatevirtual

◆ setCancelDeferred()

void Thread::setCancelDeferred ( bool val)
protectedinherited

Set the cancel type for this thread.

true means cancel requests are deferred until the next cancellation point. false means they occur instantly. This is protected, it should be called only from a thread's own run method. See the pthreads(7) man page for a list of the cancellation points.

Note: non-deferred canceling is difficult to get right. It has not been tested with this class, and is not recommended.

References nidas::util::Thread::_cancel_deferred.

◆ setCancelEnabled()

void Thread::setCancelEnabled ( bool val)
protectedinherited

Set the cancel state for this thread - false means cancel requests are ignored.

See pthread_setcancelstate. This is protected, it should be called only from a thread's own run method.

References nidas::util::Thread::_cancel_enabled.

◆ setHeapBlock()

void nidas::core::SampleSorter::setHeapBlock ( bool val)
inlinevirtual
Parameters
valIf true, and heapSize exceeds heapMax, then wait for heapSize to be less then heapMax, which will block any SampleSources that are inserting samples into this sorter. If false, then discard any samples that are received while heapSize exceeds heapMax.

Implements nidas::core::SampleThread.

References _heapBlock, and nidas::core::getSample().

◆ setHeapMax()

void nidas::core::SampleSorter::setHeapMax ( size_t val)
inlinevirtual

Set the maximum amount of heap memory to use for sorting samples.

Parameters
valMaximum size of heap in bytes.

Implements nidas::core::SampleThread.

References _heapMax, and nidas::core::getSample().

◆ setKeepStats()

void nidas::core::SampleSorter::setKeepStats ( bool val)
inlinevirtual

◆ setLateSampleCacheSize()

void nidas::core::SampleSorter::setLateSampleCacheSize ( unsigned int val)
inlinevirtual

When aging-off samples, cache this number of samples with the latest time tags.

This number, or fewer, of samples with anomalous, late, time tags will not effect the sorting. If the late sample cache size is zero, a jump ahead in time greater than the length of the sorter in seconds will cause the sorter to be emptied and the sorting effectively disabled until samples within the sorter length of the bad sample are encountered.

Implements nidas::core::SampleThread.

References _lateSampleCacheSize, and nidas::core::getSample().

◆ setLengthSecs()

void nidas::core::SampleSorter::setLengthSecs ( float val)
inlinevirtual

◆ setNonRealTimePriority()

bool Thread::setNonRealTimePriority ( )
inherited

◆ setRealTime()

void nidas::core::SampleSorter::setRealTime ( bool val)
inlinevirtual

Is this sorter running in real-time? If so then we can screen for bad time-tags by checking against the system clock, which is trusted.

Implements nidas::core::SampleThread.

References _realTime, and nidas::core::getSample().

◆ setRealTimeFIFOPriority()

bool Thread::setRealTimeFIFOPriority ( int val)
inherited

◆ setRealTimeRoundRobinPriority()

bool Thread::setRealTimeRoundRobinPriority ( int val)
inherited

◆ setThreadScheduler()

void Thread::setThreadScheduler ( enum SchedPolicy policy,
int priority )
inherited

◆ setThreadSchedulerNolock() [1/2]

void Thread::setThreadSchedulerNolock ( )
privateinherited

If the thread is running, set the scheduling policy according to the current policy and priority attributes in this Thread.

Warn but continue if realtime priority cannot be set, otherwise raise Exception.

Exceptions
Exception

References nidas::util::Thread::_id, nidas::util::Thread::_policy, nidas::util::Thread::_priority, ELOG, nidas::util::Exception::errnoToString(), nidas::util::Thread::getName(), nidas::util::Thread::NU_THREAD_FIFO, nidas::util::Thread::NU_THREAD_RR, and WLOG.

Referenced by nidas::util::Thread::pRun(), nidas::util::Thread::setThreadScheduler(), and nidas::util::Thread::setThreadSchedulerNolock().

◆ setThreadSchedulerNolock() [2/2]

void Thread::setThreadSchedulerNolock ( enum SchedPolicy policy,
int priority )
privateinherited

Set the scheduler policy and priority for this thread, then call setThreadSchedulerNolock() to effect the settings if the thread is running.

Exceptions
Exception

References nidas::util::Thread::_policy, nidas::util::Thread::_priority, and nidas::util::Thread::setThreadSchedulerNolock().

◆ sigAction()

void Thread::sigAction ( int sig,
siginfo_t * si,
void * vptr )
staticprivateinherited

◆ signalHandler()

virtual void nidas::util::Thread::signalHandler ( int ,
siginfo_t *  )
inlineprivatevirtualinherited

Signal handler function for this thread.

The default handler just sets _interrupt to true, so that amInterrupted() or isInterrupted() will return true. Derived classes can override this method for custom signal handling. However, derived classes are limited in what they can do in their signal handler. Specifically, from the pthread_cond_signal man page:

"It is not safe to use the pthread_cond_signal() function in a signal handler that is invoked asynchronously."

Therefore do not call Cond::signal() from a signal handler.

Reimplemented in nidas::core::SensorHandler.

References nidas::util::Thread::_interrupted.

◆ size()

size_t nidas::core::SampleSorter::size ( ) const
inlinevirtual

Current number of samples in the sorter.

This method does not hold a lock to force exclusive access to the sample container. Therefore this is only an instantaneous check and shouldn't be used by methods in this class when exclusive access is required.

Implements nidas::core::SampleThread.

References _samples.

Referenced by run().

◆ start()

void Thread::start ( )
virtualinherited

◆ test()

int Thread::test ( int argc,
char ** argv )
staticinherited

a test method.

◆ testCancel()

void nidas::util::Runnable::testCancel ( ) const
inlineprotectedinherited

Check if we have been cancelled.

Calls pthread_testcancel. This is protected since it only checks the current thread - i.e. it must be called within the run method. Since it just calls pthread_testcancel, it is a cancellation point.

Referenced by nidas::util::Runnable::amInterrupted(), and TestSource::run().

◆ thr_add_sig()

void Thread::thr_add_sig ( int sig)
staticprivateinherited

◆ thr_cleanup()

void Thread::thr_cleanup ( void * me)
staticprivateinherited

References ILOG.

Referenced by nidas::util::Thread::thr_run().

◆ thr_cleanup_delete()

void Thread::thr_cleanup_delete ( void * me)
staticprivateinherited

◆ thr_run()

void * Thread::thr_run ( void * me)
staticprivateinherited

◆ thr_run_detached()

void * Thread::thr_run_detached ( void * me)
staticprivateinherited

◆ unblockSignal()

void Thread::unblockSignal ( int sig)
inherited

Install a signal handler and unblock the signal.

The signal handler will log a message about the receipt of the signal at severity LOG_INFO using the nidas::util::Logger. Then, if the signal handler is being invoked from a registered Thread, the virtual method signalHandler() for that Thread will be called.

The signal handler is installed with the sigaction() system call, and will be the action for the given signal in all threads, including the main() thread. If other threads do not wish to take action on a given signal, they should call blockSignal(sig). Or they can define their own signalHandler() method.

After installing the signal handler, the signal is added to those that are unblocked for the thread, or if the Thread is not yet running, the signal will be unblocked in the thread once it runs.

As with blockSignal(), this method is typically called on this Thread before it has started. If this Thread has started, then the signal will only be unblocked if the method is called from this Thread, i.e. from its own run() method.

To install a signal handler, and then block the signal so that it is held as pending until it is later unblocked, typically with pselect(), or sigwaitinfo(), do:

void Thread::run()
{
// get the existing signal mask
sigset_t sigmask;
pthread_sigmask(SIG_BLOCK,NULL,&sigmask);
// remove SIGUSR1 from the mask passed to pselect
sigdelset(&sigmask,SIGUSR1);
for (;;) {
pselect(nfd,&readfds,0,0,0,&sigmask);
...
}
}
...
thread.unblockSignal(SIGUSR1);
thread.blockSignal(SIGUSR1);
thread.start();
...
try {
if (thread.isRunning()) {
thread.kill(SIGUSR1);
thread.join()
}
}
Sample * getSample(sampleType type, unsigned int len)
A convienence method for getting a sample of an enumerated type from a pool.
Definition Sample.cc:70

References nidas::util::Thread::_blockedSignals, nidas::util::Thread::_unblockedSignals, nidas::util::Thread::currentThread(), nidas::util::Thread::isRunning(), and nidas::util::Thread::thr_add_sig().

Referenced by nidas::core::DerivedDataReader::DerivedDataReader(), nidas::core::FsMountWorkerThread::FsMountWorkerThread(), nidas::core::StatusListener::StatusListener(), nidas::core::StatusThread::StatusThread(), nidas::dynld::XMLConfigService::Worker::Worker(), nidas::core::XmlRpcThread::XmlRpcThread(), and nidas::dynld::UDPSampleOutput::XMLSocketListener::XMLSocketListener().

◆ unregisterThread()

void Thread::unregisterThread ( )
privateinherited

◆ unsetId()

void nidas::util::Thread::unsetId ( )
inlineprivateinherited

Member Data Documentation

◆ _blockedSignals

sigset_t nidas::util::Thread::_blockedSignals
privateinherited

◆ _cancel_deferred

bool nidas::util::Thread::_cancel_deferred
privateinherited

◆ _cancel_enabled

bool nidas::util::Thread::_cancel_enabled
privateinherited

◆ _detached

bool nidas::util::Thread::_detached
privateinherited

◆ _discardedSamples

unsigned int nidas::core::SampleSorter::_discardedSamples
private

Number of samples discarded because of _heapSize > _heapMax and heapBlock == true.

Referenced by getNumDiscardedSamples(), and ~SampleSorter().

◆ _discardWarningCount

int nidas::core::SampleSorter::_discardWarningCount
private

How often to log warnings about discardedSamples.

Referenced by SampleSorter().

◆ _doFlush

bool nidas::core::SampleSorter::_doFlush
private

Referenced by flush(), and run().

◆ _dummy

SampleT<char> nidas::core::SampleSorter::_dummy
private

Referenced by run().

◆ _earlySamples

unsigned int nidas::core::SampleSorter::_earlySamples
private

Samples which are earlier than the current latest sample in the sorter minus the sorter length.

These samples have arrived too late to be necessarily sorted. They may or may not be sorted, depending on how often the sorting buffer is read.

Referenced by getNumEarlySamples(), and ~SampleSorter().

◆ _earlyWarningCount

int nidas::core::SampleSorter::_earlyWarningCount
private

How often to log warnings about early samples.

Referenced by SampleSorter().

◆ _exception

Exception* nidas::util::Thread::_exception
privateinherited

Exception thrown by run method.

Referenced by nidas::util::Thread::join(), nidas::util::Thread::pRun(), and nidas::util::Thread::~Thread().

◆ _flushCond

nidas::util::Cond nidas::core::SampleSorter::_flushCond
private

Referenced by flush(), interrupt(), and run().

◆ _flushed

bool nidas::core::SampleSorter::_flushed
private

Referenced by flush(), and run().

◆ _fullname

std::string nidas::util::Thread::_fullname
privateinherited

◆ _heapBlock

bool nidas::core::SampleSorter::_heapBlock
private

_heapBlock controls what happens when the number of bytes in _samples exceeds _heapMax.

If _heapBlock is true and _heapSize exceeds _heapMax, then any threads which are calling SampleSorter::receive to insert a sample to this sorter will block on _heapCond until sample consumers have reduced _heapSize to less than _heapMax. If _heapBlock is false and _heapSize exceeds _heapMax then samples are discarded until _heapSize is less than _heapMax.

Referenced by getHeapBlock(), heapDecrement(), run(), and setHeapBlock().

◆ _heapCond

nidas::util::Cond nidas::core::SampleSorter::_heapCond
private

Referenced by heapDecrement(), interrupt(), and run().

◆ _heapExceeded

bool nidas::core::SampleSorter::_heapExceeded
private

Referenced by heapDecrement(), interrupt(), and run().

◆ _heapMax

size_t nidas::core::SampleSorter::_heapMax
private

Limit on the maximum size of memory to use while buffering samples.

Referenced by getHeapMax(), heapDecrement(), interrupt(), run(), and setHeapMax().

◆ _heapSize

size_t nidas::core::SampleSorter::_heapSize
private

Current heap size, in bytes.

Referenced by getHeapSize(), heapDecrement(), and interrupt().

◆ _id

pthread_t nidas::util::Thread::_id
privateinherited

◆ _interrupted

std::atomic<bool> nidas::util::Thread::_interrupted
privateinherited

◆ _lateSampleCacheSize

unsigned int nidas::core::SampleSorter::_lateSampleCacheSize
private

◆ _maxSorterLengthUsec

long long nidas::core::SampleSorter::_maxSorterLengthUsec
private

Referenced by run(), and ~SampleSorter().

◆ _mutex

Mutex nidas::util::Thread::_mutex
mutableprivateinherited

◆ _name

std::string nidas::util::Thread::_name
privateinherited

◆ _policy

SchedPolicy nidas::util::Thread::_policy
privateinherited

◆ _priority

int nidas::util::Thread::_priority
privateinherited

◆ _realTime

bool nidas::core::SampleSorter::_realTime
private

Is this sorter running in real-time? If so then we can screen for bad time-tags by checking against the system clock, which is trusted.

Referenced by getRealTime(), and setRealTime().

◆ _realTimeFutureSamples

unsigned int nidas::core::SampleSorter::_realTimeFutureSamples
private

Number of samples discarded because getRealTime() is true and the samples have timetags later than the system clock.

Referenced by getNumFutureSamples().

◆ _running

bool nidas::util::Thread::_running
privateinherited

◆ _samples

SortedSampleSet nidas::core::SampleSorter::_samples
private

Referenced by flush(), run(), size(), and ~SampleSorter().

◆ _sampleSetCond

nidas::util::Cond nidas::core::SampleSorter::_sampleSetCond
private

Referenced by flush(), interrupt(), and run().

◆ _sorterLengthUsec

unsigned int nidas::core::SampleSorter::_sorterLengthUsec
private

Length of SampleSorter, in micro-seconds.

Referenced by getLengthSecs(), run(), setLengthSecs(), and ~SampleSorter().

◆ _source

SampleSourceSupport nidas::core::SampleSorter::_source
private

◆ _unblockedSignals

sigset_t nidas::util::Thread::_unblockedSignals
privateinherited

◆ unknownName

std::string Thread::unknownName = std::string("unknown")
staticprivateinherited

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