[19] | 1 | 'threading.sbp
|
---|
[1] | 2 |
|
---|
[58] | 3 |
|
---|
| 4 | '--------------------------------------------------------------------
|
---|
| 5 | ' スレッドの優先順位
|
---|
| 6 | '--------------------------------------------------------------------
|
---|
[1] | 7 | Enum ThreadPriority
|
---|
[19] | 8 | Highest = 2
|
---|
| 9 | AboveNormal = 1
|
---|
| 10 | Normal = 0
|
---|
| 11 | BelowNormal = -1
|
---|
| 12 | Lowest = -2
|
---|
[1] | 13 | End Enum
|
---|
| 14 |
|
---|
| 15 | TypeDef PTHREAD_START_ROUTINE = *Function(args As VoidPtr) As DWord
|
---|
| 16 |
|
---|
[58] | 17 |
|
---|
| 18 | '--------------------------------------------------------------------
|
---|
| 19 | ' スレッド クラス
|
---|
| 20 | '--------------------------------------------------------------------
|
---|
[1] | 21 | Class Thread
|
---|
| 22 | m_hThread As HANDLE
|
---|
[19] | 23 | m_dwThreadId As DWord
|
---|
[1] | 24 | m_Priority As ThreadPriority
|
---|
| 25 |
|
---|
| 26 | m_fp As PTHREAD_START_ROUTINE
|
---|
| 27 | m_args As VoidPtr
|
---|
[58] | 28 |
|
---|
[1] | 29 | Public
|
---|
| 30 | Sub Thread()
|
---|
| 31 | m_hThread=0
|
---|
[19] | 32 | m_dwThreadId=0
|
---|
[1] | 33 | m_Priority=ThreadPriority.Normal
|
---|
| 34 |
|
---|
| 35 | m_fp=0
|
---|
| 36 | End Sub
|
---|
| 37 | Sub Thread(fp As PTHREAD_START_ROUTINE, args As VoidPtr)
|
---|
| 38 | m_hThread=0
|
---|
[19] | 39 | m_dwThreadId=0
|
---|
[1] | 40 | m_Priority=ThreadPriority.Normal
|
---|
| 41 |
|
---|
| 42 | m_fp=fp
|
---|
| 43 | m_args=args
|
---|
| 44 | End Sub
|
---|
| 45 |
|
---|
[19] | 46 | Sub Thread(ByRef obj As Thread)
|
---|
| 47 | m_hThread=obj.m_hThread
|
---|
| 48 | m_dwThreadId=obj.m_dwThreadId
|
---|
| 49 | m_Priority=obj.m_Priority
|
---|
| 50 | m_fp=obj.m_fp
|
---|
| 51 | m_args=obj.m_args
|
---|
| 52 | End Sub
|
---|
[1] | 53 |
|
---|
[237] | 54 | Sub Thread(hThread As HANDLE, dwThreadId As DWord, dummy As Long)
|
---|
[19] | 55 | m_hThread=hThread
|
---|
| 56 | m_dwThreadId=dwThreadId
|
---|
| 57 | End Sub
|
---|
| 58 |
|
---|
| 59 | Sub ~Thread()
|
---|
| 60 | End Sub
|
---|
| 61 |
|
---|
| 62 |
|
---|
[237] | 63 | Function Equals(thread As Thread) As Boolean
|
---|
| 64 | Return m_dwThreadId = thread.m_dwThreadId
|
---|
[19] | 65 | End Function
|
---|
| 66 |
|
---|
| 67 | '-----------------------
|
---|
| 68 | ' Public Properties
|
---|
| 69 | '-----------------------
|
---|
| 70 |
|
---|
[237] | 71 |
|
---|
[19] | 72 | 'Priority Property
|
---|
[1] | 73 | Sub Priority(value As ThreadPriority)
|
---|
| 74 | m_Priority=value
|
---|
[19] | 75 | SetThreadPriority(m_hThread,value)
|
---|
[1] | 76 | End Sub
|
---|
| 77 | Function Priority() As ThreadPriority
|
---|
| 78 | Return m_Priority
|
---|
| 79 | End Function
|
---|
| 80 |
|
---|
[19] | 81 | 'ThreadId
|
---|
| 82 | Function ThreadId() As DWord
|
---|
| 83 | Return m_dwThreadId
|
---|
| 84 | End Function
|
---|
[1] | 85 |
|
---|
[19] | 86 |
|
---|
| 87 |
|
---|
| 88 |
|
---|
[237] | 89 |
|
---|
[1] | 90 | Sub Start()
|
---|
| 91 | Dim ThreadId As DWord
|
---|
[19] | 92 | m_hThread=_beginthreadex(NULL,0,AddressOf(_run),VarPtr(This),CREATE_SUSPENDED,m_dwThreadId)
|
---|
[1] | 93 | SetThreadPriority(m_hThread,m_Priority)
|
---|
| 94 | Resume()
|
---|
| 95 | End Sub
|
---|
| 96 |
|
---|
| 97 | Private
|
---|
[19] | 98 | Function Cdecl _run() As Long
|
---|
[1] | 99 | '------------
|
---|
| 100 | ' 前処理
|
---|
| 101 | '------------
|
---|
| 102 |
|
---|
| 103 | 'GCにスレッド開始を通知
|
---|
[148] | 104 | _System_pobj_AllThreads->BeginThread(VarPtr(This),_System_GetSp() As *LONG_PTR)
|
---|
[1] | 105 |
|
---|
| 106 |
|
---|
| 107 | '------------
|
---|
| 108 | '実行
|
---|
| 109 | '------------
|
---|
[19] | 110 | _run=Run()
|
---|
[1] | 111 |
|
---|
| 112 |
|
---|
| 113 | '------------
|
---|
| 114 | '後処理
|
---|
| 115 | '------------
|
---|
| 116 |
|
---|
| 117 | 'GCにスレッド終了を通知
|
---|
[148] | 118 | _System_pobj_AllThreads->EndThread(VarPtr(This))
|
---|
[1] | 119 |
|
---|
| 120 | '自身のスレッドハンドルを閉じる
|
---|
| 121 | CloseHandle(m_hThread)
|
---|
[19] | 122 | m_hThread=0
|
---|
[1] | 123 |
|
---|
| 124 | End Function
|
---|
| 125 |
|
---|
| 126 | Public
|
---|
| 127 | Virtual Function Run() As Long
|
---|
| 128 | If m_fp Then
|
---|
| 129 | Run=m_fp(m_args)
|
---|
| 130 | End If
|
---|
| 131 | End Function
|
---|
| 132 |
|
---|
| 133 | Sub Suspend()
|
---|
[148] | 134 | If SuspendThread(m_hThread) = &HFFFFFFFF Then
|
---|
| 135 | debug
|
---|
| 136 | End If
|
---|
[1] | 137 | End Sub
|
---|
| 138 | Sub Resume()
|
---|
[148] | 139 | If ResumeThread(m_hThread) = &HFFFFFFFF Then
|
---|
| 140 | debug
|
---|
| 141 | End If
|
---|
[1] | 142 | End Sub
|
---|
| 143 |
|
---|
[19] | 144 | Function __GetContext(ByRef Context As CONTEXT) As BOOL
|
---|
| 145 | Return GetThreadContext(m_hThread,Context)
|
---|
| 146 | End Function
|
---|
[58] | 147 | Function __SetContext(ByRef Context As CONTEXT) As BOOL
|
---|
| 148 | Return SetThreadContext(m_hThread,Context)
|
---|
| 149 | End Function
|
---|
[19] | 150 |
|
---|
| 151 |
|
---|
| 152 | Static Function CurrentThread() As Thread
|
---|
| 153 | Dim obj_Thread As Thread()
|
---|
| 154 | _System_pobj_AllThreads->CurrentThread(obj_Thread)
|
---|
| 155 | Return obj_Thread
|
---|
| 156 | End Function
|
---|
| 157 | End Class
|
---|
| 158 |
|
---|
| 159 |
|
---|
[58] | 160 | '--------------------------------------------------------------------
|
---|
| 161 | ' すべてのスレッドの管理
|
---|
| 162 | '--------------------------------------------------------------------
|
---|
| 163 | ' TODO: このクラスをシングルトンにする
|
---|
[19] | 164 | Class _System_CThreadCollection
|
---|
| 165 | Public
|
---|
| 166 | ppobj_Thread As **Thread
|
---|
| 167 | pStackBase As **LONG_PTR
|
---|
| 168 | ThreadNum As Long
|
---|
| 169 |
|
---|
| 170 | CriticalSection As CRITICAL_SECTION
|
---|
| 171 |
|
---|
| 172 | Sub _System_CThreadCollection()
|
---|
[148] | 173 | ppobj_Thread=GC_malloc(1)
|
---|
[19] | 174 | pStackBase=HeapAlloc(_System_hProcessHeap,0,1)
|
---|
[58] | 175 | ppException=HeapAlloc(_System_hProcessHeap,0,1)
|
---|
[19] | 176 | ThreadNum=0
|
---|
| 177 |
|
---|
| 178 | 'クリティカルセッションを生成
|
---|
| 179 | InitializeCriticalSection(CriticalSection)
|
---|
[1] | 180 | End Sub
|
---|
[19] | 181 |
|
---|
| 182 | Sub ~_System_CThreadCollection()
|
---|
[234] | 183 | Dim i As Long
|
---|
| 184 | For i=0 To ELM(ThreadNum)
|
---|
| 185 | If ppobj_Thread[i] Then
|
---|
| 186 | If i = 0 Then
|
---|
| 187 | Delete ppobj_Thread[i]
|
---|
| 188 | End If
|
---|
| 189 | ppobj_Thread[i]=0
|
---|
| 190 | pStackBase[i]=0
|
---|
| 191 | Delete ppException[i]
|
---|
| 192 | ppException[i]=0
|
---|
| 193 | Exit For
|
---|
| 194 | End If
|
---|
| 195 | Next
|
---|
| 196 |
|
---|
[19] | 197 | HeapFree(_System_hProcessHeap,0,pStackBase)
|
---|
| 198 | pStackBase=0
|
---|
| 199 |
|
---|
[58] | 200 | HeapFree(_System_hProcessHeap,0,ppException)
|
---|
| 201 | ppException = 0
|
---|
| 202 |
|
---|
[19] | 203 | ThreadNum=0
|
---|
| 204 |
|
---|
| 205 | 'クリティカルセッションを破棄
|
---|
| 206 | DeleteCriticalSection(CriticalSection)
|
---|
| 207 | End Sub
|
---|
| 208 |
|
---|
| 209 | 'スレッドを生成
|
---|
[148] | 210 | Sub BeginThread(pThread As *Thread,NowSp As *LONG_PTR)
|
---|
[19] | 211 | EnterCriticalSection(CriticalSection)
|
---|
| 212 |
|
---|
[58] | 213 | '例外処理管理用オブジェクトを生成
|
---|
| 214 | Dim pException As *ExceptionService
|
---|
| 215 | pException = New ExceptionService
|
---|
| 216 |
|
---|
[19] | 217 | Dim i As Long
|
---|
| 218 | For i=0 To ELM(ThreadNum)
|
---|
[58] | 219 | If ppobj_Thread[i] = 0 Then
|
---|
[148] | 220 | ppobj_Thread[i] = pThread
|
---|
[58] | 221 | pStackBase[i] = NowSp
|
---|
| 222 | ppException[i] = pException
|
---|
[19] | 223 | Exit For
|
---|
| 224 | End If
|
---|
| 225 | Next
|
---|
| 226 |
|
---|
[58] | 227 | If i = ThreadNum Then
|
---|
[148] | 228 | ppobj_Thread=realloc(ppobj_Thread,(ThreadNum+1)*SizeOf(*Thread))
|
---|
| 229 | ppobj_Thread[ThreadNum]=pThread
|
---|
[19] | 230 | pStackBase=HeapReAlloc(_System_hProcessHeap,0,pStackBase,(ThreadNum+1)*SizeOf(LONG_PTR))
|
---|
| 231 | pStackBase[ThreadNum]=NowSp
|
---|
[58] | 232 | ppException=HeapReAlloc(_System_hProcessHeap,0,ppException,(ThreadNum+1)*SizeOf(*ExceptionService))
|
---|
| 233 | ppException[ThreadNum]=pException
|
---|
[19] | 234 | ThreadNum++
|
---|
| 235 | End If
|
---|
| 236 | LeaveCriticalSection(CriticalSection)
|
---|
| 237 | End Sub
|
---|
| 238 |
|
---|
| 239 | 'スレッドを終了
|
---|
[148] | 240 | Sub EndThread(pThread As *Thread)
|
---|
[19] | 241 | EnterCriticalSection(CriticalSection)
|
---|
| 242 | Dim i As Long
|
---|
| 243 | For i=0 To ELM(ThreadNum)
|
---|
[148] | 244 | If ppobj_Thread[i] = pThread Then
|
---|
| 245 | If i = 0 Then
|
---|
| 246 | Delete pThread
|
---|
| 247 | End If
|
---|
[19] | 248 | ppobj_Thread[i]=0
|
---|
| 249 | pStackBase[i]=0
|
---|
[220] | 250 | Delete ppException[i]
|
---|
| 251 | ppException[i]=0
|
---|
[19] | 252 | Exit For
|
---|
| 253 | End If
|
---|
| 254 | Next
|
---|
| 255 | LeaveCriticalSection(CriticalSection)
|
---|
| 256 | End Sub
|
---|
| 257 |
|
---|
| 258 | ' すべてのスレッドを中断
|
---|
| 259 | Sub SuspendAllThread()
|
---|
| 260 | Dim i As Long
|
---|
| 261 | For i=0 To ELM(ThreadNum)
|
---|
| 262 | If ppobj_Thread[i] Then
|
---|
| 263 | ppobj_Thread[i]->Suspend()
|
---|
| 264 | End If
|
---|
| 265 | Next
|
---|
| 266 | End Sub
|
---|
| 267 |
|
---|
| 268 | ' すべてのスレッドを再開
|
---|
| 269 | Sub ResumeAllThread()
|
---|
| 270 | Dim i As Long
|
---|
| 271 | For i=0 To ELM(ThreadNum)
|
---|
| 272 | If ppobj_Thread[i] Then
|
---|
| 273 | ppobj_Thread[i]->Resume()
|
---|
| 274 | End If
|
---|
| 275 | Next
|
---|
| 276 | End Sub
|
---|
| 277 |
|
---|
[148] | 278 | ' 自分以外のスレッドを中断
|
---|
| 279 | Sub SuspendAnotherThread()
|
---|
| 280 | Dim currentThread = Thread.CurrentThread()
|
---|
| 281 |
|
---|
| 282 | Dim i As Long
|
---|
| 283 | For i=0 To ELM(ThreadNum)
|
---|
| 284 |
|
---|
| 285 | If currentThread.Equals( ByVal ppobj_Thread[i] ) Then
|
---|
| 286 | Continue
|
---|
| 287 | End If
|
---|
| 288 |
|
---|
| 289 | If ppobj_Thread[i] Then
|
---|
| 290 | ppobj_Thread[i]->Suspend()
|
---|
| 291 | End If
|
---|
| 292 | Next
|
---|
| 293 | End Sub
|
---|
| 294 |
|
---|
| 295 | ' 自分以外のスレッドを再開
|
---|
| 296 | Sub ResumeAnotherThread()
|
---|
| 297 | Dim currentThread = Thread.CurrentThread()
|
---|
| 298 |
|
---|
| 299 | Dim i As Long
|
---|
| 300 | For i=0 To ELM(ThreadNum)
|
---|
| 301 |
|
---|
| 302 | If currentThread.Equals( ByVal ppobj_Thread[i] ) Then
|
---|
| 303 | Continue
|
---|
| 304 | End If
|
---|
| 305 |
|
---|
| 306 | If ppobj_Thread[i] Then
|
---|
| 307 | ppobj_Thread[i]->Resume()
|
---|
| 308 | End If
|
---|
| 309 | Next
|
---|
| 310 | End Sub
|
---|
| 311 |
|
---|
[58] | 312 | 'カレントスレッドを取得
|
---|
[237] | 313 | Function CurrentThread(ByRef obj_Thread As Thread) As Boolean
|
---|
[19] | 314 | Dim dwNowThreadId As DWord
|
---|
| 315 | dwNowThreadId=GetCurrentThreadId()
|
---|
| 316 |
|
---|
| 317 | Dim i As Long
|
---|
| 318 | For i=0 To ELM(ThreadNum)
|
---|
| 319 | If ppobj_Thread[i]->ThreadId=dwNowThreadId Then
|
---|
| 320 | obj_Thread.Thread(ByVal ppobj_Thread[i])
|
---|
[237] | 321 | Return True
|
---|
[19] | 322 | End If
|
---|
| 323 | Next
|
---|
| 324 |
|
---|
[237] | 325 | Return False
|
---|
[19] | 326 | End Function
|
---|
[58] | 327 |
|
---|
| 328 |
|
---|
| 329 | Private
|
---|
| 330 | '------------------------------------------
|
---|
| 331 | ' スレッド固有の例外処理制御
|
---|
| 332 | '------------------------------------------
|
---|
| 333 | ppException As **ExceptionService
|
---|
| 334 |
|
---|
| 335 | Public
|
---|
| 336 | Function GetCurrentException() As *ExceptionService
|
---|
| 337 | Dim dwNowThreadId As DWord
|
---|
| 338 | dwNowThreadId=GetCurrentThreadId()
|
---|
| 339 |
|
---|
| 340 | Dim i As Long
|
---|
| 341 | For i=0 To ELM(ThreadNum)
|
---|
| 342 | If ppobj_Thread[i]->ThreadId=dwNowThreadId Then
|
---|
| 343 | Return ppException[i]
|
---|
| 344 | End If
|
---|
| 345 | Next
|
---|
| 346 |
|
---|
| 347 | Return NULL
|
---|
| 348 | End Function
|
---|
[1] | 349 | End Class
|
---|
[19] | 350 | Dim _System_pobj_AllThreads As *_System_CThreadCollection
|
---|