Cleanup of sourceproxy Dictionary and its usages

This commit is contained in:
Midhun Mohan 2020-02-18 12:57:21 +00:00 committed by Harry
parent 6af04a1058
commit cd47acba85
3 changed files with 21 additions and 95 deletions

View file

@ -70,8 +70,8 @@ namespace GLib {
}
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate uint d_g_idle_add(IdleHandlerInternal d, IntPtr data);
static d_g_idle_add g_idle_add = FuncLoader.LoadFunction<d_g_idle_add>(FuncLoader.GetProcAddress(GLibrary.Load(Library.GLib), "g_idle_add"));
delegate uint d_g_idle_add_full(int priority, IdleHandlerInternal d, IntPtr data, DestroyNotify notify);
static d_g_idle_add_full g_idle_add_full = FuncLoader.LoadFunction<d_g_idle_add_full>(FuncLoader.GetProcAddress(GLibrary.Load(Library.GLib), "g_idle_add_full"));
public static uint Add (IdleHandler hndlr)
{
@ -81,22 +81,6 @@ namespace GLib {
var gch = GCHandle.Alloc(p);
var userData = GCHandle.ToIntPtr(gch);
p.ID = g_idle_add_full (0, (IdleHandlerInternal) p.proxy_handler, userData, DestroyHelper.NotifyHandler);
Source.AddSourceHandler (p.ID, p);
}
return p.ID;
}
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate uint d_g_idle_add_full(int priority, IdleHandlerInternal d, IntPtr data, DestroyNotify notify);
static d_g_idle_add_full g_idle_add_full = FuncLoader.LoadFunction<d_g_idle_add_full>(FuncLoader.GetProcAddress(GLibrary.Load(Library.GLib), "g_idle_add_full"));
public static uint Add (IdleHandler hndlr, Priority priority)
{
IdleProxy p = new IdleProxy (hndlr);
lock (p)
{
p.ID = g_idle_add_full ((int)priority, (IdleHandlerInternal)p.proxy_handler, IntPtr.Zero, null);
Source.AddSourceHandler (p.ID, p);
}
return p.ID;
@ -106,11 +90,6 @@ namespace GLib {
{
Source.Remove (id);
}
public static bool Remove (IdleHandler hndlr)
{
return Source.RemoveSourceHandler (hndlr);
}
}
}

View file

@ -62,7 +62,6 @@ namespace GLib {
internal void Remove ()
{
Source.RemoveSourceHandler (ID);
real_handler = null;
proxy_handler = null;
}
@ -70,8 +69,6 @@ namespace GLib {
public partial class Source : GLib.Opaque {
private static IDictionary<uint, SourceProxy> source_handlers = new Dictionary<uint, SourceProxy> ();
private Source () {}
public Source(IntPtr raw) : base(raw) {}
@ -110,57 +107,13 @@ namespace GLib {
GLib.Timeout.Add (50, new GLib.TimeoutHandler (info.Handler));
}
internal static void AddSourceHandler (uint id, SourceProxy proxy)
{
lock (Source.source_handlers) {
source_handlers [id] = proxy;
}
}
internal static void RemoveSourceHandler (uint id)
{
lock (Source.source_handlers) {
source_handlers.Remove (id);
}
}
internal static bool RemoveSourceHandler (Delegate hndlr)
{
bool result = false;
List<uint> keys = new List<uint> ();
lock (source_handlers) {
foreach (uint code in source_handlers.Keys) {
var p = Source.source_handlers [code];
if (p != null && p.real_handler == hndlr) {
keys.Add (code);
result = g_source_remove (code);
}
}
foreach (var key in keys) {
Source.RemoveSourceHandler (key);
}
}
return result;
}
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate bool d_g_source_remove(uint tag);
static d_g_source_remove g_source_remove = FuncLoader.LoadFunction<d_g_source_remove>(FuncLoader.GetProcAddress(GLibrary.Load(Library.GLib), "g_source_remove"));
public static bool Remove (uint tag)
{
// g_source_remove always returns true, so we follow that
bool ret = true;
lock (Source.source_handlers) {
if (source_handlers.Remove (tag)) {
ret = g_source_remove (tag);
}
}
return ret;
return g_source_remove (tag);
}
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate IntPtr d_g_source_get_type();

View file

@ -64,10 +64,10 @@ namespace GLib {
}
private Timeout () {}
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate uint d_g_timeout_add(uint interval, TimeoutHandlerInternal d, IntPtr data);
static d_g_timeout_add g_timeout_add = FuncLoader.LoadFunction<d_g_timeout_add>(FuncLoader.GetProcAddress(GLibrary.Load(Library.GLib), "g_timeout_add"));
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate uint d_g_timeout_add_full(int priority, uint interval, TimeoutHandlerInternal d, IntPtr data, DestroyNotify notify);
static d_g_timeout_add_full g_timeout_add_full = FuncLoader.LoadFunction<d_g_timeout_add_full>(FuncLoader.GetProcAddress(GLibrary.Load(Library.GLib), "g_timeout_add_full"));
public static uint Add (uint interval, TimeoutHandler hndlr)
{
TimeoutProxy p = new TimeoutProxy (hndlr);
@ -76,37 +76,36 @@ namespace GLib {
var gch = GCHandle.Alloc(p);
var userData = GCHandle.ToIntPtr(gch);
p.ID = g_timeout_add_full (0, interval, (TimeoutHandlerInternal) p.proxy_handler, userData, DestroyHelper.NotifyHandler);
Source.AddSourceHandler (p.ID, p);
}
return p.ID;
}
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate uint d_g_timeout_add_full(int priority, uint interval, TimeoutHandlerInternal d, IntPtr data, DestroyNotify notify);
static d_g_timeout_add_full g_timeout_add_full = FuncLoader.LoadFunction<d_g_timeout_add_full>(FuncLoader.GetProcAddress(GLibrary.Load(Library.GLib), "g_timeout_add_full"));
public static uint Add (uint interval, TimeoutHandler hndlr, Priority priority)
{
TimeoutProxy p = new TimeoutProxy (hndlr);
lock (p)
{
var gch = GCHandle.Alloc(p);
var userData = GCHandle.ToIntPtr(gch);
p.ID = g_timeout_add_full ((int)priority, interval, (TimeoutHandlerInternal) p.proxy_handler, IntPtr.Zero, null);
Source.AddSourceHandler (p.ID, p);
}
return p.ID;
}
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate uint d_g_timeout_add_seconds(uint interval, TimeoutHandlerInternal d, IntPtr data);
static d_g_timeout_add_seconds g_timeout_add_seconds = FuncLoader.LoadFunction<d_g_timeout_add_seconds>(FuncLoader.GetProcAddress(GLibrary.Load(Library.GLib), "g_timeout_add_seconds"));
delegate uint d_g_timeout_add_seconds_full(int priority, uint interval, TimeoutHandlerInternal d, IntPtr data, DestroyNotify notify);
static d_g_timeout_add_seconds_full g_timeout_add_seconds_full = FuncLoader.LoadFunction<d_g_timeout_add_seconds_full>(FuncLoader.GetProcAddress(GLibrary.Load(Library.GLib), "g_timeout_add_seconds_full"));
public static uint AddSeconds (uint interval, TimeoutHandler hndlr)
{
TimeoutProxy p = new TimeoutProxy (hndlr);
lock (p)
{
p.ID = g_timeout_add_seconds (interval, (TimeoutHandlerInternal) p.proxy_handler, IntPtr.Zero);
Source.AddSourceHandler (p.ID, p);
var gch = GCHandle.Alloc(p);
var userData = GCHandle.ToIntPtr(gch);
p.ID = g_timeout_add_seconds_full (0, interval, (TimeoutHandlerInternal) p.proxy_handler, userData, DestroyHelper.NotifyHandler);
}
return p.ID;
@ -116,11 +115,6 @@ namespace GLib {
{
Source.Remove (id);
}
public static bool Remove (TimeoutHandler hndlr)
{
return Source.RemoveSourceHandler (hndlr);
}
}
}