Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Kernel Services Thread |
| 3 | * |
| 4 | * Copyright 1999 Ulrich Weigand |
| 5 | */ |
| 6 | |
| 7 | #include <sys/time.h> |
| 8 | #include <unistd.h> |
| 9 | |
| 10 | #include "services.h" |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 11 | #include "process.h" |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 12 | #include "debugtools.h" |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 13 | |
Patrik Stridvall | b4b9fae | 1999-04-19 14:56:29 +0000 | [diff] [blame] | 14 | DEFAULT_DEBUG_CHANNEL(timer) |
| 15 | |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 16 | #define SERVICE_USE_OBJECT 0x0001 |
| 17 | #define SERVICE_USE_TIMEOUT 0x0002 |
| 18 | #define SERVICE_DISABLED 0x8000 |
| 19 | |
| 20 | typedef struct _SERVICE |
| 21 | { |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 22 | struct _SERVICE *next; |
| 23 | HANDLE self; |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 24 | |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 25 | PAPCFUNC callback; |
| 26 | ULONG_PTR callback_arg; |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 27 | |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 28 | int flags; |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 29 | |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 30 | HANDLE object; |
| 31 | long rate; |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 32 | |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 33 | struct timeval expire; |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 34 | |
| 35 | } SERVICE; |
| 36 | |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 37 | typedef struct _SERVICETABLE |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 38 | { |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 39 | HANDLE thread; |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 40 | |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 41 | SERVICE *first; |
| 42 | DWORD counter; |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 43 | |
| 44 | } SERVICETABLE; |
| 45 | |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 46 | /*********************************************************************** |
| 47 | * SERVICE_AddTimeval |
| 48 | */ |
| 49 | static void SERVICE_AddTimeval( struct timeval *time, long delta ) |
| 50 | { |
| 51 | delta += time->tv_usec; |
| 52 | time->tv_sec += delta / 1000000L; |
| 53 | time->tv_usec = delta % 1000000L; |
| 54 | } |
| 55 | |
| 56 | /*********************************************************************** |
| 57 | * SERVICE_DiffTimeval |
| 58 | */ |
| 59 | static long SERVICE_DiffTimeval( struct timeval *time1, struct timeval *time2 ) |
| 60 | { |
| 61 | return ( time1->tv_sec - time2->tv_sec ) * 1000000L |
| 62 | + ( time1->tv_usec - time2->tv_usec ); |
| 63 | } |
| 64 | |
| 65 | /*********************************************************************** |
| 66 | * SERVICE_Loop |
| 67 | */ |
| 68 | static DWORD CALLBACK SERVICE_Loop( SERVICETABLE *service ) |
| 69 | { |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 70 | HANDLE handles[MAXIMUM_WAIT_OBJECTS]; |
| 71 | int count = 0; |
| 72 | DWORD timeout = INFINITE; |
| 73 | DWORD retval = WAIT_FAILED; |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 74 | |
| 75 | while ( TRUE ) |
| 76 | { |
| 77 | PAPCFUNC callback; |
| 78 | ULONG_PTR callback_arg; |
| 79 | SERVICE *s; |
| 80 | |
| 81 | /* Check whether some condition is fulfilled */ |
| 82 | |
| 83 | struct timeval curTime; |
| 84 | gettimeofday( &curTime, NULL ); |
| 85 | |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 86 | HeapLock( GetProcessHeap() ); |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 87 | |
| 88 | callback = NULL; |
| 89 | callback_arg = 0L; |
| 90 | for ( s = service->first; s; s = s->next ) |
| 91 | { |
| 92 | if ( s->flags & SERVICE_DISABLED ) |
| 93 | continue; |
| 94 | |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 95 | if ( s->flags & SERVICE_USE_OBJECT ) |
| 96 | { |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 97 | if ( retval >= WAIT_OBJECT_0 && retval < WAIT_OBJECT_0 + count ) |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 98 | { |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 99 | if ( handles[retval - WAIT_OBJECT_0] == s->object ) |
| 100 | { |
Ulrich Weigand | 70b2e38 | 1999-05-04 16:43:38 +0000 | [diff] [blame] | 101 | retval = WAIT_TIMEOUT; |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 102 | callback = s->callback; |
| 103 | callback_arg = s->callback_arg; |
| 104 | break; |
| 105 | } |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 106 | } |
| 107 | } |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 108 | |
| 109 | if ( s->flags & SERVICE_USE_TIMEOUT ) |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 110 | { |
Ove Kaaven | 3d93102 | 1999-05-22 15:56:21 +0000 | [diff] [blame] | 111 | if ((s->expire.tv_sec < curTime.tv_sec) || |
| 112 | ((s->expire.tv_sec == curTime.tv_sec) && |
| 113 | (s->expire.tv_usec <= curTime.tv_usec))) |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 114 | { |
| 115 | SERVICE_AddTimeval( &s->expire, s->rate ); |
| 116 | callback = s->callback; |
| 117 | callback_arg = s->callback_arg; |
| 118 | break; |
| 119 | } |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 120 | } |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 123 | HeapUnlock( GetProcessHeap() ); |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 124 | |
| 125 | /* If found, call callback routine */ |
| 126 | |
| 127 | if ( callback ) |
| 128 | { |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 129 | callback( callback_arg ); |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 130 | continue; |
| 131 | } |
| 132 | |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 133 | /* If not found, determine wait condition */ |
| 134 | |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 135 | HeapLock( GetProcessHeap() ); |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 136 | |
| 137 | count = 0; |
| 138 | timeout = INFINITE; |
| 139 | for ( s = service->first; s; s = s->next ) |
| 140 | { |
| 141 | if ( s->flags & SERVICE_DISABLED ) |
| 142 | continue; |
| 143 | |
| 144 | if ( s->flags & SERVICE_USE_OBJECT ) |
Ulrich Weigand | 70b2e38 | 1999-05-04 16:43:38 +0000 | [diff] [blame] | 145 | if ( count < MAXIMUM_WAIT_OBJECTS ) |
| 146 | handles[count++] = s->object; |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 147 | |
| 148 | if ( s->flags & SERVICE_USE_TIMEOUT ) |
| 149 | { |
| 150 | long delta = SERVICE_DiffTimeval( &s->expire, &curTime ); |
| 151 | long time = (delta + 999L) / 1000L; |
| 152 | if ( time < 1 ) time = 1; |
| 153 | if ( time < timeout ) timeout = time; |
| 154 | } |
| 155 | } |
| 156 | |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 157 | HeapUnlock( GetProcessHeap() ); |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 158 | |
| 159 | |
| 160 | /* Wait until some condition satisfied */ |
| 161 | |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 162 | TRACE("Waiting for %d objects with timeout %ld\n", |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 163 | count, timeout ); |
| 164 | |
| 165 | retval = WaitForMultipleObjectsEx( count, handles, |
| 166 | FALSE, timeout, TRUE ); |
| 167 | |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 168 | TRACE("Wait returned: %ld\n", retval ); |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | return 0L; |
| 172 | } |
| 173 | |
| 174 | /*********************************************************************** |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 175 | * SERVICE_CreateServiceTable |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 176 | */ |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 177 | static BOOL SERVICE_CreateServiceTable( void ) |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 178 | { |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 179 | HANDLE thread; |
| 180 | SERVICETABLE *service_table; |
| 181 | PDB *pdb = PROCESS_Current(); |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 182 | |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 183 | service_table = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(SERVICETABLE) ); |
| 184 | if ( !service_table ) |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 185 | { |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 186 | return FALSE; |
| 187 | } |
| 188 | |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 189 | /* service_table field in PDB must be set *BEFORE* calling CreateThread |
| 190 | * otherwise the thread cleanup service will cause an infinite recursion |
| 191 | * when installed |
| 192 | */ |
| 193 | pdb->service_table = service_table; |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 194 | |
| 195 | thread = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)SERVICE_Loop, |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 196 | service_table, 0, NULL ); |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 197 | if ( thread == INVALID_HANDLE_VALUE ) |
| 198 | { |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 199 | pdb->service_table = 0; |
| 200 | HeapFree( GetProcessHeap(), 0, service_table ); |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 201 | return FALSE; |
| 202 | } |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 203 | |
| 204 | service_table->thread = thread; |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 205 | |
| 206 | return TRUE; |
| 207 | } |
| 208 | |
| 209 | /*********************************************************************** |
| 210 | * SERVICE_AddObject |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 211 | * |
| 212 | * Warning: the object supplied by the caller must not be closed. It'll |
| 213 | * be destroyed when the service is deleted. It's up to the caller |
| 214 | * to ensure that object will not be destroyed in between. |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 215 | */ |
| 216 | HANDLE SERVICE_AddObject( HANDLE object, |
| 217 | PAPCFUNC callback, ULONG_PTR callback_arg ) |
| 218 | { |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 219 | SERVICE *s; |
| 220 | SERVICETABLE *service_table; |
| 221 | HANDLE handle; |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 222 | |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 223 | if ( object == INVALID_HANDLE_VALUE || !callback ) |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 224 | return INVALID_HANDLE_VALUE; |
| 225 | |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 226 | if (PROCESS_Current()->service_table == 0 && !SERVICE_CreateServiceTable()) |
| 227 | return INVALID_HANDLE_VALUE; |
| 228 | service_table = PROCESS_Current()->service_table; |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 229 | |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 230 | s = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(SERVICE) ); |
| 231 | if ( !s ) return INVALID_HANDLE_VALUE; |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 232 | |
| 233 | s->callback = callback; |
| 234 | s->callback_arg = callback_arg; |
| 235 | s->object = object; |
| 236 | s->flags = SERVICE_USE_OBJECT; |
| 237 | |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 238 | HeapLock( GetProcessHeap() ); |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 239 | |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 240 | s->self = handle = (HANDLE)++service_table->counter; |
| 241 | s->next = service_table->first; |
| 242 | service_table->first = s; |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 243 | |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 244 | HeapUnlock( GetProcessHeap() ); |
| 245 | |
| 246 | QueueUserAPC( NULL, service_table->thread, 0L ); |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 247 | |
| 248 | return handle; |
| 249 | } |
| 250 | |
| 251 | /*********************************************************************** |
| 252 | * SERVICE_AddTimer |
| 253 | */ |
| 254 | HANDLE SERVICE_AddTimer( LONG rate, |
| 255 | PAPCFUNC callback, ULONG_PTR callback_arg ) |
| 256 | { |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 257 | SERVICE *s; |
| 258 | SERVICETABLE *service_table; |
| 259 | HANDLE handle; |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 260 | |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 261 | if ( !rate || !callback ) |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 262 | return INVALID_HANDLE_VALUE; |
| 263 | |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 264 | if (PROCESS_Current()->service_table == 0 && !SERVICE_CreateServiceTable()) |
| 265 | return INVALID_HANDLE_VALUE; |
| 266 | service_table = PROCESS_Current()->service_table; |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 267 | |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 268 | s = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(SERVICE) ); |
| 269 | if ( !s ) return INVALID_HANDLE_VALUE; |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 270 | |
| 271 | s->callback = callback; |
| 272 | s->callback_arg = callback_arg; |
| 273 | s->rate = rate; |
| 274 | s->flags = SERVICE_USE_TIMEOUT; |
| 275 | |
| 276 | gettimeofday( &s->expire, NULL ); |
| 277 | SERVICE_AddTimeval( &s->expire, s->rate ); |
| 278 | |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 279 | HeapLock( GetProcessHeap() ); |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 280 | |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 281 | s->self = handle = (HANDLE)++service_table->counter; |
| 282 | s->next = service_table->first; |
| 283 | service_table->first = s; |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 284 | |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 285 | HeapUnlock( GetProcessHeap() ); |
| 286 | |
| 287 | QueueUserAPC( NULL, service_table->thread, 0L ); |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 288 | |
| 289 | return handle; |
| 290 | } |
| 291 | |
| 292 | /*********************************************************************** |
| 293 | * SERVICE_Delete |
| 294 | */ |
| 295 | BOOL SERVICE_Delete( HANDLE service ) |
| 296 | { |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 297 | HANDLE handle = INVALID_HANDLE_VALUE; |
| 298 | BOOL retv = FALSE; |
| 299 | SERVICE **s, *next; |
| 300 | SERVICETABLE *service_table; |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 301 | |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 302 | /* service table must have been created on previous SERVICE_Add??? call */ |
| 303 | if ((service_table = PROCESS_Current()->service_table) == 0) |
| 304 | return retv; |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 305 | |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 306 | HeapLock( GetProcessHeap() ); |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 307 | |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 308 | for ( s = &service_table->first; *s; s = &(*s)->next ) |
| 309 | { |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 310 | if ( (*s)->self == service ) |
| 311 | { |
Ulrich Weigand | 70b2e38 | 1999-05-04 16:43:38 +0000 | [diff] [blame] | 312 | if ( (*s)->flags & SERVICE_USE_OBJECT ) |
| 313 | handle = (*s)->object; |
| 314 | |
| 315 | next = (*s)->next; |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 316 | HeapFree( GetProcessHeap(), 0, *s ); |
Ulrich Weigand | 70b2e38 | 1999-05-04 16:43:38 +0000 | [diff] [blame] | 317 | *s = next; |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 318 | retv = TRUE; |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 319 | break; |
| 320 | } |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 321 | } |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 322 | |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 323 | HeapUnlock( GetProcessHeap() ); |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 324 | |
Ulrich Weigand | 70b2e38 | 1999-05-04 16:43:38 +0000 | [diff] [blame] | 325 | if ( handle != INVALID_HANDLE_VALUE ) |
| 326 | CloseHandle( handle ); |
| 327 | |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 328 | QueueUserAPC( NULL, service_table->thread, 0L ); |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 329 | |
| 330 | return retv; |
| 331 | } |
| 332 | |
| 333 | /*********************************************************************** |
| 334 | * SERVICE_Enable |
| 335 | */ |
| 336 | BOOL SERVICE_Enable( HANDLE service ) |
| 337 | { |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 338 | BOOL retv = FALSE; |
| 339 | SERVICE *s; |
| 340 | SERVICETABLE *service_table; |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 341 | |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 342 | /* service table must have been created on previous SERVICE_Add??? call */ |
| 343 | if ((service_table = PROCESS_Current()->service_table) == 0) |
| 344 | return retv; |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 345 | |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 346 | HeapLock( GetProcessHeap() ); |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 347 | |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 348 | for ( s = service_table->first; s; s = s->next ) |
| 349 | { |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 350 | if ( s->self == service ) |
| 351 | { |
| 352 | if ( s->flags & SERVICE_DISABLED ) |
| 353 | { |
| 354 | s->flags &= ~SERVICE_DISABLED; |
| 355 | |
| 356 | if ( s->flags & SERVICE_USE_TIMEOUT ) |
| 357 | { |
| 358 | long delta; |
| 359 | struct timeval curTime; |
| 360 | gettimeofday( &curTime, NULL ); |
| 361 | |
| 362 | delta = SERVICE_DiffTimeval( &s->expire, &curTime ); |
| 363 | if ( delta > 0 ) |
| 364 | SERVICE_AddTimeval( &s->expire, |
| 365 | (delta / s->rate) * s->rate ); |
| 366 | } |
| 367 | } |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 368 | retv = TRUE; |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 369 | break; |
| 370 | } |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 371 | } |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 372 | |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 373 | HeapUnlock( GetProcessHeap() ); |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 374 | |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 375 | QueueUserAPC( NULL, service_table->thread, 0L ); |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 376 | |
| 377 | return retv; |
| 378 | } |
| 379 | |
| 380 | /*********************************************************************** |
| 381 | * SERVICE_Disable |
| 382 | */ |
| 383 | BOOL SERVICE_Disable( HANDLE service ) |
| 384 | { |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 385 | BOOL retv = TRUE; |
| 386 | SERVICE *s; |
| 387 | SERVICETABLE *service_table; |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 388 | |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 389 | /* service table must have been created on previous SERVICE_Add??? call */ |
| 390 | if ((service_table = PROCESS_Current()->service_table) == 0) |
| 391 | return retv; |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 392 | |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 393 | HeapLock( GetProcessHeap() ); |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 394 | |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 395 | for ( s = service_table->first; s; s = s->next ) |
| 396 | { |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 397 | if ( s->self == service ) |
| 398 | { |
| 399 | s->flags |= SERVICE_DISABLED; |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 400 | retv = TRUE; |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 401 | break; |
| 402 | } |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 403 | } |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 404 | |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 405 | HeapUnlock( GetProcessHeap() ); |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 406 | |
Eric Pouech | 63c7cdf | 1999-06-12 08:24:23 +0000 | [diff] [blame^] | 407 | QueueUserAPC( NULL, service_table->thread, 0L ); |
Ulrich Weigand | 7761cbe | 1999-04-11 15:01:20 +0000 | [diff] [blame] | 408 | |
| 409 | return retv; |
| 410 | } |
| 411 | |
| 412 | |