11try :
2- from threading import Lock , get_native_id # type: ignore[attr-defined, unused-ignore]
2+ from threading import ( # type: ignore[attr-defined, unused-ignore]
3+ Lock ,
4+ get_native_id ,
5+ )
36except ImportError : # pragma: no cover
4- from threading import Lock , get_ident as get_native_id # get_native_id is available only since python 3.8
7+ from threading import Lock # get_native_id is available only since python 3.8
8+ from threading import get_ident as get_native_id
59
610from collections import deque
7- from typing import Type , Deque , Dict , Optional
811from types import TracebackType
12+ from typing import Deque , Dict , Optional , Type
913
1014from locklib .locks .smart_lock .graph import LocksGraph
1115
12-
1316graph = LocksGraph ()
1417
1518class SmartLock :
@@ -26,40 +29,38 @@ def __exit__(self, exception_type: Optional[Type[BaseException]], exception_valu
2629 self .release ()
2730
2831 def acquire (self ) -> None :
29- id = get_native_id ()
32+ thread_id = get_native_id ()
3033 previous_element_lock = None
3134
32- with self .lock :
33- with self .graph .lock :
34- if not self .deque :
35- self .deque .appendleft (id )
36- self .local_locks [id ] = Lock ()
37- self .local_locks [id ].acquire ()
38- else :
39- previous_element = self .deque [0 ]
40- self .graph .add_link (id , previous_element )
41- self .deque .appendleft (id )
42- self .local_locks [id ] = Lock ()
43- self .local_locks [id ].acquire ()
44- previous_element_lock = self .local_locks [previous_element ]
35+ with self .lock , self .graph .lock :
36+ if not self .deque :
37+ self .deque .appendleft (thread_id )
38+ self .local_locks [thread_id ] = Lock ()
39+ self .local_locks [thread_id ].acquire ()
40+ else :
41+ previous_element = self .deque [0 ]
42+ self .graph .add_link (thread_id , previous_element )
43+ self .deque .appendleft (thread_id )
44+ self .local_locks [thread_id ] = Lock ()
45+ self .local_locks [thread_id ].acquire ()
46+ previous_element_lock = self .local_locks [previous_element ]
4547
4648 if previous_element_lock is not None :
4749 previous_element_lock .acquire ()
4850
4951 def release (self ) -> None :
50- id = get_native_id ()
52+ thread_id = get_native_id ()
5153
52- with self .lock :
53- with self .graph .lock :
54- if id not in self .local_locks :
55- raise RuntimeError ('Release unlocked lock.' )
54+ with self .lock , self .graph .lock :
55+ if thread_id not in self .local_locks :
56+ raise RuntimeError ('Release unlocked lock.' )
5657
57- self .deque .pop ()
58- lock = self .local_locks [id ]
59- del self .local_locks [id ]
58+ self .deque .pop ()
59+ lock = self .local_locks [thread_id ]
60+ del self .local_locks [thread_id ]
6061
61- if len (self .deque ) != 0 :
62- next_element = self .deque [- 1 ]
63- self .graph .delete_link (next_element , id )
62+ if len (self .deque ) != 0 :
63+ next_element = self .deque [- 1 ]
64+ self .graph .delete_link (next_element , thread_id )
6465
65- lock .release ()
66+ lock .release ()
0 commit comments