f | class Lock: | f | class Lock: |
n | const_ = None | n | cn = None |
| | | |
| def __new__(item): | | def __new__(item): |
n | if item.const_ is None: | n | if item.cn is None: |
| item.const_ = super(Lock, item).__new__(item) | | item.cn = super(Lock, item).__new__(item) |
| item.dict_semofor = {} | | item.dict_semofor = {} |
n | return item.const_ | n | return item.cn |
| | | |
| @staticmethod | | @staticmethod |
| def locked(item): | | def locked(item): |
n | item.lock = Sem() | n | item.lock = Semaphore() |
| item.counter = None | | item.count = None |
| | | |
n | def delete_obj(self): | n | def dlt_obj(self): |
| del self.lock | | del self.lock |
| try: | | try: |
| super(type(self), self).__del__ | | super(type(self), self).__del__ |
| except AttributeError: | | except AttributeError: |
| pass | | pass |
| else: | | else: |
| super(type(self), self).__del__() | | super(type(self), self).__del__() |
n | item.__del__ = delete_obj | n | item.__del__ = dlt_obj |
| return item | | return item |
| | | |
n | class Sem: | n | class Semaphore: |
| | | |
| def __init__(self): | | def __init__(self): |
| self.resources = Lock() | | self.resources = Lock() |
| | | |
n | def __get__(self, const_, owner): | n | def __get__(self, cn, owner): |
| locked = self.resources.dict_semofor.get(const_.counter) | | locked = self.resources.dict_semofor.get(cn.count) |
| if not locked: | | if not locked: |
n | self.resources.dict_semofor[const_.counter] = id(const_) | n | self.resources.dict_semofor[cn.count] = id(cn) |
| return const_.counter | | return cn.count |
| elif locked == id(const_): | | elif locked == id(cn): |
| return const_.counter | | return cn.count |
| else: | | else: |
| return None | | return None |
| | | |
n | def __delete__(self, const_): | n | def __delete__(self, cn): |
| if self.resources.dict_semofor.get(const_.counter) == id(const_): | | if self.resources.dict_semofor.get(cn.count) == id(cn): |
| self.resources.dict_semofor[const_.counter] = None | | self.resources.dict_semofor[cn.count] = None |
| const_._lock = None | | cn._lock = None |
| | | |
t | def __set__(self, const_, elem): | t | def __set__(self, cn, v): |
| if self.resources.dict_semofor.get(const_.counter) == id(const_): | | if self.resources.dict_semofor.get(cn.count) == id(cn): |
| self.resources.dict_semofor[const_.counter] = None | | self.resources.dict_semofor[cn.count] = None |
| const_.counter = elem | | cn.count = v |
| import sys | | import sys |
| exec(sys.stdin.read(), globals()) | | exec(sys.stdin.read(), globals()) |