[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 |
|
---|
[19] | 54 | Sub Thread(hThread As HANDLE,dwThreadId As DWord,dummy As Long)
|
---|
| 55 | m_hThread=hThread
|
---|
| 56 | m_dwThreadId=dwThreadId
|
---|
| 57 | End Sub
|
---|
| 58 |
|
---|
| 59 | Sub ~Thread()
|
---|
| 60 | End Sub
|
---|
| 61 |
|
---|
| 62 |
|
---|
| 63 | Function Equals(ByRef obj_Thread As Thread) As BOOL
|
---|
| 64 | If m_dwThreadId=obj_Thread.m_dwThreadId Then
|
---|
[42] | 65 | Return _System_TRUE
|
---|
[19] | 66 | End If
|
---|
[42] | 67 | Return _System_FALSE
|
---|
[19] | 68 | End Function
|
---|
| 69 |
|
---|
| 70 |
|
---|
| 71 |
|
---|
| 72 | '-----------------------
|
---|
| 73 | ' Public Properties
|
---|
| 74 | '-----------------------
|
---|
| 75 |
|
---|
| 76 | 'Priority Property
|
---|
[1] | 77 | Sub Priority(value As ThreadPriority)
|
---|
| 78 | m_Priority=value
|
---|
[19] | 79 | SetThreadPriority(m_hThread,value)
|
---|
[1] | 80 | End Sub
|
---|
| 81 | Function Priority() As ThreadPriority
|
---|
| 82 | Return m_Priority
|
---|
| 83 | End Function
|
---|
| 84 |
|
---|
[19] | 85 | 'ThreadId
|
---|
| 86 | Function ThreadId() As DWord
|
---|
| 87 | Return m_dwThreadId
|
---|
| 88 | End Function
|
---|
[1] | 89 |
|
---|
[19] | 90 |
|
---|
| 91 |
|
---|
| 92 |
|
---|
[1] | 93 | Sub Start()
|
---|
| 94 | Dim ThreadId As DWord
|
---|
[19] | 95 | m_hThread=_beginthreadex(NULL,0,AddressOf(_run),VarPtr(This),CREATE_SUSPENDED,m_dwThreadId)
|
---|
[1] | 96 | SetThreadPriority(m_hThread,m_Priority)
|
---|
| 97 | Resume()
|
---|
| 98 | End Sub
|
---|
| 99 |
|
---|
| 100 | Private
|
---|
[19] | 101 | Function Cdecl _run() As Long
|
---|
[1] | 102 | '------------
|
---|
| 103 | ' 前処理
|
---|
| 104 | '------------
|
---|
| 105 |
|
---|
| 106 | 'GCにスレッド開始を通知
|
---|
[148] | 107 | _System_pobj_AllThreads->BeginThread(VarPtr(This),_System_GetSp() As *LONG_PTR)
|
---|
[1] | 108 |
|
---|
| 109 |
|
---|
| 110 | '------------
|
---|
| 111 | '実行
|
---|
| 112 | '------------
|
---|
[19] | 113 | _run=Run()
|
---|
[1] | 114 |
|
---|
| 115 |
|
---|
| 116 | '------------
|
---|
| 117 | '後処理
|
---|
| 118 | '------------
|
---|
| 119 |
|
---|
| 120 | 'GCにスレッド終了を通知
|
---|
[148] | 121 | _System_pobj_AllThreads->EndThread(VarPtr(This))
|
---|
[1] | 122 |
|
---|
| 123 | '自身のスレッドハンドルを閉じる
|
---|
| 124 | CloseHandle(m_hThread)
|
---|
[19] | 125 | m_hThread=0
|
---|
[1] | 126 |
|
---|
| 127 | End Function
|
---|
| 128 |
|
---|
| 129 | Public
|
---|
| 130 | Virtual Function Run() As Long
|
---|
| 131 | If m_fp Then
|
---|
| 132 | Run=m_fp(m_args)
|
---|
| 133 | End If
|
---|
| 134 | End Function
|
---|
| 135 |
|
---|
| 136 | Sub Suspend()
|
---|
[148] | 137 | If SuspendThread(m_hThread) = &HFFFFFFFF Then
|
---|
| 138 | debug
|
---|
| 139 | End If
|
---|
[1] | 140 | End Sub
|
---|
| 141 | Sub Resume()
|
---|
[148] | 142 | If ResumeThread(m_hThread) = &HFFFFFFFF Then
|
---|
| 143 | debug
|
---|
| 144 | End If
|
---|
[1] | 145 | End Sub
|
---|
| 146 |
|
---|
[19] | 147 | Function __GetContext(ByRef Context As CONTEXT) As BOOL
|
---|
| 148 | Return GetThreadContext(m_hThread,Context)
|
---|
| 149 | End Function
|
---|
[58] | 150 | Function __SetContext(ByRef Context As CONTEXT) As BOOL
|
---|
| 151 | Return SetThreadContext(m_hThread,Context)
|
---|
| 152 | End Function
|
---|
[19] | 153 |
|
---|
| 154 |
|
---|
| 155 | Static Function CurrentThread() As Thread
|
---|
| 156 | Dim obj_Thread As Thread()
|
---|
| 157 | _System_pobj_AllThreads->CurrentThread(obj_Thread)
|
---|
| 158 | Return obj_Thread
|
---|
| 159 | End Function
|
---|
| 160 | End Class
|
---|
| 161 |
|
---|
| 162 |
|
---|
[58] | 163 | '--------------------------------------------------------------------
|
---|
| 164 | ' すべてのスレッドの管理
|
---|
| 165 | '--------------------------------------------------------------------
|
---|
| 166 | ' TODO: このクラスをシングルトンにする
|
---|
[19] | 167 | Class _System_CThreadCollection
|
---|
| 168 | Public
|
---|
| 169 | ppobj_Thread As **Thread
|
---|
| 170 | pStackBase As **LONG_PTR
|
---|
| 171 | ThreadNum As Long
|
---|
| 172 |
|
---|
| 173 | CriticalSection As CRITICAL_SECTION
|
---|
| 174 |
|
---|
| 175 | Sub _System_CThreadCollection()
|
---|
[148] | 176 | ppobj_Thread=GC_malloc(1)
|
---|
[19] | 177 | pStackBase=HeapAlloc(_System_hProcessHeap,0,1)
|
---|
[58] | 178 | ppException=HeapAlloc(_System_hProcessHeap,0,1)
|
---|
[19] | 179 | ThreadNum=0
|
---|
| 180 |
|
---|
| 181 | 'クリティカルセッションを生成
|
---|
| 182 | InitializeCriticalSection(CriticalSection)
|
---|
[1] | 183 | End Sub
|
---|
[19] | 184 |
|
---|
| 185 | Sub ~_System_CThreadCollection()
|
---|
| 186 | HeapFree(_System_hProcessHeap,0,pStackBase)
|
---|
| 187 | pStackBase=0
|
---|
| 188 |
|
---|
[58] | 189 | HeapFree(_System_hProcessHeap,0,ppException)
|
---|
| 190 | ppException = 0
|
---|
| 191 |
|
---|
[19] | 192 | ThreadNum=0
|
---|
| 193 |
|
---|
| 194 | 'クリティカルセッションを破棄
|
---|
| 195 | DeleteCriticalSection(CriticalSection)
|
---|
| 196 | End Sub
|
---|
| 197 |
|
---|
| 198 | 'スレッドを生成
|
---|
[148] | 199 | Sub BeginThread(pThread As *Thread,NowSp As *LONG_PTR)
|
---|
[19] | 200 | EnterCriticalSection(CriticalSection)
|
---|
| 201 |
|
---|
[58] | 202 | '例外処理管理用オブジェクトを生成
|
---|
| 203 | Dim pException As *ExceptionService
|
---|
| 204 | pException = New ExceptionService
|
---|
| 205 |
|
---|
[19] | 206 | Dim i As Long
|
---|
| 207 | For i=0 To ELM(ThreadNum)
|
---|
[58] | 208 | If ppobj_Thread[i] = 0 Then
|
---|
[148] | 209 | ppobj_Thread[i] = pThread
|
---|
[58] | 210 | pStackBase[i] = NowSp
|
---|
| 211 | ppException[i] = pException
|
---|
[19] | 212 | Exit For
|
---|
| 213 | End If
|
---|
| 214 | Next
|
---|
| 215 |
|
---|
[58] | 216 | If i = ThreadNum Then
|
---|
[148] | 217 | ppobj_Thread=realloc(ppobj_Thread,(ThreadNum+1)*SizeOf(*Thread))
|
---|
| 218 | ppobj_Thread[ThreadNum]=pThread
|
---|
[19] | 219 | pStackBase=HeapReAlloc(_System_hProcessHeap,0,pStackBase,(ThreadNum+1)*SizeOf(LONG_PTR))
|
---|
| 220 | pStackBase[ThreadNum]=NowSp
|
---|
[58] | 221 | ppException=HeapReAlloc(_System_hProcessHeap,0,ppException,(ThreadNum+1)*SizeOf(*ExceptionService))
|
---|
| 222 | ppException[ThreadNum]=pException
|
---|
[19] | 223 | ThreadNum++
|
---|
| 224 | End If
|
---|
| 225 | LeaveCriticalSection(CriticalSection)
|
---|
| 226 | End Sub
|
---|
| 227 |
|
---|
| 228 | 'スレッドを終了
|
---|
[148] | 229 | Sub EndThread(pThread As *Thread)
|
---|
[19] | 230 | EnterCriticalSection(CriticalSection)
|
---|
| 231 | Dim i As Long
|
---|
| 232 | For i=0 To ELM(ThreadNum)
|
---|
[148] | 233 | If ppobj_Thread[i] = pThread Then
|
---|
| 234 | If i = 0 Then
|
---|
| 235 | Delete pThread
|
---|
| 236 | End If
|
---|
[19] | 237 | ppobj_Thread[i]=0
|
---|
| 238 | pStackBase[i]=0
|
---|
| 239 | Exit For
|
---|
| 240 | End If
|
---|
| 241 | Next
|
---|
| 242 | LeaveCriticalSection(CriticalSection)
|
---|
| 243 | End Sub
|
---|
| 244 |
|
---|
| 245 | ' すべてのスレッドを中断
|
---|
| 246 | Sub SuspendAllThread()
|
---|
| 247 | Dim i As Long
|
---|
| 248 | For i=0 To ELM(ThreadNum)
|
---|
| 249 | If ppobj_Thread[i] Then
|
---|
| 250 | ppobj_Thread[i]->Suspend()
|
---|
| 251 | End If
|
---|
| 252 | Next
|
---|
| 253 | End Sub
|
---|
| 254 |
|
---|
| 255 | ' すべてのスレッドを再開
|
---|
| 256 | Sub ResumeAllThread()
|
---|
| 257 | Dim i As Long
|
---|
| 258 | For i=0 To ELM(ThreadNum)
|
---|
| 259 | If ppobj_Thread[i] Then
|
---|
| 260 | ppobj_Thread[i]->Resume()
|
---|
| 261 | End If
|
---|
| 262 | Next
|
---|
| 263 | End Sub
|
---|
| 264 |
|
---|
[148] | 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 |
|
---|
[58] | 299 | 'カレントスレッドを取得
|
---|
[19] | 300 | Function CurrentThread(ByRef obj_Thread As Thread) As BOOL
|
---|
| 301 | Dim dwNowThreadId As DWord
|
---|
| 302 | dwNowThreadId=GetCurrentThreadId()
|
---|
| 303 |
|
---|
| 304 | Dim i As Long
|
---|
| 305 | For i=0 To ELM(ThreadNum)
|
---|
| 306 | If ppobj_Thread[i]->ThreadId=dwNowThreadId Then
|
---|
| 307 | obj_Thread.Thread(ByVal ppobj_Thread[i])
|
---|
| 308 | Return 1
|
---|
| 309 | End If
|
---|
| 310 | Next
|
---|
| 311 |
|
---|
| 312 | Return 0
|
---|
| 313 | End Function
|
---|
[58] | 314 |
|
---|
| 315 |
|
---|
| 316 | Private
|
---|
| 317 | '------------------------------------------
|
---|
| 318 | ' スレッド固有の例外処理制御
|
---|
| 319 | '------------------------------------------
|
---|
| 320 | ppException As **ExceptionService
|
---|
| 321 |
|
---|
| 322 | Public
|
---|
| 323 | Function GetCurrentException() As *ExceptionService
|
---|
| 324 | Dim dwNowThreadId As DWord
|
---|
| 325 | dwNowThreadId=GetCurrentThreadId()
|
---|
| 326 |
|
---|
| 327 | Dim i As Long
|
---|
| 328 | For i=0 To ELM(ThreadNum)
|
---|
| 329 | If ppobj_Thread[i]->ThreadId=dwNowThreadId Then
|
---|
| 330 | Return ppException[i]
|
---|
| 331 | End If
|
---|
| 332 | Next
|
---|
| 333 |
|
---|
| 334 | Return NULL
|
---|
| 335 | End Function
|
---|
[1] | 336 | End Class
|
---|
[19] | 337 | Dim _System_pobj_AllThreads As *_System_CThreadCollection
|
---|