@@ -331,7 +331,6 @@ def make_wrapper_g_file(cls):
331
331
body .append ('using System.Diagnostics;' )
332
332
body .append ('using System.Runtime.InteropServices;' )
333
333
# body.append('using System.Diagnostics.CodeAnalysis;')
334
- body .append ('using System.Threading;' )
335
334
body .append ('using %s;' % schema .interop_namespace )
336
335
body .append ('' )
337
336
@@ -425,7 +424,6 @@ def make_proxy_g_body(cls):
425
424
# private fields
426
425
if isImpl :
427
426
result .append (privateOrProtected + ' %s* _self;' % iname )
428
- result .append (privateOrProtected + ' int _disposed = 0;' )
429
427
result .append ('' )
430
428
431
429
# ctor
@@ -434,8 +432,6 @@ def make_proxy_g_body(cls):
434
432
result .append ('{' )
435
433
result .append (indent + 'if (ptr == null) throw new ArgumentNullException("ptr");' )
436
434
result .append (indent + '_self = ptr;' )
437
- if isRefCountedImpl :
438
- result .append (indent + 'CefObjectTracker.Track(this);' )
439
435
#
440
436
# todo: diagnostics code: Interlocked.Increment(ref _objCt);
441
437
#
@@ -453,7 +449,7 @@ def make_proxy_g_body(cls):
453
449
# disposable
454
450
result .append ('~%s()' % csname )
455
451
result .append ('{' )
456
- result .append (indent + 'if (Interlocked.CompareExchange(ref _disposed, 1, 0) == 0 )' )
452
+ result .append (indent + 'if (_self != null )' )
457
453
result .append (indent + '{' )
458
454
result .append (indent + indent + 'Release();' )
459
455
result .append (indent + indent + '_self = null;' )
@@ -463,12 +459,11 @@ def make_proxy_g_body(cls):
463
459
464
460
result .append ('public void Dispose()' )
465
461
result .append ('{' )
466
- result .append (indent + 'if (Interlocked.CompareExchange(ref _disposed, 1, 0) == 0 )' )
462
+ result .append (indent + 'if (_self != null )' )
467
463
result .append (indent + '{' )
468
464
result .append (indent + indent + 'Release();' )
469
465
result .append (indent + indent + '_self = null;' )
470
466
result .append (indent + '}' )
471
- result .append (indent + 'CefObjectTracker.Untrack(this);' )
472
467
result .append (indent + 'GC.SuppressFinalize(this);' )
473
468
result .append ('}' )
474
469
result .append ('' )
@@ -538,6 +533,9 @@ def make_handler_g_body(cls):
538
533
# result.append('private bool _disposed;')
539
534
result .append ('' )
540
535
536
+ result .append ('protected object SyncRoot { get { return this; } }' )
537
+ result .append ('' )
538
+
541
539
if schema .is_reversible (cls ):
542
540
result .append ('internal static %s FromNativeOrNull(%s* ptr)' % (csname , iname ))
543
541
result .append ('{' )
@@ -546,8 +544,6 @@ def make_handler_g_body(cls):
546
544
result .append (indent + 'lock (_roots)' )
547
545
result .append (indent + '{' )
548
546
result .append (indent + indent + 'found = _roots.TryGetValue((IntPtr)ptr, out value);' )
549
- result .append (indent + indent + '// as we\' re getting the ref from the outside, it\' s our responsibility to decrement it' )
550
- result .append (indent + indent + 'value.release(ptr);' )
551
547
result .append (indent + '}' )
552
548
result .append (indent + 'return found ? value : null;' )
553
549
result .append ('}' )
@@ -606,35 +602,43 @@ def make_handler_g_body(cls):
606
602
# todo: verify self pointer in debug
607
603
result .append ('private void add_ref(%s* self)' % iname )
608
604
result .append ('{' )
609
- result .append (indent + 'if (Interlocked.Increment(ref _refct) == 1 )' )
605
+ result .append (indent + 'lock (SyncRoot )' )
610
606
result .append (indent + '{' )
611
- result .append (indent + indent + 'lock (_roots) { _roots.Add((IntPtr)_self, this); }' )
607
+ result .append (indent + indent + 'var result = ++_refct;' )
608
+ result .append (indent + indent + 'if (result == 1)' )
609
+ result .append (indent + indent + '{' )
610
+ result .append (indent + indent + indent + 'lock (_roots) { _roots.Add((IntPtr)_self, this); }' )
611
+ result .append (indent + indent + '}' )
612
612
result .append (indent + '}' )
613
613
result .append ('}' )
614
614
result .append ('' )
615
615
616
616
result .append ('private int release(%s* self)' % iname )
617
617
result .append ('{' )
618
- result .append (indent + 'if (Interlocked.Decrement(ref _refct) == 0 )' )
618
+ result .append (indent + 'lock (SyncRoot )' )
619
619
result .append (indent + '{' )
620
- result .append (indent + indent + 'lock (_roots) { _roots.Remove((IntPtr)_self); }' )
620
+ result .append (indent + indent + 'var result = --_refct;' )
621
+ result .append (indent + indent + 'if (result == 0)' )
622
+ result .append (indent + indent + '{' )
623
+ result .append (indent + indent + indent + 'lock (_roots) { _roots.Remove((IntPtr)_self); }' )
621
624
if schema .is_autodispose (cls ):
622
- result .append (indent + indent + 'Dispose();' )
623
- result .append (indent + indent + 'return 1;' )
625
+ result .append (indent + indent + indent + 'Dispose();' )
626
+ result .append (indent + indent + indent + 'return 1;' )
627
+ result .append (indent + indent + '}' )
628
+ result .append (indent + indent + 'return 0;' )
624
629
result .append (indent + '}' )
625
- result .append (indent + 'return 0;' )
626
630
result .append ('}' )
627
631
result .append ('' )
628
632
629
633
result .append ('private int has_one_ref(%s* self)' % iname )
630
634
result .append ('{' )
631
- result .append (indent + 'return _refct == 1 ? 1 : 0;' )
635
+ result .append (indent + 'lock (SyncRoot) { return _refct == 1 ? 1 : 0; } ' )
632
636
result .append ('}' )
633
637
result .append ('' )
634
638
635
639
result .append ('private int has_at_least_one_ref(%s* self)' % iname )
636
640
result .append ('{' )
637
- result .append (indent + 'return _refct != 0 ? 1 : 0;' )
641
+ result .append (indent + 'lock (SyncRoot) { return _refct != 0 ? 1 : 0; } ' )
638
642
result .append ('}' )
639
643
result .append ('' )
640
644
0 commit comments