Ignore:
Timestamp:
Jan 22, 2008, 9:19:59 PM (16 years ago)
Author:
イグトランス (egtra)
Message:

_System_CThreadCollectionでのクラスインスタンスへのポインタの使用を除去、参照変数構文へ。

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Include/Classes/System/Threading/Thread.ab

    r374 r400  
    1 'threading.sbp
    2 
     1'Thread.ab
    32
    43'--------------------------------------------------------------------
     
    115114    End Sub
    116115
    117 
    118 
    119 
    120 
    121116    Sub Start()
    122117        Dim ThreadId As DWord
     
    133128
    134129        'GCにスレッド開始を通知
    135         _System_pobj_AllThreads->BeginThread(VarPtr(This),_System_GetSp() As *LONG_PTR)
     130        _System_pobj_AllThreads->BeginThread(This, _System_GetSp() As *LONG_PTR)
    136131
    137132
     
    147142
    148143        'GCにスレッド終了を通知
    149         _System_pobj_AllThreads->EndThread(VarPtr(This))
     144        _System_pobj_AllThreads->EndThread(This)
    150145
    151146        '自身のスレッドハンドルを閉じる
     
    201196End Class
    202197
     198Dim _System_pobj_AllThreads As *Detail._System_CThreadCollection
     199
     200Namespace Detail
    203201
    204202'--------------------------------------------------------------------
     
    206204'--------------------------------------------------------------------
    207205' TODO: このクラスをシングルトンにする
     206
    208207Class _System_CThreadCollection
    209208Public
    210     ppobj_Thread As **Thread
    211     pStackBase As **LONG_PTR
     209    collection As *ThreadInfo
    212210    ThreadNum As Long
    213211
     
    215213
    216214    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
    223217        InitializeCriticalSection(CriticalSection)
    224218    End Sub
     
    227221        Dim i As Long
    228222        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
     246Private
     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
    238254            End If
    239255        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
     260Public
    282261
    283262    'スレッドを終了
    284     Sub EndThread(pThread As *Thread)
     263    Sub EndThread(thread As Thread)
    285264        EnterCriticalSection(CriticalSection)
    286265            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
    291273                    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
    298275            Next
    299276        LeaveCriticalSection(CriticalSection)
     
    303280    Sub SuspendAllThread()
    304281        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
    309288        Next
    310289    End Sub
     
    313292    Sub ResumeAllThread()
    314293        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/*
    322303    ' 自分以外のスレッドを中断
    323304    Sub SuspendAnotherThread()
    324         Dim currentThread = Thread.CurrentThread()
     305        Dim currentThread = CurrentThread()
    325306
    326307        Dim i As Long
    327308        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
    336316        Next
    337317    End Sub
     
    339319    ' 自分以外のスレッドを再開
    340320    Sub ResumeAnotherThread()
    341         Dim currentThread = Thread.CurrentThread()
     321        Dim currentThread = CurrentThread()
    342322
    343323        Dim i As Long
    344324        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*/
    356335    'カレントスレッドを取得
    357336    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
    365357            End If
    366358        Next
    367 
    368         ' TODO: エラー処理
    369         OutputDebugString( "カレントスレッドの取得に失敗" )
    370         debug
    371     End Function
    372 
     359    End Function
    373360
    374361Private
     
    376363    ' スレッド固有の例外処理制御
    377364    '------------------------------------------
    378     ppException As **ExceptionService
    379 
    380 Public
    381     Function GetCurrentException() As *ExceptionService
    382         Dim dwNowThreadId As DWord
    383         dwNowThreadId=GetCurrentThreadId()
     365
     366Public
     367    Function GetCurrentException() As ExceptionService
     368        Dim dwNowThreadId = GetCurrentThreadId()
    384369
    385370        Dim i As Long
    386371        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
    393381    End Function
    394382End Class
    395 Dim _System_pobj_AllThreads As *_System_CThreadCollection
     383
     384Type ThreadInfo
     385    thread As Thread
     386    stackBase As *LONG_PTR
     387    exception As ExceptionService
     388End Type
     389
     390End Namespace 'Detail
Note: See TracChangeset for help on using the changeset viewer.