00001 #ifndef __XRDSYSIOEVENTS_HH__ 00002 #define __XRDSYSIOEVENTS_HH__ 00003 /******************************************************************************/ 00004 /* */ 00005 /* X r d S y s I O E v e n t s . h h */ 00006 /* */ 00007 /* (c) 2012 by the Board of Trustees of the Leland Stanford, Jr., University */ 00008 /* All Rights Reserved */ 00009 /* Produced by Andrew Hanushevsky for Stanford University under contract */ 00010 /* DE-AC02-76-SFO0515 with the Department of Energy */ 00011 /* */ 00012 /* This file is part of the XRootD software suite. */ 00013 /* */ 00014 /* XRootD is free software: you can redistribute it and/or modify it under */ 00015 /* the terms of the GNU Lesser General Public License as published by the */ 00016 /* Free Software Foundation, either version 3 of the License, or (at your */ 00017 /* option) any later version. */ 00018 /* */ 00019 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */ 00020 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */ 00021 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */ 00022 /* License for more details. */ 00023 /* */ 00024 /* You should have received a copy of the GNU Lesser General Public License */ 00025 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */ 00026 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */ 00027 /* */ 00028 /* The copyright holder's institutional names and contributor's names may not */ 00029 /* be used to endorse or promote products derived from this software without */ 00030 /* specific prior written permission of the institution or contributor. */ 00031 /******************************************************************************/ 00032 00033 #include <poll.h> 00034 #include <time.h> 00035 #include <sys/types.h> 00036 00037 #include "XrdSys/XrdSysPthread.hh" 00038 00039 //----------------------------------------------------------------------------- 00053 //----------------------------------------------------------------------------- 00054 00055 namespace XrdSys 00056 { 00057 namespace IOEvents 00058 { 00059 00060 /******************************************************************************/ 00061 /* C l a s s C a l l B a c k */ 00062 /******************************************************************************/ 00063 00064 //----------------------------------------------------------------------------- 00074 //----------------------------------------------------------------------------- 00075 00076 class Channel; 00077 class CallBack 00078 { 00079 public: 00080 00081 //----------------------------------------------------------------------------- 00083 //----------------------------------------------------------------------------- 00084 00085 enum EventType 00086 { 00087 ReadyToRead = 0x01, 00088 ReadTimeOut = 0x02, 00089 ReadyToWrite = 0x04, 00090 WriteTimeOut = 0x08, 00091 ValidEvents = 0x0f 00092 }; 00093 00094 //----------------------------------------------------------------------------- 00113 //----------------------------------------------------------------------------- 00114 00115 virtual bool Event(Channel *chP, void *cbArg, int evFlags) = 0; 00116 00117 //----------------------------------------------------------------------------- 00128 //----------------------------------------------------------------------------- 00129 00130 virtual void Fatal(Channel *chP, void *cbArg, int eNum, const char *eTxt) 00131 { 00132 (void)chP; (void)cbArg; (void)eNum; (void)eTxt; 00133 }; 00134 00135 //----------------------------------------------------------------------------- 00143 //----------------------------------------------------------------------------- 00144 00145 virtual void Stop(Channel *chP, void *cbArg) { (void)chP; (void)cbArg;} 00146 00147 //----------------------------------------------------------------------------- 00149 //----------------------------------------------------------------------------- 00150 00151 CallBack() {} 00152 00153 //----------------------------------------------------------------------------- 00155 //----------------------------------------------------------------------------- 00156 00157 virtual ~CallBack() {} 00158 }; 00159 00160 /******************************************************************************/ 00161 /* C l a s s C h a n n e l */ 00162 /******************************************************************************/ 00163 00164 //----------------------------------------------------------------------------- 00168 //----------------------------------------------------------------------------- 00169 00170 class ChannelWait; 00171 class Poller; 00172 class Channel 00173 { 00174 friend class Poller; 00175 public: 00176 00177 //----------------------------------------------------------------------------- 00183 //----------------------------------------------------------------------------- 00184 00185 void Delete(); 00186 00187 //----------------------------------------------------------------------------- 00189 //----------------------------------------------------------------------------- 00190 00191 enum EventCode {readEvents = 0x01, 00192 writeEvents = 0x04, 00193 rwEvents = 0x05, 00194 errorEvents = 0x10, 00195 stopEvent = 0x20, 00196 allEvents = 0x35 00197 }; 00198 00199 //----------------------------------------------------------------------------- 00209 //----------------------------------------------------------------------------- 00210 00211 bool Disable(int events, const char **eText=0); 00212 00213 //----------------------------------------------------------------------------- 00245 //----------------------------------------------------------------------------- 00246 00247 bool Enable(int events, int timeout=0, const char **eText=0); 00248 00249 //----------------------------------------------------------------------------- 00254 //----------------------------------------------------------------------------- 00255 00256 void GetCallBack(CallBack **cbP, void **cbArg); 00257 00258 //----------------------------------------------------------------------------- 00264 //----------------------------------------------------------------------------- 00265 00266 inline int GetEvents() {return (chPoller ? static_cast<int>(chEvents) : -1);} 00267 00268 //----------------------------------------------------------------------------- 00273 //----------------------------------------------------------------------------- 00274 00275 inline int GetFD() {return chFD;} 00276 00277 //----------------------------------------------------------------------------- 00284 //----------------------------------------------------------------------------- 00285 00286 void SetCallBack(CallBack *cbP, void *cbArg=0); 00287 00288 //----------------------------------------------------------------------------- 00297 //----------------------------------------------------------------------------- 00298 00299 void SetFD(int fd); 00300 00301 //----------------------------------------------------------------------------- 00316 //----------------------------------------------------------------------------- 00317 00318 Channel(Poller *pollP, int fd, CallBack *cbP=0, void *cbArg=0); 00319 00320 private: 00321 00322 //----------------------------------------------------------------------------- 00324 //----------------------------------------------------------------------------- 00325 00326 ~Channel() {} 00327 00328 struct dlQ {Channel *next; Channel *prev;}; 00329 00330 XrdSysRecMutex chMutex; 00331 00332 dlQ attList; // List of attached channels 00333 dlQ tmoList; // List of channels in the timeout queue 00334 00335 Poller *chPoller; // The effective poller 00336 Poller *chPollXQ; // The real poller 00337 CallBack *chCB; // CallBack function 00338 void *chCBA; // CallBack argument 00339 int chFD; // Associated file descriptor 00340 int pollEnt; // Used only for poll() type pollers 00341 int chRTO; // Read timeout value (0 means none) 00342 int chWTO; // Write timeout value (0 means none) 00343 time_t rdDL; // Read deadline 00344 time_t wrDL; // Write deadline 00345 time_t deadLine; // The deadline in effect (read or write) 00346 char dlType; // The deadline type in deadLine as CallBack type 00347 char chEvents; // Enabled events as Channel type 00348 char chStat; // Channel status below (!0 -> in callback mode) 00349 enum Status {isClear = 0, isCBMode, isDead}; 00350 char inTOQ; // True if the channel is in the timeout queue 00351 char inPSet; // FD is in the actual poll set 00352 char reMod; // Modify issued while defered, re-issue needed 00353 short chFault; // Defered error, 0 if all is well 00354 00355 void Reset(Poller *thePoller, int fd, int eNum=0); 00356 }; 00357 00358 /******************************************************************************/ 00359 /* C l a s s P o l l e r */ 00360 /******************************************************************************/ 00361 00362 //----------------------------------------------------------------------------- 00368 //----------------------------------------------------------------------------- 00369 00370 class Poller 00371 { 00372 friend class BootStrap; 00373 friend class Channel; 00374 public: 00375 00376 //----------------------------------------------------------------------------- 00395 //----------------------------------------------------------------------------- 00396 00397 enum CreateOpts {optTOM}; 00398 00399 static Poller *Create(int &eNum, const char **eTxt=0, int crOpts=0); 00400 00401 //----------------------------------------------------------------------------- 00412 //----------------------------------------------------------------------------- 00413 00414 void Stop(); 00415 00416 //----------------------------------------------------------------------------- 00421 //----------------------------------------------------------------------------- 00422 00423 Poller(int cFD, int rFD); 00424 00425 //----------------------------------------------------------------------------- 00427 //----------------------------------------------------------------------------- 00428 00429 virtual ~Poller() {} 00430 00431 protected: 00432 struct PipeData; 00433 00434 void CbkTMO(); 00435 bool CbkXeq(Channel *cP, int events, int eNum, const char *eTxt); 00436 inline int GetFault(Channel *cP) {return cP->chFault;} 00437 inline int GetPollEnt(Channel *cP) {return cP->pollEnt;} 00438 int GetRequest(); 00439 bool Init(Channel *cP, int &eNum, const char **eTxt, bool &isLockd); 00440 inline void LockChannel(Channel *cP) {cP->chMutex.Lock();} 00441 int Poll2Enum(short events); 00442 int SendCmd(PipeData &cmd); 00443 void SetPollEnt(Channel *cP, int ptEnt); 00444 bool TmoAdd(Channel *cP, int tmoSet); 00445 void TmoDel(Channel *cP); 00446 int TmoGet(); 00447 inline void UnLockChannel(Channel *cP) {cP->chMutex.UnLock();} 00448 00452 virtual void Begin(XrdSysSemaphore *syncp, int &rc, const char **eTxt) = 0; 00453 00458 virtual void Exclude(Channel *cP, bool &isLocked, bool dover=1) = 0; 00459 00464 virtual bool Include(Channel *cP, 00465 int &eNum, 00466 const char **eTxt, 00467 bool &isLocked) = 0; 00468 00473 virtual bool Modify (Channel *cP, 00474 int &eNum, 00475 const char **eTxt, 00476 bool &isLocked) = 0; 00477 00482 // 00483 virtual void Shutdown() = 0; 00484 00485 // The following is common to all implementations 00486 // 00487 Channel *attBase; // -> First channel in attach queue or 0 00488 Channel *tmoBase; // -> First channel in timeout queue or 0 00489 00490 pthread_t pollTid; // Poller's thread ID 00491 00492 struct pollfd pipePoll; // Stucture to wait for pipe events 00493 int cmdFD; // FD to send PipeData commands 00494 int reqFD; // FD to recv PipeData requests 00495 struct PipeData {char req; char evt; short ent; int fd; 00496 XrdSysSemaphore *theSem; 00497 enum cmd {NoOp = 0, MdFD = 1, Post = 2, 00498 MiFD = 3, RmFD = 4, Stop = 5}; 00499 PipeData(char reQ=0, char evT=0, short enT=0, 00500 int fD =0, XrdSysSemaphore *sP=0) 00501 : req(reQ), evt(evT), ent(enT), fd(fD), 00502 theSem(sP) {} 00503 ~PipeData() {} 00504 }; 00505 PipeData reqBuff; // Buffer used by poller thread to recv data 00506 char *pipeBuff; // Read resumption point in buffer 00507 int pipeBlen; // Number of outstanding bytes 00508 char tmoMask; // Timeout mask 00509 bool wakePend; // Wakeup is effectively pending (don't send) 00510 bool chDead; // True if channel deleted by callback 00511 00512 static time_t maxTime; // Maximum time allowed 00513 00514 private: 00515 00516 void Attach(Channel *cP); 00517 void Detach(Channel *cP, bool &isLocked, bool keep=true); 00518 void WakeUp(); 00519 00520 // newPoller() called to get a specialized new poll object at in response to 00521 // Create(). A specialized implementation must be supplied. 00522 // 00523 static Poller *newPoller(int pFD[2], int &eNum, const char **eTxt); 00524 00525 XrdSysMutex adMutex; // Mutex for adding & detaching channels 00526 XrdSysMutex toMutex; // Mutex for handling the timeout list 00527 }; 00528 }; 00529 }; 00530 #endif