xrootd
Loading...
Searching...
No Matches
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 <ctime>
35#include <sys/types.h>
36
39
40//-----------------------------------------------------------------------------
54//-----------------------------------------------------------------------------
55
56namespace XrdSys
57{
58namespace IOEvents
59{
60
61/******************************************************************************/
62/* C l a s s C a l l B a c k */
63/******************************************************************************/
64
65//-----------------------------------------------------------------------------
75//-----------------------------------------------------------------------------
76
77class Channel;
79{
80public:
81
82//-----------------------------------------------------------------------------
84//-----------------------------------------------------------------------------
85
87 {
88 ReadyToRead = 0x01,
89 ReadTimeOut = 0x02,
90 ReadyToWrite = 0x04,
91 WriteTimeOut = 0x08,
92 ValidEvents = 0x0f
93 };
94
95//-----------------------------------------------------------------------------
114//-----------------------------------------------------------------------------
115
116virtual bool Event(Channel *chP, void *cbArg, int evFlags) = 0;
117
118//-----------------------------------------------------------------------------
129//-----------------------------------------------------------------------------
130
131virtual 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
146virtual void Stop(Channel *chP, void *cbArg) { (void)chP; (void)cbArg;}
147
148//-----------------------------------------------------------------------------
150//-----------------------------------------------------------------------------
151
153
154//-----------------------------------------------------------------------------
156//-----------------------------------------------------------------------------
157
158virtual ~CallBack() {}
159};
160
161/******************************************************************************/
162/* C l a s s C h a n n e l */
163/******************************************************************************/
164
165//-----------------------------------------------------------------------------
169//-----------------------------------------------------------------------------
170
171class ChannelWait;
172class Poller;
174{
175friend class Poller;
176public:
177
178//-----------------------------------------------------------------------------
184//-----------------------------------------------------------------------------
185
186 void Delete();
187
188//-----------------------------------------------------------------------------
190//-----------------------------------------------------------------------------
191
192enum 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
267inline int GetEvents() {return (chPoller ? static_cast<int>(chEvents) : -1);}
268
269//-----------------------------------------------------------------------------
274//-----------------------------------------------------------------------------
275
276inline 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
321private:
322
323//-----------------------------------------------------------------------------
325//-----------------------------------------------------------------------------
326
328
330
332
333dlQ attList; // List of attached channels
334dlQ tmoList; // List of channels in the timeout queue
335
336Poller *chPoller; // The effective poller
337Poller *chPollXQ; // The real poller
338CallBack *chCB; // CallBack function
339void *chCBA; // CallBack argument
340int chFD; // Associated file descriptor
341int pollEnt; // Used only for poll() type pollers
342int chRTO; // Read timeout value (0 means none)
343int chWTO; // Write timeout value (0 means none)
344time_t rdDL; // Read deadline
345time_t wrDL; // Write deadline
346time_t deadLine; // The deadline in effect (read or write)
347char dlType; // The deadline type in deadLine as CallBack type
348char chEvents; // Enabled events as Channel type
349char chStat; // Channel status below (!0 -> in callback mode)
351char inTOQ; // True if the channel is in the timeout queue
352char inPSet; // FD is in the actual poll set
353char reMod; // Modify issued while deferred, re-issue needed
354short chFault; // Deferred error, 0 if all is well
355
356void 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
372{
373friend class BootStrap;
374friend class Channel;
375public:
376
377//-----------------------------------------------------------------------------
396//-----------------------------------------------------------------------------
397
399
400static 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
430virtual ~Poller() {}
431
432protected:
433struct PipeData;
434
435 void CbkTMO();
436 bool CbkXeq(Channel *cP, int events, int eNum, const char *eTxt);
437inline int GetFault(Channel *cP) {return cP->chFault;}
438inline int GetPollEnt(Channel *cP) {return cP->pollEnt;}
440 bool Init(Channel *cP, int &eNum, const char **eTxt, bool &isLockd);
441inline 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();
448inline void UnLockChannel(Channel *cP) {cP->chMutex.UnLock();}
449
453virtual void Begin(XrdSysSemaphore *syncp, int &rc, const char **eTxt) = 0;
454
459virtual void Exclude(Channel *cP, bool &isLocked, bool dover=1) = 0;
460
465virtual bool Include(Channel *cP,
466 int &eNum,
467 const char **eTxt,
468 bool &isLocked) = 0;
469
474virtual bool Modify (Channel *cP,
475 int &eNum,
476 const char **eTxt,
477 bool &isLocked) = 0;
478
483//
484virtual void Shutdown() = 0;
485
486// The following is common to all implementations
487//
488Channel *attBase; // -> First channel in attach queue or 0
489Channel *tmoBase; // -> First channel in timeout queue or 0
490
491pthread_t pollTid; // Poller's thread ID
492
493struct pollfd pipePoll; // Stucture to wait for pipe events
494int cmdFD; // FD to send PipeData commands
495int reqFD; // FD to recv PipeData requests
496struct 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 };
506PipeData reqBuff; // Buffer used by poller thread to recv data
507char *pipeBuff; // Read resumption point in buffer
508int pipeBlen; // Number of outstanding bytes
509unsigned char tmoMask; // Timeout mask
510CPP_ATOMIC_TYPE(bool) wakePend; // Wakeup is effectively pending (don't send)
511bool chDead; // True if channel deleted by callback
512
513static time_t maxTime; // Maximum time allowed
514
515static pid_t parentPID; // register parent process PID
516
517private:
518
519void Attach(Channel *cP);
520void Detach(Channel *cP, bool &isLocked, bool keep=true);
521void WakeUp();
522
523// newPoller() called to get a specialized new poll object at in response to
524// Create(). A specialized implementation must be supplied.
525//
526static Poller *newPoller(int pFD[2], int &eNum, const char **eTxt);
527
528XrdSysMutex adMutex; // Mutex for adding & detaching channels
529XrdSysMutex toMutex; // Mutex for handling the timeout list
530};
531};
532};
533#endif
Definition XrdSysPthread.hh:165
void Lock()
Definition XrdSysPthread.hh:222
void UnLock()
Definition XrdSysPthread.hh:224
Definition XrdSysPthread.hh:242
Definition XrdSysPthread.hh:494
Definition XrdSysIOEvents.hh:79
virtual void Fatal(Channel *chP, void *cbArg, int eNum, const char *eTxt)
Definition XrdSysIOEvents.hh:131
virtual bool Event(Channel *chP, void *cbArg, int evFlags)=0
CallBack()
Constructor.
Definition XrdSysIOEvents.hh:152
virtual ~CallBack()
Destructor.
Definition XrdSysIOEvents.hh:158
virtual void Stop(Channel *chP, void *cbArg)
Definition XrdSysIOEvents.hh:146
EventType
Events that may cause a callback object to be activated.
Definition XrdSysIOEvents.hh:87
@ ReadyToWrite
Writing won't block.
Definition XrdSysIOEvents.hh:90
@ ReadyToRead
New data has arrived.
Definition XrdSysIOEvents.hh:88
@ ReadTimeOut
Read timeout.
Definition XrdSysIOEvents.hh:89
@ WriteTimeOut
Write timeout.
Definition XrdSysIOEvents.hh:91
@ ValidEvents
Mask to test for valid events.
Definition XrdSysIOEvents.hh:92
Definition XrdSysIOEvents.hh:174
void SetCallBack(CallBack *cbP, void *cbArg=0)
time_t wrDL
Definition XrdSysIOEvents.hh:345
short chFault
Definition XrdSysIOEvents.hh:354
dlQ tmoList
Definition XrdSysIOEvents.hh:334
void GetCallBack(CallBack **cbP, void **cbArg)
CallBack * chCB
Definition XrdSysIOEvents.hh:338
int chWTO
Definition XrdSysIOEvents.hh:343
time_t deadLine
Definition XrdSysIOEvents.hh:346
EventCode
Event bits used to feed Enable() and Disable(); can be or'd.
Definition XrdSysIOEvents.hh:192
@ allEvents
All of the above.
Definition XrdSysIOEvents.hh:197
@ errorEvents
Error event non-r/w specific.
Definition XrdSysIOEvents.hh:195
@ stopEvent
Poller stop event.
Definition XrdSysIOEvents.hh:196
@ writeEvents
Write and Write Timeouts.
Definition XrdSysIOEvents.hh:193
@ rwEvents
Both of the above.
Definition XrdSysIOEvents.hh:194
@ readEvents
Read and Read Timeouts.
Definition XrdSysIOEvents.hh:192
int GetEvents()
Definition XrdSysIOEvents.hh:267
bool Enable(int events, int timeout=0, const char **eText=0)
void * chCBA
Definition XrdSysIOEvents.hh:339
Poller * chPoller
Definition XrdSysIOEvents.hh:336
~Channel()
Destuctor is private, use Delete() to delete this object.
Definition XrdSysIOEvents.hh:327
int chFD
Definition XrdSysIOEvents.hh:340
int pollEnt
Definition XrdSysIOEvents.hh:341
Status
Definition XrdSysIOEvents.hh:350
@ isCBMode
Definition XrdSysIOEvents.hh:350
@ isDead
Definition XrdSysIOEvents.hh:350
@ isClear
Definition XrdSysIOEvents.hh:350
char dlType
Definition XrdSysIOEvents.hh:347
char inTOQ
Definition XrdSysIOEvents.hh:351
int chRTO
Definition XrdSysIOEvents.hh:342
void Reset(Poller *thePoller, int fd, int eNum=0)
int GetFD()
Definition XrdSysIOEvents.hh:276
char inPSet
Definition XrdSysIOEvents.hh:352
time_t rdDL
Definition XrdSysIOEvents.hh:344
char reMod
Definition XrdSysIOEvents.hh:353
dlQ attList
Definition XrdSysIOEvents.hh:333
char chStat
Definition XrdSysIOEvents.hh:349
Channel(Poller *pollP, int fd, CallBack *cbP=0, void *cbArg=0)
char chEvents
Definition XrdSysIOEvents.hh:348
bool Disable(int events, const char **eText=0)
XrdSysRecMutex chMutex
Definition XrdSysIOEvents.hh:331
Poller * chPollXQ
Definition XrdSysIOEvents.hh:337
Definition XrdSysIOEvents.hh:372
virtual bool Modify(Channel *cP, int &eNum, const char **eTxt, bool &isLocked)=0
char * pipeBuff
Definition XrdSysIOEvents.hh:507
virtual bool Include(Channel *cP, int &eNum, const char **eTxt, bool &isLocked)=0
int GetFault(Channel *cP)
Definition XrdSysIOEvents.hh:437
Channel * attBase
Definition XrdSysIOEvents.hh:488
void LockChannel(Channel *cP)
Definition XrdSysIOEvents.hh:441
static Poller * Create(int &eNum, const char **eTxt=0, int crOpts=0)
Channel * tmoBase
Definition XrdSysIOEvents.hh:489
int GetPollEnt(Channel *cP)
Definition XrdSysIOEvents.hh:438
int reqFD
Definition XrdSysIOEvents.hh:495
pthread_t pollTid
Definition XrdSysIOEvents.hh:491
Poller(int cFD, int rFD)
CPP_ATOMIC_TYPE(bool) wakePend
void TmoDel(Channel *cP)
struct pollfd pipePoll
Definition XrdSysIOEvents.hh:493
virtual void Begin(XrdSysSemaphore *syncp, int &rc, const char **eTxt)=0
bool CbkXeq(Channel *cP, int events, int eNum, const char *eTxt)
XrdSysMutex toMutex
Definition XrdSysIOEvents.hh:529
virtual ~Poller()
Destructor. Stop() is effecively called when this object is deleted.
Definition XrdSysIOEvents.hh:430
int cmdFD
Definition XrdSysIOEvents.hh:494
static time_t maxTime
Definition XrdSysIOEvents.hh:513
int SendCmd(PipeData &cmd)
unsigned char tmoMask
Definition XrdSysIOEvents.hh:509
void UnLockChannel(Channel *cP)
Definition XrdSysIOEvents.hh:448
virtual void Shutdown()=0
int pipeBlen
Definition XrdSysIOEvents.hh:508
void Attach(Channel *cP)
static Poller * newPoller(int pFD[2], int &eNum, const char **eTxt)
int Poll2Enum(short events)
friend class BootStrap
Definition XrdSysIOEvents.hh:373
virtual void Exclude(Channel *cP, bool &isLocked, bool dover=1)=0
PipeData reqBuff
Definition XrdSysIOEvents.hh:506
XrdSysMutex adMutex
Definition XrdSysIOEvents.hh:528
bool TmoAdd(Channel *cP, int tmoSet)
bool chDead
Definition XrdSysIOEvents.hh:511
CreateOpts
Definition XrdSysIOEvents.hh:398
@ optTOM
Definition XrdSysIOEvents.hh:398
void Detach(Channel *cP, bool &isLocked, bool keep=true)
void SetPollEnt(Channel *cP, int ptEnt)
static pid_t parentPID
Definition XrdSysIOEvents.hh:515
bool Init(Channel *cP, int &eNum, const char **eTxt, bool &isLockd)
Definition XrdClPollerBuiltIn.hh:28
Definition XrdSysIOEvents.hh:329
Channel * next
Definition XrdSysIOEvents.hh:329
Channel * prev
Definition XrdSysIOEvents.hh:329
Definition XrdSysIOEvents.hh:496
XrdSysSemaphore * theSem
Definition XrdSysIOEvents.hh:497
char evt
Definition XrdSysIOEvents.hh:496
char req
Definition XrdSysIOEvents.hh:496
cmd
Definition XrdSysIOEvents.hh:498
@ MiFD
Definition XrdSysIOEvents.hh:499
@ RmFD
Definition XrdSysIOEvents.hh:499
@ Post
Definition XrdSysIOEvents.hh:498
@ Stop
Definition XrdSysIOEvents.hh:499
@ MdFD
Definition XrdSysIOEvents.hh:498
@ NoOp
Definition XrdSysIOEvents.hh:498
int fd
Definition XrdSysIOEvents.hh:496
short ent
Definition XrdSysIOEvents.hh:496
~PipeData()
Definition XrdSysIOEvents.hh:504
PipeData(char reQ=0, char evT=0, short enT=0, int fD=0, XrdSysSemaphore *sP=0)
Definition XrdSysIOEvents.hh:500