Revision 621b43ed

b/lib/locking.py
51 51
#: (seconds)
52 52
_LOCK_ACQUIRE_MIN_TIMEOUT = (1.0 / 1000)
53 53

  
54
# Internal lock acquisition modes for L{LockSet}
55
(_LS_ACQUIRE_EXACT,
56
 _LS_ACQUIRE_ALL) = range(1, 3)
57

  
54 58

  
55 59
def ssynchronized(mylock, shared=0):
56 60
  """Shared Synchronization decorator.
......
1140 1144
        if isinstance(names, basestring):
1141 1145
          names = [names]
1142 1146

  
1143
        return self.__acquire_inner(names, False, shared, priority,
1147
        return self.__acquire_inner(names, _LS_ACQUIRE_EXACT, shared, priority,
1144 1148
                                    running_timeout.Remaining, test_notify)
1145 1149

  
1146 1150
      else:
......
1160 1164
          # note we own the set-lock
1161 1165
          self._add_owned()
1162 1166

  
1163
          return self.__acquire_inner(self.__names(), True, shared, priority,
1164
                                      running_timeout.Remaining, test_notify)
1167
          return self.__acquire_inner(self.__names(), _LS_ACQUIRE_ALL, shared,
1168
                                      priority, running_timeout.Remaining,
1169
                                      test_notify)
1165 1170
        except:
1166 1171
          # We shouldn't have problems adding the lock to the owners list, but
1167 1172
          # if we did we'll try to release this lock and re-raise exception.
......
1173 1178
    except _AcquireTimeout:
1174 1179
      return None
1175 1180

  
1176
  def __acquire_inner(self, names, want_all, shared, priority,
1181
  def __acquire_inner(self, names, mode, shared, priority,
1177 1182
                      timeout_fn, test_notify):
1178 1183
    """Inner logic for acquiring a number of locks.
1179 1184

  
1180 1185
    @param names: Names of the locks to be acquired
1181
    @param want_all: Whether all locks in the set should be acquired
1186
    @param mode: Lock acquisition mode
1182 1187
    @param shared: Whether to acquire in shared mode
1183 1188
    @param timeout_fn: Function returning remaining timeout
1184 1189
    @param priority: Priority for acquiring locks
1185 1190
    @param test_notify: Special callback function for unittesting
1186 1191

  
1187 1192
    """
1193
    assert mode in (_LS_ACQUIRE_EXACT, _LS_ACQUIRE_ALL)
1194

  
1188 1195
    acquire_list = []
1189 1196

  
1190 1197
    # First we look the locks up on __lockdict. We have no way of being sure
......
1195 1202
      try:
1196 1203
        lock = self.__lockdict[lname] # raises KeyError if lock is not there
1197 1204
      except KeyError:
1198
        if want_all:
1199
          # We are acquiring all the set, it doesn't matter if this particular
1200
          # element is not there anymore.
1201
          continue
1202

  
1203
        raise errors.LockError("Non-existing lock %s in set %s (it may have"
1204
                               " been removed)" % (lname, self.name))
1205

  
1206
      acquire_list.append((lname, lock))
1205
        # We are acquiring the whole set, it doesn't matter if this particular
1206
        # element is not there anymore. If, however, only certain names should
1207
        # be acquired, not finding a lock is an error.
1208
        if mode == _LS_ACQUIRE_EXACT:
1209
          raise errors.LockError("Lock '%s' not found in set '%s' (it may have"
1210
                                 " been removed)" % (lname, self.name))
1211
      else:
1212
        acquire_list.append((lname, lock))
1207 1213

  
1208 1214
    # This will hold the locknames we effectively acquired.
1209 1215
    acquired = set()
......
1228 1234
                                     priority=priority,
1229 1235
                                     test_notify=test_notify_fn)
1230 1236
        except errors.LockError:
1231
          if want_all:
1232
            # We are acquiring all the set, it doesn't matter if this
1237
          if mode == _LS_ACQUIRE_ALL:
1238
            # We are acquiring the whole set, it doesn't matter if this
1233 1239
            # particular element is not there anymore.
1234 1240
            continue
1235 1241

  
1236
          raise errors.LockError("Non-existing lock %s in set %s (it may"
1237
                                 " have been removed)" % (lname, self.name))
1242
          raise errors.LockError("Lock '%s' not found in set '%s' (it may have"
1243
                                 " been removed)" % (lname, self.name))
1238 1244

  
1239 1245
        if not acq_success:
1240 1246
          # Couldn't get lock or timeout occurred

Also available in: Unified diff