XRootD
XrdSysXSLock Class Reference

#include <XrdSysXSLock.hh>

+ Collaboration diagram for XrdSysXSLock:

Public Member Functions

 XrdSysXSLock ()
 
 ~XrdSysXSLock ()
 
void Lock (const XrdSysXS_Type usage)
 
void UnLock (const XrdSysXS_Type usage=xs_None)
 

Detailed Description

Definition at line 44 of file XrdSysXSLock.hh.

Constructor & Destructor Documentation

◆ XrdSysXSLock()

XrdSysXSLock::XrdSysXSLock ( )
inline

Definition at line 52 of file XrdSysXSLock.hh.

52  : cur_usage(xs_None), cur_count(0), exc_wait(0),
53  shr_wait(0), toggle(0), WantShr(0), WantExc(0) {}
@ xs_None
Definition: XrdSysXSLock.hh:38

◆ ~XrdSysXSLock()

XrdSysXSLock::~XrdSysXSLock ( )

Definition at line 38 of file XrdSysXSLock.cc.

39 {
40 
41 // Prevent usage while destroying object but make sure no one else is using it
42 //
43  LockContext.Lock();
44  if (cur_count || shr_wait || exc_wait)
45  {LockContext.UnLock();
46  abort();
47  }
48  LockContext.UnLock();
49 }

References XrdSysMutex::Lock(), and XrdSysMutex::UnLock().

+ Here is the call graph for this function:

Member Function Documentation

◆ Lock()

void XrdSysXSLock::Lock ( const XrdSysXS_Type  usage)

Definition at line 55 of file XrdSysXSLock.cc.

56 {
57 
58 // Serialize access to this object
59 //
60  LockContext.Lock();
61 
62 // This loop continues until we can acquire the resource. We are gauranteed
63 // to eventually acquire it regardless of the unblocking order.
64 //
65  while(cur_count)
66  {
67  // If usage is compatible with current usage get the lock right away
68  //
69  if (usage == xs_Shared && cur_usage == xs_Shared && !exc_wait) break;
70 
71  // Indicate that we are waiting
72  //
73  if (usage == xs_Shared) shr_wait++;
74  else exc_wait++;
75 
76  // Usage is not compatible. We must wait for current lock mode to end
77  //
78  LockContext.UnLock();
79  if (usage == xs_Shared) WantShr.Wait();
80  else WantExc.Wait();
81  LockContext.Lock();
82  }
83 
84 // We obtained the right to use this object
85 //
86  cur_usage = usage;
87  cur_count++;
88  LockContext.UnLock();
89 }
void usage()
@ xs_Shared
Definition: XrdSysXSLock.hh:38

References XrdSysMutex::Lock(), XrdSysMutex::UnLock(), usage(), XrdSysSemaphore::Wait(), and xs_Shared.

Referenced by XrdAccAccess::Access(), XrdRmcData::Detach(), XrdOucGMap::dn2user(), and XrdAccAccess::SwapTabs().

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

◆ UnLock()

void XrdSysXSLock::UnLock ( const XrdSysXS_Type  usage = xs_None)

Definition at line 95 of file XrdSysXSLock.cc.

96 {
97 
98 // Serialize access to our data
99 //
100  LockContext.Lock();
101 
102 // Make sure that the lock is currently being used
103 //
104  if (!cur_count)
105  {LockContext.UnLock();
106  std::cerr << "XSLock: Attempt to unlock inactive lock." <<std::endl;
107  throw "XSLock: unlocking inactive lock.";
108  }
109 
110 // Verify that usage is correct
111 //
112  if (usage && cur_usage != usage)
113  {LockContext.UnLock();
114  std::cerr << "XSLock: Incorrect unlock usage - "
115  << (cur_usage == xs_Shared ? "shr" : "exc") << "!="
116  << ( usage == xs_Shared ? "shr" : "exc") << std::endl;
117  throw "XSLock: invalid unlock usage specified.";
118  }
119 
120 // Unlock the current object. If no locks exist then check if we can let another
121 // thread use this object. The logic is tricky but we are trying to avoid
122 // starvation in an environment that has no thread ordering.
123 //
124  cur_count--;
125  if (!cur_count)
126  if (exc_wait && (toggle || !shr_wait))
127  {toggle = 0; WantExc.Post(); exc_wait--;}
128  else {while(shr_wait) {WantShr.Post(); shr_wait--;}
129  toggle = 1;}
130  else if (!toggle) {while(shr_wait) {WantShr.Post(); shr_wait--;}
131  toggle = 1;}
132 
133  LockContext.UnLock();
134 }

References XrdSysMutex::Lock(), XrdSysSemaphore::Post(), XrdSysMutex::UnLock(), usage(), and xs_Shared.

Referenced by XrdAccAccess::Access(), XrdRmcData::Detach(), XrdOucGMap::dn2user(), and XrdAccAccess::SwapTabs().

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

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