1"""
2Support for @synchronized blocks
3
4The python class object_lock is a contextmanager for with statements that
5can also be used manually.
6"""
7import objc as _objc
8
9class object_lock(object):
10    """
11    A context manager that implements the same feature as
12    @synchronized statements in Objective-C. Locking can also
13    be done manually using the ``lock`` and ``unlock`` methods.
14
15    The mutex for object ``anObject`` is represented by
16    ``objc.object_lock(anObject)``.
17    """
18    def __init__(self, value):
19        self.__value = value
20
21    def __enter__(self):
22        _objc._objc_sync_enter(self.__value)
23
24    def __exit__(self, type, value, tp):
25        _objc._objc_sync_exit(self.__value)
26
27    def lock(self):
28        _objc._objc_sync_enter(self.__value)
29
30    def unlock(self):
31        _objc._objc_sync_exit(self.__value)
32