Changeset 19


Ignore:
Timestamp:
Nov 27, 2006, 2:51:04 AM (18 years ago)
Author:
dai
Message:
 
File:
1 edited

Legend:

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

    r1 r19  
    1 'threading.ab
     1'threading.sbp
    22
    33Enum ThreadPriority
    4     Highest = THREAD_PRIORITY_HIGHEST
    5     AboveNormal = THREAD_PRIORITY_ABOVE_NORMAL
    6     Normal = THREAD_PRIORITY_NORMAL
    7     BelowNormal = THREAD_PRIORITY_BELOW_NORMAL
    8     Lowest = THREAD_PRIORITY_LOWEST
     4    Highest =      2
     5    AboveNormal =  1
     6    Normal =       0
     7    BelowNormal = -1
     8    Lowest =      -2
    99End Enum
    1010
     
    1313Class Thread
    1414    m_hThread As HANDLE
     15    m_dwThreadId As DWord
     16
    1517    m_Priority As ThreadPriority
    1618
     
    2022    Sub Thread()
    2123        m_hThread=0
     24        m_dwThreadId=0
    2225        m_Priority=ThreadPriority.Normal
    2326
     
    2629    Sub Thread(fp As PTHREAD_START_ROUTINE, args As VoidPtr)
    2730        m_hThread=0
     31        m_dwThreadId=0
    2832        m_Priority=ThreadPriority.Normal
    2933
     
    3236    End Sub
    3337
    34 
     38    Sub Thread(ByRef obj As Thread)
     39        m_hThread=obj.m_hThread
     40        m_dwThreadId=obj.m_dwThreadId
     41        m_Priority=obj.m_Priority
     42        m_fp=obj.m_fp
     43        m_args=obj.m_args
     44    End Sub
     45
     46    Sub Thread(hThread As HANDLE,dwThreadId As DWord,dummy As Long)
     47        m_hThread=hThread
     48        m_dwThreadId=dwThreadId
     49        m_Priority=ThreadPriority.Normal
     50    End Sub
     51
     52    Sub ~Thread()
     53    End Sub
     54
     55
     56    Function Equals(ByRef obj_Thread As Thread) As BOOL
     57        If m_dwThreadId=obj_Thread.m_dwThreadId Then
     58            Return TRUE
     59        End If
     60        Return FALSE
     61    End Function
     62
     63
     64
     65    '-----------------------
     66    ' Public Properties
     67    '-----------------------
     68
     69    'Priority Property
    3570    Sub Priority(value As ThreadPriority)
    3671        m_Priority=value
    37         If m_hThread Then
    38             SetThreadPriority(m_hThread, value)
    39         End If
     72        SetThreadPriority(m_hThread,value)
    4073    End Sub
    4174    Function Priority() As ThreadPriority
     
    4376    End Function
    4477
     78    'ThreadId
     79    Function ThreadId() As DWord
     80        Return m_dwThreadId
     81    End Function
     82
     83
     84
    4585
    4686    Sub Start()
    4787        Dim ThreadId As DWord
    48         m_hThread=_beginthreadex(NULL,0,AddressOf(ThreadEntryPoint),VarPtr(This),CREATE_SUSPENDED,ThreadId)
     88        m_hThread=_beginthreadex(NULL,0,AddressOf(_run),VarPtr(This),CREATE_SUSPENDED,m_dwThreadId)
    4989        SetThreadPriority(m_hThread,m_Priority)
    5090        Resume()
     
    5292
    5393Private
    54     Static Function ThreadEntryPoint(pv As VoidPtr) As DWord
    55         Dim pThis As *Thread
    56         pThis = pv
    57         ThreadEntryPoint = pThis->ThreadStartup() As DWord
    58     End Function
    59 
    60     Function ThreadStartup() As Long
     94    Function Cdecl _run() As Long
    6195        '------------
    6296        ' 前処理
     
    6498
    6599        'GCにスレッド開始を通知
    66         _System_GC.BeginThread(m_hThread,_System_GetSp() As *LONG_PTR)
     100        _System_pobj_AllThreads->BeginThread(This,_System_GetSp() As *LONG_PTR)
    67101
    68102
     
    70104        '実行
    71105        '------------
    72         ThreadStartup = Run()
     106        _run=Run()
    73107
    74108
     
    78112
    79113        'GCにスレッド終了を通知
    80         _System_GC.EndThread(m_hThread)
     114        _System_pobj_AllThreads->EndThread(This)
    81115
    82116        '自身のスレッドハンドルを閉じる
    83117        CloseHandle(m_hThread)
    84         m_hThread = 0
     118        m_hThread=0
    85119
    86120    End Function
     
    100134    End Sub
    101135
    102     Sub Join()
    103         If m_hThread Then
    104             WaitForSingleObject(m_hThread, INFINITE)
    105         End If
    106     End Sub
     136    Function __GetContext(ByRef Context As CONTEXT) As BOOL
     137        Return GetThreadContext(m_hThread,Context)
     138    End Function
     139
     140
     141    Static Function CurrentThread() As Thread
     142        Dim obj_Thread As Thread()
     143        _System_pobj_AllThreads->CurrentThread(obj_Thread)
     144        Return obj_Thread
     145    End Function
    107146End Class
     147
     148
     149'すべてのスレッドの管理
     150Class _System_CThreadCollection
     151Public
     152    ppobj_Thread As **Thread
     153    pStackBase As **LONG_PTR
     154    ThreadNum As Long
     155
     156    CriticalSection As CRITICAL_SECTION
     157
     158    Sub _System_CThreadCollection()
     159        ppobj_Thread=HeapAlloc(_System_hProcessHeap,0,1)
     160        pStackBase=HeapAlloc(_System_hProcessHeap,0,1)
     161        ThreadNum=0
     162
     163        'クリティカルセッションを生成
     164        InitializeCriticalSection(CriticalSection)
     165    End Sub
     166
     167    Sub ~_System_CThreadCollection()
     168    End Sub
     169
     170    Sub Finalize()
     171        HeapFree(_System_hProcessHeap,0,ppobj_Thread)
     172        ppobj_Thread=0
     173
     174        HeapFree(_System_hProcessHeap,0,pStackBase)
     175        pStackBase=0
     176
     177        ThreadNum=0
     178
     179        'クリティカルセッションを破棄
     180        DeleteCriticalSection(CriticalSection)
     181    End Sub
     182
     183    'スレッドを生成
     184    Sub BeginThread(ByRef obj_Thread As Thread,NowSp As *LONG_PTR)
     185        EnterCriticalSection(CriticalSection)
     186
     187            Dim pobj_NewThread As *Thread
     188            pobj_NewThread=New Thread(obj_Thread)
     189
     190            Dim i As Long
     191            For i=0 To ELM(ThreadNum)
     192                If ppobj_Thread[i]=0 Then
     193                    ppobj_Thread[i]=pobj_NewThread
     194                    pStackBase[i]=NowSp
     195                    Exit For
     196                End If
     197            Next
     198
     199            If i=ThreadNum Then
     200                ppobj_Thread=HeapReAlloc(_System_hProcessHeap,0,ppobj_Thread,(ThreadNum+1)*SizeOf(*Thread))
     201                ppobj_Thread[ThreadNum]=pobj_NewThread
     202                pStackBase=HeapReAlloc(_System_hProcessHeap,0,pStackBase,(ThreadNum+1)*SizeOf(LONG_PTR))
     203                pStackBase[ThreadNum]=NowSp
     204                ThreadNum++
     205            End If
     206        LeaveCriticalSection(CriticalSection)
     207    End Sub
     208
     209    'スレッドを終了
     210    Sub EndThread(ByRef obj_Thread As Thread)
     211        EnterCriticalSection(CriticalSection)
     212            Dim i As Long
     213            For i=0 To ELM(ThreadNum)
     214                If ppobj_Thread[i]->Equals(obj_Thread) Then
     215                    Delete ppobj_Thread[i]
     216                    ppobj_Thread[i]=0
     217                    pStackBase[i]=0
     218                    Exit For
     219                End If
     220            Next
     221        LeaveCriticalSection(CriticalSection)
     222    End Sub
     223
     224    ' すべてのスレッドを中断
     225    Sub SuspendAllThread()
     226        Dim i As Long
     227        For i=0 To ELM(ThreadNum)
     228            If ppobj_Thread[i] Then
     229                ppobj_Thread[i]->Suspend()
     230            End If
     231        Next
     232    End Sub
     233
     234    ' すべてのスレッドを再開
     235    Sub ResumeAllThread()
     236        Dim i As Long
     237        For i=0 To ELM(ThreadNum)
     238            If ppobj_Thread[i] Then
     239                ppobj_Thread[i]->Resume()
     240            End If
     241        Next
     242    End Sub
     243
     244
     245    Function CurrentThread(ByRef obj_Thread As Thread) As BOOL
     246        Dim dwNowThreadId As DWord
     247        dwNowThreadId=GetCurrentThreadId()
     248
     249        Dim i As Long
     250        For i=0 To ELM(ThreadNum)
     251            If ppobj_Thread[i]->ThreadId=dwNowThreadId Then
     252                obj_Thread.Thread(ByVal ppobj_Thread[i])
     253                Return 1
     254            End If
     255        Next
     256
     257        Return 0
     258    End Function
     259End Class
     260Dim _System_pobj_AllThreads As *_System_CThreadCollection
Note: See TracChangeset for help on using the changeset viewer.