xrootd
XrdObject.hh
Go to the documentation of this file.
1 #ifndef __XRD_OBJECT_H__
2 #define __XRD_OBJECT_H__
3 /******************************************************************************/
4 /* */
5 /* X r d O b j e c t . h h */
6 /* */
7 /*(c) 2004 by the Board of Trustees of the Leland Stanford, Jr., University */
8 /*Produced by Andrew Hanushevsky for Stanford University under contract */
9 /* DE-AC02-76-SFO0515 with the Deprtment of Energy */
10 /* */
11 /* This file is part of the XRootD software suite. */
12 /* */
13 /* XRootD is free software: you can redistribute it and/or modify it under */
14 /* the terms of the GNU Lesser General Public License as published by the */
15 /* Free Software Foundation, either version 3 of the License, or (at your */
16 /* option) any later version. */
17 /* */
18 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
19 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
20 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
21 /* License for more details. */
22 /* */
23 /* You should have received a copy of the GNU Lesser General Public License */
24 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
25 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
26 /* */
27 /* The copyright holder's institutional names and contributor's names may not */
28 /* be used to endorse or promote products derived from this software without */
29 /* specific prior written permission of the institution or contributor. */
30 /******************************************************************************/
31 
32 #include <string.h>
33 #include <strings.h>
34 #include <time.h>
35 #include <sys/types.h>
36 
37 #include "Xrd/XrdJob.hh"
38 
39 // The classes here are templates for singly linked list handling that allows
40 // elements to be added to either end but be removed only from the front. Most
41 // objects in this package are managed in queues of this type.
42 
43 /******************************************************************************/
44 /* x r d _ O b j e c t */
45 /******************************************************************************/
46 
47 template <class T>
48 class XrdObjectQ;
49 
50 template <class T>
51 class XrdObject
52 {
53 public:
54 friend class XrdObjectQ<T>;
55 
56 
57 // Item() supplies the item value associated with itself (used with Next()).
58 //
59 T *objectItem() {return Item;}
60 
61 // Next() supplies the next list node.
62 //
64 
65 // Set the item pointer
66 //
67 void setItem(T *ival) {Item = ival;}
68 
69  XrdObject(T *ival=0) {Next = 0; Item = ival; QTime = 0;}
71 
72 private:
74 T *Item;
75 time_t QTime; // Only used for time-managed objects
76 };
77 
78 /******************************************************************************/
79 /* x r d _ O b j e c t Q */
80 /******************************************************************************/
81 
82 // Note to properly cleanup this type of queue you must call Set() at least
83 // once to cause the time element to be sceduled.
84 
85 class XrdOucTrace;
86 class XrdScheduler;
87 
88 template <class T>
89 class XrdObjectQ : public XrdJob
90 {
91 public:
92 
93 inline T *Pop() {XrdObject<T> *Node;
94  QMutex.Lock();
95  if ((Node = First)) {First = First->Next; Count--;}
96  QMutex.UnLock();
97  if (Node) return Node->Item;
98  return (T *)0;
99  }
100 
101 inline void Push(XrdObject<T> *Node)
102  {Node->QTime = Curage;
103  QMutex.Lock();
104  if (Count >= MaxinQ) delete Node->Item;
105  else {Node->Next = First;
106  First = Node;
107  Count++;
108  }
109  QMutex.UnLock();
110  }
111 
112  void Set(int inQMax, time_t agemax=1800);
113 
114  void Set(XrdScheduler *sp, XrdOucTrace *tp, int TraceChk=0)
115  {Sched = sp; Trace = tp; TraceON = TraceChk;}
116 
117  void DoIt();
118 
119  XrdObjectQ(const char *id, const char *desc) : XrdJob(desc)
120  {Curage = Count = 0; Maxage = 0; TraceID = id;
121  MaxinQ = 32; MininQ = 16; First = 0;
122  }
123 
125 
126 private:
127 
130 int Count;
131 int Curage;
132 int MininQ;
133 int MaxinQ;
134 time_t Maxage;
138 const char *TraceID;
139 };
140 
141 #include "Xrd/XrdObject.icc"
142 #endif
XrdObject< T > * Next
Definition: XrdObject.hh:73
int MaxinQ
Definition: XrdObject.hh:133
Definition: XrdObject.hh:51
~XrdObject()
Definition: XrdObject.hh:70
time_t Maxage
Definition: XrdObject.hh:134
XrdObjectQ(const char *id, const char *desc)
Definition: XrdObject.hh:119
time_t QTime
Definition: XrdObject.hh:75
void Set(XrdScheduler *sp, XrdOucTrace *tp, int TraceChk=0)
Definition: XrdObject.hh:114
~XrdObjectQ()
Definition: XrdObject.hh:124
void DoIt()
int Count
Definition: XrdObject.hh:130
Definition: XrdOucTrace.hh:35
void Push(XrdObject< T > *Node)
Definition: XrdObject.hh:101
T * objectItem()
Definition: XrdObject.hh:59
Definition: XrdScheduler.hh:44
Definition: XrdSysPthread.hh:165
XrdOucTrace * Trace
Definition: XrdObject.hh:136
T * Item
Definition: XrdObject.hh:74
int Curage
Definition: XrdObject.hh:131
int TraceON
Definition: XrdObject.hh:137
void setItem(T *ival)
Definition: XrdObject.hh:67
XrdObject(T *ival=0)
Definition: XrdObject.hh:69
XrdObject< T > * nextObject()
Definition: XrdObject.hh:63
void Lock()
Definition: XrdSysPthread.hh:222
XrdScheduler * Sched
Definition: XrdObject.hh:135
void Set(int inQMax, time_t agemax=1800)
const char * TraceID
Definition: XrdObject.hh:138
XrdObject< T > * First
Definition: XrdObject.hh:129
void UnLock()
Definition: XrdSysPthread.hh:224
Definition: XrdObject.hh:48
int MininQ
Definition: XrdObject.hh:132
XrdSysMutex QMutex
Definition: XrdObject.hh:128
Definition: XrdJob.hh:42
T * Pop()
Definition: XrdObject.hh:93