Sweden-Number/scheduler/services.c

295 lines
7.2 KiB
C
Raw Normal View History

1999-04-11 17:01:20 +02:00
/*
* Kernel Services Thread
*
* Copyright 1999 Ulrich Weigand
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1999-04-11 17:01:20 +02:00
*/
#include <sys/time.h>
#include <unistd.h>
#include "services.h"
#include "wine/debug.h"
1999-04-11 17:01:20 +02:00
WINE_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;
service_thread = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)SERVICE_Loop,
NULL, 0, NULL );
return (service_thread != 0);
1999-04-11 17:01:20 +02:00
}
/***********************************************************************
* 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 || 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;
}