Browse Source

2007-10-24 Dick Porter <[email protected]>

	* Thread.cs: Assign the ManagedThreadId when it is asked for,
	rather than in the constructor, so ThreadPool threads also work.
	Fixes bug 335579 (and doesn't break 325367 or 325566)



svn path=/trunk/mcs/; revision=88077
Dick Porter 18 years ago
parent
commit
eb888d87d3

+ 6 - 0
mcs/class/corlib/System.Threading/ChangeLog

@@ -1,3 +1,9 @@
+2007-10-24  Dick Porter  <[email protected]>
+
+	* Thread.cs: Assign the ManagedThreadId when it is asked for,
+	rather than in the constructor, so ThreadPool threads also work.
+	Fixes bug 335579 (and doesn't break 325367 or 325566)
+
 2007-10-24  Atsushi Enomoto  <[email protected]>
 
 	* SynchronizationContext.cs : removed SendOrPost().

+ 11 - 7
mcs/class/corlib/System.Threading/Thread.cs

@@ -310,7 +310,6 @@ namespace System.Threading {
 			threadstart=start;
 
 			Thread_init ();
-			managed_id=GetNewManagedId();
 		}
 
 #if NET_2_0
@@ -838,7 +837,6 @@ namespace System.Threading {
 
 			threadstart = start;
 			stack_size = maxStackSize;
-			managed_id = GetNewManagedId();
 		}
 
 		public Thread (ParameterizedThreadStart start)
@@ -847,7 +845,6 @@ namespace System.Threading {
 				throw new ArgumentNullException ("start");
 
 			threadstart = start;
-			managed_id = GetNewManagedId();
 		}
 
 		public Thread (ParameterizedThreadStart start, int maxStackSize)
@@ -859,7 +856,6 @@ namespace System.Threading {
 
 			threadstart = start;
 			stack_size = maxStackSize;
-			managed_id = GetNewManagedId();
 		}
 
 		[MonoTODO ("limited to CompressedStack support")]
@@ -874,8 +870,16 @@ namespace System.Threading {
 		}
 
 		public int ManagedThreadId {
-		[ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
-			get { return managed_id; }
+			[ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
+			get {
+				if (managed_id == 0) {
+					int new_managed_id = GetNewManagedId ();
+					
+					Interlocked.CompareExchange (ref managed_id, new_managed_id, 0);
+				}
+				
+				return managed_id;
+			}
 		}
 
 		[MonoTODO("Not implemented")]
@@ -933,7 +937,7 @@ namespace System.Threading {
 		[ComVisible (false)]
 		public override int GetHashCode ()
 		{
-			return managed_id;
+			return ManagedThreadId;
 		}
 
 		public void Start (object parameter)