-
Notifications
You must be signed in to change notification settings - Fork 149
/
XrdClSIDManager.cc
183 lines (164 loc) · 6.83 KB
/
XrdClSIDManager.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//------------------------------------------------------------------------------
// Copyright (c) 2011-2012 by European Organization for Nuclear Research (CERN)
// Author: Lukasz Janyst <ljanyst@cern.ch>
//------------------------------------------------------------------------------
// XRootD is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// XRootD is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with XRootD. If not, see <http://www.gnu.org/licenses/>.
//------------------------------------------------------------------------------
#include "XrdCl/XrdClSIDManager.hh"
namespace XrdCl
{
//----------------------------------------------------------------------------
// Allocate a SID
//---------------------------------------------------------------------------
Status SIDManager::AllocateSID( uint8_t sid[2] )
{
XrdSysMutexHelper scopedLock( pMutex );
uint16_t allocSID = 1;
//--------------------------------------------------------------------------
// Get a SID from the list of free SIDs if it's not empty
//--------------------------------------------------------------------------
if( !pFreeSIDs.empty() )
{
allocSID = pFreeSIDs.front();
pFreeSIDs.pop_front();
}
//--------------------------------------------------------------------------
// Allocate a new SID if possible
//--------------------------------------------------------------------------
else
{
if( pSIDCeiling == 0xffff )
return Status( stError, errNoMoreFreeSIDs );
allocSID = pSIDCeiling++;
}
memcpy( sid, &allocSID, 2 );
return Status();
}
//----------------------------------------------------------------------------
// Release the SID that is no longer needed
//----------------------------------------------------------------------------
void SIDManager::ReleaseSID( uint8_t sid[2] )
{
XrdSysMutexHelper scopedLock( pMutex );
uint16_t relSID = 0;
memcpy( &relSID, sid, 2 );
pFreeSIDs.push_back( relSID );
}
//----------------------------------------------------------------------------
// Register a SID of a request that timed out
//----------------------------------------------------------------------------
void SIDManager::TimeOutSID( uint8_t sid[2] )
{
XrdSysMutexHelper scopedLock( pMutex );
uint16_t tiSID = 0;
memcpy( &tiSID, sid, 2 );
pTimeOutSIDs.insert( tiSID );
}
//----------------------------------------------------------------------------
// Check if a SID is timed out
//----------------------------------------------------------------------------
bool SIDManager::IsTimedOut( uint8_t sid[2] )
{
XrdSysMutexHelper scopedLock( pMutex );
uint16_t tiSID = 0;
memcpy( &tiSID, sid, 2 );
std::set<uint16_t>::iterator it = pTimeOutSIDs.find( tiSID );
if( it != pTimeOutSIDs.end() )
return true;
return false;
}
//----------------------------------------------------------------------------
// Release a timed out SID
//-----------------------------------------------------------------------------
void SIDManager::ReleaseTimedOut( uint8_t sid[2] )
{
XrdSysMutexHelper scopedLock( pMutex );
uint16_t tiSID = 0;
memcpy( &tiSID, sid, 2 );
pTimeOutSIDs.erase( tiSID );
pFreeSIDs.push_back( tiSID );
}
//------------------------------------------------------------------------
// Release all timed out SIDs
//------------------------------------------------------------------------
void SIDManager::ReleaseAllTimedOut()
{
XrdSysMutexHelper scopedLock( pMutex );
std::set<uint16_t>::iterator it;
for( it = pTimeOutSIDs.begin(); it != pTimeOutSIDs.end(); ++it )
pFreeSIDs.push_back( *it );
pTimeOutSIDs.clear();
}
//----------------------------------------------------------------------------
// Get number of allocated SIDs
//----------------------------------------------------------------------------
uint16_t SIDManager::GetNumberOfAllocatedSIDs() const
{
XrdSysMutexHelper scopedLock( pMutex );
return pSIDCeiling - pFreeSIDs.size() - pTimeOutSIDs.size() - 1;
}
//----------------------------------------------------------------------------
// Returns a pointer to the SIDManager object
//----------------------------------------------------------------------------
std::shared_ptr<SIDManager> SIDMgrPool::GetSIDMgr( const URL &url )
{
//--------------------------------------------------------------------------
// Look for an instance of SID manager in the pool
//--------------------------------------------------------------------------
XrdSysMutexHelper lck1( mtx );
SIDManager *mgr = 0;
auto itr = pool.find( url.GetHostId() );
if( itr == pool.end() )
{
mgr = new SIDManager();
pool[url.GetHostId()] = mgr;
}
else mgr = itr->second;
//--------------------------------------------------------------------------
// Update the reference counter
//--------------------------------------------------------------------------
XrdSysMutexHelper lck2( mgr->pMutex );
++mgr->pRefCount;
//--------------------------------------------------------------------------
// Create a shared pointer that will recycle the SID manager
//--------------------------------------------------------------------------
RecycleSidMgr deleter;
std::shared_ptr<SIDManager> ptr( mgr, deleter );
return std::move( ptr );
}
void SIDMgrPool::Recycle( SIDManager *mgr )
{
//--------------------------------------------------------------------------
// Lock the pool, we need to do it in the same order as in 'GetSIDMgr'
//--------------------------------------------------------------------------
XrdSysMutexHelper lck1( mtx );
//--------------------------------------------------------------------------
// Lock the SID manager object
//--------------------------------------------------------------------------
XrdSysMutexHelper lck2( mgr->pMutex );
--mgr->pRefCount;
if( !mgr->pRefCount )
{
//------------------------------------------------------------------------
// Remove the SID manager from the pool
//------------------------------------------------------------------------
auto itr = pool.begin();
for( ; itr != pool.end() ; ++itr )
if( itr->second == mgr ) break;
pool.erase( itr );
lck2.UnLock();
delete mgr;
}
}
}