Skip to content
This repository was archived by the owner on Oct 1, 2024. It is now read-only.

Commit 48d9fee

Browse files
authored
Merge pull request #150 from DavidKarlas/avoidJITing
Convert Timeout/Idle.Handler method from instance to static, so it's …
2 parents 89e266f + 64cea27 commit 48d9fee

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

glib/Idle.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ namespace GLib {
3232
public class Idle {
3333

3434
[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
35-
delegate bool IdleHandlerInternal ();
35+
delegate bool IdleHandlerInternal (IntPtr ptr);
3636

3737

3838
internal class IdleProxy : SourceProxy {
@@ -65,14 +65,17 @@ protected virtual void Dispose (bool disposing)
6565
Source.Remove (ID);
6666
}
6767

68-
public bool Handler ()
68+
static bool Handler (IntPtr data)
6969
{
7070
try {
71-
IdleHandler idle_handler = (IdleHandler) real_handler;
71+
SourceProxy obj;
72+
lock(proxies)
73+
obj = proxies [(int)data];
74+
IdleHandler idle_handler = (IdleHandler)obj.real_handler;
7275

7376
bool cont = idle_handler ();
7477
if (!cont)
75-
Remove ();
78+
obj.Remove ();
7679
return cont;
7780
} catch (Exception e) {
7881
ExceptionManager.RaiseUnhandledException (e, false);
@@ -91,7 +94,7 @@ private Idle ()
9194
public static uint Add (IdleHandler hndlr)
9295
{
9396
IdleProxy p = new IdleProxy (hndlr);
94-
p.ID = g_idle_add ((IdleHandlerInternal) p.proxy_handler, IntPtr.Zero);
97+
p.ID = g_idle_add ((IdleHandlerInternal)p.proxy_handler, (IntPtr)p.proxyId);
9598
lock (Source.source_handlers)
9699
Source.source_handlers [p.ID] = p;
97100

@@ -113,6 +116,10 @@ public static bool Remove (IdleHandler hndlr)
113116
if (p != null && p.real_handler == (System.Delegate) hndlr) {
114117
keys.Add (code);
115118
result = g_source_remove (code);
119+
p.proxy_handler = null;
120+
p.real_handler = null;
121+
lock(SourceProxy.proxies)
122+
SourceProxy.proxies.Remove (p.proxyId);
116123
}
117124
}
118125

glib/Source.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ namespace GLib {
2323

2424
using System;
2525
using System.Collections;
26+
using System.Collections.Generic;
2627
using System.Runtime.InteropServices;
28+
using System.Threading;
2729

2830
public delegate bool GSourceFunc ();
2931

@@ -35,12 +37,28 @@ internal class SourceProxy {
3537
internal Delegate proxy_handler;
3638
internal uint ID;
3739

40+
internal int proxyId;
41+
static int idCounter;
42+
internal static Dictionary<int, SourceProxy> proxies = new Dictionary<int, SourceProxy> ();
43+
44+
protected SourceProxy ()
45+
{
46+
lock(proxies) {
47+
do {
48+
proxyId = idCounter++;
49+
} while (proxies.ContainsKey (proxyId));
50+
proxies [proxyId] = this;
51+
}
52+
}
53+
3854
internal void Remove ()
3955
{
4056
lock (Source.source_handlers)
4157
Source.source_handlers.Remove (ID);
4258
real_handler = null;
4359
proxy_handler = null;
60+
lock(proxies)
61+
proxies.Remove (proxyId);
4462
}
4563
}
4664

glib/Timeout.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace GLib {
2929
public class Timeout {
3030

3131
[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
32-
delegate bool TimeoutHandlerInternal ();
32+
delegate bool TimeoutHandlerInternal (IntPtr ptr);
3333

3434
internal class TimeoutProxy : SourceProxy {
3535
public TimeoutProxy (TimeoutHandler real)
@@ -62,14 +62,17 @@ protected virtual void Dispose (bool disposing)
6262
Source.Remove (ID);
6363
}
6464

65-
public bool Handler ()
65+
static bool Handler (IntPtr data)
6666
{
6767
try {
68-
TimeoutHandler timeout_handler = (TimeoutHandler) real_handler;
68+
SourceProxy obj;
69+
lock (proxies)
70+
obj = proxies [(int)data];
71+
TimeoutHandler timeout_handler = (TimeoutHandler)obj.real_handler;
6972

7073
bool cont = timeout_handler ();
7174
if (!cont)
72-
Remove ();
75+
obj.Remove ();
7376
return cont;
7477
} catch (Exception e) {
7578
ExceptionManager.RaiseUnhandledException (e, false);
@@ -87,7 +90,7 @@ public static uint Add (uint interval, TimeoutHandler hndlr)
8790
{
8891
TimeoutProxy p = new TimeoutProxy (hndlr);
8992

90-
p.ID = g_timeout_add (interval, (TimeoutHandlerInternal) p.proxy_handler, IntPtr.Zero);
93+
p.ID = g_timeout_add (interval, (TimeoutHandlerInternal) p.proxy_handler, (IntPtr)p.proxyId);
9194
lock (Source.source_handlers)
9295
Source.source_handlers [p.ID] = p;
9396

0 commit comments

Comments
 (0)