X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mcs%2Fclass%2Fcorlib%2FSystem.Threading%2FTimer.cs;h=d1546a8bfcaa80632664c766dd64e69f70977b22;hb=71a9582ddaf71b09930cadde06bcbff58f53deb6;hp=917c66729ae4a6a5aa1cbdcb06f40bcfb29ed1d8;hpb=c251b192a55c255f278c9b9ad2ec949264a36526;p=mono.git diff --git a/mcs/class/corlib/System.Threading/Timer.cs b/mcs/class/corlib/System.Threading/Timer.cs index 917c66729ae..d1546a8bfca 100644 --- a/mcs/class/corlib/System.Threading/Timer.cs +++ b/mcs/class/corlib/System.Threading/Timer.cs @@ -29,19 +29,17 @@ // using System.Runtime.InteropServices; +using System.Collections.Generic; using System.Collections; +using System.Runtime.CompilerServices; namespace System.Threading { [ComVisible (true)] public sealed class Timer -#if MOONLIGHT - : IDisposable -#else : MarshalByRefObject, IDisposable -#endif { - static Scheduler scheduler = Scheduler.Instance; + static readonly Scheduler scheduler = Scheduler.Instance; #region Timer instance fields TimerCallback callback; object state; @@ -156,7 +154,7 @@ namespace System.Threading return true; } } else { - nr = dueTime * TimeSpan.TicksPerMillisecond + DateTime.GetTimeMonotonic (); + nr = dueTime * TimeSpan.TicksPerMillisecond + GetTimeMonotonic (); } scheduler.Change (this, nr); @@ -168,10 +166,19 @@ namespace System.Threading if (notifyObject == null) throw new ArgumentNullException ("notifyObject"); Dispose (); - NativeEventCalls.SetEvent_internal (notifyObject.Handle); + NativeEventCalls.SetEvent (notifyObject.SafeWaitHandle); return true; } + // extracted from ../../../../external/referencesource/mscorlib/system/threading/timer.cs + internal void KeepRootedWhileScheduled() + { + } + + // TODO: Environment.TickCount should be enough as is everywhere else + [MethodImplAttribute(MethodImplOptions.InternalCall)] + static extern long GetTimeMonotonic (); + sealed class TimerComparer : IComparer { public int Compare (object x, object y) { @@ -314,22 +321,19 @@ namespace System.Threading return idx; } - static WaitCallback TimerCaller = new WaitCallback (TimerCB); static void TimerCB (object o) { Timer timer = (Timer) o; - try { - timer.callback (timer.state); - } catch {} + timer.callback (timer.state); } void SchedulerThread () { Thread.CurrentThread.Name = "Timer-Scheduler"; - ArrayList new_time = new ArrayList (512); + var new_time = new List (512); while (true) { int ms_wait = -1; - long ticks = DateTime.GetTimeMonotonic (); + long ticks = GetTimeMonotonic (); lock (this) { changed.Reset (); //PrintList (); @@ -343,18 +347,14 @@ namespace System.Threading list.RemoveAt (i); count--; i--; -#if MOONLIGHT - ThreadPool.QueueUserWorkItem (TimerCaller, timer); -#else - ThreadPool.UnsafeQueueUserWorkItem (TimerCaller, timer); -#endif + ThreadPool.UnsafeQueueUserWorkItem (TimerCB, timer); long period = timer.period_ms; long due_time = timer.due_time_ms; bool no_more = (period == -1 || ((period == 0 || period == Timeout.Infinite) && due_time != Timeout.Infinite)); if (no_more) { timer.next_run = Int64.MaxValue; } else { - timer.next_run = DateTime.GetTimeMonotonic () + TimeSpan.TicksPerMillisecond * timer.period_ms; + timer.next_run = GetTimeMonotonic () + TimeSpan.TicksPerMillisecond * timer.period_ms; new_time.Add (timer); } } @@ -362,7 +362,7 @@ namespace System.Threading // Reschedule timers with a new due time count = new_time.Count; for (i = 0; i < count; i++) { - Timer timer = (Timer) new_time [i]; + Timer timer = new_time [i]; Add (timer); } new_time.Clear (); @@ -381,10 +381,14 @@ namespace System.Threading //PrintList (); ms_wait = -1; if (min_next_run != Int64.MaxValue) { - long diff = min_next_run - DateTime.GetTimeMonotonic (); - ms_wait = (int)(diff / TimeSpan.TicksPerMillisecond); - if (ms_wait < 0) - ms_wait = 0; + long diff = (min_next_run - GetTimeMonotonic ()) / TimeSpan.TicksPerMillisecond; + if (diff > Int32.MaxValue) + ms_wait = Int32.MaxValue - 1; + else { + ms_wait = (int)(diff); + if (ms_wait < 0) + ms_wait = 0; + } } } // Wait until due time or a timer is changed and moves from/to the first place in the list. @@ -392,7 +396,7 @@ namespace System.Threading } } - void ShrinkIfNeeded (ArrayList list, int initial) + void ShrinkIfNeeded (List list, int initial) { int capacity = list.Capacity; int count = list.Count;