From cd47acba85bff2723433606c87834f1e36160818 Mon Sep 17 00:00:00 2001 From: Midhun Mohan Date: Tue, 18 Feb 2020 12:57:21 +0000 Subject: [PATCH] Cleanup of sourceproxy Dictionary and its usages --- Source/Libs/GLibSharp/Idle.cs | 31 ++++--------------- Source/Libs/GLibSharp/Source.cs | 51 ++------------------------------ Source/Libs/GLibSharp/Timeout.cs | 34 +++++++++------------ 3 files changed, 21 insertions(+), 95 deletions(-) diff --git a/Source/Libs/GLibSharp/Idle.cs b/Source/Libs/GLibSharp/Idle.cs index 9dda13518..bbdde15f4 100644 --- a/Source/Libs/GLibSharp/Idle.cs +++ b/Source/Libs/GLibSharp/Idle.cs @@ -10,7 +10,7 @@ // Copyright (c) 2009 Novell, Inc. // // 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. // // This program is distributed in the hope that it will be useful, @@ -64,14 +64,14 @@ namespace GLib { return false; } } - + private Idle () { } - + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - delegate uint d_g_idle_add(IdleHandlerInternal d, IntPtr data); - static d_g_idle_add g_idle_add = FuncLoader.LoadFunction(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(FuncLoader.GetProcAddress(GLibrary.Load(Library.GLib), "g_idle_add_full")); public static uint Add (IdleHandler hndlr) { @@ -81,36 +81,15 @@ 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(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) { Source.Remove (id); } - - public static bool Remove (IdleHandler hndlr) - { - return Source.RemoveSourceHandler (hndlr); - } } } diff --git a/Source/Libs/GLibSharp/Source.cs b/Source/Libs/GLibSharp/Source.cs index df362894c..3449102f9 100644 --- a/Source/Libs/GLibSharp/Source.cs +++ b/Source/Libs/GLibSharp/Source.cs @@ -5,7 +5,7 @@ // Copyright (c) 2002 Mike Kestner // // 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. // // This program is distributed in the hope that it will be useful, @@ -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 source_handlers = new Dictionary (); - 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 keys = new List (); - - 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(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(); diff --git a/Source/Libs/GLibSharp/Timeout.cs b/Source/Libs/GLibSharp/Timeout.cs index 2b48d66e1..8e17e464d 100644 --- a/Source/Libs/GLibSharp/Timeout.cs +++ b/Source/Libs/GLibSharp/Timeout.cs @@ -8,7 +8,7 @@ // Copyright (c) 2009 Novell, Inc. // // 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. // // This program is distributed in the hope that it will be useful, @@ -62,12 +62,12 @@ namespace GLib { 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(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(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(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(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(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); - } } }