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

@ -10,7 +10,7 @@
// Copyright (c) 2009 Novell, Inc. // Copyright (c) 2009 Novell, Inc.
// //
// This program is free software; you can redistribute it and/or // This program is free software; you can redistribute it and/or
// modify it under the terms of version 2 of the Lesser GNU General // modify it under the terms of version 2 of the Lesser GNU General
// Public License as published by the Free Software Foundation. // Public License as published by the Free Software Foundation.
// //
// This program is distributed in the hope that it will be useful, // This program is distributed in the hope that it will be useful,
@ -64,14 +64,14 @@ namespace GLib {
return false; return false;
} }
} }
private Idle () private Idle ()
{ {
} }
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate uint d_g_idle_add(IdleHandlerInternal d, IntPtr data); delegate uint d_g_idle_add_full(int priority, IdleHandlerInternal d, IntPtr data, DestroyNotify notify);
static d_g_idle_add g_idle_add = FuncLoader.LoadFunction<d_g_idle_add>(FuncLoader.GetProcAddress(GLibrary.Load(Library.GLib), "g_idle_add")); 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) public static uint Add (IdleHandler hndlr)
{ {
@ -81,36 +81,15 @@ namespace GLib {
var gch = GCHandle.Alloc(p); var gch = GCHandle.Alloc(p);
var userData = GCHandle.ToIntPtr(gch); var userData = GCHandle.ToIntPtr(gch);
p.ID = g_idle_add_full (0, (IdleHandlerInternal) p.proxy_handler, userData, DestroyHelper.NotifyHandler); p.ID = g_idle_add_full (0, (IdleHandlerInternal) p.proxy_handler, userData, DestroyHelper.NotifyHandler);
Source.AddSourceHandler (p.ID, p);
} }
return p.ID; 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;
}
public static void Remove (uint id) public static void Remove (uint id)
{ {
Source.Remove (id); Source.Remove (id);
} }
public static bool Remove (IdleHandler hndlr)
{
return Source.RemoveSourceHandler (hndlr);
}
} }
} }

View File

@ -5,7 +5,7 @@
// Copyright (c) 2002 Mike Kestner // Copyright (c) 2002 Mike Kestner
// //
// This program is free software; you can redistribute it and/or // This program is free software; you can redistribute it and/or
// modify it under the terms of version 2 of the Lesser GNU General // modify it under the terms of version 2 of the Lesser GNU General
// Public License as published by the Free Software Foundation. // Public License as published by the Free Software Foundation.
// //
// This program is distributed in the hope that it will be useful, // This program is distributed in the hope that it will be useful,
@ -62,7 +62,6 @@ namespace GLib {
internal void Remove () internal void Remove ()
{ {
Source.RemoveSourceHandler (ID);
real_handler = null; real_handler = null;
proxy_handler = null; proxy_handler = null;
} }
@ -70,8 +69,6 @@ namespace GLib {
public partial class Source : GLib.Opaque { public partial class Source : GLib.Opaque {
private static IDictionary<uint, SourceProxy> source_handlers = new Dictionary<uint, SourceProxy> ();
private Source () {} private Source () {}
public Source(IntPtr raw) : base(raw) {} public Source(IntPtr raw) : base(raw) {}
@ -110,57 +107,13 @@ namespace GLib {
GLib.Timeout.Add (50, new GLib.TimeoutHandler (info.Handler)); 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)] [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate bool d_g_source_remove(uint tag); 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")); 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) public static bool Remove (uint tag)
{ {
// g_source_remove always returns true, so we follow that return g_source_remove (tag);
bool ret = true;
lock (Source.source_handlers) {
if (source_handlers.Remove (tag)) {
ret = g_source_remove (tag);
}
}
return ret;
} }
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate IntPtr d_g_source_get_type(); delegate IntPtr d_g_source_get_type();

View File

@ -8,7 +8,7 @@
// Copyright (c) 2009 Novell, Inc. // Copyright (c) 2009 Novell, Inc.
// //
// This program is free software; you can redistribute it and/or // This program is free software; you can redistribute it and/or
// modify it under the terms of version 2 of the Lesser GNU General // modify it under the terms of version 2 of the Lesser GNU General
// Public License as published by the Free Software Foundation. // Public License as published by the Free Software Foundation.
// //
// This program is distributed in the hope that it will be useful, // This program is distributed in the hope that it will be useful,
@ -62,12 +62,12 @@ namespace GLib {
return false; return false;
} }
} }
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"));
private Timeout () {}
[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) public static uint Add (uint interval, TimeoutHandler hndlr)
{ {
TimeoutProxy p = new TimeoutProxy (hndlr); TimeoutProxy p = new TimeoutProxy (hndlr);
@ -76,37 +76,36 @@ namespace GLib {
var gch = GCHandle.Alloc(p); var gch = GCHandle.Alloc(p);
var userData = GCHandle.ToIntPtr(gch); var userData = GCHandle.ToIntPtr(gch);
p.ID = g_timeout_add_full (0, interval, (TimeoutHandlerInternal) p.proxy_handler, userData, DestroyHelper.NotifyHandler); p.ID = g_timeout_add_full (0, interval, (TimeoutHandlerInternal) p.proxy_handler, userData, DestroyHelper.NotifyHandler);
Source.AddSourceHandler (p.ID, p);
} }
return p.ID; 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) public static uint Add (uint interval, TimeoutHandler hndlr, Priority priority)
{ {
TimeoutProxy p = new TimeoutProxy (hndlr); TimeoutProxy p = new TimeoutProxy (hndlr);
lock (p) 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); p.ID = g_timeout_add_full ((int)priority, interval, (TimeoutHandlerInternal) p.proxy_handler, IntPtr.Zero, null);
Source.AddSourceHandler (p.ID, p);
} }
return p.ID; return p.ID;
} }
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate uint d_g_timeout_add_seconds(uint interval, TimeoutHandlerInternal d, IntPtr data); delegate uint d_g_timeout_add_seconds_full(int priority, uint interval, TimeoutHandlerInternal d, IntPtr data, DestroyNotify notify);
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")); 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) public static uint AddSeconds (uint interval, TimeoutHandler hndlr)
{ {
TimeoutProxy p = new TimeoutProxy (hndlr); TimeoutProxy p = new TimeoutProxy (hndlr);
lock (p) lock (p)
{ {
p.ID = g_timeout_add_seconds (interval, (TimeoutHandlerInternal) p.proxy_handler, IntPtr.Zero); var gch = GCHandle.Alloc(p);
Source.AddSourceHandler (p.ID, 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; return p.ID;
@ -116,11 +115,6 @@ namespace GLib {
{ {
Source.Remove (id); Source.Remove (id);
} }
public static bool Remove (TimeoutHandler hndlr)
{
return Source.RemoveSourceHandler (hndlr);
}
} }
} }