diff performance & tree.lock/unlock

Denys Duchier duchier at ps.uni-sb.de
Thu Jan 12 15:09:55 GMT 2006


Aaron Bentley <aaron.bentley at utoronto.ca> writes:

> This approach basically makes sense, though some code will get a bit
> uglier.

Aaron explained on IRC that he meant that there will probably be more
lock/unlock+try/finally that need to be introduced.  I thought of using
decorators for that, e.g. something like this:


def needs_tree_write_lock(i):
    def decorate(callable):
        def decorated(*args,**keys):
            tree = args[i]
            tree.lock_write()
            try:
                return callable(*args,**keys)
            finally:
                tree.unlock()
        return decorated
    return decorate

def needs_tree_read_lock(i):
    def decorate(callable):
        def decorated(*args,**keys):
            tree = args[i]
            tree.lock_read()
            try:
                return callable(*args,**keys)
            finally:
                tree.unlock()
        return decorated
    return decorate


For example:

@needs_tree_read_lock(0)
@needs_tree_read_lock(1)
def compare_trees(old_tree, new_tree, want_unchanged=False, specific_files=None):


but of course that's a little brittle because you have to be careful to keep the
index argument up to date.

opinions? suggestions?

Cheers,

--Denys





More information about the bazaar mailing list