1999-04-11 17:01:20 +02:00
|
|
|
/*
|
|
|
|
* Kernel Services Thread
|
|
|
|
*
|
|
|
|
* Copyright 1999 Ulrich Weigand
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "services.h"
|
1999-05-23 12:25:25 +02:00
|
|
|
#include "debugtools.h"
|
1999-04-11 17:01:20 +02:00
|
|
|
|
2000-08-10 00:31:24 +02:00
|
|
|
DEFAULT_DEBUG_CHANNEL(timer);
|
1999-04-19 16:56:29 +02:00
|
|
|
|
1999-04-11 17:01:20 +02:00
|
|
|
typedef struct _SERVICE
|
|
|
|
{
|
1999-06-12 10:24:23 +02:00
|
|
|
struct _SERVICE *next;
|
|
|
|
HANDLE self;
|
1999-04-11 17:01:20 +02:00
|
|
|
|
1999-06-12 10:24:23 +02:00
|
|
|
PAPCFUNC callback;
|
|
|
|
ULONG_PTR callback_arg;
|
1999-04-11 17:01:20 +02:00
|
|
|
|
1999-12-13 02:42:03 +01:00
|
|
|
BOOL disabled;
|
1999-06-12 10:24:23 +02:00
|
|
|
HANDLE object;
|
1999-04-11 17:01:20 +02:00
|
|
|
} SERVICE;
|
|
|
|
|
|
|
|
|
2000-08-10 00:31:24 +02:00
|
|
|
static HANDLE service_thread;
|
|
|
|
static SERVICE *service_first;
|
|
|
|
static DWORD service_counter;
|
1999-04-11 17:01:20 +02:00
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* SERVICE_Loop
|
|
|
|
*/
|
2000-08-10 00:31:24 +02:00
|
|
|
static DWORD CALLBACK SERVICE_Loop( void *dummy )
|
1999-04-11 17:01:20 +02:00
|
|
|
{
|
1999-06-12 10:24:23 +02:00
|
|
|
HANDLE handles[MAXIMUM_WAIT_OBJECTS];
|
|
|
|
int count = 0;
|
|
|
|
DWORD retval = WAIT_FAILED;
|
1999-04-11 17:01:20 +02:00
|
|
|
|
|
|
|
while ( TRUE )
|
|
|
|
{
|
|
|
|
PAPCFUNC callback;
|
|
|
|
ULONG_PTR callback_arg;
|
|
|
|
SERVICE *s;
|
|
|
|
|
|
|
|
/* Check whether some condition is fulfilled */
|
|
|
|
|
1999-06-12 10:24:23 +02:00
|
|
|
HeapLock( GetProcessHeap() );
|
1999-04-11 17:01:20 +02:00
|
|
|
|
|
|
|
callback = NULL;
|
|
|
|
callback_arg = 0L;
|
2000-08-10 00:31:24 +02:00
|
|
|
for ( s = service_first; s; s = s->next )
|
1999-04-11 17:01:20 +02:00
|
|
|
{
|
1999-12-13 02:42:03 +01:00
|
|
|
if (s->disabled) continue;
|
|
|
|
|
|
|
|
if ( retval >= WAIT_OBJECT_0 && retval < WAIT_OBJECT_0 + count )
|
1999-06-12 10:24:23 +02:00
|
|
|
{
|
1999-12-13 02:42:03 +01:00
|
|
|
if ( handles[retval - WAIT_OBJECT_0] == s->object )
|
1999-04-11 17:01:20 +02:00
|
|
|
{
|
1999-12-13 02:42:03 +01:00
|
|
|
retval = WAIT_TIMEOUT;
|
1999-04-11 17:01:20 +02:00
|
|
|
callback = s->callback;
|
|
|
|
callback_arg = s->callback_arg;
|
|
|
|
break;
|
|
|
|
}
|
1999-12-13 02:42:03 +01:00
|
|
|
}
|
1999-04-11 17:01:20 +02:00
|
|
|
}
|
|
|
|
|
1999-06-12 10:24:23 +02:00
|
|
|
HeapUnlock( GetProcessHeap() );
|
1999-04-11 17:01:20 +02:00
|
|
|
|
|
|
|
/* If found, call callback routine */
|
|
|
|
|
|
|
|
if ( callback )
|
|
|
|
{
|
1999-06-12 10:24:23 +02:00
|
|
|
callback( callback_arg );
|
1999-04-11 17:01:20 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If not found, determine wait condition */
|
|
|
|
|
1999-06-12 10:24:23 +02:00
|
|
|
HeapLock( GetProcessHeap() );
|
1999-04-11 17:01:20 +02:00
|
|
|
|
|
|
|
count = 0;
|
2000-08-10 00:31:24 +02:00
|
|
|
for ( s = service_first; s; s = s->next )
|
1999-04-11 17:01:20 +02:00
|
|
|
{
|
1999-12-13 02:42:03 +01:00
|
|
|
if (s->disabled) continue;
|
1999-04-11 17:01:20 +02:00
|
|
|
|
1999-12-13 02:42:03 +01:00
|
|
|
if ( count < MAXIMUM_WAIT_OBJECTS )
|
|
|
|
handles[count++] = s->object;
|
1999-04-11 17:01:20 +02:00
|
|
|
}
|
|
|
|
|
1999-06-12 10:24:23 +02:00
|
|
|
HeapUnlock( GetProcessHeap() );
|
1999-04-11 17:01:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* Wait until some condition satisfied */
|
|
|
|
|
1999-12-13 02:42:03 +01:00
|
|
|
TRACE("Waiting for %d objects\n", count );
|
1999-04-11 17:01:20 +02:00
|
|
|
|
1999-12-13 02:42:03 +01:00
|
|
|
retval = WaitForMultipleObjectsEx( count, handles, FALSE, INFINITE, TRUE );
|
1999-04-11 17:01:20 +02:00
|
|
|
|
1999-05-23 12:25:25 +02:00
|
|
|
TRACE("Wait returned: %ld\n", retval );
|
1999-04-11 17:01:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0L;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
1999-06-12 10:24:23 +02:00
|
|
|
* SERVICE_CreateServiceTable
|
1999-04-11 17:01:20 +02:00
|
|
|
*/
|
1999-06-12 10:24:23 +02:00
|
|
|
static BOOL SERVICE_CreateServiceTable( void )
|
1999-04-11 17:01:20 +02:00
|
|
|
{
|
2000-08-10 00:31:24 +02:00
|
|
|
/* service_thread must be set *BEFORE* calling CreateThread
|
1999-06-12 10:24:23 +02:00
|
|
|
* otherwise the thread cleanup service will cause an infinite recursion
|
|
|
|
* when installed
|
|
|
|
*/
|
2000-08-10 00:31:24 +02:00
|
|
|
service_thread = INVALID_HANDLE_VALUE;
|
1999-04-11 17:01:20 +02:00
|
|
|
|
2000-08-10 00:31:24 +02:00
|
|
|
service_thread = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)SERVICE_Loop,
|
|
|
|
NULL, 0, NULL );
|
|
|
|
if ( service_thread == INVALID_HANDLE_VALUE )
|
1999-04-11 17:01:20 +02:00
|
|
|
{
|
2000-08-10 00:31:24 +02:00
|
|
|
service_thread = 0;
|
1999-04-11 17:01:20 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* SERVICE_AddObject
|
1999-06-12 10:24:23 +02:00
|
|
|
*
|
|
|
|
* Warning: the object supplied by the caller must not be closed. It'll
|
|
|
|
* be destroyed when the service is deleted. It's up to the caller
|
|
|
|
* to ensure that object will not be destroyed in between.
|
1999-04-11 17:01:20 +02:00
|
|
|
*/
|
|
|
|
HANDLE SERVICE_AddObject( HANDLE object,
|
|
|
|
PAPCFUNC callback, ULONG_PTR callback_arg )
|
|
|
|
{
|
1999-06-12 10:24:23 +02:00
|
|
|
SERVICE *s;
|
|
|
|
HANDLE handle;
|
1999-05-04 18:43:38 +02:00
|
|
|
|
1999-06-12 10:24:23 +02:00
|
|
|
if ( object == INVALID_HANDLE_VALUE || !callback )
|
1999-04-11 17:01:20 +02:00
|
|
|
return INVALID_HANDLE_VALUE;
|
|
|
|
|
2000-08-10 00:31:24 +02:00
|
|
|
if (!service_thread && !SERVICE_CreateServiceTable()) return INVALID_HANDLE_VALUE;
|
1999-04-11 17:01:20 +02:00
|
|
|
|
1999-06-12 10:24:23 +02:00
|
|
|
s = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(SERVICE) );
|
|
|
|
if ( !s ) return INVALID_HANDLE_VALUE;
|
1999-04-11 17:01:20 +02:00
|
|
|
|
|
|
|
s->callback = callback;
|
|
|
|
s->callback_arg = callback_arg;
|
|
|
|
s->object = object;
|
1999-12-13 02:42:03 +01:00
|
|
|
s->disabled = FALSE;
|
1999-04-11 17:01:20 +02:00
|
|
|
|
1999-06-12 10:24:23 +02:00
|
|
|
HeapLock( GetProcessHeap() );
|
|
|
|
|
2000-08-10 00:31:24 +02:00
|
|
|
s->self = handle = (HANDLE)++service_counter;
|
|
|
|
s->next = service_first;
|
|
|
|
service_first = s;
|
1999-04-11 17:01:20 +02:00
|
|
|
|
1999-06-12 10:24:23 +02:00
|
|
|
HeapUnlock( GetProcessHeap() );
|
1999-04-11 17:01:20 +02:00
|
|
|
|
2000-08-10 00:31:24 +02:00
|
|
|
QueueUserAPC( NULL, service_thread, 0L );
|
1999-04-11 17:01:20 +02:00
|
|
|
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* SERVICE_AddTimer
|
|
|
|
*/
|
|
|
|
HANDLE SERVICE_AddTimer( LONG rate,
|
|
|
|
PAPCFUNC callback, ULONG_PTR callback_arg )
|
|
|
|
{
|
1999-12-13 02:42:03 +01:00
|
|
|
HANDLE handle, ret;
|
|
|
|
LARGE_INTEGER when;
|
1999-04-11 17:01:20 +02:00
|
|
|
|
1999-06-12 10:24:23 +02:00
|
|
|
if ( !rate || !callback )
|
1999-04-11 17:01:20 +02:00
|
|
|
return INVALID_HANDLE_VALUE;
|
|
|
|
|
1999-12-13 02:42:03 +01:00
|
|
|
handle = CreateWaitableTimerA( NULL, FALSE, NULL );
|
|
|
|
if (!handle) return INVALID_HANDLE_VALUE;
|
1999-04-11 17:01:20 +02:00
|
|
|
|
1999-12-13 02:42:03 +01:00
|
|
|
if (!rate) rate = 1;
|
|
|
|
when.s.LowPart = when.s.HighPart = 0;
|
|
|
|
if (!SetWaitableTimer( handle, &when, rate, NULL, NULL, FALSE ))
|
|
|
|
{
|
|
|
|
CloseHandle( handle );
|
|
|
|
return INVALID_HANDLE_VALUE;
|
|
|
|
}
|
1999-04-11 17:01:20 +02:00
|
|
|
|
1999-12-13 02:42:03 +01:00
|
|
|
if ((ret = SERVICE_AddObject( handle, callback, callback_arg )) == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
CloseHandle( handle );
|
|
|
|
return INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
return ret;
|
1999-04-11 17:01:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* SERVICE_Delete
|
|
|
|
*/
|
|
|
|
BOOL SERVICE_Delete( HANDLE service )
|
|
|
|
{
|
1999-06-12 10:24:23 +02:00
|
|
|
HANDLE handle = INVALID_HANDLE_VALUE;
|
|
|
|
BOOL retv = FALSE;
|
|
|
|
SERVICE **s, *next;
|
1999-04-11 17:01:20 +02:00
|
|
|
|
1999-06-12 10:24:23 +02:00
|
|
|
HeapLock( GetProcessHeap() );
|
1999-04-11 17:01:20 +02:00
|
|
|
|
2000-08-10 00:31:24 +02:00
|
|
|
for ( s = &service_first; *s; s = &(*s)->next )
|
1999-06-12 10:24:23 +02:00
|
|
|
{
|
1999-04-11 17:01:20 +02:00
|
|
|
if ( (*s)->self == service )
|
|
|
|
{
|
1999-12-13 02:42:03 +01:00
|
|
|
handle = (*s)->object;
|
1999-05-04 18:43:38 +02:00
|
|
|
next = (*s)->next;
|
1999-06-12 10:24:23 +02:00
|
|
|
HeapFree( GetProcessHeap(), 0, *s );
|
1999-05-04 18:43:38 +02:00
|
|
|
*s = next;
|
1999-06-12 10:24:23 +02:00
|
|
|
retv = TRUE;
|
1999-04-11 17:01:20 +02:00
|
|
|
break;
|
|
|
|
}
|
1999-06-12 10:24:23 +02:00
|
|
|
}
|
1999-04-11 17:01:20 +02:00
|
|
|
|
1999-06-12 10:24:23 +02:00
|
|
|
HeapUnlock( GetProcessHeap() );
|
1999-04-11 17:01:20 +02:00
|
|
|
|
1999-05-04 18:43:38 +02:00
|
|
|
if ( handle != INVALID_HANDLE_VALUE )
|
|
|
|
CloseHandle( handle );
|
|
|
|
|
2000-08-10 00:31:24 +02:00
|
|
|
QueueUserAPC( NULL, service_thread, 0L );
|
1999-04-11 17:01:20 +02:00
|
|
|
|
|
|
|
return retv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* SERVICE_Enable
|
|
|
|
*/
|
|
|
|
BOOL SERVICE_Enable( HANDLE service )
|
|
|
|
{
|
1999-06-12 10:24:23 +02:00
|
|
|
BOOL retv = FALSE;
|
|
|
|
SERVICE *s;
|
1999-04-11 17:01:20 +02:00
|
|
|
|
1999-06-12 10:24:23 +02:00
|
|
|
HeapLock( GetProcessHeap() );
|
1999-04-11 17:01:20 +02:00
|
|
|
|
2000-08-10 00:31:24 +02:00
|
|
|
for ( s = service_first; s; s = s->next )
|
1999-06-12 10:24:23 +02:00
|
|
|
{
|
1999-04-11 17:01:20 +02:00
|
|
|
if ( s->self == service )
|
|
|
|
{
|
1999-12-13 02:42:03 +01:00
|
|
|
s->disabled = FALSE;
|
1999-06-12 10:24:23 +02:00
|
|
|
retv = TRUE;
|
1999-04-11 17:01:20 +02:00
|
|
|
break;
|
|
|
|
}
|
1999-06-12 10:24:23 +02:00
|
|
|
}
|
1999-04-11 17:01:20 +02:00
|
|
|
|
1999-06-12 10:24:23 +02:00
|
|
|
HeapUnlock( GetProcessHeap() );
|
1999-04-11 17:01:20 +02:00
|
|
|
|
2000-08-10 00:31:24 +02:00
|
|
|
QueueUserAPC( NULL, service_thread, 0L );
|
1999-04-11 17:01:20 +02:00
|
|
|
|
|
|
|
return retv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* SERVICE_Disable
|
|
|
|
*/
|
|
|
|
BOOL SERVICE_Disable( HANDLE service )
|
|
|
|
{
|
1999-06-12 10:24:23 +02:00
|
|
|
BOOL retv = TRUE;
|
|
|
|
SERVICE *s;
|
1999-04-11 17:01:20 +02:00
|
|
|
|
1999-06-12 10:24:23 +02:00
|
|
|
HeapLock( GetProcessHeap() );
|
1999-04-11 17:01:20 +02:00
|
|
|
|
2000-08-10 00:31:24 +02:00
|
|
|
for ( s = service_first; s; s = s->next )
|
1999-06-12 10:24:23 +02:00
|
|
|
{
|
1999-04-11 17:01:20 +02:00
|
|
|
if ( s->self == service )
|
|
|
|
{
|
1999-12-13 02:42:03 +01:00
|
|
|
s->disabled = TRUE;
|
1999-06-12 10:24:23 +02:00
|
|
|
retv = TRUE;
|
1999-04-11 17:01:20 +02:00
|
|
|
break;
|
|
|
|
}
|
1999-06-12 10:24:23 +02:00
|
|
|
}
|
1999-04-11 17:01:20 +02:00
|
|
|
|
1999-06-12 10:24:23 +02:00
|
|
|
HeapUnlock( GetProcessHeap() );
|
1999-04-11 17:01:20 +02:00
|
|
|
|
2000-08-10 00:31:24 +02:00
|
|
|
QueueUserAPC( NULL, service_thread, 0L );
|
1999-04-11 17:01:20 +02:00
|
|
|
|
|
|
|
return retv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|