00001 #ifndef __XRDSSISCALE_HH__ 00002 #define __XRDSSISCALE_HH__ 00003 /******************************************************************************/ 00004 /* */ 00005 /* X r d S s i S c a l e . h h */ 00006 /* */ 00007 /* (c) 2013 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 Department 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 00034 #include "XrdSys/XrdSysPthread.hh" 00035 00036 class XrdSsiScale 00037 { 00038 public: 00039 00040 static const int maxSprd =256; 00041 static const int maxEnt = 32; // Must be power of two 00042 static const int entShft = 8; // Allows a spread of 256 00043 static const unsigned int maxPend = 65500; 00044 00045 int getEnt() {entMutex.Lock(); 00046 if (pendCnt[nowEnt] < maxPend) 00047 {pendCnt[nowEnt]++; 00048 if (maxSpread) return Spread(nowEnt); 00049 entMutex.UnLock(); 00050 return nowEnt << entShft; 00051 } 00052 int xEnt = (nowEnt < maxEnt ? nowEnt+1 : 0); 00053 int zEnt = maxEnt; 00054 do {for (int i = xEnt; i < zEnt; i++) 00055 {if (pendCnt[i] < maxPend) 00056 {pendCnt[i]++; 00057 nowEnt = i; 00058 if (maxSpread) return Spread(i); 00059 entMutex.UnLock(); 00060 return i; 00061 00062 } 00063 } 00064 if (!xEnt) break; 00065 xEnt = 0; zEnt = nowEnt; 00066 } while(true); 00067 entMutex.UnLock(); 00068 return -1; 00069 } 00070 00071 void retEnt(int xEnt) {xEnt >>= entShft; 00072 if (xEnt >= 0 && xEnt < maxEnt) 00073 {entMutex.Lock(); 00074 if (pendCnt[xEnt]) pendCnt[xEnt]--; 00075 entMutex.UnLock(); 00076 } 00077 } 00078 00079 bool rsvEnt(int xEnt) {xEnt >>= entShft; 00080 if (xEnt < 0 && xEnt >= maxEnt) return false; 00081 entMutex.Lock(); 00082 if (pendCnt[nowEnt] < maxPend) 00083 {pendCnt[nowEnt]++; 00084 entMutex.UnLock(); 00085 return true; 00086 } 00087 entMutex.UnLock(); 00088 return false; 00089 } 00090 00091 void setSpread(short sval) {if (sval <= 0) maxSpread = 0; 00092 else if (sval < maxSprd) maxSpread = sval; 00093 else maxSpread = maxSprd; 00094 } 00095 00096 XrdSsiScale() : nowEnt(0), maxSpread(4), nowSpread(0) 00097 {memset(pendCnt, 0, sizeof(uint16_t)*maxEnt);} 00098 00099 ~XrdSsiScale() {} 00100 00101 private: 00102 00103 int Spread(int ent) // Called with entMutex locked and return unlocked. 00104 {int n = nowSpread; 00105 nowSpread++; 00106 if (nowSpread >= maxSpread) nowSpread = 0; 00107 entMutex.UnLock(); 00108 return (ent << entShft) | n; 00109 } 00110 00111 XrdSysMutex entMutex; 00112 uint16_t pendCnt[maxEnt]; 00113 int nowEnt; 00114 short maxSpread; 00115 short nowSpread; 00116 }; 00117 #endif