Revision 9216a9f7 lib/locking.py

b/lib/locking.py
153 153

  
154 154
    assert self.__npass_shr == 0, "SharedLock: internal fairness violation"
155 155

  
156
  def __shared_acquire(self):
157
    """Acquire the lock in shared mode
158

  
159
    This is a private function that presumes you are already holding the
160
    internal lock.
161

  
162
    """
163
    self.__nwait_shr += 1
164
    try:
165
      wait = False
166
      # If there is an exclusive holder waiting we have to wait.
167
      # We'll only do this once, though, when we start waiting for
168
      # the lock. Then we'll just wait while there are no
169
      # exclusive holders.
170
      if self.__nwait_exc > 0:
171
        # TODO: if !blocking...
172
        wait = True
173
        self.__wait(self.__turn_shr)
174

  
175
      while self.__exc is not None:
176
        wait = True
177
        # TODO: if !blocking...
178
        self.__wait(self.__turn_shr)
179

  
180
      self.__shr.add(threading.currentThread())
181

  
182
      # If we were waiting note that we passed
183
      if wait:
184
        self.__npass_shr -= 1
185

  
186
    finally:
187
      self.__nwait_shr -= 1
188

  
189
    assert self.__npass_shr >= 0, "Internal fairness condition weirdness"
190

  
156 191
  def acquire(self, blocking=1, shared=0):
157 192
    """Acquire a shared lock.
158 193

  
......
176 211
      assert self.__npass_shr >= 0, "Internal fairness condition weirdness"
177 212

  
178 213
      if shared:
179
        self.__nwait_shr += 1
180
        try:
181
          wait = False
182
          # If there is an exclusive holder waiting we have to wait.
183
          # We'll only do this once, though, when we start waiting for
184
          # the lock. Then we'll just wait while there are no
185
          # exclusive holders.
186
          if self.__nwait_exc > 0:
187
            # TODO: if !blocking...
188
            wait = True
189
            self.__wait(self.__turn_shr)
190

  
191
          while self.__exc is not None:
192
            wait = True
193
            # TODO: if !blocking...
194
            self.__wait(self.__turn_shr)
195

  
196
          self.__shr.add(threading.currentThread())
197

  
198
          # If we were waiting note that we passed
199
          if wait:
200
            self.__npass_shr -= 1
201

  
202
        finally:
203
          self.__nwait_shr -= 1
204

  
205
        assert self.__npass_shr >= 0, "Internal fairness condition weirdness"
214
        self.__shared_acquire()
206 215
      else:
207 216
        # TODO: if !blocking...
208 217
        # (or modify __exclusive_acquire for non-blocking mode)
......
353 362
      else:
354 363
        self.__owners[threading.currentThread()] = set([name])
355 364

  
356

  
357 365
  def _del_owned(self, name=None):
358 366
    """Note the current thread owns the given lock"""
359 367

  

Also available in: Unified diff