Skip to content

Commit

Permalink
RDOIBT-1838 - CefGlue: Duplicate key reference (#109)
Browse files Browse the repository at this point in the history
Revert replacement of lock by Interlocked
  • Loading branch information
OS-filipecampos authored Sep 22, 2023
1 parent 5a52f85 commit 393f0fb
Show file tree
Hide file tree
Showing 60 changed files with 1,073 additions and 483 deletions.
30 changes: 20 additions & 10 deletions CefGlue.Interop.Gen/make_interop.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,8 @@ def make_handler_g_body(cls):
# result.append('private bool _disposed;')
result.append('')

result.append('protected object SyncRoot { get { return this; } }')
result.append('')

if schema.is_reversible(cls):
result.append('internal static %s FromNativeOrNull(%s* ptr)' % (csname, iname))
Expand Down Expand Up @@ -521,7 +523,7 @@ def make_handler_g_body(cls):
result.append('protected %s()' % csname)
result.append('{')
result.append(indent + '_self = %s.Alloc();' % iname)
result.append('');
result.append('')
for func in funcs:
result.append(indent + '%(delegate_slot)s = new %(iname)s.%(delegate_type)s(%(csn_name)s);' % func)
result.append(indent + '_self->%(field_name)s = Marshal.GetFunctionPointerForDelegate(%(delegate_slot)s);' % func)
Expand Down Expand Up @@ -558,35 +560,43 @@ def make_handler_g_body(cls):
# todo: verify self pointer in debug
result.append('private void add_ref(%s* self)' % iname)
result.append('{')
result.append(indent + 'if (Interlocked.Increment(ref _refct) == 1)')
result.append(indent + 'lock (SyncRoot)')
result.append(indent + '{')
result.append(indent + indent + 'lock (_roots) { _roots.Add((IntPtr)_self, this); }')
result.append(indent + indent + 'var result = ++_refct;')
result.append(indent + indent + 'if (result == 1)')
result.append(indent + indent + '{')
result.append(indent + indent + indent + 'lock (_roots) { _roots.Add((IntPtr)_self, this); }')
result.append(indent + indent + '}')
result.append(indent + '}')
result.append('}')
result.append('')

result.append('private int release(%s* self)' % iname)
result.append('{')
result.append(indent + 'if (Interlocked.Decrement(ref _refct) == 0)')
result.append(indent + 'lock (SyncRoot)')
result.append(indent + '{')
result.append(indent + indent + 'lock (_roots) { _roots.Remove((IntPtr)_self); }')
result.append(indent + indent + 'var result = --_refct;')
result.append(indent + indent + 'if (result == 0)')
result.append(indent + indent + '{')
result.append(indent + indent + indent + 'lock (_roots) { _roots.Remove((IntPtr)_self); }')
if schema.is_autodispose(cls):
result.append(indent + indent + 'Dispose();')
result.append(indent + indent + 'return 1;')
result.append(indent + indent + indent + 'Dispose();')
result.append(indent + indent + indent + 'return 1;')
result.append(indent + indent + '}')
result.append(indent + indent + 'return 0;')
result.append(indent + '}')
result.append(indent + 'return 0;')
result.append('}')
result.append('')

result.append('private int has_one_ref(%s* self)' % iname)
result.append('{')
result.append(indent + 'return _refct == 1 ? 1 : 0;')
result.append(indent + 'lock (SyncRoot) { return _refct == 1 ? 1 : 0; }')
result.append('}')
result.append('')

result.append('private int has_at_least_one_ref(%s* self)' % iname)
result.append('{')
result.append(indent + 'return _refct != 0 ? 1 : 0;')
result.append(indent + 'lock (SyncRoot) { return _refct != 0 ? 1 : 0; }')
result.append('}')
result.append('')

Expand Down
26 changes: 18 additions & 8 deletions CefGlue/Classes.g/CefAccessibilityHandler.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public abstract unsafe partial class CefAccessibilityHandler
private int _refct;
private cef_accessibility_handler_t* _self;

protected object SyncRoot { get { return this; } }

private cef_accessibility_handler_t.add_ref_delegate _ds0;
private cef_accessibility_handler_t.release_delegate _ds1;
private cef_accessibility_handler_t.has_one_ref_delegate _ds2;
Expand Down Expand Up @@ -59,30 +61,38 @@ protected virtual void Dispose(bool disposing)

private void add_ref(cef_accessibility_handler_t* self)
{
if (Interlocked.Increment(ref _refct) == 1)
lock (SyncRoot)
{
lock (_roots) { _roots.Add((IntPtr)_self, this); }
var result = ++_refct;
if (result == 1)
{
lock (_roots) { _roots.Add((IntPtr)_self, this); }
}
}
}

private int release(cef_accessibility_handler_t* self)
{
if (Interlocked.Decrement(ref _refct) == 0)
lock (SyncRoot)
{
lock (_roots) { _roots.Remove((IntPtr)_self); }
return 1;
var result = --_refct;
if (result == 0)
{
lock (_roots) { _roots.Remove((IntPtr)_self); }
return 1;
}
return 0;
}
return 0;
}

private int has_one_ref(cef_accessibility_handler_t* self)
{
return _refct == 1 ? 1 : 0;
lock (SyncRoot) { return _refct == 1 ? 1 : 0; }
}

private int has_at_least_one_ref(cef_accessibility_handler_t* self)
{
return _refct != 0 ? 1 : 0;
lock (SyncRoot) { return _refct != 0 ? 1 : 0; }
}

internal cef_accessibility_handler_t* ToNative()
Expand Down
26 changes: 18 additions & 8 deletions CefGlue/Classes.g/CefApp.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public abstract unsafe partial class CefApp
private int _refct;
private cef_app_t* _self;

protected object SyncRoot { get { return this; } }

private cef_app_t.add_ref_delegate _ds0;
private cef_app_t.release_delegate _ds1;
private cef_app_t.has_one_ref_delegate _ds2;
Expand Down Expand Up @@ -68,30 +70,38 @@ protected virtual void Dispose(bool disposing)

private void add_ref(cef_app_t* self)
{
if (Interlocked.Increment(ref _refct) == 1)
lock (SyncRoot)
{
lock (_roots) { _roots.Add((IntPtr)_self, this); }
var result = ++_refct;
if (result == 1)
{
lock (_roots) { _roots.Add((IntPtr)_self, this); }
}
}
}

private int release(cef_app_t* self)
{
if (Interlocked.Decrement(ref _refct) == 0)
lock (SyncRoot)
{
lock (_roots) { _roots.Remove((IntPtr)_self); }
return 1;
var result = --_refct;
if (result == 0)
{
lock (_roots) { _roots.Remove((IntPtr)_self); }
return 1;
}
return 0;
}
return 0;
}

private int has_one_ref(cef_app_t* self)
{
return _refct == 1 ? 1 : 0;
lock (SyncRoot) { return _refct == 1 ? 1 : 0; }
}

private int has_at_least_one_ref(cef_app_t* self)
{
return _refct != 0 ? 1 : 0;
lock (SyncRoot) { return _refct != 0 ? 1 : 0; }
}

internal cef_app_t* ToNative()
Expand Down
26 changes: 18 additions & 8 deletions CefGlue/Classes.g/CefAudioHandler.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public abstract unsafe partial class CefAudioHandler
private int _refct;
private cef_audio_handler_t* _self;

protected object SyncRoot { get { return this; } }

private cef_audio_handler_t.add_ref_delegate _ds0;
private cef_audio_handler_t.release_delegate _ds1;
private cef_audio_handler_t.has_one_ref_delegate _ds2;
Expand Down Expand Up @@ -68,30 +70,38 @@ protected virtual void Dispose(bool disposing)

private void add_ref(cef_audio_handler_t* self)
{
if (Interlocked.Increment(ref _refct) == 1)
lock (SyncRoot)
{
lock (_roots) { _roots.Add((IntPtr)_self, this); }
var result = ++_refct;
if (result == 1)
{
lock (_roots) { _roots.Add((IntPtr)_self, this); }
}
}
}

private int release(cef_audio_handler_t* self)
{
if (Interlocked.Decrement(ref _refct) == 0)
lock (SyncRoot)
{
lock (_roots) { _roots.Remove((IntPtr)_self); }
return 1;
var result = --_refct;
if (result == 0)
{
lock (_roots) { _roots.Remove((IntPtr)_self); }
return 1;
}
return 0;
}
return 0;
}

private int has_one_ref(cef_audio_handler_t* self)
{
return _refct == 1 ? 1 : 0;
lock (SyncRoot) { return _refct == 1 ? 1 : 0; }
}

private int has_at_least_one_ref(cef_audio_handler_t* self)
{
return _refct != 0 ? 1 : 0;
lock (SyncRoot) { return _refct != 0 ? 1 : 0; }
}

internal cef_audio_handler_t* ToNative()
Expand Down
26 changes: 18 additions & 8 deletions CefGlue/Classes.g/CefBrowserProcessHandler.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public abstract unsafe partial class CefBrowserProcessHandler
private int _refct;
private cef_browser_process_handler_t* _self;

protected object SyncRoot { get { return this; } }

private cef_browser_process_handler_t.add_ref_delegate _ds0;
private cef_browser_process_handler_t.release_delegate _ds1;
private cef_browser_process_handler_t.has_one_ref_delegate _ds2;
Expand Down Expand Up @@ -65,30 +67,38 @@ protected virtual void Dispose(bool disposing)

private void add_ref(cef_browser_process_handler_t* self)
{
if (Interlocked.Increment(ref _refct) == 1)
lock (SyncRoot)
{
lock (_roots) { _roots.Add((IntPtr)_self, this); }
var result = ++_refct;
if (result == 1)
{
lock (_roots) { _roots.Add((IntPtr)_self, this); }
}
}
}

private int release(cef_browser_process_handler_t* self)
{
if (Interlocked.Decrement(ref _refct) == 0)
lock (SyncRoot)
{
lock (_roots) { _roots.Remove((IntPtr)_self); }
return 1;
var result = --_refct;
if (result == 0)
{
lock (_roots) { _roots.Remove((IntPtr)_self); }
return 1;
}
return 0;
}
return 0;
}

private int has_one_ref(cef_browser_process_handler_t* self)
{
return _refct == 1 ? 1 : 0;
lock (SyncRoot) { return _refct == 1 ? 1 : 0; }
}

private int has_at_least_one_ref(cef_browser_process_handler_t* self)
{
return _refct != 0 ? 1 : 0;
lock (SyncRoot) { return _refct != 0 ? 1 : 0; }
}

internal cef_browser_process_handler_t* ToNative()
Expand Down
26 changes: 18 additions & 8 deletions CefGlue/Classes.g/CefClient.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public abstract unsafe partial class CefClient
private int _refct;
private cef_client_t* _self;

protected object SyncRoot { get { return this; } }

internal static CefClient FromNativeOrNull(cef_client_t* ptr)
{
CefClient value = null;
Expand Down Expand Up @@ -130,30 +132,38 @@ protected virtual void Dispose(bool disposing)

private void add_ref(cef_client_t* self)
{
if (Interlocked.Increment(ref _refct) == 1)
lock (SyncRoot)
{
lock (_roots) { _roots.Add((IntPtr)_self, this); }
var result = ++_refct;
if (result == 1)
{
lock (_roots) { _roots.Add((IntPtr)_self, this); }
}
}
}

private int release(cef_client_t* self)
{
if (Interlocked.Decrement(ref _refct) == 0)
lock (SyncRoot)
{
lock (_roots) { _roots.Remove((IntPtr)_self); }
return 1;
var result = --_refct;
if (result == 0)
{
lock (_roots) { _roots.Remove((IntPtr)_self); }
return 1;
}
return 0;
}
return 0;
}

private int has_one_ref(cef_client_t* self)
{
return _refct == 1 ? 1 : 0;
lock (SyncRoot) { return _refct == 1 ? 1 : 0; }
}

private int has_at_least_one_ref(cef_client_t* self)
{
return _refct != 0 ? 1 : 0;
lock (SyncRoot) { return _refct != 0 ? 1 : 0; }
}

internal cef_client_t* ToNative()
Expand Down
Loading

0 comments on commit 393f0fb

Please sign in to comment.