Revision 806e20fd

b/lib/locking.py
372 372
    # Check we don't already own locks at this level
373 373
    assert not self._is_owned(), "Cannot acquire locks in the same set twice"
374 374

  
375
    # Support passing in a single resource to acquire rather than many
376
    if isinstance(names, basestring):
377
      names = [names]
378
    else:
379
      names.sort()
375
    try:
376
      # Support passing in a single resource to acquire rather than many
377
      if isinstance(names, basestring):
378
        names = [names]
379
      else:
380
        names.sort()
380 381

  
381
    acquire_list = []
382
    # First we look the locks up on __lockdict. We have no way of being sure
383
    # they will still be there after, but this makes it a lot faster should
384
    # just one of them be the already wrong
385
    for lname in names:
386
      try:
387
        lock = self.__lockdict[lname] # raises KeyError if the lock is not there
388
        acquire_list.append((lname, lock))
389
      except (KeyError):
390
        raise errors.LockError('non-existing lock in set (%s)' % lname)
391

  
392
    # This will hold the locknames we effectively acquired.
393
    acquired = set()
394
    # Now acquire_list contains a sorted list of resources and locks we want.
395
    # In order to get them we loop on this (private) list and acquire() them.
396
    # We gave no real guarantee they will still exist till this is done but
397
    # .acquire() itself is safe and will alert us if the lock gets deleted.
398
    for (lname, lock) in acquire_list:
399
      try:
400
        lock.acquire(shared=shared) # raises LockError if the lock is deleted
382
      acquire_list = []
383
      # First we look the locks up on __lockdict. We have no way of being sure
384
      # they will still be there after, but this makes it a lot faster should
385
      # just one of them be the already wrong
386
      for lname in names:
401 387
        try:
402
          # now the lock cannot be deleted, we have it!
403
          self._add_owned(lname)
404
          acquired.add(lname)
405
        except:
406
          # We shouldn't have problems adding the lock to the owners list, but
407
          # if we did we'll try to release this lock and re-raise exception.
408
          # Of course something is going to be really wrong, after this.
409
          lock.release()
410
          raise
411

  
412
      except (errors.LockError):
413
        name_fail = lname
414
        for lname in self._list_owned():
415
          self.__lockdict[lname].release()
416
          self._del_owned(lname)
417
        raise errors.LockError('non-existing lock in set (%s)' % name_fail)
388
          lock = self.__lockdict[lname] # raises KeyError if the lock is not there
389
          acquire_list.append((lname, lock))
390
        except (KeyError):
391
          raise errors.LockError('non-existing lock in set (%s)' % lname)
392

  
393
      # This will hold the locknames we effectively acquired.
394
      acquired = set()
395
      # Now acquire_list contains a sorted list of resources and locks we want.
396
      # In order to get them we loop on this (private) list and acquire() them.
397
      # We gave no real guarantee they will still exist till this is done but
398
      # .acquire() itself is safe and will alert us if the lock gets deleted.
399
      for (lname, lock) in acquire_list:
400
        try:
401
          lock.acquire(shared=shared) # raises LockError if the lock is deleted
402
          try:
403
            # now the lock cannot be deleted, we have it!
404
            self._add_owned(lname)
405
            acquired.add(lname)
406
          except:
407
            # We shouldn't have problems adding the lock to the owners list, but
408
            # if we did we'll try to release this lock and re-raise exception.
409
            # Of course something is going to be really wrong, after this.
410
            lock.release()
411
            raise
412

  
413
        except (errors.LockError):
414
          name_fail = lname
415
          for lname in self._list_owned():
416
            self.__lockdict[lname].release()
417
            self._del_owned(lname)
418
          raise errors.LockError('non-existing lock in set (%s)' % name_fail)
419

  
420
    except:
421
      raise
418 422

  
419 423
    return acquired
420 424

  

Also available in: Unified diff