Майстер Михаил (Севастополь) SemClass 15005
Могиленец Виктор, Севастополь SemClass 15323
f1from collections import dequef1from collections import deque
22
3class Lock:3class Lock:
4    busy = {}4    busy = {}
5    sems = {}5    sems = {}
66
7    def __init__(self, par):7    def __init__(self, par):
8        self.par = par8        self.par = par
9        self._lock = None9        self._lock = None
10        self.sem = None10        self.sem = None
1111
12    def __str__(self):12    def __str__(self):
13        return str(self.par)13        return str(self.par)
1414
15    @property15    @property
16    def lock(self):16    def lock(self):
17        if self.sem not in Lock.busy:17        if self.sem not in Lock.busy:
18            for k, v in Lock.sems.items():18            for k, v in Lock.sems.items():
19                if self.par in v:19                if self.par in v:
20                    self._lock = self.sem20                    self._lock = self.sem
21                    Lock.busy[self.sem] = self.par21                    Lock.busy[self.sem] = self.par
n22        elif self.par == Lock.busy[self.sem]:n22        elif self.par != Lock.busy[self.sem]:
23            pass
24        else:
25            self._lock = None23            self._lock = None
26        return self._lock24        return self._lock
2725
28    @lock.setter26    @lock.setter
29    def lock(self, value):27    def lock(self, value):
n30        tmp = Lock.busy.items()n28        busy = Lock.busy.items()
31        for k, v in tmp:29        for k, v in busy:
32            if self.par == v:30            if self.par == v:
33                del Lock.busy[k]31                del Lock.busy[k]
34                break32                break
35        for k, v in Lock.sems.items():33        for k, v in Lock.sems.items():
36            if self.par in v:34            if self.par in v:
37                Lock.sems[k].remove(self.par)35                Lock.sems[k].remove(self.par)
38                break36                break
39        self.sem = value37        self.sem = value
40        try:38        try:
41            Lock.sems[value].append(self.par)39            Lock.sems[value].append(self.par)
42        except:40        except:
43            Lock.sems[value] = deque([self.par])41            Lock.sems[value] = deque([self.par])
4442
45    @lock.deleter43    @lock.deleter
46    def lock(self):44    def lock(self):
n47        tmp = Lock.busy.items()n45        busy = Lock.busy.items()
48        for k, v in tmp:46        for k, v in busy:
49            if self.par == v:47            if self.par == v:
50                del Lock.busy[k]48                del Lock.busy[k]
51                break49                break
52        for k, v in Lock.sems.items():50        for k, v in Lock.sems.items():
53            if v:51            if v:
54                if self.par == v[0]:52                if self.par == v[0]:
55                    Lock.sems[k].popleft()53                    Lock.sems[k].popleft()
5654
57    def __del__(self):55    def __del__(self):
n58        tmp = Lock.busy.items()n56        busy = Lock.busy.items()
59        for k, v in tmp:57        for k, v in busy:
60            if self.par == v:58            if self.par == v:
61                del Lock.busy[k]59                del Lock.busy[k]
62                break60                break
63        for k, v in Lock.sems.items():61        for k, v in Lock.sems.items():
64            if self.par in v:62            if self.par in v:
65                Lock.sems[k].remove(self.par)63                Lock.sems[k].remove(self.par)
6664
67    def locked(self):65    def locked(self):
t68 t
69        class locked_class(Lock):
70            pass
71        return locked_class66        return Lock
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op