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 /* All Rights Reserved. See XrdInfo.cc for complete License Terms */ 00009 /*Produced by Andrew Hanushevsky for Stanford University under contract */ 00010 /* DE-AC03-76-SFO0515 with the Deprtment of Energy */ 00011 /******************************************************************************/ 00012 00013 // $Id: XrdObject.hh 22437 2008-03-04 14:35:16Z rdm $ 00014 00015 #include <string.h> 00016 #include <strings.h> 00017 #include <time.h> 00018 #include <sys/types.h> 00019 00020 #include "Xrd/XrdJob.hh" 00021 00022 // The classes here are templates for singly linked list handling that allows 00023 // elements to be added to either end but be removed only from the front. Most 00024 // objects in this package are managed in queues of this type. 00025 00026 /******************************************************************************/ 00027 /* x r d _ O b j e c t */ 00028 /******************************************************************************/ 00029 00030 template <class T> 00031 class XrdObjectQ; 00032 00033 template <class T> 00034 class XrdObject 00035 { 00036 public: 00037 friend class XrdObjectQ<T>; 00038 00039 00040 // Item() supplies the item value associated with itself (used with Next()). 00041 // 00042 T *objectItem() {return Item;} 00043 00044 // Next() supplies the next list node. 00045 // 00046 XrdObject<T> *nextObject() {return Next;} 00047 00048 // Set the item pointer 00049 // 00050 void setItem(T *ival) {Item = ival;} 00051 00052 XrdObject(T *ival=0) {Next = 0; Item = ival; QTime = 0;} 00053 ~XrdObject() {} 00054 00055 private: 00056 XrdObject<T> *Next; 00057 T *Item; 00058 time_t QTime; // Only used for time-managed objects 00059 }; 00060 00061 /******************************************************************************/ 00062 /* x r d _ O b j e c t Q */ 00063 /******************************************************************************/ 00064 00065 // Note to properly cleanup this type of queue you must call Set() at least 00066 // once to cause the time element to be sceduled. 00067 00068 class XrdOucTrace; 00069 class XrdScheduler; 00070 00071 template <class T> 00072 class XrdObjectQ : public XrdJob 00073 { 00074 public: 00075 00076 inline T *Pop() {XrdObject<T> *Node; 00077 QMutex.Lock(); 00078 if ((Node = First)) {First = First->Next; Count--;} 00079 QMutex.UnLock(); 00080 if (Node) return Node->Item; 00081 return (T *)0; 00082 } 00083 00084 inline void Push(XrdObject<T> *Node) 00085 {Node->QTime = Curage; 00086 QMutex.Lock(); 00087 if (Count >= MaxinQ) delete Node->Item; 00088 else {Node->Next = First; 00089 First = Node; 00090 Count++; 00091 } 00092 QMutex.UnLock(); 00093 } 00094 00095 void Set(int inQMax, time_t agemax=1800); 00096 00097 void Set(XrdScheduler *sp, XrdOucTrace *tp, int TraceChk=0) 00098 {Sched = sp; Trace = tp; TraceON = TraceChk;} 00099 00100 void DoIt(); 00101 00102 XrdObjectQ(const char *id, const char *desc) : XrdJob(desc) 00103 {Curage = Count = 0; Maxage = 0; TraceID = id; 00104 MaxinQ = 32; MininQ = 16; First = 0; 00105 } 00106 00107 ~XrdObjectQ() {} 00108 00109 private: 00110 00111 XrdSysMutex QMutex; 00112 XrdObject<T> *First; 00113 int Count; 00114 int Curage; 00115 int MininQ; 00116 int MaxinQ; 00117 time_t Maxage; 00118 XrdScheduler *Sched; 00119 XrdOucTrace *Trace; 00120 int TraceON; 00121 const char *TraceID; 00122 }; 00123 00124 #include "Xrd/XrdObject.icc" 00125 #endif