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  };
92 
93 //-----------------------------------------------------------------------------
110 //-----------------------------------------------------------------------------
111 
112 virtual bool Event(Channel *chP, void *cbArg, int evFlags) = 0;
113 
114 //-----------------------------------------------------------------------------
125 //-----------------------------------------------------------------------------
126 
127 virtual void Fatal(Channel *chP, void *cbArg, int eNum, const char *eTxt)
128 {
129  (void)chP; (void)cbArg; (void)eNum; (void)eTxt;
130 };
131 
132 //-----------------------------------------------------------------------------
140 //-----------------------------------------------------------------------------
141 
142 virtual void Stop(Channel *chP, void *cbArg)
143 {
144  (void)chP; (void)cbArg;
145 }
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 //-----------------------------------------------------------------------------
179 //-----------------------------------------------------------------------------
180 
181 enum EventCode {readEvents = 0x01,
182  writeEvents = 0x04,
183  rwEvents = 0x05,
184  errorEvents = 0x10,
185  stopEvent = 0x20,
186  allEvents = 0x35
187  };
188 
189 //-----------------------------------------------------------------------------
199 //-----------------------------------------------------------------------------
200 
201  bool Disable(int events, const char **eText=0);
202 
203 //-----------------------------------------------------------------------------
235 //-----------------------------------------------------------------------------
236 
237  bool Enable(int events, int timeout=0, const char **eText=0);
238 
239 //-----------------------------------------------------------------------------
244 //-----------------------------------------------------------------------------
245 
246  void GetCallBack(CallBack **cbP, void **cbArg);
247 
248 //-----------------------------------------------------------------------------
254 //-----------------------------------------------------------------------------
255 
256  int GetEvents() {return (chPoller ? static_cast<int>(chEvents) : -1);}
257 
258 //-----------------------------------------------------------------------------
263 //-----------------------------------------------------------------------------
264 
265  int GetFD() {return chFD;}
266 
267 //-----------------------------------------------------------------------------
274 //-----------------------------------------------------------------------------
275 
276  void SetCallBack(CallBack *cbP, void *cbArg=0);
277 
278 //-----------------------------------------------------------------------------
287 //-----------------------------------------------------------------------------
288 
289  void SetFD(int fd);
290 
291 //-----------------------------------------------------------------------------
306 //-----------------------------------------------------------------------------
307 
308  Channel(Poller *pollP, int fd, CallBack *cbP=0, void *cbArg=0);
309 
310 //-----------------------------------------------------------------------------
314 //-----------------------------------------------------------------------------
315 
316  ~Channel();
317 
318 private:
319 
320 struct dlQ {Channel *next; Channel *prev;};
321 
323 
324 dlQ attList; // List of attached channels
325 dlQ tmoList; // List of channels in the timeout queue
326 
327 Poller *chPoller; // The effective poller
328 Poller *chPollXQ; // The real poller
329 CallBack *chCB; // CallBack function
330 void *chCBA; // CallBack argument
331 int chFD; // Associated file descriptor
332 int pollEnt; // Used only for poll() type pollers
333 int chRTO; // Read timeout value (0 means none)
334 int chWTO; // Write timeout value (0 means none)
335 time_t rdDL; // Read deadline
336 time_t wrDL; // Write deadline
337 time_t deadLine; // The deadline in effect (read or write)
338 char dlType; // The deadline type in deadLine as CallBack type
339 char chEvents; // Enabled events as Channel type
340 char chStat; // Channel status below (!0 -> in callback mode)
342 char inTOQ; // True if the channel is in the timeout queue
343 char inPSet; // FD is in the actual poll set
344 char reMod; // Modify issued while defered, re-issue needed
345 short chFault; // Defered error, 0 if all is well
346 
347 void Reset(Poller *thePoller, int fd, int eNum=0);
348 };
349 
350 /******************************************************************************/
351 /* C l a s s P o l l e r */
352 /******************************************************************************/
353 
354 //-----------------------------------------------------------------------------
360 //-----------------------------------------------------------------------------
361 
362 class Poller
363 {
364 friend class BootStrap;
365 friend class Channel;
366 public:
367 
368 //-----------------------------------------------------------------------------
382 //-----------------------------------------------------------------------------
383 
384 static Poller *Create(int &eNum, const char **eTxt=0);
385 
386 //-----------------------------------------------------------------------------
397 //-----------------------------------------------------------------------------
398 
399  void Stop();
400 
401 //-----------------------------------------------------------------------------
406 //-----------------------------------------------------------------------------
407 
408  Poller(int cFD, int rFD);
409 
410 //-----------------------------------------------------------------------------
412 //-----------------------------------------------------------------------------
413 
414 virtual ~Poller() {}
415 
416 protected:
417 struct PipeData;
418 
419  void CbkTMO();
420  bool CbkXeq(Channel *cP, int events, int eNum, const char *eTxt);
421 inline int GetFault(Channel *cP) {return cP->chFault;}
422 inline int GetPollEnt(Channel *cP) {return cP->pollEnt;}
423  int GetRequest();
424  bool Init(Channel *cP, int &eNum, const char **eTxt, bool &isLockd);
425 inline void LockChannel(Channel *cP) {cP->chMutex.Lock();}
426  int Poll2Enum(short events);
427  int SendCmd(PipeData &cmd);
428  void SetPollEnt(Channel *cP, int ptEnt);
429  bool TmoAdd(Channel *cP);
430  void TmoDel(Channel *cP);
431  int TmoGet();
432 inline void UnLockChannel(Channel *cP) {cP->chMutex.UnLock();}
433 
437 virtual void Begin(XrdSysSemaphore *syncp, int &rc, const char **eTxt) = 0;
438 
443 virtual void Exclude(Channel *cP, bool &isLocked, bool dover=1) = 0;
444 
449 virtual bool Include(Channel *cP,
450  int &eNum,
451  const char **eTxt,
452  bool &isLocked) = 0;
453 
458 virtual bool Modify (Channel *cP,
459  int &eNum,
460  const char **eTxt,
461  bool &isLocked) = 0;
462 
467 //
468 virtual void Shutdown() = 0;
469 
470 // The following is common to all implementations
471 //
472 Channel *attBase; // -> First channel in attach queue or 0
473 Channel *tmoBase; // -> First channel in timeout queue or 0
474 
475 pthread_t pollTid; // Poller's thread ID
476 
477 struct pollfd pipePoll; // Stucture to wait for pipe events
478 int cmdFD; // FD to send PipeData commands
479 int reqFD; // FD to recv PipeData requests
480 struct PipeData {char req; char evt; short ent; int fd;
482  enum cmd {NoOp = 0, MdFD = 1, Post = 2,
483  MiFD = 3, RmFD = 4, Stop = 5};
484  };
485 PipeData reqBuff; // Buffer used by poller thread to recv data
486 char *pipeBuff; // Read resumption point in buffer
487 int pipeBlen; // Number of outstanding bytes
488 bool wakePend; // Wakeup is effectively pending (don't send)
489 bool chDead; // True if channel deleted by callback
490 
491 static time_t maxTime; // Maximum time allowed
492 
493 private:
494 
495 void Attach(Channel *cP);
496 void Detach(Channel *cP, bool &isLocked, bool keep=true);
497 void WakeUp();
498 
499 // newPoller() called to get a specialized new poll object at in response to
500 // Create(). A specialized implementation must be supplied.
501 //
502 static Poller *newPoller(int pFD[2], int &eNum, const char **eTxt);
503 
504 XrdSysMutex adMutex; // Mutex for adding & detaching channels
505 XrdSysMutex toMutex; // Mutex for handling the timeout list
506 };
507 };
508 };
509 #endif
Channel * prev
Definition: XrdSysIOEvents.hh:320
time_t wrDL
Definition: XrdSysIOEvents.hh:336
static Poller * newPoller(int pFD[2], int &eNum, const char **eTxt)
Definition: XrdSysIOEvents.hh:482
Definition: XrdSysPthread.hh:168
int GetEvents()
Definition: XrdSysIOEvents.hh:256
char chEvents
Definition: XrdSysIOEvents.hh:339
virtual void Exclude(Channel *cP, bool &isLocked, bool dover=1)=0
char dlType
Definition: XrdSysIOEvents.hh:338
int reqFD
Definition: XrdSysIOEvents.hh:479
Channel * attBase
Definition: XrdSysIOEvents.hh:472
virtual void Stop(Channel *chP, void *cbArg)
Definition: XrdSysIOEvents.hh:142
Status
Definition: XrdSysIOEvents.hh:341
Definition: XrdSysIOEvents.hh:483
virtual void Fatal(Channel *chP, void *cbArg, int eNum, const char *eTxt)
Definition: XrdSysIOEvents.hh:127
int GetFD()
Definition: XrdSysIOEvents.hh:265
int cmdFD
Definition: XrdSysIOEvents.hh:478
int SendCmd(PipeData &cmd)
Write and Write Timeouts.
Definition: XrdSysIOEvents.hh:182
dlQ attList
Definition: XrdSysIOEvents.hh:324
time_t deadLine
Definition: XrdSysIOEvents.hh:337
virtual bool Include(Channel *cP, int &eNum, const char **eTxt, bool &isLocked)=0
Channel * next
Definition: XrdSysIOEvents.hh:320
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:414
Definition: XrdSysIOEvents.hh:341
void Attach(Channel *cP)
virtual void Shutdown()=0
All of the above.
Definition: XrdSysIOEvents.hh:186
char reMod
Definition: XrdSysIOEvents.hh:344
virtual bool Modify(Channel *cP, int &eNum, const char **eTxt, bool &isLocked)=0
Poller * chPoller
Definition: XrdSysIOEvents.hh:327
char inPSet
Definition: XrdSysIOEvents.hh:343
Channel * tmoBase
Definition: XrdSysIOEvents.hh:473
short chFault
Definition: XrdSysIOEvents.hh:345
Error event non-r/w specific.
Definition: XrdSysIOEvents.hh:184
Poller stop event.
Definition: XrdSysIOEvents.hh:185
Read and Read Timeouts.
Definition: XrdSysIOEvents.hh:181
Definition: XrdSysIOEvents.hh:341
XrdSysRecMutex chMutex
Definition: XrdSysIOEvents.hh:322
char * pipeBuff
Definition: XrdSysIOEvents.hh:486
Definition: XrdSysIOEvents.hh:341
Definition: XrdSysPthread.hh:140
bool wakePend
Definition: XrdSysIOEvents.hh:488
short ent
Definition: XrdSysIOEvents.hh:480
Channel(Poller *pollP, int fd, CallBack *cbP=0, void *cbArg=0)
Definition: XrdSysIOEvents.hh:77
char chStat
Definition: XrdSysIOEvents.hh:340
Definition: XrdSysIOEvents.hh:480
void UnLockChannel(Channel *cP)
Definition: XrdSysIOEvents.hh:432
pthread_t pollTid
Definition: XrdSysIOEvents.hh:475
int GetPollEnt(Channel *cP)
Definition: XrdSysIOEvents.hh:422
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
Definition: XrdSysPthread.hh:329
Poller(int cFD, int rFD)
Definition: XrdSysIOEvents.hh:362
int chFD
Definition: XrdSysIOEvents.hh:331
EventType
Events that may cause a callback object to be activated.
Definition: XrdSysIOEvents.hh:85
Definition: XrdSysIOEvents.hh:320
Definition: XrdSysIOEvents.hh:482
char req
Definition: XrdSysIOEvents.hh:480
static Poller * Create(int &eNum, const char **eTxt=0)
Writing won&#39;t block.
Definition: XrdSysIOEvents.hh:89
Definition: XrdSysIOEvents.hh:482
int chRTO
Definition: XrdSysIOEvents.hh:333
char inTOQ
Definition: XrdSysIOEvents.hh:342
Write timeout.
Definition: XrdSysIOEvents.hh:90
XrdSysSemaphore * theSem
Definition: XrdSysIOEvents.hh:481
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:480
Poller * chPollXQ
Definition: XrdSysIOEvents.hh:328
void Lock()
Definition: XrdSysPthread.hh:149
bool chDead
Definition: XrdSysIOEvents.hh:489
Definition: XrdSysIOEvents.hh:483
void TmoDel(Channel *cP)
Both of the above.
Definition: XrdSysIOEvents.hh:183
int fd
Definition: XrdSysIOEvents.hh:480
XrdSysMutex toMutex
Definition: XrdSysIOEvents.hh:505
Definition: XrdSysIOEvents.hh:483
int chWTO
Definition: XrdSysIOEvents.hh:334
virtual bool Event(Channel *chP, void *cbArg, int evFlags)=0
static time_t maxTime
Definition: XrdSysIOEvents.hh:491
Definition: XrdSysIOEvents.hh:172
int pollEnt
Definition: XrdSysIOEvents.hh:332
XrdSysMutex adMutex
Definition: XrdSysIOEvents.hh:504
CallBack()
Constructor.
Definition: XrdSysIOEvents.hh:151
friend class BootStrap
Definition: XrdSysIOEvents.hh:364
EventCode
Event bits used to feed Enable() and Disable(); can be or&#39;d.
Definition: XrdSysIOEvents.hh:181
void UnLock()
Definition: XrdSysPthread.hh:151
struct pollfd pipePoll
Definition: XrdSysIOEvents.hh:477
bool TmoAdd(Channel *cP)
void LockChannel(Channel *cP)
Definition: XrdSysIOEvents.hh:425
dlQ tmoList
Definition: XrdSysIOEvents.hh:325
CallBack * chCB
Definition: XrdSysIOEvents.hh:329
bool Disable(int events, const char **eText=0)
cmd
Definition: XrdSysIOEvents.hh:482
bool CbkXeq(Channel *cP, int events, int eNum, const char *eTxt)
void * chCBA
Definition: XrdSysIOEvents.hh:330
PipeData reqBuff
Definition: XrdSysIOEvents.hh:485
int GetFault(Channel *cP)
Definition: XrdSysIOEvents.hh:421
int pipeBlen
Definition: XrdSysIOEvents.hh:487
time_t rdDL
Definition: XrdSysIOEvents.hh:335
New data has arrived.
Definition: XrdSysIOEvents.hh:87