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 
39 //-----------------------------------------------------------------------------
53 //-----------------------------------------------------------------------------
54 
55 namespace XrdSys
56 {
57 namespace IOEvents
58 {
59 
60 /******************************************************************************/
61 /* C l a s s C a l l B a c k */
62 /******************************************************************************/
63 
64 //-----------------------------------------------------------------------------
74 //-----------------------------------------------------------------------------
75 
76 class Channel;
77 class CallBack
78 {
79 public:
80 
81 //-----------------------------------------------------------------------------
83 //-----------------------------------------------------------------------------
84 
85  enum EventType
86  {
87  ReadyToRead = 0x01,
88  ReadTimeOut = 0x02,
89  ReadyToWrite = 0x04,
90  WriteTimeOut = 0x08,
91  ValidEvents = 0x0f
92  };
93 
94 //-----------------------------------------------------------------------------
113 //-----------------------------------------------------------------------------
114 
115 virtual bool Event(Channel *chP, void *cbArg, int evFlags) = 0;
116 
117 //-----------------------------------------------------------------------------
128 //-----------------------------------------------------------------------------
129 
130 virtual void Fatal(Channel *chP, void *cbArg, int eNum, const char *eTxt)
131 {
132  (void)chP; (void)cbArg; (void)eNum; (void)eTxt;
133 };
134 
135 //-----------------------------------------------------------------------------
143 //-----------------------------------------------------------------------------
144 
145 virtual void Stop(Channel *chP, void *cbArg) { (void)chP; (void)cbArg;}
146 
147 //-----------------------------------------------------------------------------
149 //-----------------------------------------------------------------------------
150 
151  CallBack() {}
152 
153 //-----------------------------------------------------------------------------
155 //-----------------------------------------------------------------------------
156 
157 virtual ~CallBack() {}
158 };
159 
160 /******************************************************************************/
161 /* C l a s s C h a n n e l */
162 /******************************************************************************/
163 
164 //-----------------------------------------------------------------------------
168 //-----------------------------------------------------------------------------
169 
170 class ChannelWait;
171 class Poller;
172 class Channel
173 {
174 friend class Poller;
175 public:
176 
177 //-----------------------------------------------------------------------------
183 //-----------------------------------------------------------------------------
184 
185  void Delete();
186 
187 //-----------------------------------------------------------------------------
189 //-----------------------------------------------------------------------------
190 
191 enum EventCode {readEvents = 0x01,
192  writeEvents = 0x04,
193  rwEvents = 0x05,
194  errorEvents = 0x10,
195  stopEvent = 0x20,
196  allEvents = 0x35
197  };
198 
199 //-----------------------------------------------------------------------------
209 //-----------------------------------------------------------------------------
210 
211  bool Disable(int events, const char **eText=0);
212 
213 //-----------------------------------------------------------------------------
245 //-----------------------------------------------------------------------------
246 
247  bool Enable(int events, int timeout=0, const char **eText=0);
248 
249 //-----------------------------------------------------------------------------
254 //-----------------------------------------------------------------------------
255 
256  void GetCallBack(CallBack **cbP, void **cbArg);
257 
258 //-----------------------------------------------------------------------------
264 //-----------------------------------------------------------------------------
265 
266 inline int GetEvents() {return (chPoller ? static_cast<int>(chEvents) : -1);}
267 
268 //-----------------------------------------------------------------------------
273 //-----------------------------------------------------------------------------
274 
275 inline int GetFD() {return chFD;}
276 
277 //-----------------------------------------------------------------------------
284 //-----------------------------------------------------------------------------
285 
286  void SetCallBack(CallBack *cbP, void *cbArg=0);
287 
288 //-----------------------------------------------------------------------------
297 //-----------------------------------------------------------------------------
298 
299  void SetFD(int fd);
300 
301 //-----------------------------------------------------------------------------
316 //-----------------------------------------------------------------------------
317 
318  Channel(Poller *pollP, int fd, CallBack *cbP=0, void *cbArg=0);
319 
320 private:
321 
322 //-----------------------------------------------------------------------------
324 //-----------------------------------------------------------------------------
325 
326  ~Channel() {}
327 
328 struct dlQ {Channel *next; Channel *prev;};
329 
331 
332 dlQ attList; // List of attached channels
333 dlQ tmoList; // List of channels in the timeout queue
334 
335 Poller *chPoller; // The effective poller
336 Poller *chPollXQ; // The real poller
337 CallBack *chCB; // CallBack function
338 void *chCBA; // CallBack argument
339 int chFD; // Associated file descriptor
340 int pollEnt; // Used only for poll() type pollers
341 int chRTO; // Read timeout value (0 means none)
342 int chWTO; // Write timeout value (0 means none)
343 time_t rdDL; // Read deadline
344 time_t wrDL; // Write deadline
345 time_t deadLine; // The deadline in effect (read or write)
346 char dlType; // The deadline type in deadLine as CallBack type
347 char chEvents; // Enabled events as Channel type
348 char chStat; // Channel status below (!0 -> in callback mode)
350 char inTOQ; // True if the channel is in the timeout queue
351 char inPSet; // FD is in the actual poll set
352 char reMod; // Modify issued while defered, re-issue needed
353 short chFault; // Defered error, 0 if all is well
354 
355 void Reset(Poller *thePoller, int fd, int eNum=0);
356 };
357 
358 /******************************************************************************/
359 /* C l a s s P o l l e r */
360 /******************************************************************************/
361 
362 //-----------------------------------------------------------------------------
368 //-----------------------------------------------------------------------------
369 
370 class Poller
371 {
372 friend class BootStrap;
373 friend class Channel;
374 public:
375 
376 //-----------------------------------------------------------------------------
395 //-----------------------------------------------------------------------------
396 
398 
399 static Poller *Create(int &eNum, const char **eTxt=0, int crOpts=0);
400 
401 //-----------------------------------------------------------------------------
412 //-----------------------------------------------------------------------------
413 
414  void Stop();
415 
416 //-----------------------------------------------------------------------------
421 //-----------------------------------------------------------------------------
422 
423  Poller(int cFD, int rFD);
424 
425 //-----------------------------------------------------------------------------
427 //-----------------------------------------------------------------------------
428 
429 virtual ~Poller() {}
430 
431 protected:
432 struct PipeData;
433 
434  void CbkTMO();
435  bool CbkXeq(Channel *cP, int events, int eNum, const char *eTxt);
436 inline int GetFault(Channel *cP) {return cP->chFault;}
437 inline int GetPollEnt(Channel *cP) {return cP->pollEnt;}
438  int GetRequest();
439  bool Init(Channel *cP, int &eNum, const char **eTxt, bool &isLockd);
440 inline void LockChannel(Channel *cP) {cP->chMutex.Lock();}
441  int Poll2Enum(short events);
442  int SendCmd(PipeData &cmd);
443  void SetPollEnt(Channel *cP, int ptEnt);
444  bool TmoAdd(Channel *cP, int tmoSet);
445  void TmoDel(Channel *cP);
446  int TmoGet();
447 inline void UnLockChannel(Channel *cP) {cP->chMutex.UnLock();}
448 
452 virtual void Begin(XrdSysSemaphore *syncp, int &rc, const char **eTxt) = 0;
453 
458 virtual void Exclude(Channel *cP, bool &isLocked, bool dover=1) = 0;
459 
464 virtual bool Include(Channel *cP,
465  int &eNum,
466  const char **eTxt,
467  bool &isLocked) = 0;
468 
473 virtual bool Modify (Channel *cP,
474  int &eNum,
475  const char **eTxt,
476  bool &isLocked) = 0;
477 
482 //
483 virtual void Shutdown() = 0;
484 
485 // The following is common to all implementations
486 //
487 Channel *attBase; // -> First channel in attach queue or 0
488 Channel *tmoBase; // -> First channel in timeout queue or 0
489 
490 pthread_t pollTid; // Poller's thread ID
491 
492 struct pollfd pipePoll; // Stucture to wait for pipe events
493 int cmdFD; // FD to send PipeData commands
494 int reqFD; // FD to recv PipeData requests
495 struct PipeData {char req; char evt; short ent; int fd;
497  enum cmd {NoOp = 0, MdFD = 1, Post = 2,
498  MiFD = 3, RmFD = 4, Stop = 5};
499  PipeData(char reQ=0, char evT=0, short enT=0,
500  int fD =0, XrdSysSemaphore *sP=0)
501  : req(reQ), evt(evT), ent(enT), fd(fD),
502  theSem(sP) {}
504  };
505 PipeData reqBuff; // Buffer used by poller thread to recv data
506 char *pipeBuff; // Read resumption point in buffer
507 int pipeBlen; // Number of outstanding bytes
508 char tmoMask; // Timeout mask
509 bool wakePend; // Wakeup is effectively pending (don't send)
510 bool chDead; // True if channel deleted by callback
511 
512 static time_t maxTime; // Maximum time allowed
513 
514 private:
515 
516 void Attach(Channel *cP);
517 void Detach(Channel *cP, bool &isLocked, bool keep=true);
518 void WakeUp();
519 
520 // newPoller() called to get a specialized new poll object at in response to
521 // Create(). A specialized implementation must be supplied.
522 //
523 static Poller *newPoller(int pFD[2], int &eNum, const char **eTxt);
524 
525 XrdSysMutex adMutex; // Mutex for adding & detaching channels
526 XrdSysMutex toMutex; // Mutex for handling the timeout list
527 };
528 };
529 };
530 #endif
Channel * prev
Definition: XrdSysIOEvents.hh:328
time_t wrDL
Definition: XrdSysIOEvents.hh:344
static Poller * newPoller(int pFD[2], int &eNum, const char **eTxt)
Definition: XrdSysIOEvents.hh:497
Definition: XrdSysPthread.hh:168
int GetEvents()
Definition: XrdSysIOEvents.hh:266
char chEvents
Definition: XrdSysIOEvents.hh:347
virtual void Exclude(Channel *cP, bool &isLocked, bool dover=1)=0
char dlType
Definition: XrdSysIOEvents.hh:346
int reqFD
Definition: XrdSysIOEvents.hh:494
Channel * attBase
Definition: XrdSysIOEvents.hh:487
virtual void Stop(Channel *chP, void *cbArg)
Definition: XrdSysIOEvents.hh:145
Status
Definition: XrdSysIOEvents.hh:349
Definition: XrdSysIOEvents.hh:498
virtual void Fatal(Channel *chP, void *cbArg, int eNum, const char *eTxt)
Definition: XrdSysIOEvents.hh:130
int GetFD()
Definition: XrdSysIOEvents.hh:275
int cmdFD
Definition: XrdSysIOEvents.hh:493
int SendCmd(PipeData &cmd)
Write and Write Timeouts.
Definition: XrdSysIOEvents.hh:192
dlQ attList
Definition: XrdSysIOEvents.hh:332
time_t deadLine
Definition: XrdSysIOEvents.hh:345
virtual bool Include(Channel *cP, int &eNum, const char **eTxt, bool &isLocked)=0
Channel * next
Definition: XrdSysIOEvents.hh:328
virtual ~CallBack()
Destructor.
Definition: XrdSysIOEvents.hh:157
int Poll2Enum(short events)
virtual ~Poller()
Destructor. Stop() is effecively called when this object is deleted.
Definition: XrdSysIOEvents.hh:429
Definition: XrdSysIOEvents.hh:349
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:196
char reMod
Definition: XrdSysIOEvents.hh:352
virtual bool Modify(Channel *cP, int &eNum, const char **eTxt, bool &isLocked)=0
Poller * chPoller
Definition: XrdSysIOEvents.hh:335
char inPSet
Definition: XrdSysIOEvents.hh:351
Channel * tmoBase
Definition: XrdSysIOEvents.hh:488
short chFault
Definition: XrdSysIOEvents.hh:353
Error event non-r/w specific.
Definition: XrdSysIOEvents.hh:194
Poller stop event.
Definition: XrdSysIOEvents.hh:195
char tmoMask
Definition: XrdSysIOEvents.hh:508
Read and Read Timeouts.
Definition: XrdSysIOEvents.hh:191
Definition: XrdSysIOEvents.hh:349
XrdSysRecMutex chMutex
Definition: XrdSysIOEvents.hh:330
char * pipeBuff
Definition: XrdSysIOEvents.hh:506
Definition: XrdSysIOEvents.hh:349
Definition: XrdSysPthread.hh:140
bool wakePend
Definition: XrdSysIOEvents.hh:509
short ent
Definition: XrdSysIOEvents.hh:495
Mask to test for valid events.
Definition: XrdSysIOEvents.hh:91
Channel(Poller *pollP, int fd, CallBack *cbP=0, void *cbArg=0)
Definition: XrdSysIOEvents.hh:77
char chStat
Definition: XrdSysIOEvents.hh:348
Definition: XrdSysIOEvents.hh:495
void UnLockChannel(Channel *cP)
Definition: XrdSysIOEvents.hh:447
pthread_t pollTid
Definition: XrdSysIOEvents.hh:490
int GetPollEnt(Channel *cP)
Definition: XrdSysIOEvents.hh:437
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:88
~PipeData()
Definition: XrdSysIOEvents.hh:503
Definition: XrdSysPthread.hh:329
Poller(int cFD, int rFD)
Definition: XrdSysIOEvents.hh:370
int chFD
Definition: XrdSysIOEvents.hh:339
EventType
Events that may cause a callback object to be activated.
Definition: XrdSysIOEvents.hh:85
Definition: XrdSysIOEvents.hh:328
Definition: XrdSysIOEvents.hh:497
char req
Definition: XrdSysIOEvents.hh:495
Writing won&#39;t block.
Definition: XrdSysIOEvents.hh:89
Definition: XrdSysIOEvents.hh:497
int chRTO
Definition: XrdSysIOEvents.hh:341
char inTOQ
Definition: XrdSysIOEvents.hh:350
Write timeout.
Definition: XrdSysIOEvents.hh:90
XrdSysSemaphore * theSem
Definition: XrdSysIOEvents.hh:496
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:495
Poller * chPollXQ
Definition: XrdSysIOEvents.hh:336
void Lock()
Definition: XrdSysPthread.hh:149
bool chDead
Definition: XrdSysIOEvents.hh:510
Definition: XrdSysIOEvents.hh:498
void TmoDel(Channel *cP)
Both of the above.
Definition: XrdSysIOEvents.hh:193
PipeData(char reQ=0, char evT=0, short enT=0, int fD=0, XrdSysSemaphore *sP=0)
Definition: XrdSysIOEvents.hh:499
int fd
Definition: XrdSysIOEvents.hh:495
XrdSysMutex toMutex
Definition: XrdSysIOEvents.hh:526
Definition: XrdSysIOEvents.hh:498
int chWTO
Definition: XrdSysIOEvents.hh:342
virtual bool Event(Channel *chP, void *cbArg, int evFlags)=0
static time_t maxTime
Definition: XrdSysIOEvents.hh:512
Definition: XrdSysIOEvents.hh:172
int pollEnt
Definition: XrdSysIOEvents.hh:340
XrdSysMutex adMutex
Definition: XrdSysIOEvents.hh:525
CallBack()
Constructor.
Definition: XrdSysIOEvents.hh:151
friend class BootStrap
Definition: XrdSysIOEvents.hh:372
EventCode
Event bits used to feed Enable() and Disable(); can be or&#39;d.
Definition: XrdSysIOEvents.hh:191
void UnLock()
Definition: XrdSysPthread.hh:151
struct pollfd pipePoll
Definition: XrdSysIOEvents.hh:492
void LockChannel(Channel *cP)
Definition: XrdSysIOEvents.hh:440
Definition: XrdSysIOEvents.hh:397
dlQ tmoList
Definition: XrdSysIOEvents.hh:333
CallBack * chCB
Definition: XrdSysIOEvents.hh:337
bool Disable(int events, const char **eText=0)
cmd
Definition: XrdSysIOEvents.hh:497
bool CbkXeq(Channel *cP, int events, int eNum, const char *eTxt)
void * chCBA
Definition: XrdSysIOEvents.hh:338
PipeData reqBuff
Definition: XrdSysIOEvents.hh:505
int GetFault(Channel *cP)
Definition: XrdSysIOEvents.hh:436
int pipeBlen
Definition: XrdSysIOEvents.hh:507
time_t rdDL
Definition: XrdSysIOEvents.hh:343
CreateOpts
Definition: XrdSysIOEvents.hh:397
New data has arrived.
Definition: XrdSysIOEvents.hh:87
~Channel()
Destuctor is private, use Delete() to delete this object.
Definition: XrdSysIOEvents.hh:326