2 * Kernel Services Thread
4 * Copyright 1999 Ulrich Weigand
13 DEFAULT_DEBUG_CHANNEL(timer
)
16 #define SERVICE_USE_OBJECT 0x0001
17 #define SERVICE_USE_TIMEOUT 0x0002
18 #define SERVICE_DISABLED 0x8000
20 typedef struct _SERVICE
22 struct _SERVICE
*next
;
26 ULONG_PTR callback_arg
;
33 struct timeval expire
;
47 static SERVICETABLE
*Service
= NULL
;
50 /***********************************************************************
53 static void SERVICE_AddTimeval( struct timeval
*time
, long delta
)
55 delta
+= time
->tv_usec
;
56 time
->tv_sec
+= delta
/ 1000000L;
57 time
->tv_usec
= delta
% 1000000L;
60 /***********************************************************************
63 static long SERVICE_DiffTimeval( struct timeval
*time1
, struct timeval
*time2
)
65 return ( time1
->tv_sec
- time2
->tv_sec
) * 1000000L
66 + ( time1
->tv_usec
- time2
->tv_usec
);
69 /***********************************************************************
72 static DWORD CALLBACK
SERVICE_Loop( SERVICETABLE
*service
)
74 HANDLE handles
[MAXIMUM_WAIT_OBJECTS
];
76 DWORD timeout
= INFINITE
;
77 DWORD retval
= WAIT_FAILED
;
82 ULONG_PTR callback_arg
;
85 /* Check whether some condition is fulfilled */
87 struct timeval curTime
;
88 gettimeofday( &curTime
, NULL
);
90 HeapLock( service
->heap
);
94 for ( s
= service
->first
; s
; s
= s
->next
)
96 if ( s
->flags
& SERVICE_DISABLED
)
99 if ( s
->flags
& SERVICE_USE_OBJECT
)
100 if ( retval
>= WAIT_OBJECT_0
&& retval
< WAIT_OBJECT_0
+ count
)
101 if ( handles
[retval
- WAIT_OBJECT_0
] == s
->object
)
103 retval
= WAIT_TIMEOUT
;
104 callback
= s
->callback
;
105 callback_arg
= s
->callback_arg
;
109 if ( s
->flags
& SERVICE_USE_TIMEOUT
)
110 if ( timercmp( &s
->expire
, &curTime
, < ) )
112 SERVICE_AddTimeval( &s
->expire
, s
->rate
);
113 callback
= s
->callback
;
114 callback_arg
= s
->callback_arg
;
119 HeapUnlock( service
->heap
);
122 /* If found, call callback routine */
126 callback( callback_arg
);
131 /* If not found, determine wait condition */
133 HeapLock( service
->heap
);
137 for ( s
= service
->first
; s
; s
= s
->next
)
139 if ( s
->flags
& SERVICE_DISABLED
)
142 if ( s
->flags
& SERVICE_USE_OBJECT
)
143 if ( count
< MAXIMUM_WAIT_OBJECTS
)
144 handles
[count
++] = s
->object
;
146 if ( s
->flags
& SERVICE_USE_TIMEOUT
)
148 long delta
= SERVICE_DiffTimeval( &s
->expire
, &curTime
);
149 long time
= (delta
+ 999L) / 1000L;
150 if ( time
< 1 ) time
= 1;
151 if ( time
< timeout
) timeout
= time
;
155 HeapUnlock( service
->heap
);
158 /* Wait until some condition satisfied */
160 TRACE( timer
, "Waiting for %d objects with timeout %ld\n",
163 retval
= WaitForMultipleObjectsEx( count
, handles
,
164 FALSE
, timeout
, TRUE
);
166 TRACE( timer
, "Wait returned: %ld\n", retval
);
172 /***********************************************************************
175 BOOL
SERVICE_Init( void )
179 heap
= HeapCreate( HEAP_GROWABLE
, 0x1000, 0 );
180 if ( !heap
) return FALSE
;
182 Service
= HeapAlloc( heap
, HEAP_ZERO_MEMORY
, sizeof(SERVICETABLE
) );
189 Service
->heap
= heap
;
191 thread
= CreateThread( NULL
, 0, (LPTHREAD_START_ROUTINE
)SERVICE_Loop
,
193 if ( thread
== INVALID_HANDLE_VALUE
)
199 Service
->thread
= ConvertToGlobalHandle( thread
);
204 /***********************************************************************
207 void SERVICE_Exit( void )
209 TerminateThread( Service
->thread
, 0 );
212 /***********************************************************************
215 HANDLE
SERVICE_AddObject( HANDLE object
,
216 PAPCFUNC callback
, ULONG_PTR callback_arg
)
221 object
= ConvertToGlobalHandle( object
); /* FIXME */
223 if ( !Service
|| object
== INVALID_HANDLE_VALUE
|| !callback
)
224 return INVALID_HANDLE_VALUE
;
226 s
= HeapAlloc( Service
->heap
, HEAP_ZERO_MEMORY
, sizeof(SERVICE
) );
227 if ( !s
) return INVALID_HANDLE_VALUE
;
229 HeapLock( Service
->heap
);
231 s
->callback
= callback
;
232 s
->callback_arg
= callback_arg
;
234 s
->flags
= SERVICE_USE_OBJECT
;
236 s
->self
= handle
= (HANDLE
)++Service
->counter
;
237 s
->next
= Service
->first
;
240 HeapUnlock( Service
->heap
);
242 QueueUserAPC( NULL
, Service
->thread
, 0L );
247 /***********************************************************************
250 HANDLE
SERVICE_AddTimer( LONG rate
,
251 PAPCFUNC callback
, ULONG_PTR callback_arg
)
256 if ( !Service
|| !rate
|| !callback
)
257 return INVALID_HANDLE_VALUE
;
259 s
= HeapAlloc( Service
->heap
, HEAP_ZERO_MEMORY
, sizeof(SERVICE
) );
260 if ( !s
) return INVALID_HANDLE_VALUE
;
262 HeapLock( Service
->heap
);
264 s
->callback
= callback
;
265 s
->callback_arg
= callback_arg
;
267 s
->flags
= SERVICE_USE_TIMEOUT
;
269 gettimeofday( &s
->expire
, NULL
);
270 SERVICE_AddTimeval( &s
->expire
, s
->rate
);
272 s
->self
= handle
= (HANDLE
)++Service
->counter
;
273 s
->next
= Service
->first
;
276 HeapUnlock( Service
->heap
);
278 QueueUserAPC( NULL
, Service
->thread
, 0L );
283 /***********************************************************************
286 BOOL
SERVICE_Delete( HANDLE service
)
288 HANDLE handle
= INVALID_HANDLE_VALUE
;
292 if ( !Service
) return retv
;
294 HeapLock( Service
->heap
);
296 for ( s
= &Service
->first
; *s
; s
= &(*s
)->next
)
297 if ( (*s
)->self
== service
)
299 if ( (*s
)->flags
& SERVICE_USE_OBJECT
)
300 handle
= (*s
)->object
;
303 HeapFree( Service
->heap
, 0, *s
);
309 HeapUnlock( Service
->heap
);
311 if ( handle
!= INVALID_HANDLE_VALUE
)
312 CloseHandle( handle
);
314 QueueUserAPC( NULL
, Service
->thread
, 0L );
319 /***********************************************************************
322 BOOL
SERVICE_Enable( HANDLE service
)
327 if ( !Service
) return retv
;
329 HeapLock( Service
->heap
);
331 for ( s
= Service
->first
; s
; s
= s
->next
)
332 if ( s
->self
== service
)
334 if ( s
->flags
& SERVICE_DISABLED
)
336 s
->flags
&= ~SERVICE_DISABLED
;
338 if ( s
->flags
& SERVICE_USE_TIMEOUT
)
341 struct timeval curTime
;
342 gettimeofday( &curTime
, NULL
);
344 delta
= SERVICE_DiffTimeval( &s
->expire
, &curTime
);
346 SERVICE_AddTimeval( &s
->expire
,
347 (delta
/ s
->rate
) * s
->rate
);
354 HeapUnlock( Service
->heap
);
356 QueueUserAPC( NULL
, Service
->thread
, 0L );
361 /***********************************************************************
364 BOOL
SERVICE_Disable( HANDLE service
)
369 if ( !Service
) return retv
;
371 HeapLock( Service
->heap
);
373 for ( s
= Service
->first
; s
; s
= s
->next
)
374 if ( s
->self
== service
)
376 s
->flags
|= SERVICE_DISABLED
;
381 HeapUnlock( Service
->heap
);
383 QueueUserAPC( NULL
, Service
->thread
, 0L );