@@ -228,8 +228,7 @@ class Var(RefBase):
228
228
"_is_bound" ,
229
229
"_tl" ,
230
230
"_meta" ,
231
- "_rlock" ,
232
- "_wlock" ,
231
+ "_lock" ,
233
232
"_watches" ,
234
233
"_validator" ,
235
234
)
@@ -248,9 +247,7 @@ def __init__(
248
247
self ._is_bound = False
249
248
self ._tl = None
250
249
self ._meta = meta
251
- lock = RWLockFair ()
252
- self ._rlock = lock .gen_rlock ()
253
- self ._wlock = lock .gen_wlock ()
250
+ self ._lock = RWLockFair ()
254
251
self ._watches = lmap .PersistentMap .empty ()
255
252
self ._validator = None
256
253
@@ -281,7 +278,7 @@ def dynamic(self) -> bool:
281
278
return self ._dynamic
282
279
283
280
def set_dynamic (self , dynamic : bool ) -> None :
284
- with self ._wlock :
281
+ with self ._lock . gen_wlock () :
285
282
if dynamic == self ._dynamic :
286
283
return
287
284
@@ -309,18 +306,18 @@ def _set_root(self, newval) -> None:
309
306
310
307
@property
311
308
def root (self ):
312
- with self ._rlock :
309
+ with self ._lock . gen_rlock () :
313
310
return self ._root
314
311
315
312
def bind_root (self , val ) -> None :
316
313
"""Atomically update the root binding of this Var to val."""
317
- with self ._wlock :
314
+ with self ._lock . gen_wlock () :
318
315
self ._set_root (val )
319
316
320
317
def alter_root (self , f , * args ) -> None :
321
318
"""Atomically alter the root binding of this Var to the result of calling
322
319
f with the existing root value and any additional arguments."""
323
- with self ._wlock :
320
+ with self ._lock . gen_wlock () :
324
321
self ._set_root (f (self ._root , * args ))
325
322
326
323
def push_bindings (self , val ):
@@ -347,7 +344,7 @@ def value(self):
347
344
348
345
For non-dynamic Vars, this will just be the root. For dynamic Vars, this will
349
346
be any thread-local binding if one is defined. Otherwise, the root value."""
350
- with self ._rlock :
347
+ with self ._lock . gen_rlock () :
351
348
if self ._dynamic :
352
349
assert self ._tl is not None
353
350
if len (self ._tl .bindings ) > 0 :
@@ -359,7 +356,7 @@ def set_value(self, v) -> None:
359
356
360
357
If the Var is not dynamic, this is equivalent to binding the root value. If the
361
358
Var is dynamic, this will set the thread-local bindings for the Var."""
362
- with self ._wlock :
359
+ with self ._lock . gen_wlock () :
363
360
if self ._dynamic :
364
361
assert self ._tl is not None
365
362
self ._validate (v )
@@ -551,8 +548,7 @@ class Namespace(ReferenceBase):
551
548
"_name" ,
552
549
"_module" ,
553
550
"_meta" ,
554
- "_rlock" ,
555
- "_wlock" ,
551
+ "_lock" ,
556
552
"_interns" ,
557
553
"_refers" ,
558
554
"_aliases" ,
@@ -567,9 +563,7 @@ def __init__(
567
563
self ._module = Maybe (module ).or_else (lambda : _new_module (name .as_python_sym ()))
568
564
569
565
self ._meta : Optional [IPersistentMap ] = None
570
- lock = RWLockFair ()
571
- self ._rlock = lock .gen_rlock ()
572
- self ._wlock = lock .gen_wlock ()
566
+ self ._lock = RWLockFair ()
573
567
574
568
self ._aliases : NamespaceMap = lmap .PersistentMap .empty ()
575
569
self ._imports : ModuleMap = lmap .map (
@@ -605,36 +599,36 @@ def module(self, m: BasilispModule):
605
599
def aliases (self ) -> NamespaceMap :
606
600
"""A mapping between a symbolic alias and another Namespace. The
607
601
fully qualified name of a namespace is also an alias for itself."""
608
- with self ._rlock :
602
+ with self ._lock . gen_rlock () :
609
603
return self ._aliases
610
604
611
605
@property
612
606
def imports (self ) -> ModuleMap :
613
607
"""A mapping of names to Python modules imported into the current
614
608
namespace."""
615
- with self ._rlock :
609
+ with self ._lock . gen_rlock () :
616
610
return self ._imports
617
611
618
612
@property
619
613
def import_aliases (self ) -> AliasMap :
620
614
"""A mapping of a symbolic alias and a Python module name."""
621
- with self ._rlock :
615
+ with self ._lock . gen_rlock () :
622
616
return self ._import_aliases
623
617
624
618
@property
625
619
def interns (self ) -> VarMap :
626
620
"""A mapping between a symbolic name and a Var. The Var may point to
627
621
code, data, or nothing, if it is unbound. Vars in `interns` are
628
622
interned in _this_ namespace."""
629
- with self ._rlock :
623
+ with self ._lock . gen_rlock () :
630
624
return self ._interns
631
625
632
626
@property
633
627
def refers (self ) -> VarMap :
634
628
"""A mapping between a symbolic name and a Var. Vars in refers are
635
629
interned in another namespace and are only referred to without an
636
630
alias in this namespace."""
637
- with self ._rlock :
631
+ with self ._lock . gen_rlock () :
638
632
return self ._refers
639
633
640
634
def __repr__ (self ):
@@ -665,41 +659,41 @@ def require(self, ns_name: str, *aliases: sym.Symbol) -> BasilispModule:
665
659
666
660
def add_alias (self , namespace : "Namespace" , * aliases : sym .Symbol ) -> None :
667
661
"""Add Symbol aliases for the given Namespace."""
668
- with self ._wlock :
662
+ with self ._lock . gen_wlock () :
669
663
new_m = self ._aliases
670
664
for alias in aliases :
671
665
new_m = new_m .assoc (alias , namespace )
672
666
self ._aliases = new_m
673
667
674
668
def get_alias (self , alias : sym .Symbol ) -> "Optional[Namespace]" :
675
669
"""Get the Namespace aliased by Symbol or None if it does not exist."""
676
- with self ._rlock :
670
+ with self ._lock . gen_rlock () :
677
671
return self ._aliases .val_at (alias , None )
678
672
679
673
def remove_alias (self , alias : sym .Symbol ) -> None :
680
674
"""Remove the Namespace aliased by Symbol. Return None."""
681
- with self ._wlock :
675
+ with self ._lock . gen_wlock () :
682
676
self ._aliases = self ._aliases .dissoc (alias )
683
677
684
678
def intern (self , sym : sym .Symbol , var : Var , force : bool = False ) -> Var :
685
679
"""Intern the Var given in this namespace mapped by the given Symbol.
686
680
If the Symbol already maps to a Var, this method _will not overwrite_
687
681
the existing Var mapping unless the force keyword argument is given
688
682
and is True."""
689
- with self ._wlock :
683
+ with self ._lock . gen_wlock () :
690
684
old_var = self ._interns .val_at (sym , None )
691
685
if old_var is None or force :
692
686
self ._interns = self ._interns .assoc (sym , var )
693
687
return self ._interns .val_at (sym )
694
688
695
689
def unmap (self , sym : sym .Symbol ) -> None :
696
- with self ._wlock :
690
+ with self ._lock . gen_wlock () :
697
691
self ._interns = self ._interns .dissoc (sym )
698
692
699
693
def find (self , sym : sym .Symbol ) -> Optional [Var ]:
700
694
"""Find Vars mapped by the given Symbol input or None if no Vars are
701
695
mapped by that Symbol."""
702
- with self ._rlock :
696
+ with self ._lock . gen_rlock () :
703
697
v = self ._interns .val_at (sym , None )
704
698
if v is None :
705
699
return self ._refers .val_at (sym , None )
@@ -708,7 +702,7 @@ def find(self, sym: sym.Symbol) -> Optional[Var]:
708
702
def add_import (self , sym : sym .Symbol , module : Module , * aliases : sym .Symbol ) -> None :
709
703
"""Add the Symbol as an imported Symbol in this Namespace. If aliases are given,
710
704
the aliases will be applied to the"""
711
- with self ._wlock :
705
+ with self ._lock . gen_wlock () :
712
706
self ._imports = self ._imports .assoc (sym , module )
713
707
if aliases :
714
708
m = self ._import_aliases
@@ -722,7 +716,7 @@ def get_import(self, sym: sym.Symbol) -> Optional[BasilispModule]:
722
716
723
717
First try to resolve a module directly with the given name. If no module
724
718
can be resolved, attempt to resolve the module using import aliases."""
725
- with self ._rlock :
719
+ with self ._lock . gen_rlock () :
726
720
mod = self ._imports .val_at (sym , None )
727
721
if mod is None :
728
722
alias = self ._import_aliases .get (sym , None )
@@ -734,17 +728,17 @@ def get_import(self, sym: sym.Symbol) -> Optional[BasilispModule]:
734
728
def add_refer (self , sym : sym .Symbol , var : Var ) -> None :
735
729
"""Refer var in this namespace under the name sym."""
736
730
if not var .is_private :
737
- with self ._wlock :
731
+ with self ._lock . gen_wlock () :
738
732
self ._refers = self ._refers .assoc (sym , var )
739
733
740
734
def get_refer (self , sym : sym .Symbol ) -> Optional [Var ]:
741
735
"""Get the Var referred by Symbol or None if it does not exist."""
742
- with self ._rlock :
736
+ with self ._lock . gen_rlock () :
743
737
return self ._refers .val_at (sym , None )
744
738
745
739
def refer_all (self , other_ns : "Namespace" ) -> None :
746
740
"""Refer all the Vars in the other namespace."""
747
- with self ._wlock :
741
+ with self ._lock . gen_wlock () :
748
742
final_refers = self ._refers
749
743
for s , var in other_ns .interns .items ():
750
744
if not var .is_private :
0 commit comments