PipedSignal Class Reference

#include <PipedSignal.h>

Inheritance diagram for PipedSignal:

Inheritance graph
[legend]
Collaboration diagram for PipedSignal:

Collaboration graph
[legend]

List of all members.


Detailed Description

Used for lock-free signalling of threads, and to allow a select thread to be awoken by signal.

Two common uses for PipedSignal. First, as a basic mutex/condition variable style signal, and later as a second as a way to awaken threads in select.

First (condition variable style signal):

 PipedSignal p;

 // thread 1:
 p.wait();

 // thread 2:
 p.signal();

Second (used for select):

 PipedSignal p;

 // thread 1:
 fd_set rset;
 int selectMax = 0;
 // add any number of other sockets to select for reading on.
 p.addToSet(rset, selectMax);
 SysUtil::select(selectMax, rset);
 p.clear(rset);

 // thread 2:
 p.signal();

PipedSignal provides challenges for portability. On Linux, you can select on a pipe, allowing a simple version which in the constructor creates a pipe and uses it for signalling and waiting.

However, on Windows, you cannot select on a pipe. To overcome this, the Windows implementation spawns a new thread, waits for it to connect a socket over localhost, and then the new thread dies. Additionally, a new port number series must be used to allocate the PipedSignal server sockets.

A separate implemenation was briefly devised on Windows (kept in a compiled out version here) which supports only the first model. It does so by actually using a mutex and condition variable with pthread_cond_timed_wait. The primary use of the select version is in the Transport scheduler, to wake the scheduler thread. This can be worked around with a smaller timeout on select.

Definition at line 105 of file PipedSignal.h.


Public Member Functions

void addToSet (fd_set &s, socket_t &selectMax) const
 adds the PipedSignal read fd to the fd_set s, and updates selectMax if needed.
bool clear (fd_set &s)
 if using addToSet, after select call clear with the resulting read set to read bytes from it.
void signal ()
 used to awaken a piped signal thread
int wait (uint64_t usec=0)
 wait for the read fd to be signalled, with an optional timeout (0 means sleep forever)
void close ()
 cleans up the state from the PipedSignal

Friends

void _runNewThread (pthread_t *t, func f, void *arg, pthread_attr_t *attr, const char *fname, bool joinThread)
 Generally you should not use this directly. Exceptions are if you don't want joinThread or if you want to specify a different string.
void _runNewThreadClass (pthread_t *t, RunThreadClass *c, classfunc f, void *arg, pthread_attr_t *attr, const char *fname, bool joinThread)
 Generally you should not use this directly. Exceptions are if you don't want joinThread or if you want to specify a different string.

Member Function Documentation

void PipedSignal::signal (  )  [inline]

used to awaken a piped signal thread

an interesting side effect of using PipedSignal is that the signal() can be called before wait(), and then wait() will return immediately as there should be bytes to read.

Definition at line 215 of file PipedSignal.h.

References SockUtil::errorWouldBlock(), and Log::perror().


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

Generated on Sat Oct 4 21:30:29 2008 for Mace Library by  doxygen 1.5.5