Sweden-Number/scheduler/services.c

287 lines
6.6 KiB
C
Raw Normal View History

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"
#include "debugtools.h"
1999-04-11 17:01:20 +02:00
DEFAULT_DEBUG_CHANNEL(timer);
1999-04-11 17:01:20 +02:00
typedef struct _SERVICE
{
struct _SERVICE *next;
HANDLE self;
1999-04-11 17:01:20 +02:00
PAPCFUNC callback;
ULONG_PTR callback_arg;
1999-04-11 17:01:20 +02:00
BOOL disabled;
HANDLE object;
1999-04-11 17:01:20 +02:00
} SERVICE;
static HANDLE service_thread;
static SERVICE *service_first;
static DWORD service_counter;
1999-04-11 17:01:20 +02:00
/***********************************************************************
* SERVICE_Loop
*/
static DWORD CALLBACK SERVICE_Loop( void *dummy )
1999-04-11 17:01:20 +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 */
HeapLock( GetProcessHeap() );
1999-04-11 17:01:20 +02:00
callback = NULL;
callback_arg = 0L;
for ( s = service_first; s; s = s->next )
1999-04-11 17:01:20 +02:00
{
if (s->disabled) continue;
if ( retval >= WAIT_OBJECT_0 && retval < WAIT_OBJECT_0 + count )
{
if ( handles[retval - WAIT_OBJECT_0] == s->object )
1999-04-11 17:01:20 +02:00
{
retval = WAIT_TIMEOUT;
1999-04-11 17:01:20 +02:00
callback = s->callback;
callback_arg = s->callback_arg;
break;
}
}
1999-04-11 17:01:20 +02:00
}
HeapUnlock( GetProcessHeap() );
1999-04-11 17:01:20 +02:00
/* If found, call callback routine */
if ( callback )
{
callback( callback_arg );
1999-04-11 17:01:20 +02:00
continue;
}
/* If not found, determine wait condition */
HeapLock( GetProcessHeap() );
1999-04-11 17:01:20 +02:00
count = 0;
for ( s = service_first; s; s = s->next )
1999-04-11 17:01:20 +02:00
{
if (s->disabled) continue;
1999-04-11 17:01:20 +02:00
if ( count < MAXIMUM_WAIT_OBJECTS )
handles[count++] = s->object;
1999-04-11 17:01:20 +02:00
}
HeapUnlock( GetProcessHeap() );
1999-04-11 17:01:20 +02:00
/* Wait until some condition satisfied */
TRACE("Waiting for %d objects\n", count );
1999-04-11 17:01:20 +02:00
retval = WaitForMultipleObjectsEx( count, handles, FALSE, INFINITE, TRUE );
1999-04-11 17:01:20 +02:00
TRACE("Wait returned: %ld\n", retval );
1999-04-11 17:01:20 +02:00
}
return 0L;
}
/***********************************************************************
* SERVICE_CreateServiceTable
1999-04-11 17:01:20 +02:00
*/
static BOOL SERVICE_CreateServiceTable( void )
1999-04-11 17:01:20 +02:00
{
/* service_thread must be set *BEFORE* calling CreateThread
* otherwise the thread cleanup service will cause an infinite recursion
* when installed
*/
service_thread = INVALID_HANDLE_VALUE;
1999-04-11 17:01:20 +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
{
service_thread = 0;
1999-04-11 17:01:20 +02:00
return FALSE;
}
return TRUE;
}
/***********************************************************************
* SERVICE_AddObject
*
* 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 )
{
SERVICE *s;
HANDLE handle;
if ( object == INVALID_HANDLE_VALUE || !callback )
1999-04-11 17:01:20 +02:00
return INVALID_HANDLE_VALUE;
if (!service_thread && !SERVICE_CreateServiceTable()) return INVALID_HANDLE_VALUE;
1999-04-11 17:01:20 +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;
s->disabled = FALSE;
1999-04-11 17:01:20 +02:00
HeapLock( GetProcessHeap() );
s->self = handle = (HANDLE)++service_counter;
s->next = service_first;
service_first = s;
1999-04-11 17:01:20 +02:00
HeapUnlock( GetProcessHeap() );
1999-04-11 17:01:20 +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 )
{
HANDLE handle, ret;
LARGE_INTEGER when;
1999-04-11 17:01:20 +02:00
if ( !rate || !callback )
1999-04-11 17:01:20 +02:00
return INVALID_HANDLE_VALUE;
handle = CreateWaitableTimerA( NULL, FALSE, NULL );
if (!handle) return INVALID_HANDLE_VALUE;
1999-04-11 17:01:20 +02: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
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 )
{
HANDLE handle = INVALID_HANDLE_VALUE;
BOOL retv = FALSE;
SERVICE **s, *next;
1999-04-11 17:01:20 +02:00
HeapLock( GetProcessHeap() );
1999-04-11 17:01:20 +02:00
for ( s = &service_first; *s; s = &(*s)->next )
{
1999-04-11 17:01:20 +02:00
if ( (*s)->self == service )
{
handle = (*s)->object;
next = (*s)->next;
HeapFree( GetProcessHeap(), 0, *s );
*s = next;
retv = TRUE;
1999-04-11 17:01:20 +02:00
break;
}
}
1999-04-11 17:01:20 +02:00
HeapUnlock( GetProcessHeap() );
1999-04-11 17:01:20 +02:00
if ( handle != INVALID_HANDLE_VALUE )
CloseHandle( handle );
QueueUserAPC( NULL, service_thread, 0L );
1999-04-11 17:01:20 +02:00
return retv;
}
/***********************************************************************
* SERVICE_Enable
*/
BOOL SERVICE_Enable( HANDLE service )
{
BOOL retv = FALSE;
SERVICE *s;
1999-04-11 17:01:20 +02:00
HeapLock( GetProcessHeap() );
1999-04-11 17:01:20 +02:00
for ( s = service_first; s; s = s->next )
{
1999-04-11 17:01:20 +02:00
if ( s->self == service )
{
s->disabled = FALSE;
retv = TRUE;
1999-04-11 17:01:20 +02:00
break;
}
}
1999-04-11 17:01:20 +02:00
HeapUnlock( GetProcessHeap() );
1999-04-11 17:01:20 +02:00
QueueUserAPC( NULL, service_thread, 0L );
1999-04-11 17:01:20 +02:00
return retv;
}
/***********************************************************************
* SERVICE_Disable
*/
BOOL SERVICE_Disable( HANDLE service )
{
BOOL retv = TRUE;
SERVICE *s;
1999-04-11 17:01:20 +02:00
HeapLock( GetProcessHeap() );
1999-04-11 17:01:20 +02:00
for ( s = service_first; s; s = s->next )
{
1999-04-11 17:01:20 +02:00
if ( s->self == service )
{
s->disabled = TRUE;
retv = TRUE;
1999-04-11 17:01:20 +02:00
break;
}
}
1999-04-11 17:01:20 +02:00
HeapUnlock( GetProcessHeap() );
1999-04-11 17:01:20 +02:00
QueueUserAPC( NULL, service_thread, 0L );
1999-04-11 17:01:20 +02:00
return retv;
}