1 | 'threading.sbp
|
---|
2 |
|
---|
3 |
|
---|
4 | '--------------------------------------------------------------------
|
---|
5 | ' スレッドの優先順位
|
---|
6 | '--------------------------------------------------------------------
|
---|
7 | Enum ThreadPriority
|
---|
8 | Highest = 2
|
---|
9 | AboveNormal = 1
|
---|
10 | Normal = 0
|
---|
11 | BelowNormal = -1
|
---|
12 | Lowest = -2
|
---|
13 | End Enum
|
---|
14 |
|
---|
15 | TypeDef PTHREAD_START_ROUTINE = *Function(args As VoidPtr) As DWord
|
---|
16 |
|
---|
17 |
|
---|
18 | '--------------------------------------------------------------------
|
---|
19 | ' スレッド クラス
|
---|
20 | '--------------------------------------------------------------------
|
---|
21 | Class Thread
|
---|
22 | m_hThread As HANDLE
|
---|
23 | m_dwThreadId As DWord
|
---|
24 | m_Priority As ThreadPriority
|
---|
25 |
|
---|
26 | m_fp As PTHREAD_START_ROUTINE
|
---|
27 | m_args As VoidPtr
|
---|
28 |
|
---|
29 | Public
|
---|
30 | Sub Thread()
|
---|
31 | m_hThread=0
|
---|
32 | m_dwThreadId=0
|
---|
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
|
---|
39 | m_dwThreadId=0
|
---|
40 | m_Priority=ThreadPriority.Normal
|
---|
41 |
|
---|
42 | m_fp=fp
|
---|
43 | m_args=args
|
---|
44 | End Sub
|
---|
45 |
|
---|
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
|
---|
53 |
|
---|
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(thread As Thread) As Boolean
|
---|
64 | Return m_dwThreadId = thread.m_dwThreadId
|
---|
65 | End Function
|
---|
66 |
|
---|
67 | '-----------------------
|
---|
68 | ' Public Properties
|
---|
69 | '-----------------------
|
---|
70 |
|
---|
71 |
|
---|
72 | 'Priority Property
|
---|
73 | Sub Priority(value As ThreadPriority)
|
---|
74 | m_Priority=value
|
---|
75 | SetThreadPriority(m_hThread,value)
|
---|
76 | End Sub
|
---|
77 | Function Priority() As ThreadPriority
|
---|
78 | Return m_Priority
|
---|
79 | End Function
|
---|
80 |
|
---|
81 | 'ThreadId
|
---|
82 | Function ThreadId() As DWord
|
---|
83 | Return m_dwThreadId
|
---|
84 | End Function
|
---|
85 |
|
---|
86 |
|
---|
87 |
|
---|
88 |
|
---|
89 |
|
---|
90 | Sub Start()
|
---|
91 | Dim ThreadId As DWord
|
---|
92 | m_hThread=_beginthreadex(NULL,0,AddressOf(_run),VarPtr(This),CREATE_SUSPENDED,m_dwThreadId)
|
---|
93 | SetThreadPriority(m_hThread,m_Priority)
|
---|
94 | Resume()
|
---|
95 | End Sub
|
---|
96 |
|
---|
97 | Private
|
---|
98 | Function Cdecl _run() As Long
|
---|
99 | '------------
|
---|
100 | ' 前処理
|
---|
101 | '------------
|
---|
102 |
|
---|
103 | 'GCにスレッド開始を通知
|
---|
104 | _System_pobj_AllThreads->BeginThread(VarPtr(This),_System_GetSp() As *LONG_PTR)
|
---|
105 |
|
---|
106 |
|
---|
107 | '------------
|
---|
108 | '実行
|
---|
109 | '------------
|
---|
110 | _run=Run()
|
---|
111 |
|
---|
112 |
|
---|
113 | '------------
|
---|
114 | '後処理
|
---|
115 | '------------
|
---|
116 |
|
---|
117 | 'GCにスレッド終了を通知
|
---|
118 | _System_pobj_AllThreads->EndThread(VarPtr(This))
|
---|
119 |
|
---|
120 | '自身のスレッドハンドルを閉じる
|
---|
121 | CloseHandle(m_hThread)
|
---|
122 | m_hThread=0
|
---|
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()
|
---|
134 | If SuspendThread(m_hThread) = &HFFFFFFFF Then
|
---|
135 | debug
|
---|
136 | End If
|
---|
137 | End Sub
|
---|
138 | Sub Resume()
|
---|
139 | If ResumeThread(m_hThread) = &HFFFFFFFF Then
|
---|
140 | debug
|
---|
141 | End If
|
---|
142 | End Sub
|
---|
143 |
|
---|
144 | Function __GetContext(ByRef Context As CONTEXT) As BOOL
|
---|
145 | Return GetThreadContext(m_hThread,Context)
|
---|
146 | End Function
|
---|
147 | Function __SetContext(ByRef Context As CONTEXT) As BOOL
|
---|
148 | Return SetThreadContext(m_hThread,Context)
|
---|
149 | End Function
|
---|
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 |
|
---|
160 | '--------------------------------------------------------------------
|
---|
161 | ' すべてのスレッドの管理
|
---|
162 | '--------------------------------------------------------------------
|
---|
163 | ' TODO: このクラスをシングルトンにする
|
---|
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()
|
---|
173 | ppobj_Thread=GC_malloc(1)
|
---|
174 | pStackBase=HeapAlloc(_System_hProcessHeap,0,1)
|
---|
175 | ppException=HeapAlloc(_System_hProcessHeap,0,1)
|
---|
176 | ThreadNum=0
|
---|
177 |
|
---|
178 | 'クリティカルセッションを生成
|
---|
179 | InitializeCriticalSection(CriticalSection)
|
---|
180 | End Sub
|
---|
181 |
|
---|
182 | Sub ~_System_CThreadCollection()
|
---|
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 |
|
---|
197 | HeapFree(_System_hProcessHeap,0,pStackBase)
|
---|
198 | pStackBase=0
|
---|
199 |
|
---|
200 | HeapFree(_System_hProcessHeap,0,ppException)
|
---|
201 | ppException = 0
|
---|
202 |
|
---|
203 | ThreadNum=0
|
---|
204 |
|
---|
205 | 'クリティカルセッションを破棄
|
---|
206 | DeleteCriticalSection(CriticalSection)
|
---|
207 | End Sub
|
---|
208 |
|
---|
209 | 'スレッドを生成
|
---|
210 | Sub BeginThread(pThread As *Thread,NowSp As *LONG_PTR)
|
---|
211 | EnterCriticalSection(CriticalSection)
|
---|
212 |
|
---|
213 | '例外処理管理用オブジェクトを生成
|
---|
214 | Dim pException As *ExceptionService
|
---|
215 | pException = New ExceptionService
|
---|
216 |
|
---|
217 | Dim i As Long
|
---|
218 | For i=0 To ELM(ThreadNum)
|
---|
219 | If ppobj_Thread[i] = 0 Then
|
---|
220 | ppobj_Thread[i] = pThread
|
---|
221 | pStackBase[i] = NowSp
|
---|
222 | ppException[i] = pException
|
---|
223 | Exit For
|
---|
224 | End If
|
---|
225 | Next
|
---|
226 |
|
---|
227 | If i = ThreadNum Then
|
---|
228 | ppobj_Thread=realloc(ppobj_Thread,(ThreadNum+1)*SizeOf(*Thread))
|
---|
229 | ppobj_Thread[ThreadNum]=pThread
|
---|
230 | pStackBase=HeapReAlloc(_System_hProcessHeap,0,pStackBase,(ThreadNum+1)*SizeOf(LONG_PTR))
|
---|
231 | pStackBase[ThreadNum]=NowSp
|
---|
232 | ppException=HeapReAlloc(_System_hProcessHeap,0,ppException,(ThreadNum+1)*SizeOf(*ExceptionService))
|
---|
233 | ppException[ThreadNum]=pException
|
---|
234 | ThreadNum++
|
---|
235 | End If
|
---|
236 | LeaveCriticalSection(CriticalSection)
|
---|
237 | End Sub
|
---|
238 |
|
---|
239 | 'スレッドを終了
|
---|
240 | Sub EndThread(pThread As *Thread)
|
---|
241 | EnterCriticalSection(CriticalSection)
|
---|
242 | Dim i As Long
|
---|
243 | For i=0 To ELM(ThreadNum)
|
---|
244 | If ppobj_Thread[i] = pThread Then
|
---|
245 | If i = 0 Then
|
---|
246 | Delete pThread
|
---|
247 | End If
|
---|
248 | ppobj_Thread[i]=0
|
---|
249 | pStackBase[i]=0
|
---|
250 | Delete ppException[i]
|
---|
251 | ppException[i]=0
|
---|
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 |
|
---|
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 |
|
---|
312 | 'カレントスレッドを取得
|
---|
313 | Function CurrentThread(ByRef obj_Thread As Thread) As Boolean
|
---|
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])
|
---|
321 | Return True
|
---|
322 | End If
|
---|
323 | Next
|
---|
324 |
|
---|
325 | Return False
|
---|
326 | End Function
|
---|
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
|
---|
349 | End Class
|
---|
350 | Dim _System_pobj_AllThreads As *_System_CThreadCollection
|
---|