xrootd
XrdPoll.hh
Go to the documentation of this file.
00001 #ifndef __XRD_POLL_H__
00002 #define __XRD_POLL_H__
00003 /******************************************************************************/
00004 /*                                                                            */
00005 /*                            X r d P o l l . h h                             */
00006 /*                                                                            */
00007 /* (c) 2004 by the Board of Trustees of the Leland Stanford, Jr., University  */
00008 /*   Produced by Andrew Hanushevsky for Stanford University under contract    */
00009 /*              DE-AC02-76-SFO0515 with the Department of Energy              */
00010 /*                                                                            */
00011 /* This file is part of the XRootD software suite.                            */
00012 /*                                                                            */
00013 /* XRootD is free software: you can redistribute it and/or modify it under    */
00014 /* the terms of the GNU Lesser General Public License as published by the     */
00015 /* Free Software Foundation, either version 3 of the License, or (at your     */
00016 /* option) any later version.                                                 */
00017 /*                                                                            */
00018 /* XRootD is distributed in the hope that it will be useful, but WITHOUT      */
00019 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or      */
00020 /* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public       */
00021 /* License for more details.                                                  */
00022 /*                                                                            */
00023 /* You should have received a copy of the GNU Lesser General Public License   */
00024 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file  */
00025 /* COPYING (GPL license).  If not, see <http://www.gnu.org/licenses/>.        */
00026 /*                                                                            */
00027 /* The copyright holder's institutional names and contributor's names may not */
00028 /* be used to endorse or promote products derived from this software without  */
00029 /* specific prior written permission of the institution or contributor.       */
00030 /******************************************************************************/
00031 
00032 #include <sys/poll.h>
00033 #include "XrdSys/XrdSysPthread.hh"
00034 
00035 #define XRD_NUMPOLLERS 3
00036 
00037 class XrdOucTrace;
00038 class XrdSysError;
00039 class XrdLink;
00040 class XrdScheduler;
00041 class XrdSysSemaphore;
00042   
00043 class XrdPoll
00044 {
00045 public:
00046 
00047 // Attach() is called when a new link needs to be assigned to a poller
00048 //
00049 static  int   Attach(XrdLink *lp);    // Implementation supplied
00050 
00051 // Detach() is called when a link is being discarded
00052 //
00053 static  void  Detach(XrdLink *lp);   //  Implementation supplied
00054 
00055 // Disable() is called when we need to mask interrupts from a link
00056 //
00057 virtual void  Disable(XrdLink *lp, const char *etxt=0) = 0;
00058 
00059 // Enable() is called when we want to receive interrupts from a link
00060 //
00061 virtual int   Enable(XrdLink *lp)  = 0;
00062 
00063 // Finish() is called to allow a link to gracefully terminate when scheduled
00064 //
00065 static  int   Finish(XrdLink *lp, const char *etxt=0); //Implementation supplied
00066 
00067 // Init()   is called to set pointers to external interfaces at config time.
00068 //
00069 static  void  Init(XrdSysError *eP, XrdOucTrace *tP, XrdScheduler *sP)
00070                   {XrdLog = eP; XrdTrace = tP; XrdSched = sP;}
00071 
00072 // Poll2Text() converts bits in an revents item to text
00073 //
00074 static  char *Poll2Text(short events); // Implementation supplied
00075 
00076 // Setup() is called at config time to perform poller configuration
00077 //
00078 static  int   Setup(int numfd);        // Implementation supplied
00079 
00080 // Start() is called via a thread for each poller that was created
00081 //
00082 virtual void  Start(XrdSysSemaphore *syncp, int &rc) = 0;
00083 
00084 // Stats() is called to provide statistics on polling
00085 //
00086 static  int   Stats(char *buff, int blen, int do_sync=0);
00087 
00088 // Identification of the thread handling this object
00089 //
00090            int         PID;       // Poller ID
00091            pthread_t   TID;       // Thread ID
00092 
00093 // The following table reference the pollers in effect
00094 //
00095 static     XrdPoll   *Pollers[XRD_NUMPOLLERS];
00096 
00097            XrdPoll();
00098 virtual   ~XrdPoll() {}
00099 
00100 protected:
00101 
00102 static     const char   *TraceID;                  // For tracing
00103 static     XrdOucTrace  *XrdTrace;
00104 static     XrdSysError  *XrdLog;
00105 static     XrdScheduler *XrdSched;
00106 
00107 // Gets the next request on the poll pipe. This is common to all implentations.
00108 //
00109            int         getRequest();             // Implementation supplied
00110 
00111 // Exclude() called to exclude a link from a poll set
00112 //
00113 virtual    void        Exclude(XrdLink *lp) = 0;
00114 
00115 // Include() called to include a link in a poll set
00116 //
00117 virtual    int         Include(XrdLink *lp) = 0;
00118 
00119 // newPoller() called to get a new poll object at initialization time
00120 //             Even though static, an implementation must be supplied.
00121 //
00122 static     XrdPoll   *newPoller(int pollid, int numfd)    /* = 0 */;
00123 
00124 // The following is common to all implementations
00125 //
00126 XrdSysMutex   PollPipe;
00127 struct pollfd PipePoll;
00128 int           CmdFD;      // FD to send PipeData commands
00129 int           ReqFD;      // FD to recv PipeData requests
00130 struct        PipeData {union {XrdSysSemaphore  *theSem;
00131                                struct {int fd;
00132                                        int ent;} Arg;
00133                               } Parms;
00134                         enum cmd {EnFD, DiFD, RmFD, Post};
00135                         cmd req;
00136                        };
00137               PipeData ReqBuff;
00138 char         *PipeBuff;
00139 int           PipeBlen;
00140 
00141 // The following are statistical counters each implementation must maintain
00142 //
00143            int         numEnabled;     // Count of Enable() calls
00144            int         numEvents;      // Count of poll fd's dispatched
00145            int         numInterrupts;  // Number of interrupts (e.g., signals)
00146 
00147 private:
00148 
00149 static     XrdSysMutex  doingAttach;
00150            int          numAttached;    // Number of fd's attached to poller
00151 };
00152 #endif