import functools


def synchronized(lock):
    """ Synchronization decorator.

    from http://wiki.python.org/moin/PythonDecoratorLibrary

    @param lock Lock or RLock instance
    @return decorator that provides automatic locking
    """
    def wrapper(func):
        @functools.wraps(func)
        def inner(*args, **kwds):
            lock.acquire()
            try:
                return func(*args, **kwds)
            finally:
                lock.release()
        return inner
    return wrapper