import threading
import functools


def synchronized(wrapped):
    """Simple synchronization decorator.

    Decorating a method like so:

    .. code-block:: python

      @synchronized
      def foo(self, *args):
        ...

    ensures that only one thread will execute the foo method at a time.
    """
    lock = threading.RLock()

    @functools.wraps(wrapped)
    def wrapper(*args, **kwargs):
        with lock:
            return wrapped(*args, **kwargs)

    return wrapper