Skip to content

Commit

Permalink
Ensure that [Parameterized]ThreadStart code is always wrapped in a NS…
Browse files Browse the repository at this point in the history
…AutoreleasePool for MonoTouch. Fix bug mono#1999
  • Loading branch information
spouliot committed Nov 30, 2011
1 parent e79c9e9 commit a2d3a48
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions mcs/class/corlib/System.Threading/Thread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ public static void SpinWait (int iterations)
}

#if MOONLIGHT
private void StartSafe ()
private void StartInternal ()
{
current_thread = this;

Expand Down Expand Up @@ -670,9 +670,32 @@ static internal void MoonlightUnhandledException (Exception e)
}
}
}
#endif

private void StartUnsafe ()
#elif MONOTOUCH
static ConstructorInfo nsautoreleasepool_ctor;

IDisposable GetNSAutoreleasePool ()
{
if (nsautoreleasepool_ctor == null) {
Type t = Type.GetType ("MonoTouch.Foundation.NSAutoreleasePool, monotouch, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null");
nsautoreleasepool_ctor = t.GetConstructor (Type.EmptyTypes);
}
return (IDisposable) nsautoreleasepool_ctor.Invoke (null);
}

private void StartInternal ()
{
using (var pool = GetNSAutoreleasePool ()) {
current_thread = this;

if (threadstart is ThreadStart) {
((ThreadStart) threadstart) ();
} else {
((ParameterizedThreadStart) threadstart) (start_obj);
}
}
}
#else
private void StartInternal ()
{
current_thread = this;

Expand All @@ -682,19 +705,15 @@ private void StartUnsafe ()
((ParameterizedThreadStart) threadstart) (start_obj);
}
}

#endif
public void Start() {
// propagate informations from the original thread to the new thread
if (!ExecutionContext.IsFlowSuppressed ())
ec_to_set = ExecutionContext.Capture ();
Internal._serialized_principal = CurrentThread.Internal._serialized_principal;

// Thread_internal creates and starts the new thread,
#if MOONLIGHT
if (Thread_internal((ThreadStart) StartSafe) == (IntPtr) 0)
#else
if (Thread_internal((ThreadStart) StartUnsafe) == (IntPtr) 0)
#endif
if (Thread_internal((ThreadStart) StartInternal) == (IntPtr) 0)
throw new SystemException ("Thread creation failed.");
}

Expand Down

0 comments on commit a2d3a48

Please sign in to comment.