Exit transition immediately if already in the desired state.

This commit is contained in:
Lance Stout 2012-08-10 12:41:02 -07:00
parent 4e12e228cb
commit d11a67702e

View file

@ -29,7 +29,7 @@ class StateMachine(object):
if state in self.__states: if state in self.__states:
raise IndexError("The state '%s' is already in the StateMachine." % state) raise IndexError("The state '%s' is already in the StateMachine." % state)
self.__states.append(state) self.__states.append(state)
finally: finally:
self.lock.release() self.lock.release()
@ -90,6 +90,9 @@ class StateMachine(object):
log.debug("Could not acquire lock") log.debug("Could not acquire lock")
return False return False
if self.__current_state == to_state:
return True
while not self.__current_state in from_states: while not self.__current_state in from_states:
# detect timeout: # detect timeout:
remainder = start + wait - time.time() remainder = start + wait - time.time()
@ -108,7 +111,7 @@ class StateMachine(object):
# some 'false' value returned from func, # some 'false' value returned from func,
# indicating that transition should not occur: # indicating that transition should not occur:
if not return_val: if not return_val:
return return_val return return_val
log.debug(' ==== TRANSITION %s -> %s', self.__current_state, to_state) log.debug(' ==== TRANSITION %s -> %s', self.__current_state, to_state)
@ -193,9 +196,9 @@ class StateMachine(object):
while not self.__current_state in states: while not self.__current_state in states:
# detect timeout: # detect timeout:
remainder = start + wait - time.time() remainder = start + wait - time.time()
if remainder > 0: if remainder > 0:
self.lock.wait(remainder) self.lock.wait(remainder)
else: else:
self.lock.release() self.lock.release()
return False return False
self.lock.release() self.lock.release()
@ -241,7 +244,7 @@ class _StateCtx:
while not self.state_machine[self.from_state] or not self.state_machine.lock.acquire(False): while not self.state_machine[self.from_state] or not self.state_machine.lock.acquire(False):
# detect timeout: # detect timeout:
remainder = start + self.wait - time.time() remainder = start + self.wait - time.time()
if remainder > 0: if remainder > 0:
self.state_machine.lock.wait(remainder) self.state_machine.lock.wait(remainder)
else: else:
log.debug('StateMachine timeout while waiting for state: %s', self.from_state) log.debug('StateMachine timeout while waiting for state: %s', self.from_state)