Logo ROOT   6.18/05
Reference Guide
XpdObject.cxx
Go to the documentation of this file.
1// @(#)root/proofd:$Id$
2// Author: Gerardo Ganis Feb 2013
3
4/*************************************************************************
5 * Copyright (C) 1995-2005, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12//////////////////////////////////////////////////////////////////////////
13// //
14// XpdObject //
15// //
16// Authors: G. Ganis, CERN, 2013 //
17// //
18// Auxilliary class to stack protocols. //
19// //
20//////////////////////////////////////////////////////////////////////////
21
22#include "XpdObject.h"
23#include "XrdProofdAux.h"
24#include "XrdProofdProtocol.h"
25#include "Xrd/XrdScheduler.hh"
26#include "XrdOuc/XrdOucTrace.hh"
27
28////////////////////////////////////////////////////////////////////////////////
29/// Pop up a protocol object
30
32{
33 XpdObject *node;
34 fQMutex.Lock();
35 if ((node = fFirst)) {
37 fCount--;
38 }
39 fQMutex.UnLock();
40 if (node) return node->fItem;
41 return (XrdProofdProtocol *)0;
42}
43
44////////////////////////////////////////////////////////////////////////////////
45/// Push back a protocol
46
48{
49 node->fQTime = fCurage;
50 fQMutex.Lock();
51 if (fCount >= fMaxinQ) {
52 delete node->fItem;
53 } else {
54 node->fNext = fFirst;
55 fFirst = node;
56 fCount++;
57 }
58 fQMutex.UnLock();
59}
60
61////////////////////////////////////////////////////////////////////////////////
62/// Lock the data area and set the values
63
64void XpdObjectQ::Set(int inQMax, time_t agemax)
65{
66 fQMutex.Lock();
67 fMaxinQ = inQMax; fMaxage = agemax;
68 if (!(fMininQ = inQMax/2)) fMininQ = 1;
69 fQMutex.UnLock();
70
71 // Schedule ourselves using the new values
72 if (agemax > 0)
73 fSched->Schedule((XrdJob *)this, agemax + time(0));
74}
75
76////////////////////////////////////////////////////////////////////////////////
77/// Process method
78
80{
81 XpdObject *pp, *p;
82 int oldcnt, agemax;
83
84 // Lock the anchor and see if we met the threshold for deletion
85 //
86 fQMutex.Lock();
87 agemax = fMaxage;
88 if ((oldcnt = fCount) > fMininQ) {
89
90 // Prepare to scan down the queue.
91 if ((pp = fFirst)) {
92 p = pp->fNext;
93 } else { p = 0; }
94
95 // Find the first object that's been idle for too long
96 while(p && (p->fQTime >= fCurage)) { pp = p; p = p->fNext;}
97
98 // Now delete half of the idle objects. The object queue element must be
99 // part of the actual object being deleted for this to properly work.
100 if (pp) {
101 while (p) {
102 pp->fNext = p->fNext;
103 delete p->fItem;
104 fCount--;
105 p = ((pp = pp->fNext) ? pp->fNext : 0);
106 }
107 }
108 }
109
110 // Increase the age and unlock the queue
111 fCurage++;
112 fQMutex.UnLock();
113
114 // Trace as needed
115 if (fTraceON && fTrace->Tracing(fTraceON))
116 {fTrace->Beg(fTraceID);
117 cerr <<Comment <<" trim done; " <<fCount <<" of " <<oldcnt <<" kept";
118 fTrace->End();
119 }
120
121 // Reschedule ourselves if we must do so
122 if (agemax > 0)
123 fSched->Schedule((XrdJob *)this, agemax+time(0));
124}
125
int fMininQ
Definition: XpdObject.h:91
int fTraceON
Definition: XpdObject.h:96
XrdOucTrace * fTrace
Definition: XpdObject.h:94
XrdSysMutex fQMutex
Definition: XpdObject.h:87
const char * fTraceID
Definition: XpdObject.h:97
void Set(int inQMax, time_t agemax=1800)
Lock the data area and set the values.
Definition: XpdObject.cxx:64
int fCount
Definition: XpdObject.h:89
XrdScheduler * fSched
Definition: XpdObject.h:95
time_t fMaxage
Definition: XpdObject.h:93
void Push(XpdObject *Node)
Push back a protocol.
Definition: XpdObject.cxx:47
int fMaxinQ
Definition: XpdObject.h:92
XpdObject * fFirst
Definition: XpdObject.h:88
int fCurage
Definition: XpdObject.h:90
XrdProofdProtocol * Pop()
Pop up a protocol object.
Definition: XpdObject.cxx:31
void DoIt()
Process method.
Definition: XpdObject.cxx:79
XrdProofdProtocol * fItem
Definition: XpdObject.h:56
XpdObject * fNext
Definition: XpdObject.h:55
time_t fQTime
Definition: XpdObject.h:57