Skip to content

Commit 10a65ca

Browse files
Revert ref counting implementation and CefObjectTracker (see issue #88)
1 parent 324ad7a commit 10a65ca

File tree

4 files changed

+46
-139
lines changed

4 files changed

+46
-139
lines changed

CefGlue.Interop.Gen/make_interop.py

+22-18
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,6 @@ def make_wrapper_g_file(cls):
331331
body.append('using System.Diagnostics;')
332332
body.append('using System.Runtime.InteropServices;')
333333
# body.append('using System.Diagnostics.CodeAnalysis;')
334-
body.append('using System.Threading;')
335334
body.append('using %s;' % schema.interop_namespace)
336335
body.append('')
337336

@@ -425,7 +424,6 @@ def make_proxy_g_body(cls):
425424
# private fields
426425
if isImpl:
427426
result.append(privateOrProtected + ' %s* _self;' % iname)
428-
result.append(privateOrProtected + ' int _disposed = 0;')
429427
result.append('')
430428

431429
# ctor
@@ -434,8 +432,6 @@ def make_proxy_g_body(cls):
434432
result.append('{')
435433
result.append(indent + 'if (ptr == null) throw new ArgumentNullException("ptr");')
436434
result.append(indent + '_self = ptr;')
437-
if isRefCountedImpl:
438-
result.append(indent + 'CefObjectTracker.Track(this);')
439435
#
440436
# todo: diagnostics code: Interlocked.Increment(ref _objCt);
441437
#
@@ -453,7 +449,7 @@ def make_proxy_g_body(cls):
453449
# disposable
454450
result.append('~%s()' % csname)
455451
result.append('{')
456-
result.append(indent + 'if (Interlocked.CompareExchange(ref _disposed, 1, 0) == 0)')
452+
result.append(indent + 'if (_self != null)')
457453
result.append(indent + '{')
458454
result.append(indent + indent + 'Release();')
459455
result.append(indent + indent + '_self = null;')
@@ -463,12 +459,11 @@ def make_proxy_g_body(cls):
463459

464460
result.append('public void Dispose()')
465461
result.append('{')
466-
result.append(indent + 'if (Interlocked.CompareExchange(ref _disposed, 1, 0) == 0)')
462+
result.append(indent + 'if (_self != null)')
467463
result.append(indent + '{')
468464
result.append(indent + indent + 'Release();')
469465
result.append(indent + indent + '_self = null;')
470466
result.append(indent + '}')
471-
result.append(indent + 'CefObjectTracker.Untrack(this);')
472467
result.append(indent + 'GC.SuppressFinalize(this);')
473468
result.append('}')
474469
result.append('')
@@ -538,6 +533,9 @@ def make_handler_g_body(cls):
538533
# result.append('private bool _disposed;')
539534
result.append('')
540535

536+
result.append('protected object SyncRoot { get { return this; } }')
537+
result.append('')
538+
541539
if schema.is_reversible(cls):
542540
result.append('internal static %s FromNativeOrNull(%s* ptr)' % (csname, iname))
543541
result.append('{')
@@ -546,8 +544,6 @@ def make_handler_g_body(cls):
546544
result.append(indent + 'lock (_roots)')
547545
result.append(indent + '{')
548546
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);')
551547
result.append(indent + '}')
552548
result.append(indent + 'return found ? value : null;')
553549
result.append('}')
@@ -606,35 +602,43 @@ def make_handler_g_body(cls):
606602
# todo: verify self pointer in debug
607603
result.append('private void add_ref(%s* self)' % iname)
608604
result.append('{')
609-
result.append(indent + 'if (Interlocked.Increment(ref _refct) == 1)')
605+
result.append(indent + 'lock (SyncRoot)')
610606
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 + '}')
612612
result.append(indent + '}')
613613
result.append('}')
614614
result.append('')
615615

616616
result.append('private int release(%s* self)' % iname)
617617
result.append('{')
618-
result.append(indent + 'if (Interlocked.Decrement(ref _refct) == 0)')
618+
result.append(indent + 'lock (SyncRoot)')
619619
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); }')
621624
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;')
624629
result.append(indent + '}')
625-
result.append(indent + 'return 0;')
626630
result.append('}')
627631
result.append('')
628632

629633
result.append('private int has_one_ref(%s* self)' % iname)
630634
result.append('{')
631-
result.append(indent + 'return _refct == 1 ? 1 : 0;')
635+
result.append(indent + 'lock (SyncRoot) { return _refct == 1 ? 1 : 0; }')
632636
result.append('}')
633637
result.append('')
634638

635639
result.append('private int has_at_least_one_ref(%s* self)' % iname)
636640
result.append('{')
637-
result.append(indent + 'return _refct != 0 ? 1 : 0;')
641+
result.append(indent + 'lock (SyncRoot) { return _refct != 0 ? 1 : 0; }')
638642
result.append('}')
639643
result.append('')
640644

CefGlue/CefGlue.csproj

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
</PropertyGroup>
1515

1616
<ItemGroup>
17-
<Compile Include="CefObjectTracker.cs" />
1817
<Compile Include="CefRuntime.cs" />
1918
<Compile Include="CefRuntimePlatform.cs" />
2019
<Compile Include="Classes.Handlers\CefAccessibilityHandler.cs" />

CefGlue/CefObjectTracker.cs

-90
This file was deleted.

CefGlue/Classes.Handlers/CefV8Handler.cs

+24-30
Original file line numberDiff line numberDiff line change
@@ -13,50 +13,44 @@
1313
/// </summary>
1414
public abstract unsafe partial class CefV8Handler
1515
{
16-
private static readonly CefV8Value[] emtpyArgs = new CefV8Value[0];
16+
private static readonly CefV8Value[] s_emtpyArgs = new CefV8Value[0];
1717

1818
private int execute(cef_v8handler_t* self, cef_string_t* name, cef_v8value_t* @object, UIntPtr argumentsCount, cef_v8value_t** arguments, cef_v8value_t** retval, cef_string_t* exception)
1919
{
2020
CheckSelf(self);
2121

22-
using (CefObjectTracker.StartTracking())
22+
var m_name = cef_string_t.ToString(name);
23+
var m_obj = CefV8Value.FromNative(@object);
24+
var argc = (int)argumentsCount;
25+
CefV8Value[] m_arguments;
26+
if (argc == 0) { m_arguments = s_emtpyArgs; }
27+
else
2328
{
24-
CheckSelf(self);
25-
26-
var m_name = cef_string_t.ToString(name);
27-
var m_obj = CefV8Value.FromNative(@object);
28-
var argc = (int)argumentsCount;
29-
CefV8Value[] m_arguments;
30-
if (argc == 0) { m_arguments = emtpyArgs; }
31-
else
29+
m_arguments = new CefV8Value[argc];
30+
for (var i = 0; i < argc; i++)
3231
{
33-
m_arguments = new CefV8Value[argc];
34-
for (var i = 0; i < argc; i++)
35-
{
36-
m_arguments[i] = CefV8Value.FromNative(arguments[i]);
37-
}
32+
m_arguments[i] = CefV8Value.FromNative(arguments[i]);
3833
}
34+
}
3935

36+
CefV8Value m_returnValue;
37+
string m_exception;
4038

41-
CefV8Value m_returnValue;
42-
string m_exception;
43-
44-
var handled = Execute(m_name, m_obj, m_arguments, out m_returnValue, out m_exception);
39+
var handled = Execute(m_name, m_obj, m_arguments, out m_returnValue, out m_exception);
4540

46-
if (handled)
41+
if (handled)
42+
{
43+
if (m_exception != null)
4744
{
48-
if (m_exception != null)
49-
{
50-
cef_string_t.Copy(m_exception, exception);
51-
}
52-
else if (m_returnValue != null)
53-
{
54-
*retval = m_returnValue.ToNative();
55-
}
45+
cef_string_t.Copy(m_exception, exception);
46+
}
47+
else if (m_returnValue != null)
48+
{
49+
*retval = m_returnValue.ToNative();
5650
}
57-
58-
return handled ? 1 : 0;
5951
}
52+
53+
return handled ? 1 : 0;
6054
}
6155

6256
/// <summary>

0 commit comments

Comments
 (0)