Changeset 148


Ignore:
Timestamp:
Mar 11, 2007, 4:51:49 AM (17 years ago)
Author:
dai
Message:

ThreadクラスをGCに対応させた。

File:
1 edited

Legend:

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

    r58 r148  
    2222    m_hThread As HANDLE
    2323    m_dwThreadId As DWord
    24 
    2524    m_Priority As ThreadPriority
    2625
     
    5655        m_hThread=hThread
    5756        m_dwThreadId=dwThreadId
    58         m_Priority=ThreadPriority.Normal
    5957    End Sub
    6058
     
    107105
    108106        'GCにスレッド開始を通知
    109         _System_pobj_AllThreads->BeginThread(This,_System_GetSp() As *LONG_PTR)
     107        _System_pobj_AllThreads->BeginThread(VarPtr(This),_System_GetSp() As *LONG_PTR)
    110108
    111109
     
    121119
    122120        'GCにスレッド終了を通知
    123         _System_pobj_AllThreads->EndThread(This)
     121        _System_pobj_AllThreads->EndThread(VarPtr(This))
    124122
    125123        '自身のスレッドハンドルを閉じる
     
    137135
    138136    Sub Suspend()
    139         SuspendThread(m_hThread)
     137        If SuspendThread(m_hThread) = &HFFFFFFFF Then
     138            debug
     139        End If
    140140    End Sub
    141141    Sub Resume()
    142         ResumeThread(m_hThread)
     142        If ResumeThread(m_hThread) = &HFFFFFFFF Then
     143            debug
     144        End If
    143145    End Sub
    144146
     
    172174
    173175    Sub _System_CThreadCollection()
    174         ppobj_Thread=HeapAlloc(_System_hProcessHeap,0,1)
     176        ppobj_Thread=GC_malloc(1)
    175177        pStackBase=HeapAlloc(_System_hProcessHeap,0,1)
    176178        ppException=HeapAlloc(_System_hProcessHeap,0,1)
     
    182184
    183185    Sub ~_System_CThreadCollection()
    184     End Sub
    185 
    186     Sub Finalize()
    187         HeapFree(_System_hProcessHeap,0,ppobj_Thread)
    188         ppobj_Thread=0
    189 
    190186        HeapFree(_System_hProcessHeap,0,pStackBase)
    191187        pStackBase=0
     
    201197
    202198    'スレッドを生成
    203     Sub BeginThread(ByRef obj_Thread As Thread,NowSp As *LONG_PTR)
     199    Sub BeginThread(pThread As *Thread,NowSp As *LONG_PTR)
    204200        EnterCriticalSection(CriticalSection)
    205 
    206             'スレッドオブジェクトを生成
    207             Dim pobj_NewThread As *Thread
    208             pobj_NewThread=New Thread(obj_Thread)
    209201
    210202            '例外処理管理用オブジェクトを生成
     
    215207            For i=0 To ELM(ThreadNum)
    216208                If ppobj_Thread[i] = 0 Then
    217                     ppobj_Thread[i] = pobj_NewThread
     209                    ppobj_Thread[i] = pThread
    218210                    pStackBase[i] = NowSp
    219211                    ppException[i] = pException
     
    223215
    224216            If i = ThreadNum Then
    225                 ppobj_Thread=HeapReAlloc(_System_hProcessHeap,0,ppobj_Thread,(ThreadNum+1)*SizeOf(*Thread))
    226                 ppobj_Thread[ThreadNum]=pobj_NewThread
     217                ppobj_Thread=realloc(ppobj_Thread,(ThreadNum+1)*SizeOf(*Thread))
     218                ppobj_Thread[ThreadNum]=pThread
    227219                pStackBase=HeapReAlloc(_System_hProcessHeap,0,pStackBase,(ThreadNum+1)*SizeOf(LONG_PTR))
    228220                pStackBase[ThreadNum]=NowSp
     
    235227
    236228    'スレッドを終了
    237     Sub EndThread(ByRef obj_Thread As Thread)
     229    Sub EndThread(pThread As *Thread)
    238230        EnterCriticalSection(CriticalSection)
    239231            Dim i As Long
    240232            For i=0 To ELM(ThreadNum)
    241                 If ppobj_Thread[i]->Equals(obj_Thread) Then
    242                     Delete ppobj_Thread[i]
     233                If ppobj_Thread[i] = pThread Then
     234                    If i = 0 Then
     235                        Delete pThread
     236                    End If
    243237                    ppobj_Thread[i]=0
    244238                    pStackBase[i]=0
     
    269263    End Sub
    270264
     265    ' 自分以外のスレッドを中断
     266    Sub SuspendAnotherThread()
     267        Dim currentThread = Thread.CurrentThread()
     268
     269        Dim i As Long
     270        For i=0 To ELM(ThreadNum)
     271
     272            If currentThread.Equals( ByVal ppobj_Thread[i] ) Then
     273                Continue
     274            End If
     275
     276            If ppobj_Thread[i] Then
     277                ppobj_Thread[i]->Suspend()
     278            End If
     279        Next
     280    End Sub
     281
     282    ' 自分以外のスレッドを再開
     283    Sub ResumeAnotherThread()
     284        Dim currentThread = Thread.CurrentThread()
     285
     286        Dim i As Long
     287        For i=0 To ELM(ThreadNum)
     288
     289            If currentThread.Equals( ByVal ppobj_Thread[i] ) Then
     290                Continue
     291            End If
     292
     293            If ppobj_Thread[i] Then
     294                ppobj_Thread[i]->Resume()
     295            End If
     296        Next
     297    End Sub
     298
    271299    'カレントスレッドを取得
    272300    Function CurrentThread(ByRef obj_Thread As Thread) As BOOL
Note: See TracChangeset for help on using the changeset viewer.