xrootd
XrdSsiScale.hh
Go to the documentation of this file.
1 #ifndef __XRDSSISCALE_HH__
2 #define __XRDSSISCALE_HH__
3 /******************************************************************************/
4 /* */
5 /* X r d S s i S c a l e . h h */
6 /* */
7 /* (c) 2013 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 Department 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 
34 #include "XrdSys/XrdSysPthread.hh"
35 
37 {
38 public:
39 
40 static const int maxSprd =256;
41 static const int maxEnt = 32; // Must be power of two
42 static const int entShft = 8; // Allows a spread of 256
43 static const unsigned int maxPend = 65500;
44 
45 int getEnt() {entMutex.Lock();
46  if (pendCnt[nowEnt] < maxPend)
47  {pendCnt[nowEnt]++;
48  if (maxSpread) return Spread(nowEnt);
49  entMutex.UnLock();
50  return nowEnt << entShft;
51  }
52  int xEnt = (nowEnt < maxEnt ? nowEnt+1 : 0);
53  int zEnt = maxEnt;
54  do {for (int i = xEnt; i < zEnt; i++)
55  {if (pendCnt[i] < maxPend)
56  {pendCnt[i]++;
57  nowEnt = i;
58  if (maxSpread) return Spread(i);
59  entMutex.UnLock();
60  return i;
61 
62  }
63  }
64  if (!xEnt) break;
65  xEnt = 0; zEnt = nowEnt;
66  } while(true);
67  entMutex.UnLock();
68  return -1;
69  }
70 
71 void retEnt(int xEnt) {xEnt >>= entShft;
72  if (xEnt >= 0 && xEnt < maxEnt)
73  {entMutex.Lock();
74  if (pendCnt[xEnt]) pendCnt[xEnt]--;
75  entMutex.UnLock();
76  }
77  }
78 
79 bool rsvEnt(int xEnt) {xEnt >>= entShft;
80  if (xEnt < 0 && xEnt >= maxEnt) return false;
81  entMutex.Lock();
82  if (pendCnt[nowEnt] < maxPend)
83  {pendCnt[nowEnt]++;
84  entMutex.UnLock();
85  return true;
86  }
87  entMutex.UnLock();
88  return false;
89  }
90 
91 void setSpread(short sval) {if (sval <= 0) maxSpread = 0;
92  else if (sval < maxSprd) maxSpread = sval;
93  else maxSpread = maxSprd;
94  }
95 
97  {memset(pendCnt, 0, sizeof(uint16_t)*maxEnt);}
98 
100 
101 private:
102 
103 int Spread(int ent) // Called with entMutex locked and return unlocked.
104  {int n = nowSpread;
105  nowSpread++;
106  if (nowSpread >= maxSpread) nowSpread = 0;
107  entMutex.UnLock();
108  return (ent << entShft) | n;
109  }
110 
112 uint16_t pendCnt[maxEnt];
113 int nowEnt;
114 short maxSpread;
115 short nowSpread;
116 };
117 #endif
uint16_t pendCnt[maxEnt]
Definition: XrdSsiScale.hh:112
int nowEnt
Definition: XrdSsiScale.hh:113
~XrdSsiScale()
Definition: XrdSsiScale.hh:99
short nowSpread
Definition: XrdSsiScale.hh:115
int getEnt()
Definition: XrdSsiScale.hh:45
Definition: XrdSysPthread.hh:165
void retEnt(int xEnt)
Definition: XrdSsiScale.hh:71
int Spread(int ent)
Definition: XrdSsiScale.hh:103
void setSpread(short sval)
Definition: XrdSsiScale.hh:91
short maxSpread
Definition: XrdSsiScale.hh:114
static const unsigned int maxPend
Definition: XrdSsiScale.hh:43
XrdSysMutex entMutex
Definition: XrdSsiScale.hh:111
static const int entShft
Definition: XrdSsiScale.hh:42
Definition: XrdSsiScale.hh:36
void Lock()
Definition: XrdSysPthread.hh:220
XrdSsiScale()
Definition: XrdSsiScale.hh:96
bool rsvEnt(int xEnt)
Definition: XrdSsiScale.hh:79
static const int maxEnt
Definition: XrdSsiScale.hh:41
void UnLock()
Definition: XrdSysPthread.hh:222
static const int maxSprd
Definition: XrdSsiScale.hh:40