Changeset 18 for Include/system/gc.sbp


Ignore:
Timestamp:
Nov 27, 2006, 2:50:43 AM (17 years ago)
Author:
dai
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Include/system/gc.sbp

    r1 r18  
    2929    iAllSize As SIZE_T
    3030
    31     'スレッド情報
    32     phThread As *HANDLE
    33     pStackBase As **LONG_PTR
    34     ThreadNum As Long
    35 
    3631    CriticalSection As CRITICAL_SECTION
    3732
     
    4742        iAllSize=0
    4843
    49         'スレッド情報
    50         phThread=HeapAlloc(_System_hProcessHeap,0,1)
    51         pStackBase=HeapAlloc(_System_hProcessHeap,0,1)
    52         ThreadNum=0
     44        'スレッド情報管理用オブジェクトを生成
     45        _System_pobj_AllThreads=_System_malloc(SizeOf(_System_CThreadCollection)+SizeOf(LONG_PTR))
     46        _System_pobj_AllThreads->_System_CThreadCollection()
    5347
    5448        'クリティカルセッションを生成
    5549        InitializeCriticalSection(CriticalSection)
    5650
    57         '開始時のスレッドを通知
     51
     52        '---------------------------
     53        ' 開始時のスレッドを通知
     54        '---------------------------
    5855        Dim hTargetThread As HANDLE
    5956        DuplicateHandle(GetCurrentProcess(),
     
    6158            GetCurrentProcess(),
    6259            hTargetThread, 0, FALSE, DUPLICATE_SAME_ACCESS)     'カレントスレッドのハンドルを複製
    63         BeginThread(hTargetThread,_System_gc_StackRoot_StartPtr As *LONG_PTR)
     60
     61
     62        '自身のThreadオブジェクトを生成
     63        Dim obj_Thread As Thread(hTargetThread,GetCurrentThreadId(),0)
     64
     65        _System_pobj_AllThreads->BeginThread(obj_Thread,_System_gc_StackRoot_StartPtr As *LONG_PTR)
    6466    End Sub
    6567    Sub ~_System_CGarbageCollection()
     
    7981        ' すべてのスレッドを一時停止
    8082        '-------------------------------------
     83        _System_pobj_AllThreads->SuspendAllThread()
     84
     85
    8186        Dim i As Long
    82         For i=0 To ELM(ThreadNum)
    83             If phThread[i] Then
    84                 SuspendThread(phThread[i])
    85             End If
    86         Next
    87 
    88 
    8987        For i=0 To ELM(n)
    9088            If ppPtr[i] Then HeapFree(_System_hProcessHeap,0,ppPtr[i])
     
    9896        pbFlags=0
    9997
    100         'スレッド情報
    101         HeapFree(_System_hProcessHeap,0,phThread)
    102         phThread=0
    103         HeapFree(_System_hProcessHeap,0,pStackBase)
    104         pStackBase=0
     98        'スレッド情報管理用オブジェクトを破棄
     99        _System_pobj_AllThreads->Finalize()
     100        _System_free(_System_pobj_AllThreads)
     101        _System_pobj_AllThreads=0
    105102
    106103        'クリティカルセッションを破棄
     
    159156
    160157    Function __realloc(lpMem As VoidPtr, size As SIZE_T) As VoidPtr
    161         Dim i As Long
    162         For i=0 To ELM(n)
    163             If ppPtr[i]=lpMem Then
    164                 iAllSize+=size-pSize[i]
    165 
    166                 pSize[i]=size
    167                 ppPtr[i]=HeapReAlloc(_System_hProcessHeap,HEAP_ZERO_MEMORY,lpMem,size)
    168                 Return ppPtr[i]
    169             End If
    170         Next
     158        EnterCriticalSection(CriticalSection)
     159            Dim i As Long
     160            For i=0 To ELM(n)
     161                If ppPtr[i]=lpMem Then
     162                    iAllSize+=size-pSize[i]
     163
     164                    pSize[i]=size
     165                    ppPtr[i]=HeapReAlloc(_System_hProcessHeap,HEAP_ZERO_MEMORY,lpMem,size)
     166
     167                    LeaveCriticalSection(CriticalSection)
     168                    Return ppPtr[i]
     169                End If
     170            Next
     171        LeaveCriticalSection(CriticalSection)
    171172        Return 0
    172173    End Function
    173174
    174175    Sub __free(lpMem As VoidPtr)
    175         Dim i As Long
    176         For i=0 To ELM(n)
    177             If ppPtr[i]=lpMem Then
    178                 If pbFlags[i] and _System_GC_FLAG_NEEDFREE Then
    179                     iAllSize-=pSize[i]
    180 
    181                     HeapFree(_System_hProcessHeap,0,ppPtr[i])
    182                     ppPtr[i]=0
    183                     pSize[i]=0
    184                 Else
    185                     OutputDebugString(Ex"GCが管理しているメモリ空間を解放しようとしました。\r\n")
     176        EnterCriticalSection(CriticalSection)
     177            Dim i As Long
     178            For i=0 To ELM(n)
     179                If ppPtr[i]=lpMem Then
     180                    If pbFlags[i] and _System_GC_FLAG_NEEDFREE Then
     181                        iAllSize-=pSize[i]
     182
     183                        HeapFree(_System_hProcessHeap,0,ppPtr[i])
     184                        ppPtr[i]=0
     185                        pSize[i]=0
     186                    Else
     187                        OutputDebugString(Ex"GCが管理しているメモリ空間を解放しようとしました。\r\n")
     188                    End If
    186189                End If
    187             End If
    188         Next
     190            Next
     191        LeaveCriticalSection(CriticalSection)
    189192    End Sub
    190193
     
    212215        ' すべてのスレッドを一時停止
    213216        '-------------------------------------
    214         Dim i As Long
    215         For i=0 To ELM(ThreadNum)
    216             If phThread[i] Then
    217                 SuspendThread(phThread[i])
    218             End If
    219         Next
     217        _System_pobj_AllThreads->SuspendAllThread()
    220218
    221219
     
    232230        'ローカル領域をルートに指定してスキャン
    233231        '-----------------------------------------------
     232        Dim Context As CONTEXT
    234233        Dim NowSp As *LONG_PTR
    235         Dim Context As CONTEXT
    236234        Dim size As LONG_PTR
    237         For i=0 To ELM(ThreadNum)
    238             If phThread[i] Then
     235        Dim i As Long
     236        For i=0 To ELM(_System_pobj_AllThreads->ThreadNum)
     237            If _System_pobj_AllThreads->ppobj_Thread[i] Then
    239238                FillMemory(VarPtr(Context),SizeOf(CONTEXT),0)
    240239                Context.ContextFlags=CONTEXT_CONTROL
    241                 If GetThreadContext(phThread[i],Context)=0 Then
     240                If _System_pobj_AllThreads->ppobj_Thread[i]->__GetContext(Context)=0 Then
    242241                    OutputDebugString(Ex"レジスタ情報の取得に失敗しました。\r\n")
    243242                End If
     
    249248#endif
    250249
    251                 size=(pStackBase[i] As LONG_PTR)-(NowSp As LONG_PTR)
     250                size=(_System_pobj_AllThreads->pStackBase[i] As LONG_PTR)-(NowSp As LONG_PTR)
    252251
    253252                scan(NowSp,size)
     
    282281        ' すべてのスレッドを再開
    283282        '-------------------------------------
    284         For i=0 To ELM(ThreadNum)
    285             If phThread[i] Then
    286                 ResumeThread(phThread[i])
    287             End If
    288         Next
    289     End Function
    290 
    291 
    292 
    293     '----------------------------
    294     ' スレッド制御のメソッド
    295     '----------------------------
    296     Sub BeginThread(hThread As HANDLE,NowSp As *LONG_PTR)
    297         EnterCriticalSection(CriticalSection)
    298             Dim i As Long
    299             For i=0 To ELM(ThreadNum)
    300                 If phThread[i]=0 Then
    301                     phThread[i]=hThread
    302                     pStackBase[i]=NowSp
    303                     Exit For
    304                 End If
    305             Next
    306 
    307             If i=ThreadNum Then
    308                 phThread=HeapReAlloc(_System_hProcessHeap,0,phThread,(ThreadNum+1)*SizeOf(HANDLE))
    309                 phThread[ThreadNum]=hThread
    310                 pStackBase=HeapReAlloc(_System_hProcessHeap,0,pStackBase,(ThreadNum+1)*SizeOf(LONG_PTR))
    311                 pStackBase[ThreadNum]=NowSp
    312                 ThreadNum++
    313             End If
    314         LeaveCriticalSection(CriticalSection)
    315     End Sub
    316     Sub EndThread(hThread As HANDLE)
    317         EnterCriticalSection(CriticalSection)
    318             Dim i As Long
    319             For i=0 To ELM(ThreadNum)
    320                 If phThread[i]=hThread Then
    321                     phThread[i]=0
    322                     pStackBase[i]=0
    323                     Exit For
    324                 End If
    325             Next
    326         LeaveCriticalSection(CriticalSection)
    327     End Sub
     283        _System_pobj_AllThreads->ResumeAllThread()
     284    End Function
    328285
    329286
Note: See TracChangeset for help on using the changeset viewer.