Чэнь Сюаньдун, 619/2 группа SemClass 15643
Ван Цзыци, 614 группа SemClass 15644
t1class LockDescriptor:t1class LockDescriptor:
2    _semaphores = {}2    _semaphores = {}
3    _waiting_queues = {}3    _waiting_queues = {}
4    _temp = {}4    _temp = {}
55
6    def __get__(self, instance, owner):6    def __get__(self, instance, owner):
7        instance_id = id(instance)7        instance_id = id(instance)
8        name = self._temp.get(instance_id, None)8        name = self._temp.get(instance_id, None)
9        if name is not None and name not in self._semaphores.values():9        if name is not None and name not in self._semaphores.values():
10            self._semaphores[instance_id] = name10            self._semaphores[instance_id] = name
11        return self._semaphores.get(id(instance), None)11        return self._semaphores.get(id(instance), None)
1212
13    def __set__(self, instance, name):13    def __set__(self, instance, name):
14        instance_id = id(instance)14        instance_id = id(instance)
15        current_lock = self._semaphores.get(instance_id)15        current_lock = self._semaphores.get(instance_id)
16        if current_lock == name:16        if current_lock == name:
17            self.__delete__(instance)17            self.__delete__(instance)
18            if self._waiting_queues.get(name):18            if self._waiting_queues.get(name):
19                next_instance = self._waiting_queues[name].pop(0)19                next_instance = self._waiting_queues[name].pop(0)
20                self._temp[id(next_instance)] = name20                self._temp[id(next_instance)] = name
21        elif (instance_id, name) not in self._temp.items() and name not in self._semaphores.values():21        elif (instance_id, name) not in self._temp.items() and name not in self._semaphores.values():
22            self._temp[instance_id] = name22            self._temp[instance_id] = name
23        else:23        else:
24            if current_lock is not None and current_lock != name:24            if current_lock is not None and current_lock != name:
25                del self._semaphores[instance_id]25                del self._semaphores[instance_id]
26                del self._temp[instance_id]26                del self._temp[instance_id]
27            self._waiting_queues.setdefault(name, []).append(instance)27            self._waiting_queues.setdefault(name, []).append(instance)
2828
29    def __delete__(self, instance):29    def __delete__(self, instance):
30        instance_id = id(instance)30        instance_id = id(instance)
31        semaphore_to_release = self._semaphores.pop(instance_id, None)31        semaphore_to_release = self._semaphores.pop(instance_id, None)
32        self._temp.pop(instance_id, None)32        self._temp.pop(instance_id, None)
33        if semaphore_to_release:33        if semaphore_to_release:
34            if semaphore_to_release in self._waiting_queues:34            if semaphore_to_release in self._waiting_queues:
35                waiting_instances = self._waiting_queues[semaphore_to_release]35                waiting_instances = self._waiting_queues[semaphore_to_release]
36                if waiting_instances:36                if waiting_instances:
37                    next_instance = waiting_instances.pop(0)37                    next_instance = waiting_instances.pop(0)
38                    self._temp[id(next_instance)] = semaphore_to_release38                    self._temp[id(next_instance)] = semaphore_to_release
3939
40    @classmethod40    @classmethod
41    def release_all(cls, instance):41    def release_all(cls, instance):
42        instance_id = id(instance)42        instance_id = id(instance)
43        semaphore_to_release = cls._semaphores.pop(instance_id, None)43        semaphore_to_release = cls._semaphores.pop(instance_id, None)
44        cls._temp.pop(instance_id, None)44        cls._temp.pop(instance_id, None)
45        if semaphore_to_release and semaphore_to_release in cls._waiting_queues:45        if semaphore_to_release and semaphore_to_release in cls._waiting_queues:
46            waiting_instances = cls._waiting_queues[semaphore_to_release]46            waiting_instances = cls._waiting_queues[semaphore_to_release]
47            if waiting_instances:47            if waiting_instances:
48                next_instance = waiting_instances.pop(0)48                next_instance = waiting_instances.pop(0)
49                cls._temp[id(next_instance)] = semaphore_to_release49                cls._temp[id(next_instance)] = semaphore_to_release
5050
51class Lock:51class Lock:
5252
53    @staticmethod53    @staticmethod
54    def locked(cls):54    def locked(cls):
5555
56        class Wrapped(cls):56        class Wrapped(cls):
57            lock = LockDescriptor()57            lock = LockDescriptor()
5858
59            def __del__(self):59            def __del__(self):
60                try:60                try:
61                    LockDescriptor.release_all(self)61                    LockDescriptor.release_all(self)
62                except AttributeError:62                except AttributeError:
63                    pass63                    pass
64                super_del = getattr(super(), '__del__', None)64                super_del = getattr(super(), '__del__', None)
65                if callable(super_del):65                if callable(super_del):
66                    super_del(self)66                    super_del(self)
67        return Wrapped67        return Wrapped
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op