xrootd
XrdObject.hh
Go to the documentation of this file.
00001 #ifndef __XRD_OBJECT_H__
00002 #define __XRD_OBJECT_H__
00003 /******************************************************************************/
00004 /*                                                                            */
00005 /*                          X r d O b j e c t . 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 Deprtment 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 <string.h>
00033 #include <strings.h>
00034 #include <time.h>
00035 #include <sys/types.h>
00036 
00037 #include "Xrd/XrdJob.hh"
00038 
00039 // The classes here are templates for singly linked list handling that allows
00040 // elements to be added to either end but be removed only from the front. Most
00041 // objects in this package are managed in queues of this type.
00042   
00043 /******************************************************************************/
00044 /*                            x r d _ O b j e c t                             */
00045 /******************************************************************************/
00046 
00047 template <class T>
00048 class XrdObjectQ;
00049   
00050 template <class T>
00051 class XrdObject
00052 {
00053 public:
00054 friend class XrdObjectQ<T>;
00055 
00056 
00057 // Item() supplies the item value associated with itself (used with Next()).
00058 //
00059 T             *objectItem() {return Item;}
00060 
00061 // Next() supplies the next list node.
00062 //
00063 XrdObject<T> *nextObject() {return Next;}
00064 
00065 // Set the item pointer
00066 //
00067 void           setItem(T *ival) {Item = ival;}
00068 
00069                XrdObject(T *ival=0) {Next = 0; Item = ival; QTime = 0;}
00070               ~XrdObject() {}
00071 
00072 private:
00073 XrdObject<T> *Next;
00074 T             *Item;
00075 time_t         QTime;  // Only used for time-managed objects
00076 };
00077 
00078 /******************************************************************************/
00079 /*                           x r d _ O b j e c t Q                            */
00080 /******************************************************************************/
00081   
00082 // Note to properly cleanup this type of queue you must call Set() at least
00083 // once to cause the time element to be sceduled.
00084 
00085 class XrdOucTrace;
00086 class XrdScheduler;
00087   
00088 template <class T>
00089 class XrdObjectQ : public XrdJob
00090 {
00091 public:
00092 
00093 inline T      *Pop() {XrdObject<T> *Node;
00094                       QMutex.Lock();
00095                       if ((Node = First)) {First = First->Next; Count--;}
00096                       QMutex.UnLock();
00097                       if (Node) return Node->Item;
00098                       return (T *)0;
00099                      }
00100 
00101 inline void    Push(XrdObject<T> *Node)
00102                      {Node->QTime = Curage;
00103                       QMutex.Lock();
00104                       if (Count >= MaxinQ) delete Node->Item;
00105                          else {Node->Next = First;
00106                                First = Node;
00107                                Count++;
00108                               }
00109                       QMutex.UnLock();
00110                      }
00111 
00112        void    Set(int inQMax, time_t agemax=1800);
00113 
00114        void    Set(XrdScheduler *sp, XrdOucTrace *tp, int TraceChk=0)
00115                       {Sched = sp; Trace = tp; TraceON = TraceChk;}
00116 
00117        void    DoIt();
00118 
00119        XrdObjectQ(const char *id, const char *desc) : XrdJob(desc)
00120                   {Curage = Count = 0; Maxage = 0; TraceID = id;
00121                    MaxinQ = 32; MininQ = 16; First = 0;
00122                   }
00123 
00124       ~XrdObjectQ() {}
00125 
00126 private:
00127 
00128 XrdSysMutex    QMutex;
00129 XrdObject<T>  *First;
00130 int            Count;
00131 int            Curage;
00132 int            MininQ;
00133 int            MaxinQ;
00134 time_t         Maxage;
00135 XrdScheduler  *Sched;
00136 XrdOucTrace   *Trace;
00137 int            TraceON;
00138 const char    *TraceID;
00139 };
00140 
00141 #include "Xrd/XrdObject.icc"
00142 #endif