Index: trunk/Include/Classes/System/Threading/Thread.ab
===================================================================
--- trunk/Include/Classes/System/Threading/Thread.ab	(revision 374)
+++ trunk/Include/Classes/System/Threading/Thread.ab	(revision 400)
@@ -1,4 +1,3 @@
-'threading.sbp
-
+'Thread.ab
 
 '--------------------------------------------------------------------
@@ -115,8 +114,4 @@
 	End Sub
 
-
-
-
-
 	Sub Start()
 		Dim ThreadId As DWord
@@ -133,5 +128,5 @@
 
 		'GCにスレッド開始を通知
-		_System_pobj_AllThreads->BeginThread(VarPtr(This),_System_GetSp() As *LONG_PTR)
+		_System_pobj_AllThreads->BeginThread(This, _System_GetSp() As *LONG_PTR)
 
 
@@ -147,5 +142,5 @@
 
 		'GCにスレッド終了を通知
-		_System_pobj_AllThreads->EndThread(VarPtr(This))
+		_System_pobj_AllThreads->EndThread(This)
 
 		'自身のスレッドハンドルを閉じる
@@ -201,4 +196,7 @@
 End Class
 
+Dim _System_pobj_AllThreads As *Detail._System_CThreadCollection
+
+Namespace Detail
 
 '--------------------------------------------------------------------
@@ -206,8 +204,8 @@
 '--------------------------------------------------------------------
 ' TODO: このクラスをシングルトンにする
+
 Class _System_CThreadCollection
 Public
-	ppobj_Thread As **Thread
-	pStackBase As **LONG_PTR
+	collection As *ThreadInfo
 	ThreadNum As Long
 
@@ -215,10 +213,6 @@
 
 	Sub _System_CThreadCollection()
-		ppobj_Thread=GC_malloc(1)
-		pStackBase=HeapAlloc(_System_hProcessHeap,0,1)
-		ppException=HeapAlloc(_System_hProcessHeap,0,1)
-		ThreadNum=0
-
-		'クリティカルセッションを生成
+		collection = GC_malloc(1)
+		ThreadNum = 0
 		InitializeCriticalSection(CriticalSection)
 	End Sub
@@ -227,73 +221,56 @@
 		Dim i As Long
 		For i=0 To ELM(ThreadNum)
-			If ppobj_Thread[i] Then
-				If i = 0 Then
-					Delete ppobj_Thread[i]
-				End If
-				ppobj_Thread[i]=0
-				pStackBase[i]=0
-				Delete ppException[i]
-				ppException[i]=0
-				Exit For
+			With collection[i]
+				If .thread Then
+					.thread = Nothing
+					.stackBase = 0
+					.exception = Nothing
+				End If
+			End With
+		Next
+		collection = 0
+		DeleteCriticalSection(CriticalSection)
+	End Sub
+
+	'スレッドを生成
+	Sub BeginThread(thread As Thread, NowSp As *LONG_PTR)
+		EnterCriticalSection(CriticalSection)
+		Dim i = FindFreeIndex
+		With collection[i]
+			.thread = thread
+			.stackBase = NowSp
+			.exception = New ExceptionService '例外処理管理用オブジェクトを生成
+		End With
+		LeaveCriticalSection(CriticalSection)
+	End Sub
+Private
+	'クリティカルセション内で呼ぶこと
+	Function FindFreeIndex() As Long
+		Dim i As Long
+		For i = 0 To ELM(ThreadNum)
+			If ActiveBasic.IsNothing(collection[i].thread) Then
+				FindFreeIndex = i
+				Exit Function
 			End If
 		Next
-
-		HeapFree(_System_hProcessHeap,0,pStackBase)
-		pStackBase=0
-
-		HeapFree(_System_hProcessHeap,0,ppException)
-		ppException = 0
-
-		ThreadNum=0
-
-		'クリティカルセッションを破棄
-		DeleteCriticalSection(CriticalSection)
-	End Sub
-
-	'スレッドを生成
-	Sub BeginThread(pThread As *Thread,NowSp As *LONG_PTR)
-		EnterCriticalSection(CriticalSection)
-
-			'例外処理管理用オブジェクトを生成
-			Dim pException As *ExceptionService
-			pException = New ExceptionService
-
-			Dim i As Long
-			For i=0 To ELM(ThreadNum)
-				If ppobj_Thread[i] = 0 Then
-					ppobj_Thread[i] = pThread
-					pStackBase[i] = NowSp
-					ppException[i] = pException
-					Exit For
-				End If
-			Next
-
-			If i = ThreadNum Then
-				ppobj_Thread=realloc(ppobj_Thread,(ThreadNum+1)*SizeOf(*Thread))
-				ppobj_Thread[ThreadNum]=pThread
-				pStackBase=HeapReAlloc(_System_hProcessHeap,0,pStackBase,(ThreadNum+1)*SizeOf(LONG_PTR))
-				pStackBase[ThreadNum]=NowSp
-				ppException=HeapReAlloc(_System_hProcessHeap,0,ppException,(ThreadNum+1)*SizeOf(*ExceptionService))
-				ppException[ThreadNum]=pException
-				ThreadNum++
-			End If
-		LeaveCriticalSection(CriticalSection)
-	End Sub
+		ThreadNum++
+		collection = realloc(collection, ThreadNum * SizeOf(ThreadInfo))
+		FindFreeIndex = i
+	End Function
+Public
 
 	'スレッドを終了
-	Sub EndThread(pThread As *Thread)
+	Sub EndThread(thread As Thread)
 		EnterCriticalSection(CriticalSection)
 			Dim i As Long
-			For i=0 To ELM(ThreadNum)
-				If ppobj_Thread[i] = pThread Then
-					If i = 0 Then
-						Delete pThread
+			For i = 0 To ELM(ThreadNum)
+				With collection[i]
+					If thread.Equals(.thread) Then
+						.thread = Nothing
+						.stackBase = 0
+						.exception = Nothing
+						Exit For
 					End If
-					ppobj_Thread[i]=0
-					pStackBase[i]=0
-					Delete ppException[i]
-					ppException[i]=0
-					Exit For
-				End If
+				End With
 			Next
 		LeaveCriticalSection(CriticalSection)
@@ -303,8 +280,10 @@
 	Sub SuspendAllThread()
 		Dim i As Long
-		For i=0 To ELM(ThreadNum)
-			If ppobj_Thread[i] Then
-				ppobj_Thread[i]->Suspend()
-			End If
+		For i = 0 To ELM(ThreadNum)
+			With collection[i]
+				If Not ActiveBasic.IsNothing(.thread) Then
+					.thread.Suspend()
+				End If
+			End With
 		Next
 	End Sub
@@ -313,25 +292,26 @@
 	Sub ResumeAllThread()
 		Dim i As Long
-		For i=0 To ELM(ThreadNum)
-			If ppobj_Thread[i] Then
-				ppobj_Thread[i]->Resume()
-			End If
-		Next
-	End Sub
-
+		For i = 0 To ELM(ThreadNum)
+			With collection[i]
+				If Not ActiveBasic.IsNothing(.thread) Then
+					.thread.Resume()
+				End If
+			End With
+		Next
+	End Sub
+/*
 	' 自分以外のスレッドを中断
 	Sub SuspendAnotherThread()
-		Dim currentThread = Thread.CurrentThread()
+		Dim currentThread = CurrentThread()
 
 		Dim i As Long
 		For i=0 To ELM(ThreadNum)
-
-			If currentThread.Equals( ppobj_Thread[i] As Object ) Then
-				Continue
-			End If
-
-			If ppobj_Thread[i] Then
-				ppobj_Thread[i]->Suspend()
-			End If
+			With collection[i]
+				If currentThread.Equals(.thread) Then
+					Continue
+				ElseIf Not ActiveBasic.IsNothing(.thread) Then
+					.thread.Suspend()
+				End If
+			End With
 		Next
 	End Sub
@@ -339,36 +319,43 @@
 	' 自分以外のスレッドを再開
 	Sub ResumeAnotherThread()
-		Dim currentThread = Thread.CurrentThread()
+		Dim currentThread = CurrentThread()
 
 		Dim i As Long
 		For i=0 To ELM(ThreadNum)
-
-			If currentThread.Equals( ppobj_Thread[i] As Object ) Then
-				Continue
-			End If
-
-			If ppobj_Thread[i] Then
-				ppobj_Thread[i]->Resume()
-			End If
-		Next
-	End Sub
-
+			With collection[i]
+				If currentThread.Equals(.thread) Then
+					Continue
+				ElseIf Not ActiveBasic.IsNothing(.thread) Then
+					.thread.Resume()
+				End If
+			End With
+		Next
+	End Sub
+*/
 	'カレントスレッドを取得
 	Function CurrentThread() As Thread
-		Dim dwNowThreadId As DWord
-		dwNowThreadId=GetCurrentThreadId()
-
-		Dim i As Long
-		For i=0 To ELM(ThreadNum)
-			If ppobj_Thread[i]->ThreadId=dwNowThreadId Then
-				Return ByVal ppobj_Thread[i]
+		Dim p = CurrentThreadInfo()
+		If p = 0 Then
+			' TODO: エラー処理
+			OutputDebugString( Ex"カレントスレッドの取得に失敗\r\n" )
+			debug
+			Exit Function
+		End If
+		CurrentThread = p->thread
+	End Function
+
+	Function CurrentThreadInfo() As *ThreadInfo
+		CurrentThreadInfo = FindThreadInfo(GetCurrentThreadId())
+	End Function
+
+	Function FindThreadInfo(threadID As DWord) As *ThreadInfo
+		Dim i As Long
+		For i = 0 To ELM(ThreadNum)
+			If collection[i].thread.ThreadId = threadID Then
+				FindThreadInfo = VarPtr(collection[i])
+				Exit Function
 			End If
 		Next
-
-		' TODO: エラー処理
-		OutputDebugString( "カレントスレッドの取得に失敗" )
-		debug
-	End Function
-
+	End Function
 
 Private
@@ -376,20 +363,28 @@
 	' スレッド固有の例外処理制御
 	'------------------------------------------
-	ppException As **ExceptionService
-
-Public
-	Function GetCurrentException() As *ExceptionService
-		Dim dwNowThreadId As DWord
-		dwNowThreadId=GetCurrentThreadId()
+
+Public
+	Function GetCurrentException() As ExceptionService
+		Dim dwNowThreadId = GetCurrentThreadId()
 
 		Dim i As Long
 		For i=0 To ELM(ThreadNum)
-			If ppobj_Thread[i]->ThreadId=dwNowThreadId Then
-				Return ppException[i]
-			End If
-		Next
-
-		Return NULL
+			With collection[i]
+				If .thread.ThreadId = dwNowThreadId Then
+					Return .exception
+				End If
+			End With
+		Next
+
+		OutputDebugString( Ex"カレントスレッドの取得に失敗\r\n" )
+		Return Nothing
 	End Function
 End Class
-Dim _System_pobj_AllThreads As *_System_CThreadCollection
+
+Type ThreadInfo
+	thread As Thread
+	stackBase As *LONG_PTR
+	exception As ExceptionService
+End Type
+
+End Namespace 'Detail
