1 /******************************************************************************
2 *
3 * Copyright (C) 2007, The Gentee Group. All rights reserved.
4 * This file is part of the Gentee open source project - http://www.gentee.com.
5 *
6 * THIS FILE IS PROVIDED UNDER THE TERMS OF THE GENTEE LICENSE ("AGREEMENT").
7 * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE CONSTITUTES RECIPIENTS
8 * ACCEPTANCE OF THE AGREEMENT.
9 *
10 * Author: Alexey Krivonogov ( gentee )
11 *
12 ******************************************************************************/
13
14 /*-----------------------------------------------------------------------------
15 * Id: thread L "Thread"
16 *
17 * Summary: This library allows you to create threads and work with them.
18 The methods described above are applied to variables of the
19 #b(thread) type. For using this library, it is required to specify
20 the file thread.g (from lib\thread
21 subfolder) with include command. #srcg[
22 |include : $"...\gentee\lib\thread\thread.g"]
23 *
24 * List: *#lng/methods#,thread_create,thread_getexitcode,thread_isactive,
25 thread_resume,thread_suspend,thread_terminate,thread_wait,
26 *#lng/funcs#,exitthread,sleep
27 *
28 -----------------------------------------------------------------------------*/
29
30 define <export>
31 {
32 STILL_ACTIVE = 0x00000103
33 INFINITE = 0xFFFFFFFF
34 WAIT_FAILED = 0xFFFFFFFF
35 }
36
37 type thread
38 {
39 uint handle
40 uint pmem
41 }
42
43 import "kernel32.dll"
44 {
45 uint CreateThread( uint, uint, uint, uint, uint, uint )
46 ExitThread( uint )
47 uint GetExitCodeThread( uint, uint )
48 uint GetTickCount()
49 uint ResumeThread( uint )
50 Sleep( uint )
51 uint SuspendThread( uint )
52 uint TerminateThread( uint, uint )
53 uint WaitForSingleObject( uint, uint )
54 }
55
56 /*-----------------------------------------------------------------------------
57 * Id: exitthread F
58 *
59 * Summary: Exiting the current thread.
60 *
61 * Params: code - Thread exit code.
62 *
63 -----------------------------------------------------------------------------*/
64
65 func exitthread( uint code )
66 {
67 ExitThread( code )
68 }
69
70 /*-----------------------------------------------------------------------------
71 * Id: thread_create F2
72 *
73 * Summary: Create a thread.
74 *
75 * Params: idfunc - The pointer to the function that will be called as a new /
76 thread. The function must have one parameter. You can get /
77 the pointer using the operator &.
78 param - Additional parameter.
79 *
80 * Return: The handle of the created thread is returned. It returns 0 in case
81 of an error.
82 *
83 -----------------------------------------------------------------------------*/
84
85 method uint thread.create( uint idfunc, uint param )
86 {
87 uint id
88
89 .pmem = callback( idfunc, 1 )
90
91 // return this.handle = createthread( .pmem, idfunc, param )
92 return .handle = CreateThread( 0, 0, .pmem, param, 0, &id )
93 }
94
95 method thread.delete
96 {
97 if .pmem
98 {
99 freecallback( .pmem )
100 .pmem = 0
101 }
102 }
103
104 /*-----------------------------------------------------------------------------
105 * Id: thread_isactive F3
106 *
107 * Summary: Checking if a thread is active.
108 *
109 * Return: Returns 1 if the thread is active and 0 otherwise.
110 *
111 -----------------------------------------------------------------------------*/
112
113 method uint thread.isactive()
114 {
115 uint result
116
117 if GetExitCodeThread( this.handle, &result )
118 {
119 return result == $STILL_ACTIVE
120 }
121 return 0
122 }
123
124 /*-----------------------------------------------------------------------------
125 * Id: thread_getexitcode F2
126 *
127 * Summary: Get the thread exit code.
128 *
129 * Params: result - The pointer to a variable of the uint type the thread exit /
130 code will be written to. If the thread is still active, /
131 the value $STILL_ACTIVE will be written.
132 *
133 * Return: #lng/retf#
134 *
135 -----------------------------------------------------------------------------*/
136
137 method uint thread.getexitcode( uint result )
138 {
139 return GetExitCodeThread( this.handle, result )
140 }
141
142 /*-----------------------------------------------------------------------------
143 * Id: thread_resume F3
144 *
145 * Summary: Resuming a thread. Resume a thread paused with the
146 #a(thread_suspend ) method.
147 *
148 * Return: #lng/retf#
149 *
150 -----------------------------------------------------------------------------*/
151
152 method uint thread.resume()
153 {
154 return ResumeThread( this.handle ) != 0xFFFFFFFF
155 }
156
157 /*-----------------------------------------------------------------------------
158 * Id: thread_suspend F3
159 *
160 * Summary: Stop a thread.
161 *
162 * Return: #lng/retf#
163 *
164 -----------------------------------------------------------------------------*/
165
166 method uint thread.suspend()
167 {
168 return SuspendThread( this.handle ) != 0xFFFFFFFF
169 }
170
171 /*-----------------------------------------------------------------------------
172 * Id: thread_terminate F2
173 *
174 * Summary: Terminating a thread.
175 *
176 * Params: code - Thread termination code.
177 *
178 * Return: #lng/retf#
179 *
180 -----------------------------------------------------------------------------*/
181
182 method uint thread.terminate( uint code )
183 {
184 this.delete()
185 return TerminateThread( this.handle, code )
186 }
187
188 /*-----------------------------------------------------------------------------
189 * Id: sleep F
190 *
191 * Summary: Pause the current thread for the specified time.
192 *
193 * Params: msec - The time for pausing the thread in milliseconds.
194 *
195 * Return: #lng/retf#
196 *
197 -----------------------------------------------------------------------------*/
198
199 func sleep( uint msec )
200 {
201 Sleep( msec )
202 }
203
204 /*-----------------------------------------------------------------------------
205 ** Id: thread_wait F3
206 *
207 * Summary: Waiting till a thread is exited.
208 *
209 * Return: #lng/retf#
210 *
211 -----------------------------------------------------------------------------*/
212
213 method uint thread.wait()
214 {
215 return WaitForSingleObject( this.handle, $INFINITE ) != $WAIT_FAILED
216 }
217
Редактировать