|
16 | 16 | from devito.tools import (Pickable, as_tuple, dtype_to_ctype, |
17 | 17 | frozendict, memoized_meth, sympy_mutex, CustomDtype) |
18 | 18 | from devito.types.args import ArgProvider |
19 | | -from devito.types.caching import Cached, Uncached |
| 19 | +from devito.types.caching import CacheManager, Cached, Uncached |
20 | 20 | from devito.types.lazy import Evaluable |
21 | 21 | from devito.types.utils import DimensionTuple |
22 | 22 |
|
@@ -559,24 +559,31 @@ def _cache_key(cls, *args, **kwargs): |
559 | 559 | def __new__(cls, *args, **kwargs): |
560 | 560 | assumptions, kwargs = cls._filter_assumptions(**kwargs) |
561 | 561 | key = cls._cache_key(*args, **{**assumptions, **kwargs}) |
562 | | - obj = cls._cache_get(key) |
563 | 562 |
|
| 563 | + # Initial cache lookup (not locked) |
| 564 | + obj = cls._cache_get(key) |
564 | 565 | if obj is not None: |
565 | 566 | return obj |
566 | 567 |
|
567 | | - # Not in cache. Create a new Symbol via sympy.Symbol |
568 | | - args = list(args) |
569 | | - name = kwargs.pop('name', None) or args.pop(0) |
570 | | - newobj = cls.__xnew__(cls, name, **assumptions) |
| 568 | + # Lock against the symbol cache and double-check the cache |
| 569 | + with CacheManager.lock(): |
| 570 | + obj = cls._cache_get(key) |
| 571 | + if obj is not None: |
| 572 | + return obj |
571 | 573 |
|
572 | | - # Initialization |
573 | | - newobj._dtype = cls.__dtype_setup__(**kwargs) |
574 | | - newobj.__init_finalize__(name, *args, **kwargs) |
| 574 | + # Not in cache. Create a new Symbol via sympy.Symbol |
| 575 | + args = list(args) |
| 576 | + name = kwargs.pop('name', None) or args.pop(0) |
| 577 | + newobj = cls.__xnew__(cls, name, **assumptions) |
575 | 578 |
|
576 | | - # Store new instance in symbol cache |
577 | | - Cached.__init__(newobj, key) |
| 579 | + # Initialization |
| 580 | + newobj._dtype = cls.__dtype_setup__(**kwargs) |
| 581 | + newobj.__init_finalize__(name, *args, **kwargs) |
578 | 582 |
|
579 | | - return newobj |
| 583 | + # Store new instance in symbol cache |
| 584 | + Cached.__init__(newobj, key) |
| 585 | + |
| 586 | + return newobj |
580 | 587 |
|
581 | 588 | __hash__ = Cached.__hash__ |
582 | 589 |
|
|
0 commit comments