3 using System
.Collections
;
11 // Note that the way we manipulate Action.Count and closure.Id
12 // is violently non-threadsafe. Fixing this should be pretty
13 // easy, and is left as an exercise to the reader.
15 public interface Handle { }
17 // Should be private, but that is just too messy
19 static public int Count
= 0;
21 private class Closure
: Handle
{
24 private GLib
.TimeoutHandler timeout_handler
;
25 private GLib
.IdleHandler idle_handler
;
27 public Closure (GLib
.TimeoutHandler timeout_handler
)
29 this.timeout_handler
= timeout_handler
;
33 public Closure (GLib
.IdleHandler idle_handler
)
35 this.idle_handler
= idle_handler
;
39 public bool Handler ()
45 if (timeout_handler
!= null)
46 rv
= timeout_handler ();
47 else if (idle_handler
!= null)
54 BludgeonMain
.Shutdown ();
60 // RENAMEME: It is linguistically awkward that we pass in
61 // "handlers", and get back "handles".
62 static public Handle
Add (GLib
.IdleHandler idle_handler
)
65 c
= new Closure (idle_handler
);
66 c
.Id
= GLib
.Idle
.Add (new GLib
.IdleHandler (c
.Handler
));
70 static public Handle
Add (uint t
, GLib
.TimeoutHandler timeout_handler
)
73 c
= new Closure (timeout_handler
);
74 c
.Id
= GLib
.Timeout
.Add (t
, new GLib
.TimeoutHandler (c
.Handler
));
78 static public void Cancel (Handle h
)
80 Closure c
= (Closure
) h
;
83 GLib
.Source
.Remove (c
.Id
);
87 // We add an empty idle task, which will increment
88 // the count up back above 0. This keeps us from
89 // shutting down the system until the next time control
90 // is passed to the main loop.