xrootd
XrdSysIOEvents.hh
Go to the documentation of this file.
1 #ifndef __XRDSYSIOEVENTS_HH__
2 #define __XRDSYSIOEVENTS_HH__
3 /******************************************************************************/
4 /* */
5 /* X r d S y s I O E v e n t s . h h */
6 /* */
7 /* (c) 2012 by the Board of Trustees of the Leland Stanford, Jr., University */
8 /* All Rights Reserved */
9 /* Produced by Andrew Hanushevsky for Stanford University under contract */
10 /* DE-AC02-76-SFO0515 with the Department of Energy */
11 /* */
12 /* This file is part of the XRootD software suite. */
13 /* */
14 /* XRootD is free software: you can redistribute it and/or modify it under */
15 /* the terms of the GNU Lesser General Public License as published by the */
16 /* Free Software Foundation, either version 3 of the License, or (at your */
17 /* option) any later version. */
18 /* */
19 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
20 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
21 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
22 /* License for more details. */
23 /* */
24 /* You should have received a copy of the GNU Lesser General Public License */
25 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
26 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
27 /* */
28 /* The copyright holder's institutional names and contributor's names may not */
29 /* be used to endorse or promote products derived from this software without */
30 /* specific prior written permission of the institution or contributor. */
31 /******************************************************************************/
32 
33 #include <poll.h>
34 #include <time.h>
35 #include <sys/types.h>
36 
37 #include "XrdSys/XrdSysPthread.hh"
38 #include "XrdSys/XrdSysAtomics.hh"
39 
40 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
55 
56 namespace XrdSys
57 {
58 namespace IOEvents
59 {
60 
61 /******************************************************************************/
62 /* C l a s s C a l l B a c k */
63 /******************************************************************************/
64 
65 //-----------------------------------------------------------------------------
75 //-----------------------------------------------------------------------------
76 
77 class Channel;
78 class CallBack
79 {
80 public:
81 
82 //-----------------------------------------------------------------------------
84 //-----------------------------------------------------------------------------
85 
86  enum EventType
87  {
88  ReadyToRead = 0x01,
89  ReadTimeOut = 0x02,
90  ReadyToWrite = 0x04,
91  WriteTimeOut = 0x08,
92  ValidEvents = 0x0f
93  };
94 
95 //-----------------------------------------------------------------------------
114 //-----------------------------------------------------------------------------
115 
116 virtual bool Event(Channel *chP, void *cbArg, int evFlags) = 0;
117 
118 //-----------------------------------------------------------------------------
129 //-----------------------------------------------------------------------------
130 
131 virtual void Fatal(Channel *chP, void *cbArg, int eNum, const char *eTxt)
132 {
133  (void)chP; (void)cbArg; (void)eNum; (void)eTxt;
134 };
135 
136 //-----------------------------------------------------------------------------
144 //-----------------------------------------------------------------------------
145 
146 virtual void Stop(Channel *chP, void *cbArg) { (void)chP; (void)cbArg;}
147 
148 //-----------------------------------------------------------------------------
150 //-----------------------------------------------------------------------------
151 
152  CallBack() {}
153 
154 //-----------------------------------------------------------------------------
156 //-----------------------------------------------------------------------------
157 
158 virtual ~CallBack() {}
159 };
160 
161 /******************************************************************************/
162 /* C l a s s C h a n n e l */
163 /******************************************************************************/
164 
165 //-----------------------------------------------------------------------------
169 //-----------------------------------------------------------------------------
170 
171 class ChannelWait;
172 class Poller;
173 class Channel
174 {
175 friend class Poller;
176 public:
177 
178 //-----------------------------------------------------------------------------
184 //-----------------------------------------------------------------------------
185 
186  void Delete();
187 
188 //-----------------------------------------------------------------------------
190 //-----------------------------------------------------------------------------
191 
192 enum EventCode {readEvents = 0x01,
193  writeEvents = 0x04,
194  rwEvents = 0x05,
195  errorEvents = 0x10,
196  stopEvent = 0x20,
197  allEvents = 0x35
198  };
199 
200 //-----------------------------------------------------------------------------
210 //-----------------------------------------------------------------------------
211 
212  bool Disable(int events, const char **eText=0);
213 
214 //-----------------------------------------------------------------------------
246 //-----------------------------------------------------------------------------
247 
248  bool Enable(int events, int timeout=0, const char **eText=0);
249 
250 //-----------------------------------------------------------------------------
255 //-----------------------------------------------------------------------------
256 
257  void GetCallBack(CallBack **cbP, void **cbArg);
258 
259 //-----------------------------------------------------------------------------
265 //-----------------------------------------------------------------------------
266 
267 inline int GetEvents() {return (chPoller ? static_cast<int>(chEvents) : -1);}
268 
269 //-----------------------------------------------------------------------------
274 //-----------------------------------------------------------------------------
275 
276 inline int GetFD() {return chFD;}
277 
278 //-----------------------------------------------------------------------------
285 //-----------------------------------------------------------------------------
286 
287  void SetCallBack(CallBack *cbP, void *cbArg=0);
288 
289 //-----------------------------------------------------------------------------
298 //-----------------------------------------------------------------------------
299 
300  void SetFD(int fd);
301 
302 //-----------------------------------------------------------------------------
317 //-----------------------------------------------------------------------------
318 
319  Channel(Poller *pollP, int fd, CallBack *cbP=0, void *cbArg=0);
320 
321 private:
322 
323 //-----------------------------------------------------------------------------
325 //-----------------------------------------------------------------------------
326 
327  ~Channel() {}
328 
329 struct dlQ {Channel *next; Channel *prev;};
330 
332 
333 dlQ attList; // List of attached channels
334 dlQ tmoList; // List of channels in the timeout queue
335 
336 Poller *chPoller; // The effective poller
337 Poller *chPollXQ; // The real poller
338 CallBack *chCB; // CallBack function
339 void *chCBA; // CallBack argument
340 int chFD; // Associated file descriptor
341 int pollEnt; // Used only for poll() type pollers
342 int chRTO; // Read timeout value (0 means none)
343 int chWTO; // Write timeout value (0 means none)
344 time_t rdDL; // Read deadline
345 time_t wrDL; // Write deadline
346 time_t deadLine; // The deadline in effect (read or write)
347 char dlType; // The deadline type in deadLine as CallBack type
348 char chEvents; // Enabled events as Channel type
349 char chStat; // Channel status below (!0 -> in callback mode)
351 char inTOQ; // True if the channel is in the timeout queue
352 char inPSet; // FD is in the actual poll set
353 char reMod; // Modify issued while defered, re-issue needed
354 short chFault; // Defered error, 0 if all is well
355 
356 void Reset(Poller *thePoller, int fd, int eNum=0);
357 };
358 
359 /******************************************************************************/
360 /* C l a s s P o l l e r */
361 /******************************************************************************/
362 
363 //-----------------------------------------------------------------------------
369 //-----------------------------------------------------------------------------
370 
371 class Poller
372 {
373 friend class BootStrap;
374 friend class Channel;
375 public:
376 
377 //-----------------------------------------------------------------------------
396 //-----------------------------------------------------------------------------
397 
399 
400 static Poller *Create(int &eNum, const char **eTxt=0, int crOpts=0);
401 
402 //-----------------------------------------------------------------------------
413 //-----------------------------------------------------------------------------
414 
415  void Stop();
416 
417 //-----------------------------------------------------------------------------
422 //-----------------------------------------------------------------------------
423 
424  Poller(int cFD, int rFD);
425 
426 //-----------------------------------------------------------------------------
428 //-----------------------------------------------------------------------------
429 
430 virtual ~Poller() {}
431 
432 protected:
433 struct PipeData;
434 
435  void CbkTMO();
436  bool CbkXeq(Channel *cP, int events, int eNum, const char *eTxt);
437 inline int GetFault(Channel *cP) {return cP->chFault;}
438 inline int GetPollEnt(Channel *cP) {return cP->pollEnt;}
439  int GetRequest();
440  bool Init(Channel *cP, int &eNum, const char **eTxt, bool &isLockd);
441 inline void LockChannel(Channel *cP) {cP->chMutex.Lock();}
442  int Poll2Enum(short events);
443  int SendCmd(PipeData &cmd);
444  void SetPollEnt(Channel *cP, int ptEnt);
445  bool TmoAdd(Channel *cP, int tmoSet);
446  void TmoDel(Channel *cP);
447  int TmoGet();
448 inline void UnLockChannel(Channel *cP) {cP->chMutex.UnLock();}
449 
453 virtual void Begin(XrdSysSemaphore *syncp, int &rc, const char **eTxt) = 0;
454 
459 virtual void Exclude(Channel *cP, bool &isLocked, bool dover=1) = 0;
460 
465 virtual bool Include(Channel *cP,
466  int &eNum,
467  const char **eTxt,
468  bool &isLocked) = 0;
469 
474 virtual bool Modify (Channel *cP,
475  int &eNum,
476  const char **eTxt,
477  bool &isLocked) = 0;
478 
483 //
484 virtual void Shutdown() = 0;
485 
486 // The following is common to all implementations
487 //
488 Channel *attBase; // -> First channel in attach queue or 0
489 Channel *tmoBase; // -> First channel in timeout queue or 0
490 
491 pthread_t pollTid; // Poller's thread ID
492 
493 struct pollfd pipePoll; // Stucture to wait for pipe events
494 int cmdFD; // FD to send PipeData commands
495 int reqFD; // FD to recv PipeData requests
496 struct PipeData {char req; char evt; short ent; int fd;
498  enum cmd {NoOp = 0, MdFD = 1, Post = 2,
499  MiFD = 3, RmFD = 4, Stop = 5};
500  PipeData(char reQ=0, char evT=0, short enT=0,
501  int fD =0, XrdSysSemaphore *sP=0)
502  : req(reQ), evt(evT), ent(enT), fd(fD),
503  theSem(sP) {}
505  };
506 PipeData reqBuff; // Buffer used by poller thread to recv data
507 char *pipeBuff; // Read resumption point in buffer
508 int pipeBlen; // Number of outstanding bytes
509 unsigned char tmoMask; // Timeout mask
510 CPP_ATOMIC_TYPE(bool) wakePend; // Wakeup is effectively pending (don't send)
511 bool chDead; // True if channel deleted by callback
512 
513 static time_t maxTime; // Maximum time allowed
514 
515 private:
516 
517 void Attach(Channel *cP);
518 void Detach(Channel *cP, bool &isLocked, bool keep=true);
519 void WakeUp();
520 
521 // newPoller() called to get a specialized new poll object at in response to
522 // Create(). A specialized implementation must be supplied.
523 //
524 static Poller *newPoller(int pFD[2], int &eNum, const char **eTxt);
525 
526 XrdSysMutex adMutex; // Mutex for adding & detaching channels
527 XrdSysMutex toMutex; // Mutex for handling the timeout list
528 };
529 };
530 };
531 #endif
Channel * prev
Definition: XrdSysIOEvents.hh:329
time_t wrDL
Definition: XrdSysIOEvents.hh:345
static Poller * newPoller(int pFD[2], int &eNum, const char **eTxt)
Definition: XrdSysIOEvents.hh:498
Definition: XrdSysPthread.hh:168
int GetEvents()
Definition: XrdSysIOEvents.hh:267
char chEvents
Definition: XrdSysIOEvents.hh:348
virtual void Exclude(Channel *cP, bool &isLocked, bool dover=1)=0
char dlType
Definition: XrdSysIOEvents.hh:347
int reqFD
Definition: XrdSysIOEvents.hh:495
Channel * attBase
Definition: XrdSysIOEvents.hh:488
virtual void Stop(Channel *chP, void *cbArg)
Definition: XrdSysIOEvents.hh:146
Status
Definition: XrdSysIOEvents.hh:350
Definition: XrdSysIOEvents.hh:499
virtual void Fatal(Channel *chP, void *cbArg, int eNum, const char *eTxt)
Definition: XrdSysIOEvents.hh:131
int GetFD()
Definition: XrdSysIOEvents.hh:276
int cmdFD
Definition: XrdSysIOEvents.hh:494
int SendCmd(PipeData &cmd)
Write and Write Timeouts.
Definition: XrdSysIOEvents.hh:193
dlQ attList
Definition: XrdSysIOEvents.hh:333
time_t deadLine
Definition: XrdSysIOEvents.hh:346
virtual bool Include(Channel *cP, int &eNum, const char **eTxt, bool &isLocked)=0
Channel * next
Definition: XrdSysIOEvents.hh:329
virtual ~CallBack()
Destructor.
Definition: XrdSysIOEvents.hh:158
int Poll2Enum(short events)
virtual ~Poller()
Destructor. Stop() is effecively called when this object is deleted.
Definition: XrdSysIOEvents.hh:430
Definition: XrdSysIOEvents.hh:350
static Poller * Create(int &eNum, const char **eTxt=0, int crOpts=0)
void Attach(Channel *cP)
virtual void Shutdown()=0
bool TmoAdd(Channel *cP, int tmoSet)
All of the above.
Definition: XrdSysIOEvents.hh:197
char reMod
Definition: XrdSysIOEvents.hh:353
virtual bool Modify(Channel *cP, int &eNum, const char **eTxt, bool &isLocked)=0
Poller * chPoller
Definition: XrdSysIOEvents.hh:336
char inPSet
Definition: XrdSysIOEvents.hh:352
Channel * tmoBase
Definition: XrdSysIOEvents.hh:489
short chFault
Definition: XrdSysIOEvents.hh:354
Error event non-r/w specific.
Definition: XrdSysIOEvents.hh:195
CPP_ATOMIC_TYPE(bool) wakePend
unsigned char tmoMask
Definition: XrdSysIOEvents.hh:509
Poller stop event.
Definition: XrdSysIOEvents.hh:196
Read and Read Timeouts.
Definition: XrdSysIOEvents.hh:192
Definition: XrdSysIOEvents.hh:350
XrdSysRecMutex chMutex
Definition: XrdSysIOEvents.hh:331
char * pipeBuff
Definition: XrdSysIOEvents.hh:507
Definition: XrdSysIOEvents.hh:350
Definition: XrdSysPthread.hh:140
short ent
Definition: XrdSysIOEvents.hh:496
Mask to test for valid events.
Definition: XrdSysIOEvents.hh:92
Channel(Poller *pollP, int fd, CallBack *cbP=0, void *cbArg=0)
Definition: XrdSysIOEvents.hh:78
char chStat
Definition: XrdSysIOEvents.hh:349
Definition: XrdSysIOEvents.hh:496
void UnLockChannel(Channel *cP)
Definition: XrdSysIOEvents.hh:448
pthread_t pollTid
Definition: XrdSysIOEvents.hh:491
int GetPollEnt(Channel *cP)
Definition: XrdSysIOEvents.hh:438
void Reset(Poller *thePoller, int fd, int eNum=0)
void SetCallBack(CallBack *cbP, void *cbArg=0)
void SetPollEnt(Channel *cP, int ptEnt)
void GetCallBack(CallBack **cbP, void **cbArg)
Read timeout.
Definition: XrdSysIOEvents.hh:89
~PipeData()
Definition: XrdSysIOEvents.hh:504
Definition: XrdSysPthread.hh:332
Poller(int cFD, int rFD)
Definition: XrdSysIOEvents.hh:371
int chFD
Definition: XrdSysIOEvents.hh:340
EventType
Events that may cause a callback object to be activated.
Definition: XrdSysIOEvents.hh:86
Definition: XrdSysIOEvents.hh:329
Definition: XrdSysIOEvents.hh:498
char req
Definition: XrdSysIOEvents.hh:496
Writing won&#39;t block.
Definition: XrdSysIOEvents.hh:90
Definition: XrdSysIOEvents.hh:498
int chRTO
Definition: XrdSysIOEvents.hh:342
char inTOQ
Definition: XrdSysIOEvents.hh:351
Write timeout.
Definition: XrdSysIOEvents.hh:91
XrdSysSemaphore * theSem
Definition: XrdSysIOEvents.hh:497
void Detach(Channel *cP, bool &isLocked, bool keep=true)
bool Init(Channel *cP, int &eNum, const char **eTxt, bool &isLockd)
bool Enable(int events, int timeout=0, const char **eText=0)
virtual void Begin(XrdSysSemaphore *syncp, int &rc, const char **eTxt)=0
char evt
Definition: XrdSysIOEvents.hh:496
Poller * chPollXQ
Definition: XrdSysIOEvents.hh:337
void Lock()
Definition: XrdSysPthread.hh:149
bool chDead
Definition: XrdSysIOEvents.hh:511
Definition: XrdSysIOEvents.hh:499
void TmoDel(Channel *cP)
Both of the above.
Definition: XrdSysIOEvents.hh:194
PipeData(char reQ=0, char evT=0, short enT=0, int fD=0, XrdSysSemaphore *sP=0)
Definition: XrdSysIOEvents.hh:500
int fd
Definition: XrdSysIOEvents.hh:496
XrdSysMutex toMutex
Definition: XrdSysIOEvents.hh:527
Definition: XrdSysIOEvents.hh:499
int chWTO
Definition: XrdSysIOEvents.hh:343
virtual bool Event(Channel *chP, void *cbArg, int evFlags)=0
static time_t maxTime
Definition: XrdSysIOEvents.hh:513
Definition: XrdSysIOEvents.hh:173
int pollEnt
Definition: XrdSysIOEvents.hh:341
XrdSysMutex adMutex
Definition: XrdSysIOEvents.hh:526
CallBack()
Constructor.
Definition: XrdSysIOEvents.hh:152
friend class BootStrap
Definition: XrdSysIOEvents.hh:373
EventCode
Event bits used to feed Enable() and Disable(); can be or&#39;d.
Definition: XrdSysIOEvents.hh:192
void UnLock()
Definition: XrdSysPthread.hh:151
struct pollfd pipePoll
Definition: XrdSysIOEvents.hh:493
void LockChannel(Channel *cP)
Definition: XrdSysIOEvents.hh:441
Definition: XrdSysIOEvents.hh:398
dlQ tmoList
Definition: XrdSysIOEvents.hh:334
CallBack * chCB
Definition: XrdSysIOEvents.hh:338
bool Disable(int events, const char **eText=0)
cmd
Definition: XrdSysIOEvents.hh:498
bool CbkXeq(Channel *cP, int events, int eNum, const char *eTxt)
void * chCBA
Definition: XrdSysIOEvents.hh:339
PipeData reqBuff
Definition: XrdSysIOEvents.hh:506
int GetFault(Channel *cP)
Definition: XrdSysIOEvents.hh:437
int pipeBlen
Definition: XrdSysIOEvents.hh:508
time_t rdDL
Definition: XrdSysIOEvents.hh:344
CreateOpts
Definition: XrdSysIOEvents.hh:398
New data has arrived.
Definition: XrdSysIOEvents.hh:88
~Channel()
Destuctor is private, use Delete() to delete this object.
Definition: XrdSysIOEvents.hh:327