/loggerhead/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/loggerhead/trunk

« back to all changes in this revision

Viewing changes to loggerhead/history.py

  • Committer: Michael Hudson
  • Date: 2010-05-07 01:03:17 UTC
  • mfrom: (414.1.1 loggerhead)
  • Revision ID: michael.hudson@canonical.com-20100507010317-kr5ipx6qgn65636o
(mkanat) use a global lock to protect access to the lru cache

Show diffs side-by-side

added added

removed removed

Lines of Context:
152
152
                filename=rich_filename(paths[1], kind),
153
153
                file_id=file_id))
154
154
 
 
155
# The lru_cache is not thread-safe, so we need a lock around it for
 
156
# all threads.
 
157
rev_info_memory_cache_lock = threading.RLock()
155
158
 
156
159
class RevInfoMemoryCache(object):
157
160
    """A store that validates values against the revids they were stored with.
168
171
 
169
172
    def __init__(self, cache):
170
173
        self._cache = cache
171
 
        # lru_cache is not thread-safe, so we need to lock all accesses.
172
 
        # It is even modified when doing a get() on it.
173
 
        self._lock = threading.RLock()
174
174
 
175
175
    def get(self, key, revid):
176
176
        """Return the data associated with `key`, subject to a revid check.
178
178
        If a value was stored under `key`, with the same revid, return it.
179
179
        Otherwise return None.
180
180
        """
181
 
        self._lock.acquire()
 
181
        rev_info_memory_cache_lock.acquire()
182
182
        try:
183
183
            cached = self._cache.get(key)
184
184
        finally:
185
 
            self._lock.release()
 
185
            rev_info_memory_cache_lock.release()
186
186
        if cached is None:
187
187
            return None
188
188
        stored_revid, data = cached
194
194
    def set(self, key, revid, data):
195
195
        """Store `data` under `key`, to be checked against `revid` on get().
196
196
        """
197
 
        self._lock.acquire()
 
197
        rev_info_memory_cache_lock.acquire()
198
198
        try:
199
199
            self._cache[key] = (revid, data)
200
200
        finally:
201
 
            self._lock.release()
 
201
            rev_info_memory_cache_lock.release()
202
202
 
203
203
# Used to store locks that prevent multiple threads from building a 
204
204
# revision graph for the same branch at the same time, because that can