Могиленец Виктор, Севастополь SemClass 15323
Майстер Михаил (Севастополь) SemClass 15005
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:
23            self._lock = None25            self._lock = None
24        return self._lock26        return self._lock
2527
26    @lock.setter28    @lock.setter
27    def lock(self, value):29    def lock(self, value):
n28        busy = Lock.busy.items()n30        tmp = Lock.busy.items()
29        for k, v in busy:31        for k, v in tmp:
30            if self.par == v:32            if self.par == v:
31                del Lock.busy[k]33                del Lock.busy[k]
32                break34                break
33        for k, v in Lock.sems.items():35        for k, v in Lock.sems.items():
34            if self.par in v:36            if self.par in v:
35                Lock.sems[k].remove(self.par)37                Lock.sems[k].remove(self.par)
36                break38                break
37        self.sem = value39        self.sem = value
38        try:40        try:
39            Lock.sems[value].append(self.par)41            Lock.sems[value].append(self.par)
40        except:42        except:
41            Lock.sems[value] = deque([self.par])43            Lock.sems[value] = deque([self.par])
4244
43    @lock.deleter45    @lock.deleter
44    def lock(self):46    def lock(self):
n45        busy = Lock.busy.items()n47        tmp = Lock.busy.items()
46        for k, v in busy:48        for k, v in tmp:
47            if self.par == v:49            if self.par == v:
48                del Lock.busy[k]50                del Lock.busy[k]
49                break51                break
50        for k, v in Lock.sems.items():52        for k, v in Lock.sems.items():
51            if v:53            if v:
52                if self.par == v[0]:54                if self.par == v[0]:
53                    Lock.sems[k].popleft()55                    Lock.sems[k].popleft()
5456
55    def __del__(self):57    def __del__(self):
n56        busy = Lock.busy.items()n58        tmp = Lock.busy.items()
57        for k, v in busy:59        for k, v in tmp:
58            if self.par == v:60            if self.par == v:
59                del Lock.busy[k]61                del Lock.busy[k]
60                break62                break
61        for k, v in Lock.sems.items():63        for k, v in Lock.sems.items():
62            if self.par in v:64            if self.par in v:
63                Lock.sems[k].remove(self.par)65                Lock.sems[k].remove(self.par)
6466
65    def locked(self):67    def locked(self):
tt68 
69        class locked_class(Lock):
70            pass
66        return Lock71        return locked_class
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op