Revision 557838c1 lib/locking.py

b/lib/locking.py
28 28
import os
29 29
import select
30 30
import threading
31
import time
32 31
import errno
33 32
import weakref
34 33
import logging
......
73 72
  return wrap
74 73

  
75 74

  
76
class RunningTimeout(object):
77
  """Class to calculate remaining timeout when doing several operations.
78

  
79
  """
80
  __slots__ = [
81
    "_allow_negative",
82
    "_start_time",
83
    "_time_fn",
84
    "_timeout",
85
    ]
86

  
87
  def __init__(self, timeout, allow_negative, _time_fn=time.time):
88
    """Initializes this class.
89

  
90
    @type timeout: float
91
    @param timeout: Timeout duration
92
    @type allow_negative: bool
93
    @param allow_negative: Whether to return values below zero
94
    @param _time_fn: Time function for unittests
95

  
96
    """
97
    object.__init__(self)
98

  
99
    if timeout is not None and timeout < 0.0:
100
      raise ValueError("Timeout must not be negative")
101

  
102
    self._timeout = timeout
103
    self._allow_negative = allow_negative
104
    self._time_fn = _time_fn
105

  
106
    self._start_time = None
107

  
108
  def Remaining(self):
109
    """Returns the remaining timeout.
110

  
111
    """
112
    if self._timeout is None:
113
      return None
114

  
115
    # Get start time on first calculation
116
    if self._start_time is None:
117
      self._start_time = self._time_fn()
118

  
119
    # Calculate remaining time
120
    remaining_timeout = self._start_time + self._timeout - self._time_fn()
121

  
122
    if not self._allow_negative:
123
      # Ensure timeout is always >= 0
124
      return max(0.0, remaining_timeout)
125

  
126
    return remaining_timeout
127

  
128

  
129 75
class _SingleNotifyPipeConditionWaiter(object):
130 76
  """Helper class for SingleNotifyPipeCondition
131 77

  
......
155 101
    @param timeout: Timeout for waiting (can be None)
156 102

  
157 103
    """
158
    running_timeout = RunningTimeout(timeout, True)
104
    running_timeout = utils.RunningTimeout(timeout, True)
159 105

  
160 106
    while True:
161 107
      remaining_time = running_timeout.Remaining()
......
1033 979

  
1034 980
    # We need to keep track of how long we spent waiting for a lock. The
1035 981
    # timeout passed to this function is over all lock acquires.
1036
    running_timeout = RunningTimeout(timeout, False)
982
    running_timeout = utils.RunningTimeout(timeout, False)
1037 983

  
1038 984
    try:
1039 985
      if names is not None:

Also available in: Unified diff