nidas  v1.2-1520
Classes | Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
nidas::core::SampleOutputRequestThread Class Reference

Interface of an output stream of samples. More...

#include <SampleOutputRequestThread.h>

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

Classes

class  ConnectRequest
 

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

int run () throw (nidas::util::Exception)
 The method which will run in its own thread. More...
 
void interrupt ()
 Interrupt the thread. More...
 
void clear ()
 Clear all current requests. More...
 
void addConnectRequest (SampleOutput *, SampleConnectionRequester *, int delaySecs)
 Add an connect request of a SampleOutput. More...
 
void addDeleteRequest (SampleOutput *)
 Request that SampleOutputRequestThread delete the output when it can. More...
 
virtual void start () throw (Exception)
 Start the thread running, meaning execute the run method in a separate thread. More...
 
virtual int join () throw (Exception)
 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. More...
 
virtual void kill (int sig) throw (Exception)
 Send a signal to this thread. More...
 
virtual void cancel () throw (Exception)
 Cancel this thread. More...
 
virtual bool isInterrupted () const
 Return true when this thread has been interrupted. More...
 
virtual bool isRunning () const
 Is this thread running? More...
 
virtual bool isJoined () const
 Has this thread been joined? More...
 
virtual bool isDetached () const
 Is this a detached thread. More...
 
bool isCancelEnabled () const
 Return true if the cancel state of this thread is PTHREAD_CANCEL_ENABLE. More...
 
bool isCancelDeferred () const
 Return true if the cancel type of this thread is PTHREAD_CANCEL_DEFERRED. More...
 
const std::string & getName () const throw ()
 Return the name of this thread. More...
 
const std::string & getFullName () throw ()
 Return a name with a bunch of descriptive fields, specifying whether it is detached, the real-time priority, etc. More...
 
bool setRealTimeRoundRobinPriority (int val) throw (Exception)
 
bool setRealTimeFIFOPriority (int val) throw (Exception)
 
bool setNonRealTimePriority () throw (Exception)
 
void setThreadScheduler (enum SchedPolicy policy, int priority) throw (Exception)
 
void blockSignal (int)
 Block a signal in this thread. More...
 
void unblockSignal (int)
 Install a signal handler and unblock the signal. More...
 

Static Public Member Functions

static SampleOutputRequestThreadgetInstance ()
 Return pointer to instance of singleton, creating instance if necessary. More...
 
static void destroyInstance ()
 Singleton destructor. More...
 
static ThreadcurrentThread ()
 Return the thread object for the current thread. More...
 
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. More...
 
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". More...
 
static int test (int argc, char **argv)
 a test method. More...
 

Protected Member Functions

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

Private Member Functions

 SampleOutputRequestThread ()
 
 ~SampleOutputRequestThread ()
 

Private Attributes

std::list< ConnectRequest_connectRequests
 
std::list< SampleOutput * > _disconnectRequests
 
nidas::util::Cond _requestCond
 

Static Private Attributes

static SampleOutputRequestThread_instance = 0
 
static nidas::util::Mutex _instanceLock
 

Detailed Description

Interface of an output stream of samples.

Member Enumeration Documentation

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 
Enumerator
NU_THREAD_OTHER 
NU_THREAD_FIFO 
NU_THREAD_RR 

Constructor & Destructor Documentation

SampleOutputRequestThread::SampleOutputRequestThread ( )
private
nidas::core::SampleOutputRequestThread::~SampleOutputRequestThread ( )
inlineprivate

Member Function Documentation

void SampleOutputRequestThread::addConnectRequest ( SampleOutput output,
SampleConnectionRequester requester,
int  delaySecs 
)
void SampleOutputRequestThread::addDeleteRequest ( SampleOutput output)
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.

Referenced by run(), nidas::core::Looper::run(), nidas::dynld::isff::GOESOutput::run(), nidas::core::DSMServerStat::run(), and nidas::util::Thread::test().

void Thread::blockSignal ( int  sig)
inherited
void Thread::cancel ( )
throw (Exception
)
virtualinherited
void SampleOutputRequestThread::clear ( )
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(), nidas::util::Thread::getName(), and nidas::util::Thread::unknownName.

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

Thread * Thread::currentThread ( )
staticinherited

Return the thread object for the current thread.

Returns null if not found.

Referenced by nidas::util::Thread::blockSignal(), nidas::util::Thread::currentName(), and nidas::util::Thread::unblockSignal().

pthread_t Thread::currentThreadId ( )
staticinherited
void SampleOutputRequestThread::destroyInstance ( )
static
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(), nidas::util::Thread::test(), nidas::util::Thread::thr_cleanup(), and nidas::util::Thread::thr_cleanup_delete().

pthread_t nidas::util::Thread::getId ( ) const
inlineprotectedinherited
SampleOutputRequestThread * SampleOutputRequestThread::getInstance ( )
static
const std::string & Thread::getName ( ) const
throw (
)
inherited
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::core::DSMSensor::getLooper(), and nidas::util::Thread::makeFullName().

void SampleOutputRequestThread::interrupt ( )
virtual

Interrupt the thread.

It will delete itself.

Reimplemented from nidas::util::Thread.

References _requestCond, clear(), nidas::util::Thread::interrupt(), and nidas::util::Cond::signal().

bool Thread::isCancelDeferred ( ) const
inherited

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

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

bool Thread::isCancelEnabled ( ) const
inherited

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

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

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

Is this a detached thread.

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

virtual bool nidas::util::Thread::isInterrupted ( ) const
inlinevirtualinherited
virtual bool nidas::util::Thread::isJoined ( ) const
inlinevirtualinherited
virtual bool nidas::util::Thread::isRunning ( ) const
inlinevirtualinherited
int Thread::join ( )
throw (Exception
)
virtualinherited

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.

The return value is the int return value of the run method, or PTHREAD_CANCELED (-1). If the run method threw an Exception, it will be caught and then re-thrown by join.

Reimplemented in nidas::core::SensorHandler.

References nidas::util::Thread::_exception, nidas::util::Thread::_id, nidas::util::Thread::_mutex, nidas::util::Exception::errnoToString(), nidas::util::Thread::getId(), nidas::util::Thread::getName(), nidas::util::Thread::RUN_EXCEPTION, and nidas::util::Thread::RUN_OK.

Referenced by nidas::core::DSMService::checkSubThreads(), nidas::dynld::UDPSampleOutput::XMLSocketListener::checkWorkers(), nidas::dynld::UDPSampleOutput::close(), nidas::util::McSocketListener::close(), nidas::core::DerivedDataReader::deleteInstance(), nidas::dynld::UDPSampleOutput::XMLSocketListener::fireWorkers(), nidas::core::DSMService::join(), nidas::core::SensorHandler::join(), nidas::core::SamplePipeline::join(), nidas::core::DSMEngine::joinDataThreads(), nidas::dynld::isff::GOESOutput::joinThread(), nidas::core::DSMServerApp::killStatusThread(), nidas::core::DSMServerApp::killXmlRpcThread(), nidas::core::DSMEngine::killXmlRpcThread(), main(), nidas::core::Looper::removeClient(), nidas::util::ThreadJoiner::run(), nidas::core::SampleBuffer::~SampleBuffer(), nidas::core::SampleSorter::~SampleSorter(), nidas::core::ServerSocket::~ServerSocket(), and nidas::dynld::UDPSampleOutput::~UDPSampleOutput().

void Thread::kill ( int  sig)
throw (Exception
)
virtualinherited
Thread * Thread::lookupThread ( pthread_t  id)
staticinherited
int SampleOutputRequestThread::run ( )
throw (nidas::util::Exception
)
virtual
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.

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.

bool Thread::setNonRealTimePriority ( )
throw (Exception
)
inherited
bool Thread::setRealTimeFIFOPriority ( int  val)
throw (Exception
)
inherited
bool Thread::setRealTimeRoundRobinPriority ( int  val)
throw (Exception
)
inherited
void Thread::setThreadScheduler ( enum SchedPolicy  policy,
int  priority 
)
throw (Exception
)
inherited
void Thread::start ( )
throw (Exception
)
virtualinherited
int Thread::test ( int  argc,
char **  argv 
)
staticinherited
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.

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()
}
}

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().

Member Data Documentation

std::list<ConnectRequest> nidas::core::SampleOutputRequestThread::_connectRequests
private

Referenced by addConnectRequest(), clear(), and run().

std::list<SampleOutput*> nidas::core::SampleOutputRequestThread::_disconnectRequests
private

Referenced by addDeleteRequest(), clear(), and run().

SampleOutputRequestThread * SampleOutputRequestThread::_instance = 0
staticprivate
n_u::Mutex SampleOutputRequestThread::_instanceLock
staticprivate
nidas::util::Cond nidas::core::SampleOutputRequestThread::_requestCond
private

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