XRootD
XrdCl::StreamMutex Class Reference

#include <XrdClStream.hh>

+ Collaboration diagram for XrdCl::StreamMutex:

Classes

struct  MtxInfo
 

Public Member Functions

 StreamMutex ()
 
 ~StreamMutex ()
 
void AddClosing (uint16_t subStream)
 AddClosing. Notified that subStream will be closed. More...
 
void Lock ()
 Lock. Regular, non-subStream aware recursive lock. More...
 
void Lock (const std::function< void()> &func, bool &isclosing)
 
void Lock (uint16_t subStream, bool &isclosing)
 Lock. subStream number aware with the potential to abort. More...
 
void RemoveClosing (uint16_t subStream)
 RemoveClosing. Notified that subStream close has completed. More...
 
void UnLock ()
 UnLock. More...
 

Public Attributes

std::list< MtxInfo >::iterator fnlistit
 
bool hasfn
 
std::map< uint16_t, size_t > mclosing
 
XrdSysCondVar mcv
 
std::list< MtxInfomlist
 
std::map< pthread_t, std::list< MtxInfo >::iterator > mthmap
 

Detailed Description

StreamMutex

Mutex for the main Stream mutex. In the usual case acts as a recursive mutex. But supports an awareness of subStream number. In case an AsyncSocketHandler needs to be Close() any Poller callback must for the handler must complete. (Unless the close is executed within the callback thread). Thus this mutex allows aborting an acquisition attempt when the lock has been declared to be within a subStream callback and when Mutex is notified of an intent to Close that subStream.

Definition at line 61 of file XrdClStream.hh.

Constructor & Destructor Documentation

◆ StreamMutex()

XrdCl::StreamMutex::StreamMutex ( )
inline

Definition at line 64 of file XrdClStream.hh.

64 : mcv(0), hasfn(false) { }
XrdSysCondVar mcv
Definition: XrdClStream.hh:116

◆ ~StreamMutex()

XrdCl::StreamMutex::~StreamMutex ( )
inline

Definition at line 65 of file XrdClStream.hh.

66  {
67  assert( mlist.empty() );
68  assert( mclosing.empty() );
69  assert( mthmap.empty() );
70  assert( hasfn == false );
71  }
std::map< pthread_t, std::list< MtxInfo >::iterator > mthmap
Definition: XrdClStream.hh:119
std::map< uint16_t, size_t > mclosing
Definition: XrdClStream.hh:118
std::list< MtxInfo > mlist
Definition: XrdClStream.hh:117

References hasfn, mclosing, mlist, and mthmap.

Member Function Documentation

◆ AddClosing()

void XrdCl::StreamMutex::AddClosing ( uint16_t  subStream)

AddClosing. Notified that subStream will be closed.

Definition at line 98 of file XrdClStream.cc.

99  {
100  XrdSysCondVarHelper lck( mcv );
101  mclosing[subStream]++;
102  mcv.Broadcast();
103  }

References XrdSysCondVar::Broadcast(), mclosing, and mcv.

+ Here is the call graph for this function:

◆ Lock() [1/3]

void XrdCl::StreamMutex::Lock ( )

Lock. Regular, non-subStream aware recursive lock.

Definition at line 119 of file XrdClStream.cc.

120  {
121  XrdSysCondVarHelper lck( mcv );
122  if( mlist.empty() )
123  {
124  mlist.emplace_front();
125  ++mlist.front().cnt;
126  mthmap[XrdSysThread::ID()] = mlist.begin();
127  return;
128  }
129  while( 1 )
130  {
131  auto mit = mthmap.find( XrdSysThread::ID() );
132  if( mit == mthmap.end() )
133  {
134  mlist.emplace_back();
135  bool ins;
136  std::tie( mit, ins ) = mthmap.insert(
137  std::make_pair( XrdSysThread::ID(), std::prev( mlist.end() ) ) );
138  }
139  if( mit->second == mlist.begin() )
140  {
141  ++mlist.front().cnt;
142  return;
143  }
144  mcv.Wait();
145  }
146  }
static pthread_t ID(void)

References XrdSysThread::ID(), mcv, mlist, mthmap, and XrdSysCondVar::Wait().

Referenced by XrdCl::StreamMutexHelper::StreamMutexHelper().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Lock() [2/3]

void XrdCl::StreamMutex::Lock ( const std::function< void()> &  func,
bool &  isclosing 
)

Lock. Locks or otherwise returns immedately, while the Mutex will abort all waiting subStream acquisitions and execute supplied func when lock is released. Lock will be atomically acquired at last release. Only one func may be registered at a time.

Definition at line 195 of file XrdClStream.cc.

196  {
197  isclosing = false;
198  XrdSysCondVarHelper lck( mcv );
199  if( mlist.empty() )
200  {
201  mlist.emplace_front( func );
202  ++mlist.front().cnt;
203  auto lit = mlist.begin();
204  mthmap[XrdSysThread::ID()] = lit;
205  fnlistit = lit;
206  hasfn = true;
207  return;
208  }
209  while( 1 )
210  {
211  auto mit = mthmap.find( XrdSysThread::ID() );
212  if( mit == mthmap.end() )
213  {
214  if( hasfn )
215  {
216  isclosing = true;
217  return;
218  }
219  mlist.emplace_back( func );
220  hasfn = true;
221  fnlistit = std::prev( mlist.end() );
222  mcv.Broadcast();
223  isclosing = true;
224  return;
225  }
226  if( mit->second == mlist.begin() )
227  {
228  ++mlist.front().cnt;
229  return;
230  }
231  mcv.Wait();
232  }
233  }
std::list< MtxInfo >::iterator fnlistit
Definition: XrdClStream.hh:121

References XrdSysCondVar::Broadcast(), fnlistit, hasfn, XrdSysThread::ID(), mcv, mlist, mthmap, and XrdSysCondVar::Wait().

+ Here is the call graph for this function:

◆ Lock() [3/3]

void XrdCl::StreamMutex::Lock ( uint16_t  subStream,
bool &  isclosing 
)

Lock. subStream number aware with the potential to abort.

Definition at line 152 of file XrdClStream.cc.

153  {
154  isclosing = false;
155  XrdSysCondVarHelper lck( mcv );
156  if( mlist.empty() ) {
157  mlist.emplace_front();
158  ++mlist.front().cnt;
159  mthmap[XrdSysThread::ID()] = mlist.begin();
160  return;
161  }
162  while( 1 )
163  {
164  auto mit = mthmap.find( XrdSysThread::ID() );
165  if( mit == mthmap.end() )
166  {
167  mlist.emplace_back();
168  bool ins;
169  std::tie( mit, ins ) = mthmap.insert(
170  std::make_pair( XrdSysThread::ID(), std::prev( mlist.end() ) ) );
171  }
172  if( mit->second == mlist.begin() )
173  {
174  ++mlist.front().cnt;
175  return;
176  }
177  if( hasfn || mclosing.count( subStream ) )
178  {
179  isclosing = true;
180  mlist.erase( mit->second );
181  mthmap.erase( mit );
182  return;
183  }
184  mcv.Wait();
185  }
186  }

References hasfn, XrdSysThread::ID(), mclosing, mcv, mlist, mthmap, and XrdSysCondVar::Wait().

+ Here is the call graph for this function:

◆ RemoveClosing()

void XrdCl::StreamMutex::RemoveClosing ( uint16_t  subStream)

RemoveClosing. Notified that subStream close has completed.

Definition at line 108 of file XrdClStream.cc.

109  {
110  XrdSysCondVarHelper lck( mcv );
111  mclosing[subStream]--;
112  if( mclosing[subStream]==0 ) mclosing.erase( subStream );
113  mcv.Broadcast();
114  }

References XrdSysCondVar::Broadcast(), mclosing, and mcv.

+ Here is the call graph for this function:

◆ UnLock()

void XrdCl::StreamMutex::UnLock ( )

UnLock.

Definition at line 238 of file XrdClStream.cc.

239  {
240  // keep any fn callback until return in case it holds a ref count
241  std::function<void()> keepfn;
242 
243  XrdSysCondVarHelper lck( mcv );
244  auto mit = mthmap.find( XrdSysThread::ID() );
245  if( mit == mthmap.end() ) return;
246 
247  // we must have held the lock
248  assert( mit->second == mlist.begin() );
249 
250  const size_t cnt = --mlist.front().cnt;
251  if( cnt ) return;
252 
253  if( hasfn && fnlistit == mit->second )
254  {
255  hasfn = false;
256  std::swap( keepfn, mlist.front().fn );
257  }
258 
259  mlist.erase( mit->second );
260  mthmap.erase( mit );
261 
262  // next up should have zero count
263  assert( mlist.empty() || mlist.front().cnt == 0 );
264 
265  if( hasfn && fnlistit == mlist.begin() )
266  {
267  auto &lfn = mlist.front().fn;
268  ++mlist.front().cnt;
269  mthmap[XrdSysThread::ID()] = mlist.begin();
270  lck.UnLock();
271  lfn();
272  UnLock();
273  return;
274  }
275  mcv.Broadcast();
276  }
static uint64_t swap(uint64_t x)
void UnLock()
UnLock.
Definition: XrdClStream.cc:238

References XrdSysCondVar::Broadcast(), fnlistit, hasfn, XrdSysThread::ID(), mcv, mlist, mthmap, swap(), and XrdSysCondVarHelper::UnLock().

Referenced by XrdCl::StreamMutexHelper::UnLock().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ fnlistit

std::list<MtxInfo>::iterator XrdCl::StreamMutex::fnlistit

Definition at line 121 of file XrdClStream.hh.

Referenced by Lock(), and UnLock().

◆ hasfn

bool XrdCl::StreamMutex::hasfn

Definition at line 120 of file XrdClStream.hh.

Referenced by ~StreamMutex(), Lock(), and UnLock().

◆ mclosing

std::map<uint16_t, size_t> XrdCl::StreamMutex::mclosing

Definition at line 118 of file XrdClStream.hh.

Referenced by ~StreamMutex(), AddClosing(), Lock(), and RemoveClosing().

◆ mcv

XrdSysCondVar XrdCl::StreamMutex::mcv

Definition at line 116 of file XrdClStream.hh.

Referenced by AddClosing(), Lock(), RemoveClosing(), and UnLock().

◆ mlist

std::list<MtxInfo> XrdCl::StreamMutex::mlist

Definition at line 117 of file XrdClStream.hh.

Referenced by ~StreamMutex(), Lock(), and UnLock().

◆ mthmap

std::map<pthread_t, std::list<MtxInfo>::iterator> XrdCl::StreamMutex::mthmap

Definition at line 119 of file XrdClStream.hh.

Referenced by ~StreamMutex(), Lock(), and UnLock().


The documentation for this class was generated from the following files: