f | class Lock: | f | class Lock: |
n | cn = None | n | const_ = None |
| | | |
| def __new__(item): | | def __new__(item): |
n | if item.cn is None: | n | if item.const_ is None: |
| item.cn = super(Lock, item).__new__(item) | | item.const_ = super(Lock, item).__new__(item) |
| item.dict_semofor = {} | | item.dict_semofor = {} |
n | return item.cn | n | return item.const_ |
| | | |
| @staticmethod | | @staticmethod |
| def locked(item): | | def locked(item): |
n | item.lock = Semaphore() | n | item.lock = Sem() |
| item.count = None | | item.counter = None |
| | | |
n | def dlt_obj(self): | n | def delete_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__ = dlt_obj | n | item.__del__ = delete_obj |
| return item | | return item |
| | | |
n | class Semaphore: | n | class Sem: |
| | | |
| def __init__(self): | | def __init__(self): |
| self.resources = Lock() | | self.resources = Lock() |
| | | |
n | def __get__(self, cn, owner): | n | def __get__(self, const_, owner): |
| locked = self.resources.dict_semofor.get(cn.count) | | locked = self.resources.dict_semofor.get(const_.counter) |
| if not locked: | | if not locked: |
n | self.resources.dict_semofor[cn.count] = id(cn) | n | self.resources.dict_semofor[const_.counter] = id(const_) |
| return cn.count | | return const_.counter |
| elif locked == id(cn): | | elif locked == id(const_): |
| return cn.count | | return const_.counter |
| else: | | else: |
| return None | | return None |
| | | |
n | def __delete__(self, cn): | n | def __delete__(self, const_): |
| if self.resources.dict_semofor.get(cn.count) == id(cn): | | if self.resources.dict_semofor.get(const_.counter) == id(const_): |
| self.resources.dict_semofor[cn.count] = None | | self.resources.dict_semofor[const_.counter] = None |
| cn._lock = None | | const_._lock = None |
| | | |
t | def __set__(self, cn, v): | t | def __set__(self, const_, elem): |
| if self.resources.dict_semofor.get(cn.count) == id(cn): | | if self.resources.dict_semofor.get(const_.counter) == id(const_): |
| self.resources.dict_semofor[cn.count] = None | | self.resources.dict_semofor[const_.counter] = None |
| cn.count = v | | const_.counter = elem |
| import sys | | import sys |
| exec(sys.stdin.read(), globals()) | | exec(sys.stdin.read(), globals()) |