Commit | Line | Data |
---|---|---|
7761cbe0 UW |
1 | /* |
2 | * Kernel Services Thread | |
3 | * | |
4 | * Copyright 1999 Ulrich Weigand | |
5 | */ | |
6 | ||
7 | #include <sys/time.h> | |
8 | #include <unistd.h> | |
9 | ||
10 | #include "services.h" | |
63c7cdf1 | 11 | #include "process.h" |
15657090 | 12 | #include "debugtools.h" |
7761cbe0 | 13 | |
b4b9fae6 PS |
14 | DEFAULT_DEBUG_CHANNEL(timer) |
15 | ||
7761cbe0 UW |
16 | typedef struct _SERVICE |
17 | { | |
63c7cdf1 EP |
18 | struct _SERVICE *next; |
19 | HANDLE self; | |
7761cbe0 | 20 | |
63c7cdf1 EP |
21 | PAPCFUNC callback; |
22 | ULONG_PTR callback_arg; | |
7761cbe0 | 23 | |
000c9800 | 24 | BOOL disabled; |
63c7cdf1 | 25 | HANDLE object; |
7761cbe0 UW |
26 | } SERVICE; |
27 | ||
63c7cdf1 | 28 | typedef struct _SERVICETABLE |
7761cbe0 | 29 | { |
63c7cdf1 | 30 | HANDLE thread; |
7761cbe0 | 31 | |
63c7cdf1 EP |
32 | SERVICE *first; |
33 | DWORD counter; | |
7761cbe0 UW |
34 | |
35 | } SERVICETABLE; | |
36 | ||
7761cbe0 UW |
37 | /*********************************************************************** |
38 | * SERVICE_Loop | |
39 | */ | |
40 | static DWORD CALLBACK SERVICE_Loop( SERVICETABLE *service ) | |
41 | { | |
63c7cdf1 EP |
42 | HANDLE handles[MAXIMUM_WAIT_OBJECTS]; |
43 | int count = 0; | |
63c7cdf1 | 44 | DWORD retval = WAIT_FAILED; |
7761cbe0 UW |
45 | |
46 | while ( TRUE ) | |
47 | { | |
48 | PAPCFUNC callback; | |
49 | ULONG_PTR callback_arg; | |
50 | SERVICE *s; | |
51 | ||
52 | /* Check whether some condition is fulfilled */ | |
53 | ||
63c7cdf1 | 54 | HeapLock( GetProcessHeap() ); |
7761cbe0 UW |
55 | |
56 | callback = NULL; | |
57 | callback_arg = 0L; | |
58 | for ( s = service->first; s; s = s->next ) | |
59 | { | |
000c9800 AJ |
60 | if (s->disabled) continue; |
61 | ||
62 | if ( retval >= WAIT_OBJECT_0 && retval < WAIT_OBJECT_0 + count ) | |
63c7cdf1 | 63 | { |
000c9800 | 64 | if ( handles[retval - WAIT_OBJECT_0] == s->object ) |
7761cbe0 | 65 | { |
000c9800 | 66 | retval = WAIT_TIMEOUT; |
7761cbe0 UW |
67 | callback = s->callback; |
68 | callback_arg = s->callback_arg; | |
69 | break; | |
70 | } | |
000c9800 | 71 | } |
7761cbe0 UW |
72 | } |
73 | ||
63c7cdf1 | 74 | HeapUnlock( GetProcessHeap() ); |
7761cbe0 UW |
75 | |
76 | /* If found, call callback routine */ | |
77 | ||
78 | if ( callback ) | |
79 | { | |
63c7cdf1 | 80 | callback( callback_arg ); |
7761cbe0 UW |
81 | continue; |
82 | } | |
83 | ||
7761cbe0 UW |
84 | /* If not found, determine wait condition */ |
85 | ||
63c7cdf1 | 86 | HeapLock( GetProcessHeap() ); |
7761cbe0 UW |
87 | |
88 | count = 0; | |
7761cbe0 UW |
89 | for ( s = service->first; s; s = s->next ) |
90 | { | |
000c9800 | 91 | if (s->disabled) continue; |
7761cbe0 | 92 | |
000c9800 AJ |
93 | if ( count < MAXIMUM_WAIT_OBJECTS ) |
94 | handles[count++] = s->object; | |
7761cbe0 UW |
95 | } |
96 | ||
63c7cdf1 | 97 | HeapUnlock( GetProcessHeap() ); |
7761cbe0 UW |
98 | |
99 | ||
100 | /* Wait until some condition satisfied */ | |
101 | ||
000c9800 | 102 | TRACE("Waiting for %d objects\n", count ); |
7761cbe0 | 103 | |
000c9800 | 104 | retval = WaitForMultipleObjectsEx( count, handles, FALSE, INFINITE, TRUE ); |
7761cbe0 | 105 | |
15657090 | 106 | TRACE("Wait returned: %ld\n", retval ); |
7761cbe0 UW |
107 | } |
108 | ||
109 | return 0L; | |
110 | } | |
111 | ||
112 | /*********************************************************************** | |
63c7cdf1 | 113 | * SERVICE_CreateServiceTable |
7761cbe0 | 114 | */ |
63c7cdf1 | 115 | static BOOL SERVICE_CreateServiceTable( void ) |
7761cbe0 | 116 | { |
63c7cdf1 EP |
117 | HANDLE thread; |
118 | SERVICETABLE *service_table; | |
119 | PDB *pdb = PROCESS_Current(); | |
7761cbe0 | 120 | |
63c7cdf1 EP |
121 | service_table = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(SERVICETABLE) ); |
122 | if ( !service_table ) | |
7761cbe0 | 123 | { |
7761cbe0 UW |
124 | return FALSE; |
125 | } | |
126 | ||
63c7cdf1 EP |
127 | /* service_table field in PDB must be set *BEFORE* calling CreateThread |
128 | * otherwise the thread cleanup service will cause an infinite recursion | |
129 | * when installed | |
130 | */ | |
131 | pdb->service_table = service_table; | |
7761cbe0 UW |
132 | |
133 | thread = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)SERVICE_Loop, | |
63c7cdf1 | 134 | service_table, 0, NULL ); |
7761cbe0 UW |
135 | if ( thread == INVALID_HANDLE_VALUE ) |
136 | { | |
63c7cdf1 EP |
137 | pdb->service_table = 0; |
138 | HeapFree( GetProcessHeap(), 0, service_table ); | |
7761cbe0 UW |
139 | return FALSE; |
140 | } | |
63c7cdf1 EP |
141 | |
142 | service_table->thread = thread; | |
7761cbe0 UW |
143 | |
144 | return TRUE; | |
145 | } | |
146 | ||
147 | /*********************************************************************** | |
148 | * SERVICE_AddObject | |
63c7cdf1 EP |
149 | * |
150 | * Warning: the object supplied by the caller must not be closed. It'll | |
151 | * be destroyed when the service is deleted. It's up to the caller | |
152 | * to ensure that object will not be destroyed in between. | |
7761cbe0 UW |
153 | */ |
154 | HANDLE SERVICE_AddObject( HANDLE object, | |
155 | PAPCFUNC callback, ULONG_PTR callback_arg ) | |
156 | { | |
63c7cdf1 EP |
157 | SERVICE *s; |
158 | SERVICETABLE *service_table; | |
159 | HANDLE handle; | |
70b2e383 | 160 | |
63c7cdf1 | 161 | if ( object == INVALID_HANDLE_VALUE || !callback ) |
7761cbe0 UW |
162 | return INVALID_HANDLE_VALUE; |
163 | ||
63c7cdf1 EP |
164 | if (PROCESS_Current()->service_table == 0 && !SERVICE_CreateServiceTable()) |
165 | return INVALID_HANDLE_VALUE; | |
166 | service_table = PROCESS_Current()->service_table; | |
7761cbe0 | 167 | |
63c7cdf1 EP |
168 | s = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(SERVICE) ); |
169 | if ( !s ) return INVALID_HANDLE_VALUE; | |
7761cbe0 UW |
170 | |
171 | s->callback = callback; | |
172 | s->callback_arg = callback_arg; | |
173 | s->object = object; | |
000c9800 | 174 | s->disabled = FALSE; |
7761cbe0 | 175 | |
63c7cdf1 EP |
176 | HeapLock( GetProcessHeap() ); |
177 | ||
178 | s->self = handle = (HANDLE)++service_table->counter; | |
179 | s->next = service_table->first; | |
180 | service_table->first = s; | |
7761cbe0 | 181 | |
63c7cdf1 | 182 | HeapUnlock( GetProcessHeap() ); |
7761cbe0 | 183 | |
63c7cdf1 | 184 | QueueUserAPC( NULL, service_table->thread, 0L ); |
7761cbe0 UW |
185 | |
186 | return handle; | |
187 | } | |
188 | ||
189 | /*********************************************************************** | |
190 | * SERVICE_AddTimer | |
191 | */ | |
192 | HANDLE SERVICE_AddTimer( LONG rate, | |
193 | PAPCFUNC callback, ULONG_PTR callback_arg ) | |
194 | { | |
000c9800 AJ |
195 | HANDLE handle, ret; |
196 | LARGE_INTEGER when; | |
7761cbe0 | 197 | |
63c7cdf1 | 198 | if ( !rate || !callback ) |
7761cbe0 UW |
199 | return INVALID_HANDLE_VALUE; |
200 | ||
000c9800 AJ |
201 | handle = CreateWaitableTimerA( NULL, FALSE, NULL ); |
202 | if (!handle) return INVALID_HANDLE_VALUE; | |
7761cbe0 | 203 | |
000c9800 AJ |
204 | rate = (rate + 500) / 1000; /* us -> ms */ |
205 | if (!rate) rate = 1; | |
206 | when.s.LowPart = when.s.HighPart = 0; | |
207 | if (!SetWaitableTimer( handle, &when, rate, NULL, NULL, FALSE )) | |
208 | { | |
209 | CloseHandle( handle ); | |
210 | return INVALID_HANDLE_VALUE; | |
211 | } | |
7761cbe0 | 212 | |
000c9800 AJ |
213 | if ((ret = SERVICE_AddObject( handle, callback, callback_arg )) == INVALID_HANDLE_VALUE) |
214 | { | |
215 | CloseHandle( handle ); | |
216 | return INVALID_HANDLE_VALUE; | |
217 | } | |
218 | return ret; | |
7761cbe0 UW |
219 | } |
220 | ||
221 | /*********************************************************************** | |
222 | * SERVICE_Delete | |
223 | */ | |
224 | BOOL SERVICE_Delete( HANDLE service ) | |
225 | { | |
63c7cdf1 EP |
226 | HANDLE handle = INVALID_HANDLE_VALUE; |
227 | BOOL retv = FALSE; | |
228 | SERVICE **s, *next; | |
229 | SERVICETABLE *service_table; | |
7761cbe0 | 230 | |
63c7cdf1 EP |
231 | /* service table must have been created on previous SERVICE_Add??? call */ |
232 | if ((service_table = PROCESS_Current()->service_table) == 0) | |
233 | return retv; | |
7761cbe0 | 234 | |
63c7cdf1 | 235 | HeapLock( GetProcessHeap() ); |
7761cbe0 | 236 | |
63c7cdf1 EP |
237 | for ( s = &service_table->first; *s; s = &(*s)->next ) |
238 | { | |
7761cbe0 UW |
239 | if ( (*s)->self == service ) |
240 | { | |
000c9800 | 241 | handle = (*s)->object; |
70b2e383 | 242 | next = (*s)->next; |
63c7cdf1 | 243 | HeapFree( GetProcessHeap(), 0, *s ); |
70b2e383 | 244 | *s = next; |
63c7cdf1 | 245 | retv = TRUE; |
7761cbe0 UW |
246 | break; |
247 | } | |
63c7cdf1 | 248 | } |
7761cbe0 | 249 | |
63c7cdf1 | 250 | HeapUnlock( GetProcessHeap() ); |
7761cbe0 | 251 | |
70b2e383 UW |
252 | if ( handle != INVALID_HANDLE_VALUE ) |
253 | CloseHandle( handle ); | |
254 | ||
63c7cdf1 | 255 | QueueUserAPC( NULL, service_table->thread, 0L ); |
7761cbe0 UW |
256 | |
257 | return retv; | |
258 | } | |
259 | ||
260 | /*********************************************************************** | |
261 | * SERVICE_Enable | |
262 | */ | |
263 | BOOL SERVICE_Enable( HANDLE service ) | |
264 | { | |
63c7cdf1 EP |
265 | BOOL retv = FALSE; |
266 | SERVICE *s; | |
267 | SERVICETABLE *service_table; | |
7761cbe0 | 268 | |
63c7cdf1 EP |
269 | /* service table must have been created on previous SERVICE_Add??? call */ |
270 | if ((service_table = PROCESS_Current()->service_table) == 0) | |
271 | return retv; | |
7761cbe0 | 272 | |
63c7cdf1 | 273 | HeapLock( GetProcessHeap() ); |
7761cbe0 | 274 | |
63c7cdf1 EP |
275 | for ( s = service_table->first; s; s = s->next ) |
276 | { | |
7761cbe0 UW |
277 | if ( s->self == service ) |
278 | { | |
000c9800 | 279 | s->disabled = FALSE; |
63c7cdf1 | 280 | retv = TRUE; |
7761cbe0 UW |
281 | break; |
282 | } | |
63c7cdf1 | 283 | } |
7761cbe0 | 284 | |
63c7cdf1 | 285 | HeapUnlock( GetProcessHeap() ); |
7761cbe0 | 286 | |
63c7cdf1 | 287 | QueueUserAPC( NULL, service_table->thread, 0L ); |
7761cbe0 UW |
288 | |
289 | return retv; | |
290 | } | |
291 | ||
292 | /*********************************************************************** | |
293 | * SERVICE_Disable | |
294 | */ | |
295 | BOOL SERVICE_Disable( HANDLE service ) | |
296 | { | |
63c7cdf1 EP |
297 | BOOL retv = TRUE; |
298 | SERVICE *s; | |
299 | SERVICETABLE *service_table; | |
7761cbe0 | 300 | |
63c7cdf1 EP |
301 | /* service table must have been created on previous SERVICE_Add??? call */ |
302 | if ((service_table = PROCESS_Current()->service_table) == 0) | |
303 | return retv; | |
7761cbe0 | 304 | |
63c7cdf1 | 305 | HeapLock( GetProcessHeap() ); |
7761cbe0 | 306 | |
63c7cdf1 EP |
307 | for ( s = service_table->first; s; s = s->next ) |
308 | { | |
7761cbe0 UW |
309 | if ( s->self == service ) |
310 | { | |
000c9800 | 311 | s->disabled = TRUE; |
63c7cdf1 | 312 | retv = TRUE; |
7761cbe0 UW |
313 | break; |
314 | } | |
63c7cdf1 | 315 | } |
7761cbe0 | 316 | |
63c7cdf1 | 317 | HeapUnlock( GetProcessHeap() ); |
7761cbe0 | 318 | |
63c7cdf1 | 319 | QueueUserAPC( NULL, service_table->thread, 0L ); |
7761cbe0 UW |
320 | |
321 | return retv; | |
322 | } | |
323 | ||
324 |