Added headers for the msvcrt library.
[wine.git] / scheduler / services.c
blobfd156dd0da5e19fc63df3ccdeca0089aab61f320
1 /*
2 * Kernel Services Thread
4 * Copyright 1999 Ulrich Weigand
5 */
7 #include <sys/time.h>
8 #include <unistd.h>
10 #include "services.h"
11 #include "debugtools.h"
13 DEFAULT_DEBUG_CHANNEL(timer);
15 typedef struct _SERVICE
17 struct _SERVICE *next;
18 HANDLE self;
20 PAPCFUNC callback;
21 ULONG_PTR callback_arg;
23 BOOL disabled;
24 HANDLE object;
25 } SERVICE;
28 static HANDLE service_thread;
29 static SERVICE *service_first;
30 static DWORD service_counter;
32 /***********************************************************************
33 * SERVICE_Loop
35 static DWORD CALLBACK SERVICE_Loop( void *dummy )
37 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
38 int count = 0;
39 DWORD retval = WAIT_FAILED;
41 while ( TRUE )
43 PAPCFUNC callback;
44 ULONG_PTR callback_arg;
45 SERVICE *s;
47 /* Check whether some condition is fulfilled */
49 HeapLock( GetProcessHeap() );
51 callback = NULL;
52 callback_arg = 0L;
53 for ( s = service_first; s; s = s->next )
55 if (s->disabled) continue;
57 if ( retval >= WAIT_OBJECT_0 && retval < WAIT_OBJECT_0 + count )
59 if ( handles[retval - WAIT_OBJECT_0] == s->object )
61 retval = WAIT_TIMEOUT;
62 callback = s->callback;
63 callback_arg = s->callback_arg;
64 break;
69 HeapUnlock( GetProcessHeap() );
71 /* If found, call callback routine */
73 if ( callback )
75 callback( callback_arg );
76 continue;
79 /* If not found, determine wait condition */
81 HeapLock( GetProcessHeap() );
83 count = 0;
84 for ( s = service_first; s; s = s->next )
86 if (s->disabled) continue;
88 if ( count < MAXIMUM_WAIT_OBJECTS )
89 handles[count++] = s->object;
92 HeapUnlock( GetProcessHeap() );
95 /* Wait until some condition satisfied */
97 TRACE("Waiting for %d objects\n", count );
99 retval = WaitForMultipleObjectsEx( count, handles, FALSE, INFINITE, TRUE );
101 TRACE("Wait returned: %ld\n", retval );
104 return 0L;
107 /***********************************************************************
108 * SERVICE_CreateServiceTable
110 static BOOL SERVICE_CreateServiceTable( void )
112 /* service_thread must be set *BEFORE* calling CreateThread
113 * otherwise the thread cleanup service will cause an infinite recursion
114 * when installed
116 service_thread = INVALID_HANDLE_VALUE;
117 service_thread = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)SERVICE_Loop,
118 NULL, 0, NULL );
119 return (service_thread != 0);
122 /***********************************************************************
123 * SERVICE_AddObject
125 * Warning: the object supplied by the caller must not be closed. It'll
126 * be destroyed when the service is deleted. It's up to the caller
127 * to ensure that object will not be destroyed in between.
129 HANDLE SERVICE_AddObject( HANDLE object,
130 PAPCFUNC callback, ULONG_PTR callback_arg )
132 SERVICE *s;
133 HANDLE handle;
135 if ( !object || object == INVALID_HANDLE_VALUE || !callback )
136 return INVALID_HANDLE_VALUE;
138 if (!service_thread && !SERVICE_CreateServiceTable()) return INVALID_HANDLE_VALUE;
140 s = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(SERVICE) );
141 if ( !s ) return INVALID_HANDLE_VALUE;
143 s->callback = callback;
144 s->callback_arg = callback_arg;
145 s->object = object;
146 s->disabled = FALSE;
148 HeapLock( GetProcessHeap() );
150 s->self = handle = (HANDLE)++service_counter;
151 s->next = service_first;
152 service_first = s;
154 HeapUnlock( GetProcessHeap() );
156 QueueUserAPC( NULL, service_thread, 0L );
158 return handle;
161 /***********************************************************************
162 * SERVICE_AddTimer
164 HANDLE SERVICE_AddTimer( LONG rate,
165 PAPCFUNC callback, ULONG_PTR callback_arg )
167 HANDLE handle, ret;
168 LARGE_INTEGER when;
170 if ( !rate || !callback )
171 return INVALID_HANDLE_VALUE;
173 handle = CreateWaitableTimerA( NULL, FALSE, NULL );
174 if (!handle) return INVALID_HANDLE_VALUE;
176 if (!rate) rate = 1;
177 when.s.LowPart = when.s.HighPart = 0;
178 if (!SetWaitableTimer( handle, &when, rate, NULL, NULL, FALSE ))
180 CloseHandle( handle );
181 return INVALID_HANDLE_VALUE;
184 if ((ret = SERVICE_AddObject( handle, callback, callback_arg )) == INVALID_HANDLE_VALUE)
186 CloseHandle( handle );
187 return INVALID_HANDLE_VALUE;
189 return ret;
192 /***********************************************************************
193 * SERVICE_Delete
195 BOOL SERVICE_Delete( HANDLE service )
197 HANDLE handle = INVALID_HANDLE_VALUE;
198 BOOL retv = FALSE;
199 SERVICE **s, *next;
201 HeapLock( GetProcessHeap() );
203 for ( s = &service_first; *s; s = &(*s)->next )
205 if ( (*s)->self == service )
207 handle = (*s)->object;
208 next = (*s)->next;
209 HeapFree( GetProcessHeap(), 0, *s );
210 *s = next;
211 retv = TRUE;
212 break;
216 HeapUnlock( GetProcessHeap() );
218 if ( handle != INVALID_HANDLE_VALUE )
219 CloseHandle( handle );
221 QueueUserAPC( NULL, service_thread, 0L );
223 return retv;
226 /***********************************************************************
227 * SERVICE_Enable
229 BOOL SERVICE_Enable( HANDLE service )
231 BOOL retv = FALSE;
232 SERVICE *s;
234 HeapLock( GetProcessHeap() );
236 for ( s = service_first; s; s = s->next )
238 if ( s->self == service )
240 s->disabled = FALSE;
241 retv = TRUE;
242 break;
246 HeapUnlock( GetProcessHeap() );
248 QueueUserAPC( NULL, service_thread, 0L );
250 return retv;
253 /***********************************************************************
254 * SERVICE_Disable
256 BOOL SERVICE_Disable( HANDLE service )
258 BOOL retv = TRUE;
259 SERVICE *s;
261 HeapLock( GetProcessHeap() );
263 for ( s = service_first; s; s = s->next )
265 if ( s->self == service )
267 s->disabled = TRUE;
268 retv = TRUE;
269 break;
273 HeapUnlock( GetProcessHeap() );
275 QueueUserAPC( NULL, service_thread, 0L );
277 return retv;