Loading...
Searching...
No Matches
XrdSysIOEvents.hh
Go to the documentation of this file.
#ifndef __XRDSYSIOEVENTS_HH__
#define __XRDSYSIOEVENTS_HH__
/******************************************************************************/
/* */
/* X r d S y s I O E v e n t s . h h */
/* */
/* (c) 2012 by the Board of Trustees of the Leland Stanford, Jr., University */
/* All Rights Reserved */
/* Produced by Andrew Hanushevsky for Stanford University under contract */
/* DE-AC02-76-SFO0515 with the Department of Energy */
/* */
/* This file is part of the XRootD software suite. */
/* */
/* XRootD is free software: you can redistribute it and/or modify it under */
/* the terms of the GNU Lesser General Public License as published by the */
/* Free Software Foundation, either version 3 of the License, or (at your */
/* option) any later version. */
/* */
/* XRootD is distributed in the hope that it will be useful, but WITHOUT */
/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
/* License for more details. */
/* */
/* You should have received a copy of the GNU Lesser General Public License */
/* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
/* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
/* */
/* The copyright holder's institutional names and contributor's names may not */
/* be used to endorse or promote products derived from this software without */
/* specific prior written permission of the institution or contributor. */
/******************************************************************************/
#include <poll.h>
#include <ctime>
#include <sys/types.h>
#include "XrdSys/XrdSysPthread.hh"
#include "XrdSys/XrdSysAtomics.hh"
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
namespace XrdSys
{
namespace IOEvents
{
/******************************************************************************/
/* C l a s s C a l l B a c k */
/******************************************************************************/
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
class Channel;
class CallBack
{
public:
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
enum EventType
{
ReadyToRead = 0x01,
ReadTimeOut = 0x02,
ReadyToWrite = 0x04,
WriteTimeOut = 0x08,
ValidEvents = 0x0f
};
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
virtual bool Event(Channel *chP, void *cbArg, int evFlags) = 0;
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
virtual void Fatal(Channel *chP, void *cbArg, int eNum, const char *eTxt)
{
(void)chP; (void)cbArg; (void)eNum; (void)eTxt;
};
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
virtual void Stop(Channel *chP, void *cbArg) { (void)chP; (void)cbArg;}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
CallBack() {}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
virtual ~CallBack() {}
};
/******************************************************************************/
/* C l a s s C h a n n e l */
/******************************************************************************/
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
class ChannelWait;
class Poller;
class Channel
{
friend class Poller;
public:
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void Delete();
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
enum EventCode {readEvents = 0x01,
writeEvents = 0x04,
rwEvents = 0x05,
errorEvents = 0x10,
stopEvent = 0x20,
allEvents = 0x35
};
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
bool Disable(int events, const char **eText=0);
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
bool Enable(int events, int timeout=0, const char **eText=0);
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void GetCallBack(CallBack **cbP, void **cbArg);
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
inline int GetEvents() {return (chPoller ? static_cast<int>(chEvents) : -1);}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
inline int GetFD() {return chFD;}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void SetCallBack(CallBack *cbP, void *cbArg=0);
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void SetFD(int fd);
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
Channel(Poller *pollP, int fd, CallBack *cbP=0, void *cbArg=0);
private:
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
~Channel() {}
struct dlQ {Channel *next; Channel *prev;};
XrdSysRecMutex chMutex;
dlQ attList; // List of attached channels
dlQ tmoList; // List of channels in the timeout queue
Poller *chPoller; // The effective poller
Poller *chPollXQ; // The real poller
CallBack *chCB; // CallBack function
void *chCBA; // CallBack argument
int chFD; // Associated file descriptor
int pollEnt; // Used only for poll() type pollers
int chRTO; // Read timeout value (0 means none)
int chWTO; // Write timeout value (0 means none)
time_t rdDL; // Read deadline
time_t wrDL; // Write deadline
time_t deadLine; // The deadline in effect (read or write)
char dlType; // The deadline type in deadLine as CallBack type
char chEvents; // Enabled events as Channel type
char chStat; // Channel status below (!0 -> in callback mode)
enum Status {isClear = 0, isCBMode, isDead};
char inTOQ; // True if the channel is in the timeout queue
char inPSet; // FD is in the actual poll set
char reMod; // Modify issued while deferred, re-issue needed
short chFault; // Deferred error, 0 if all is well
void Reset(Poller *thePoller, int fd, int eNum=0);
};
/******************************************************************************/
/* C l a s s P o l l e r */
/******************************************************************************/
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
class Poller
{
friend class BootStrap;
friend class Channel;
public:
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
enum CreateOpts {optTOM};
static Poller *Create(int &eNum, const char **eTxt=0, int crOpts=0);
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void Stop();
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
Poller(int cFD, int rFD);
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
virtual ~Poller() {}
protected:
struct PipeData;
void CbkTMO();
bool CbkXeq(Channel *cP, int events, int eNum, const char *eTxt);
inline int GetFault(Channel *cP) {return cP->chFault;}
inline int GetPollEnt(Channel *cP) {return cP->pollEnt;}
int GetRequest();
bool Init(Channel *cP, int &eNum, const char **eTxt, bool &isLockd);
inline void LockChannel(Channel *cP) {cP->chMutex.Lock();}
int Poll2Enum(short events);
int SendCmd(PipeData &cmd);
void SetPollEnt(Channel *cP, int ptEnt);
bool TmoAdd(Channel *cP, int tmoSet);
void TmoDel(Channel *cP);
int TmoGet();
inline void UnLockChannel(Channel *cP) {cP->chMutex.UnLock();}
virtual void Begin(XrdSysSemaphore *syncp, int &rc, const char **eTxt) = 0;
virtual void Exclude(Channel *cP, bool &isLocked, bool dover=1) = 0;
virtual bool Include(Channel *cP,
int &eNum,
const char **eTxt,
bool &isLocked) = 0;
virtual bool Modify (Channel *cP,
int &eNum,
const char **eTxt,
bool &isLocked) = 0;
//
virtual void Shutdown() = 0;
// The following is common to all implementations
//
Channel *attBase; // -> First channel in attach queue or 0
Channel *tmoBase; // -> First channel in timeout queue or 0
pthread_t pollTid; // Poller's thread ID
struct pollfd pipePoll; // Stucture to wait for pipe events
int cmdFD; // FD to send PipeData commands
int reqFD; // FD to recv PipeData requests
struct PipeData {char req; char evt; short ent; int fd;
XrdSysSemaphore *theSem;
enum cmd {NoOp = 0, MdFD = 1, Post = 2,
MiFD = 3, RmFD = 4, Stop = 5};
PipeData(char reQ=0, char evT=0, short enT=0,
int fD =0, XrdSysSemaphore *sP=0)
: req(reQ), evt(evT), ent(enT), fd(fD),
theSem(sP) {}
~PipeData() {}
};
PipeData reqBuff; // Buffer used by poller thread to recv data
char *pipeBuff; // Read resumption point in buffer
int pipeBlen; // Number of outstanding bytes
unsigned char tmoMask; // Timeout mask
CPP_ATOMIC_TYPE(bool) wakePend; // Wakeup is effectively pending (don't send)
bool chDead; // True if channel deleted by callback
static time_t maxTime; // Maximum time allowed
static pid_t parentPID; // register parent process PID
private:
void Attach(Channel *cP);
void Detach(Channel *cP, bool &isLocked, bool keep=true);
void WakeUp();
// newPoller() called to get a specialized new poll object at in response to
// Create(). A specialized implementation must be supplied.
//
static Poller *newPoller(int pFD[2], int &eNum, const char **eTxt);
XrdSysMutex adMutex; // Mutex for adding & detaching channels
XrdSysMutex toMutex; // Mutex for handling the timeout list
};
};
};
#endif
Create
bool Create
Definition XrdSecpwdSrvAdmin.cc:176
XrdSysAtomics.hh
XrdSysPthread.hh
XrdSysMutex
Definition XrdSysPthread.hh:165
XrdSysMutex::Lock
void Lock()
Definition XrdSysPthread.hh:222
XrdSysMutex::UnLock
void UnLock()
Definition XrdSysPthread.hh:224
XrdSysRecMutex
Definition XrdSysPthread.hh:242
XrdSysSemaphore
Definition XrdSysPthread.hh:494
XrdSys::IOEvents::CallBack
Definition XrdSysIOEvents.hh:79
XrdSys::IOEvents::CallBack::Fatal
virtual void Fatal(Channel *chP, void *cbArg, int eNum, const char *eTxt)
Definition XrdSysIOEvents.hh:131
XrdSys::IOEvents::CallBack::Event
virtual bool Event(Channel *chP, void *cbArg, int evFlags)=0
XrdSys::IOEvents::CallBack::CallBack
CallBack()
Constructor.
Definition XrdSysIOEvents.hh:152
XrdSys::IOEvents::CallBack::~CallBack
virtual ~CallBack()
Destructor.
Definition XrdSysIOEvents.hh:158
XrdSys::IOEvents::CallBack::Stop
virtual void Stop(Channel *chP, void *cbArg)
Definition XrdSysIOEvents.hh:146
XrdSys::IOEvents::CallBack::EventType
EventType
Events that may cause a callback object to be activated.
Definition XrdSysIOEvents.hh:87
XrdSys::IOEvents::CallBack::ReadyToWrite
@ ReadyToWrite
Writing won't block.
Definition XrdSysIOEvents.hh:90
XrdSys::IOEvents::CallBack::ReadyToRead
@ ReadyToRead
New data has arrived.
Definition XrdSysIOEvents.hh:88
XrdSys::IOEvents::CallBack::ReadTimeOut
@ ReadTimeOut
Read timeout.
Definition XrdSysIOEvents.hh:89
XrdSys::IOEvents::CallBack::WriteTimeOut
@ WriteTimeOut
Write timeout.
Definition XrdSysIOEvents.hh:91
XrdSys::IOEvents::CallBack::ValidEvents
@ ValidEvents
Mask to test for valid events.
Definition XrdSysIOEvents.hh:92
XrdSys::IOEvents::Channel
Definition XrdSysIOEvents.hh:174
XrdSys::IOEvents::Channel::SetCallBack
void SetCallBack(CallBack *cbP, void *cbArg=0)
Definition XrdSysIOEvents.cc:513
XrdSys::IOEvents::Channel::GetCallBack
void GetCallBack(CallBack **cbP, void **cbArg)
Definition XrdSysIOEvents.cc:480
XrdSys::IOEvents::Channel::EventCode
EventCode
Event bits used to feed Enable() and Disable(); can be or'd.
Definition XrdSysIOEvents.hh:192
XrdSys::IOEvents::Channel::allEvents
@ allEvents
All of the above.
Definition XrdSysIOEvents.hh:197
XrdSys::IOEvents::Channel::errorEvents
@ errorEvents
Error event non-r/w specific.
Definition XrdSysIOEvents.hh:195
XrdSys::IOEvents::Channel::stopEvent
@ stopEvent
Poller stop event.
Definition XrdSysIOEvents.hh:196
XrdSys::IOEvents::Channel::writeEvents
@ writeEvents
Write and Write Timeouts.
Definition XrdSysIOEvents.hh:193
XrdSys::IOEvents::Channel::rwEvents
@ rwEvents
Both of the above.
Definition XrdSysIOEvents.hh:194
XrdSys::IOEvents::Channel::readEvents
@ readEvents
Read and Read Timeouts.
Definition XrdSysIOEvents.hh:192
XrdSys::IOEvents::Channel::GetEvents
int GetEvents()
Definition XrdSysIOEvents.hh:267
XrdSys::IOEvents::Channel::Enable
bool Enable(int events, int timeout=0, const char **eText=0)
Definition XrdSysIOEvents.cc:401
XrdSys::IOEvents::Channel::SetFD
void SetFD(int fd)
Definition XrdSysIOEvents.cc:531
XrdSys::IOEvents::Channel::Delete
void Delete()
Definition XrdSysIOEvents.cc:303
XrdSys::IOEvents::Channel::Poller
friend class Poller
Definition XrdSysIOEvents.hh:175
XrdSys::IOEvents::Channel::GetFD
int GetFD()
Definition XrdSysIOEvents.hh:276
XrdSys::IOEvents::Channel::Channel
Channel(Poller *pollP, int fd, CallBack *cbP=0, void *cbArg=0)
Definition XrdSysIOEvents.cc:285
XrdSys::IOEvents::Channel::Disable
bool Disable(int events, const char **eText=0)
Definition XrdSysIOEvents.cc:356
XrdSys::IOEvents::Poller
Definition XrdSysIOEvents.hh:372
XrdSys::IOEvents::Poller::Modify
virtual bool Modify(Channel *cP, int &eNum, const char **eTxt, bool &isLocked)=0
XrdSys::IOEvents::Poller::pipeBuff
char * pipeBuff
Definition XrdSysIOEvents.hh:507
XrdSys::IOEvents::Poller::Include
virtual bool Include(Channel *cP, int &eNum, const char **eTxt, bool &isLocked)=0
XrdSys::IOEvents::Poller::GetFault
int GetFault(Channel *cP)
Definition XrdSysIOEvents.hh:437
XrdSys::IOEvents::Poller::attBase
Channel * attBase
Definition XrdSysIOEvents.hh:488
XrdSys::IOEvents::Poller::LockChannel
void LockChannel(Channel *cP)
Definition XrdSysIOEvents.hh:441
XrdSys::IOEvents::Poller::tmoBase
Channel * tmoBase
Definition XrdSysIOEvents.hh:489
XrdSys::IOEvents::Poller::GetPollEnt
int GetPollEnt(Channel *cP)
Definition XrdSysIOEvents.hh:438
XrdSys::IOEvents::Poller::reqFD
int reqFD
Definition XrdSysIOEvents.hh:495
XrdSys::IOEvents::Poller::pollTid
pthread_t pollTid
Definition XrdSysIOEvents.hh:491
XrdSys::IOEvents::Poller::Poller
Poller(int cFD, int rFD)
Definition XrdSysIOEvents.cc:571
XrdSys::IOEvents::Poller::CPP_ATOMIC_TYPE
CPP_ATOMIC_TYPE(bool) wakePend
XrdSys::IOEvents::Poller::TmoDel
void TmoDel(Channel *cP)
Definition XrdSysIOEvents.cc:1160
XrdSys::IOEvents::Poller::pipePoll
struct pollfd pipePoll
Definition XrdSysIOEvents.hh:493
XrdSys::IOEvents::Poller::Begin
virtual void Begin(XrdSysSemaphore *syncp, int &rc, const char **eTxt)=0
XrdSys::IOEvents::Poller::CbkXeq
bool CbkXeq(Channel *cP, int events, int eNum, const char *eTxt)
Definition XrdSysIOEvents.cc:636
XrdSys::IOEvents::Poller::~Poller
virtual ~Poller()
Destructor. Stop() is effecively called when this object is deleted.
Definition XrdSysIOEvents.hh:430
XrdSys::IOEvents::Poller::TmoGet
int TmoGet()
Definition XrdSysIOEvents.cc:1180
XrdSys::IOEvents::Poller::cmdFD
int cmdFD
Definition XrdSysIOEvents.hh:494
XrdSys::IOEvents::Poller::GetRequest
int GetRequest()
Definition XrdSysIOEvents.cc:870
XrdSys::IOEvents::Poller::maxTime
static time_t maxTime
Definition XrdSysIOEvents.hh:513
XrdSys::IOEvents::Poller::SendCmd
int SendCmd(PipeData &cmd)
Definition XrdSysIOEvents.cc:993
XrdSys::IOEvents::Poller::tmoMask
unsigned char tmoMask
Definition XrdSysIOEvents.hh:509
XrdSys::IOEvents::Poller::UnLockChannel
void UnLockChannel(Channel *cP)
Definition XrdSysIOEvents.hh:448
XrdSys::IOEvents::Poller::Shutdown
virtual void Shutdown()=0
XrdSys::IOEvents::Poller::pipeBlen
int pipeBlen
Definition XrdSysIOEvents.hh:508
XrdSys::IOEvents::Poller::CbkTMO
void CbkTMO()
Definition XrdSysIOEvents.cc:614
XrdSys::IOEvents::Poller::Poll2Enum
int Poll2Enum(short events)
Definition XrdSysIOEvents.cc:978
XrdSys::IOEvents::Poller::Channel
friend class Channel
Definition XrdSysIOEvents.hh:374
XrdSys::IOEvents::Poller::BootStrap
friend class BootStrap
Definition XrdSysIOEvents.hh:373
XrdSys::IOEvents::Poller::Exclude
virtual void Exclude(Channel *cP, bool &isLocked, bool dover=1)=0
XrdSys::IOEvents::Poller::reqBuff
PipeData reqBuff
Definition XrdSysIOEvents.hh:506
XrdSys::IOEvents::Poller::TmoAdd
bool TmoAdd(Channel *cP, int tmoSet)
Definition XrdSysIOEvents.cc:1092
XrdSys::IOEvents::Poller::chDead
bool chDead
Definition XrdSysIOEvents.hh:511
XrdSys::IOEvents::Poller::CreateOpts
CreateOpts
Definition XrdSysIOEvents.hh:398
XrdSys::IOEvents::Poller::optTOM
@ optTOM
Definition XrdSysIOEvents.hh:398
XrdSys::IOEvents::Poller::Stop
void Stop()
Definition XrdSysIOEvents.cc:1030
XrdSys::IOEvents::Poller::SetPollEnt
void SetPollEnt(Channel *cP, int ptEnt)
Definition XrdSysIOEvents.cc:1021
XrdSys::IOEvents::Poller::parentPID
static pid_t parentPID
Definition XrdSysIOEvents.hh:515
XrdSys::IOEvents::Poller::Init
bool Init(Channel *cP, int &eNum, const char **eTxt, bool &isLockd)
Definition XrdSysIOEvents.cc:909
XrdSys::IOEvents
Definition XrdClPollerBuiltIn.hh:29
XrdSys
Definition XrdClPollerBuiltIn.hh:28
XrdSys::IOEvents::Poller::PipeData
Definition XrdSysIOEvents.hh:496
XrdSys::IOEvents::Poller::PipeData::theSem
XrdSysSemaphore * theSem
Definition XrdSysIOEvents.hh:497
XrdSys::IOEvents::Poller::PipeData::evt
char evt
Definition XrdSysIOEvents.hh:496
XrdSys::IOEvents::Poller::PipeData::req
char req
Definition XrdSysIOEvents.hh:496
XrdSys::IOEvents::Poller::PipeData::cmd
cmd
Definition XrdSysIOEvents.hh:498
XrdSys::IOEvents::Poller::PipeData::MiFD
@ MiFD
Definition XrdSysIOEvents.hh:499
XrdSys::IOEvents::Poller::PipeData::RmFD
@ RmFD
Definition XrdSysIOEvents.hh:499
XrdSys::IOEvents::Poller::PipeData::Post
@ Post
Definition XrdSysIOEvents.hh:498
XrdSys::IOEvents::Poller::PipeData::Stop
@ Stop
Definition XrdSysIOEvents.hh:499
XrdSys::IOEvents::Poller::PipeData::MdFD
@ MdFD
Definition XrdSysIOEvents.hh:498
XrdSys::IOEvents::Poller::PipeData::NoOp
@ NoOp
Definition XrdSysIOEvents.hh:498
XrdSys::IOEvents::Poller::PipeData::fd
int fd
Definition XrdSysIOEvents.hh:496
XrdSys::IOEvents::Poller::PipeData::ent
short ent
Definition XrdSysIOEvents.hh:496
XrdSys::IOEvents::Poller::PipeData::~PipeData
~PipeData()
Definition XrdSysIOEvents.hh:504
XrdSys::IOEvents::Poller::PipeData::PipeData
PipeData(char reQ=0, char evT=0, short enT=0, int fD=0, XrdSysSemaphore *sP=0)
Definition XrdSysIOEvents.hh:500