Changeset 400 for trunk/Include/Classes/System/Threading/Thread.ab
- Timestamp:
- Jan 22, 2008, 9:19:59 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Include/Classes/System/Threading/Thread.ab
r374 r400 1 'threading.sbp 2 1 'Thread.ab 3 2 4 3 '-------------------------------------------------------------------- … … 115 114 End Sub 116 115 117 118 119 120 121 116 Sub Start() 122 117 Dim ThreadId As DWord … … 133 128 134 129 'GCにスレッド開始を通知 135 _System_pobj_AllThreads->BeginThread( VarPtr(This),_System_GetSp() As *LONG_PTR)130 _System_pobj_AllThreads->BeginThread(This, _System_GetSp() As *LONG_PTR) 136 131 137 132 … … 147 142 148 143 'GCにスレッド終了を通知 149 _System_pobj_AllThreads->EndThread( VarPtr(This))144 _System_pobj_AllThreads->EndThread(This) 150 145 151 146 '自身のスレッドハンドルを閉じる … … 201 196 End Class 202 197 198 Dim _System_pobj_AllThreads As *Detail._System_CThreadCollection 199 200 Namespace Detail 203 201 204 202 '-------------------------------------------------------------------- … … 206 204 '-------------------------------------------------------------------- 207 205 ' TODO: このクラスをシングルトンにする 206 208 207 Class _System_CThreadCollection 209 208 Public 210 ppobj_Thread As **Thread 211 pStackBase As **LONG_PTR 209 collection As *ThreadInfo 212 210 ThreadNum As Long 213 211 … … 215 213 216 214 Sub _System_CThreadCollection() 217 ppobj_Thread=GC_malloc(1) 218 pStackBase=HeapAlloc(_System_hProcessHeap,0,1) 219 ppException=HeapAlloc(_System_hProcessHeap,0,1) 220 ThreadNum=0 221 222 'クリティカルセッションを生成 215 collection = GC_malloc(1) 216 ThreadNum = 0 223 217 InitializeCriticalSection(CriticalSection) 224 218 End Sub … … 227 221 Dim i As Long 228 222 For i=0 To ELM(ThreadNum) 229 If ppobj_Thread[i] Then 230 If i = 0 Then 231 Delete ppobj_Thread[i] 232 End If 233 ppobj_Thread[i]=0 234 pStackBase[i]=0 235 Delete ppException[i] 236 ppException[i]=0 237 Exit For 223 With collection[i] 224 If .thread Then 225 .thread = Nothing 226 .stackBase = 0 227 .exception = Nothing 228 End If 229 End With 230 Next 231 collection = 0 232 DeleteCriticalSection(CriticalSection) 233 End Sub 234 235 'スレッドを生成 236 Sub BeginThread(thread As Thread, NowSp As *LONG_PTR) 237 EnterCriticalSection(CriticalSection) 238 Dim i = FindFreeIndex 239 With collection[i] 240 .thread = thread 241 .stackBase = NowSp 242 .exception = New ExceptionService '例外処理管理用オブジェクトを生成 243 End With 244 LeaveCriticalSection(CriticalSection) 245 End Sub 246 Private 247 'クリティカルセション内で呼ぶこと 248 Function FindFreeIndex() As Long 249 Dim i As Long 250 For i = 0 To ELM(ThreadNum) 251 If ActiveBasic.IsNothing(collection[i].thread) Then 252 FindFreeIndex = i 253 Exit Function 238 254 End If 239 255 Next 240 241 HeapFree(_System_hProcessHeap,0,pStackBase) 242 pStackBase=0 243 244 HeapFree(_System_hProcessHeap,0,ppException) 245 ppException = 0 246 247 ThreadNum=0 248 249 'クリティカルセッションを破棄 250 DeleteCriticalSection(CriticalSection) 251 End Sub 252 253 'スレッドを生成 254 Sub BeginThread(pThread As *Thread,NowSp As *LONG_PTR) 255 EnterCriticalSection(CriticalSection) 256 257 '例外処理管理用オブジェクトを生成 258 Dim pException As *ExceptionService 259 pException = New ExceptionService 260 261 Dim i As Long 262 For i=0 To ELM(ThreadNum) 263 If ppobj_Thread[i] = 0 Then 264 ppobj_Thread[i] = pThread 265 pStackBase[i] = NowSp 266 ppException[i] = pException 267 Exit For 268 End If 269 Next 270 271 If i = ThreadNum Then 272 ppobj_Thread=realloc(ppobj_Thread,(ThreadNum+1)*SizeOf(*Thread)) 273 ppobj_Thread[ThreadNum]=pThread 274 pStackBase=HeapReAlloc(_System_hProcessHeap,0,pStackBase,(ThreadNum+1)*SizeOf(LONG_PTR)) 275 pStackBase[ThreadNum]=NowSp 276 ppException=HeapReAlloc(_System_hProcessHeap,0,ppException,(ThreadNum+1)*SizeOf(*ExceptionService)) 277 ppException[ThreadNum]=pException 278 ThreadNum++ 279 End If 280 LeaveCriticalSection(CriticalSection) 281 End Sub 256 ThreadNum++ 257 collection = realloc(collection, ThreadNum * SizeOf(ThreadInfo)) 258 FindFreeIndex = i 259 End Function 260 Public 282 261 283 262 'スレッドを終了 284 Sub EndThread( pThread As *Thread)263 Sub EndThread(thread As Thread) 285 264 EnterCriticalSection(CriticalSection) 286 265 Dim i As Long 287 For i=0 To ELM(ThreadNum) 288 If ppobj_Thread[i] = pThread Then 289 If i = 0 Then 290 Delete pThread 266 For i = 0 To ELM(ThreadNum) 267 With collection[i] 268 If thread.Equals(.thread) Then 269 .thread = Nothing 270 .stackBase = 0 271 .exception = Nothing 272 Exit For 291 273 End If 292 ppobj_Thread[i]=0 293 pStackBase[i]=0 294 Delete ppException[i] 295 ppException[i]=0 296 Exit For 297 End If 274 End With 298 275 Next 299 276 LeaveCriticalSection(CriticalSection) … … 303 280 Sub SuspendAllThread() 304 281 Dim i As Long 305 For i=0 To ELM(ThreadNum) 306 If ppobj_Thread[i] Then 307 ppobj_Thread[i]->Suspend() 308 End If 282 For i = 0 To ELM(ThreadNum) 283 With collection[i] 284 If Not ActiveBasic.IsNothing(.thread) Then 285 .thread.Suspend() 286 End If 287 End With 309 288 Next 310 289 End Sub … … 313 292 Sub ResumeAllThread() 314 293 Dim i As Long 315 For i=0 To ELM(ThreadNum) 316 If ppobj_Thread[i] Then 317 ppobj_Thread[i]->Resume() 318 End If 319 Next 320 End Sub 321 294 For i = 0 To ELM(ThreadNum) 295 With collection[i] 296 If Not ActiveBasic.IsNothing(.thread) Then 297 .thread.Resume() 298 End If 299 End With 300 Next 301 End Sub 302 /* 322 303 ' 自分以外のスレッドを中断 323 304 Sub SuspendAnotherThread() 324 Dim currentThread = Thread.CurrentThread()305 Dim currentThread = CurrentThread() 325 306 326 307 Dim i As Long 327 308 For i=0 To ELM(ThreadNum) 328 329 If currentThread.Equals( ppobj_Thread[i] As Object ) Then 330 Continue 331 End If 332 333 If ppobj_Thread[i] Then 334 ppobj_Thread[i]->Suspend() 335 End If 309 With collection[i] 310 If currentThread.Equals(.thread) Then 311 Continue 312 ElseIf Not ActiveBasic.IsNothing(.thread) Then 313 .thread.Suspend() 314 End If 315 End With 336 316 Next 337 317 End Sub … … 339 319 ' 自分以外のスレッドを再開 340 320 Sub ResumeAnotherThread() 341 Dim currentThread = Thread.CurrentThread()321 Dim currentThread = CurrentThread() 342 322 343 323 Dim i As Long 344 324 For i=0 To ELM(ThreadNum) 345 346 If currentThread.Equals( ppobj_Thread[i] As Object ) Then 347 Continue 348 End If 349 350 If ppobj_Thread[i] Then 351 ppobj_Thread[i]->Resume() 352 End If 353 Next 354 End Sub 355 325 With collection[i] 326 If currentThread.Equals(.thread) Then 327 Continue 328 ElseIf Not ActiveBasic.IsNothing(.thread) Then 329 .thread.Resume() 330 End If 331 End With 332 Next 333 End Sub 334 */ 356 335 'カレントスレッドを取得 357 336 Function CurrentThread() As Thread 358 Dim dwNowThreadId As DWord 359 dwNowThreadId=GetCurrentThreadId() 360 361 Dim i As Long 362 For i=0 To ELM(ThreadNum) 363 If ppobj_Thread[i]->ThreadId=dwNowThreadId Then 364 Return ByVal ppobj_Thread[i] 337 Dim p = CurrentThreadInfo() 338 If p = 0 Then 339 ' TODO: エラー処理 340 OutputDebugString( Ex"カレントスレッドの取得に失敗\r\n" ) 341 debug 342 Exit Function 343 End If 344 CurrentThread = p->thread 345 End Function 346 347 Function CurrentThreadInfo() As *ThreadInfo 348 CurrentThreadInfo = FindThreadInfo(GetCurrentThreadId()) 349 End Function 350 351 Function FindThreadInfo(threadID As DWord) As *ThreadInfo 352 Dim i As Long 353 For i = 0 To ELM(ThreadNum) 354 If collection[i].thread.ThreadId = threadID Then 355 FindThreadInfo = VarPtr(collection[i]) 356 Exit Function 365 357 End If 366 358 Next 367 368 ' TODO: エラー処理 369 OutputDebugString( "カレントスレッドの取得に失敗" ) 370 debug 371 End Function 372 359 End Function 373 360 374 361 Private … … 376 363 ' スレッド固有の例外処理制御 377 364 '------------------------------------------ 378 ppException As **ExceptionService 379 380 Public 381 Function GetCurrentException() As *ExceptionService 382 Dim dwNowThreadId As DWord 383 dwNowThreadId=GetCurrentThreadId() 365 366 Public 367 Function GetCurrentException() As ExceptionService 368 Dim dwNowThreadId = GetCurrentThreadId() 384 369 385 370 Dim i As Long 386 371 For i=0 To ELM(ThreadNum) 387 If ppobj_Thread[i]->ThreadId=dwNowThreadId Then 388 Return ppException[i] 389 End If 390 Next 391 392 Return NULL 372 With collection[i] 373 If .thread.ThreadId = dwNowThreadId Then 374 Return .exception 375 End If 376 End With 377 Next 378 379 OutputDebugString( Ex"カレントスレッドの取得に失敗\r\n" ) 380 Return Nothing 393 381 End Function 394 382 End Class 395 Dim _System_pobj_AllThreads As *_System_CThreadCollection 383 384 Type ThreadInfo 385 thread As Thread 386 stackBase As *LONG_PTR 387 exception As ExceptionService 388 End Type 389 390 End Namespace 'Detail
Note:
See TracChangeset
for help on using the changeset viewer.