1 | 'threading.sbp
|
---|
2 |
|
---|
3 | Enum ThreadPriority
|
---|
4 | Highest = 2
|
---|
5 | AboveNormal = 1
|
---|
6 | Normal = 0
|
---|
7 | BelowNormal = -1
|
---|
8 | Lowest = -2
|
---|
9 | End Enum
|
---|
10 |
|
---|
11 | TypeDef PTHREAD_START_ROUTINE = *Function(args As VoidPtr) As DWord
|
---|
12 |
|
---|
13 | Class Thread
|
---|
14 | m_hThread As HANDLE
|
---|
15 | m_dwThreadId As DWord
|
---|
16 |
|
---|
17 | m_Priority As ThreadPriority
|
---|
18 |
|
---|
19 | m_fp As PTHREAD_START_ROUTINE
|
---|
20 | m_args As VoidPtr
|
---|
21 | Public
|
---|
22 | Sub Thread()
|
---|
23 | m_hThread=0
|
---|
24 | m_dwThreadId=0
|
---|
25 | m_Priority=ThreadPriority.Normal
|
---|
26 |
|
---|
27 | m_fp=0
|
---|
28 | End Sub
|
---|
29 | Sub Thread(fp As PTHREAD_START_ROUTINE, args As VoidPtr)
|
---|
30 | m_hThread=0
|
---|
31 | m_dwThreadId=0
|
---|
32 | m_Priority=ThreadPriority.Normal
|
---|
33 |
|
---|
34 | m_fp=fp
|
---|
35 | m_args=args
|
---|
36 | End Sub
|
---|
37 |
|
---|
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 _System_TRUE
|
---|
59 | End If
|
---|
60 | Return _System_FALSE
|
---|
61 | End Function
|
---|
62 |
|
---|
63 |
|
---|
64 |
|
---|
65 | '-----------------------
|
---|
66 | ' Public Properties
|
---|
67 | '-----------------------
|
---|
68 |
|
---|
69 | 'Priority Property
|
---|
70 | Sub Priority(value As ThreadPriority)
|
---|
71 | m_Priority=value
|
---|
72 | SetThreadPriority(m_hThread,value)
|
---|
73 | End Sub
|
---|
74 | Function Priority() As ThreadPriority
|
---|
75 | Return m_Priority
|
---|
76 | End Function
|
---|
77 |
|
---|
78 | 'ThreadId
|
---|
79 | Function ThreadId() As DWord
|
---|
80 | Return m_dwThreadId
|
---|
81 | End Function
|
---|
82 |
|
---|
83 |
|
---|
84 |
|
---|
85 |
|
---|
86 | Sub Start()
|
---|
87 | Dim ThreadId As DWord
|
---|
88 | m_hThread=_beginthreadex(NULL,0,AddressOf(_run),VarPtr(This),CREATE_SUSPENDED,m_dwThreadId)
|
---|
89 | SetThreadPriority(m_hThread,m_Priority)
|
---|
90 | Resume()
|
---|
91 | End Sub
|
---|
92 |
|
---|
93 | Private
|
---|
94 | Function Cdecl _run() As Long
|
---|
95 | '------------
|
---|
96 | ' 前処理
|
---|
97 | '------------
|
---|
98 |
|
---|
99 | 'GCにスレッド開始を通知
|
---|
100 | _System_pobj_AllThreads->BeginThread(This,_System_GetSp() As *LONG_PTR)
|
---|
101 |
|
---|
102 |
|
---|
103 | '------------
|
---|
104 | '実行
|
---|
105 | '------------
|
---|
106 | _run=Run()
|
---|
107 |
|
---|
108 |
|
---|
109 | '------------
|
---|
110 | '後処理
|
---|
111 | '------------
|
---|
112 |
|
---|
113 | 'GCにスレッド終了を通知
|
---|
114 | _System_pobj_AllThreads->EndThread(This)
|
---|
115 |
|
---|
116 | '自身のスレッドハンドルを閉じる
|
---|
117 | CloseHandle(m_hThread)
|
---|
118 | m_hThread=0
|
---|
119 |
|
---|
120 | End Function
|
---|
121 |
|
---|
122 | Public
|
---|
123 | Virtual Function Run() As Long
|
---|
124 | If m_fp Then
|
---|
125 | Run=m_fp(m_args)
|
---|
126 | End If
|
---|
127 | End Function
|
---|
128 |
|
---|
129 | Sub Suspend()
|
---|
130 | SuspendThread(m_hThread)
|
---|
131 | End Sub
|
---|
132 | Sub Resume()
|
---|
133 | ResumeThread(m_hThread)
|
---|
134 | End Sub
|
---|
135 |
|
---|
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
|
---|
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
|
---|