Changeset 19
- Timestamp:
- Nov 27, 2006, 2:51:04 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Include/Classes/System/Threading/Thread.ab
r1 r19 1 'threading. ab1 'threading.sbp 2 2 3 3 Enum ThreadPriority 4 Highest = THREAD_PRIORITY_HIGHEST5 AboveNormal = THREAD_PRIORITY_ABOVE_NORMAL6 Normal = THREAD_PRIORITY_NORMAL7 BelowNormal = THREAD_PRIORITY_BELOW_NORMAL8 Lowest = THREAD_PRIORITY_LOWEST4 Highest = 2 5 AboveNormal = 1 6 Normal = 0 7 BelowNormal = -1 8 Lowest = -2 9 9 End Enum 10 10 … … 13 13 Class Thread 14 14 m_hThread As HANDLE 15 m_dwThreadId As DWord 16 15 17 m_Priority As ThreadPriority 16 18 … … 20 22 Sub Thread() 21 23 m_hThread=0 24 m_dwThreadId=0 22 25 m_Priority=ThreadPriority.Normal 23 26 … … 26 29 Sub Thread(fp As PTHREAD_START_ROUTINE, args As VoidPtr) 27 30 m_hThread=0 31 m_dwThreadId=0 28 32 m_Priority=ThreadPriority.Normal 29 33 … … 32 36 End Sub 33 37 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 35 70 Sub Priority(value As ThreadPriority) 36 71 m_Priority=value 37 If m_hThread Then 38 SetThreadPriority(m_hThread, value) 39 End If 72 SetThreadPriority(m_hThread,value) 40 73 End Sub 41 74 Function Priority() As ThreadPriority … … 43 76 End Function 44 77 78 'ThreadId 79 Function ThreadId() As DWord 80 Return m_dwThreadId 81 End Function 82 83 84 45 85 46 86 Sub Start() 47 87 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) 49 89 SetThreadPriority(m_hThread,m_Priority) 50 90 Resume() … … 52 92 53 93 Private 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 61 95 '------------ 62 96 ' 前処理 … … 64 98 65 99 'GCにスレッド開始を通知 66 _System_ GC.BeginThread(m_hThread,_System_GetSp() As *LONG_PTR)100 _System_pobj_AllThreads->BeginThread(This,_System_GetSp() As *LONG_PTR) 67 101 68 102 … … 70 104 '実行 71 105 '------------ 72 ThreadStartup =Run()106 _run=Run() 73 107 74 108 … … 78 112 79 113 'GCにスレッド終了を通知 80 _System_ GC.EndThread(m_hThread)114 _System_pobj_AllThreads->EndThread(This) 81 115 82 116 '自身のスレッドハンドルを閉じる 83 117 CloseHandle(m_hThread) 84 m_hThread =0118 m_hThread=0 85 119 86 120 End Function … … 100 134 End Sub 101 135 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 107 146 End Class 147 148 149 'すべてのスレッドの管理 150 Class _System_CThreadCollection 151 Public 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 259 End Class 260 Dim _System_pobj_AllThreads As *_System_CThreadCollection
Note:
See TracChangeset
for help on using the changeset viewer.