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 ResumeThread( uint )
49 Sleep( uint )
50 uint SuspendThread( uint )
51 uint TerminateThread( uint, uint )
52 uint WaitForSingleObject( uint, uint )
53 }
54
55 /*-----------------------------------------------------------------------------
56 * Id: exitthread F
57 *
58 * Summary: Exiting the current thread.
59 *
60 * Params: code - Thread exit code.
61 *
62 -----------------------------------------------------------------------------*/
63
64 func exitthread( uint code )
65 {
66 ExitThread( code )
67 }
68
69 /*-----------------------------------------------------------------------------
70 * Id: thread_create F2
71 *
72 * Summary: Create a thread.
73 *
74 * Params: idfunc - The pointer to the function that will be called as a new /
75 thread. The function must have one parameter. You can get /
76 the pointer using the operator &.
77 param - Additional parameter.
78 *
79 * Return: The handle of the created thread is returned. It returns 0 in case
80 of an error.
81 *
82 -----------------------------------------------------------------------------*/
83
84 method uint thread.create( uint idfunc, uint param )
85 {
86 uint id
87
88 .pmem = callback( idfunc, 1 )
89
90 // return this.handle = createthread( .pmem, idfunc, param )
91 return .handle = CreateThread( 0, 0, .pmem, param, 0, &id )
92 }
93
94 method thread.delete
95 {
96 if .pmem
97 {
98 freecallback( .pmem )
99 .pmem = 0
100 }
101 }
102
103 /*-----------------------------------------------------------------------------
104 * Id: thread_isactive F3
105 *
106 * Summary: Checking if a thread is active.
107 *
108 * Return: Returns 1 if the thread is active and 0 otherwise.
109 *
110 -----------------------------------------------------------------------------*/
111
112 method uint thread.isactive()
113 {
114 uint result
115
116 if GetExitCodeThread( this.handle, &result )
117 {
118 return result == $STILL_ACTIVE
119 }
120 return 0
121 }
122
123 /*-----------------------------------------------------------------------------
124 * Id: thread_getexitcode F2
125 *
126 * Summary: Get the thread exit code.
127 *
128 * Params: result - The pointer to a variable of the uint type the thread exit /
129 code will be written to. If the thread is still active, /
130 the value $STILL_ACTIVE will be written.
131 *
132 * Return: #lng/retf#
133 *
134 -----------------------------------------------------------------------------*/
135
136 method uint thread.getexitcode( uint result )
137 {
138 return GetExitCodeThread( this.handle, result )
139 }
140
141 /*-----------------------------------------------------------------------------
142 * Id: thread_resume F3
143 *
144 * Summary: Resuming a thread. Resume a thread paused with the
145 #a(thread_suspend ) method.
146 *
147 * Return: #lng/retf#
148 *
149 -----------------------------------------------------------------------------*/
150
151 method uint thread.resume()
152 {
153 return ResumeThread( this.handle ) != 0xFFFFFFFF
154 }
155
156 /*-----------------------------------------------------------------------------
157 * Id: thread_suspend F3
158 *
159 * Summary: Stop a thread.
160 *
161 * Return: #lng/retf#
162 *
163 -----------------------------------------------------------------------------*/
164
165 method uint thread.suspend()
166 {
167 return SuspendThread( this.handle ) != 0xFFFFFFFF
168 }
169
170 /*-----------------------------------------------------------------------------
171 * Id: thread_terminate F2
172 *
173 * Summary: Terminating a thread.
174 *
175 * Params: code - Thread termination code.
176 *
177 * Return: #lng/retf#
178 *
179 -----------------------------------------------------------------------------*/
180
181 method uint thread.terminate( uint code )
182 {
183 this.delete()
184 return TerminateThread( this.handle, code )
185 }
186
187 /*-----------------------------------------------------------------------------
188 * Id: sleep F
189 *
190 * Summary: Pause the current thread for the specified time.
191 *
192 * Params: msec - The time for pausing the thread in milliseconds.
193 *
194 * Return: #lng/retf#
195 *
196 -----------------------------------------------------------------------------*/
197
198 func sleep( uint msec )
199 {
200 Sleep( msec )
201 }
202
203 /*-----------------------------------------------------------------------------
204 ** Id: thread_wait F3
205 *
206 * Summary: Waiting till a thread is exited.
207 *
208 * Return: #lng/retf#
209 *
210 -----------------------------------------------------------------------------*/
211
212 method uint thread.wait()
213 {
214 return WaitForSingleObject( this.handle, $INFINITE ) != $WAIT_FAILED
215 }
Edit