2003-01-26 20:31:33 +03:00
|
|
|
/*
|
|
|
|
** Copyright 2003, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
|
|
|
|
** Distributed under the terms of the OpenBeOS License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <KernelExport.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
|
|
|
|
#include <kernel_daemon.h>
|
|
|
|
#include <lock.h>
|
2003-09-09 06:36:29 +04:00
|
|
|
#include <util/list.h>
|
2003-01-26 20:31:33 +03:00
|
|
|
|
|
|
|
|
|
|
|
// ToDo: the use of snooze() in the kernel_daemon() function is very inaccurate, of
|
|
|
|
// course - the time the daemons need to execute add up in each iteration.
|
|
|
|
// But since the kernel daemon is documented to be very inaccurate, this actually
|
|
|
|
// might be okay (and that's why it's implemented this way now :-).
|
|
|
|
// BeOS R5 seems to do it in the same way, anyway.
|
|
|
|
|
|
|
|
|
|
|
|
struct daemon {
|
|
|
|
list_link link;
|
2003-01-27 16:53:10 +03:00
|
|
|
daemon_hook function;
|
2003-01-26 20:31:33 +03:00
|
|
|
void *arg;
|
|
|
|
int32 frequency;
|
|
|
|
int32 offset;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
mutex gDaemonMutex;
|
|
|
|
struct list gDaemons;
|
|
|
|
|
|
|
|
|
|
|
|
static int32
|
|
|
|
kernel_daemon(void *data)
|
|
|
|
{
|
|
|
|
int32 iteration = 0;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
struct daemon *daemon = NULL;
|
|
|
|
|
|
|
|
mutex_lock(&gDaemonMutex);
|
|
|
|
|
|
|
|
// iterate through the list and execute each daemon if needed
|
|
|
|
while ((daemon = list_get_next_item(&gDaemons, daemon)) != NULL) {
|
|
|
|
if (((iteration + daemon->offset) % daemon->frequency) == 0)
|
|
|
|
daemon->function(daemon->arg, iteration);
|
|
|
|
}
|
2003-01-27 05:02:20 +03:00
|
|
|
mutex_unlock(&gDaemonMutex);
|
2003-01-26 20:31:33 +03:00
|
|
|
|
|
|
|
iteration++;
|
|
|
|
snooze(100000); // 0.1 seconds
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
2003-01-27 16:53:10 +03:00
|
|
|
unregister_kernel_daemon(daemon_hook function, void *arg)
|
2003-01-26 20:31:33 +03:00
|
|
|
{
|
|
|
|
struct daemon *daemon = NULL;
|
|
|
|
|
|
|
|
mutex_lock(&gDaemonMutex);
|
|
|
|
|
|
|
|
// search for the daemon and remove it from the list
|
|
|
|
while ((daemon = list_get_next_item(&gDaemons, daemon)) != NULL) {
|
|
|
|
if (daemon->function == function && daemon->arg == arg) {
|
|
|
|
// found it!
|
2003-01-27 02:31:38 +03:00
|
|
|
list_remove_item(&gDaemons, daemon);
|
2003-01-26 20:31:33 +03:00
|
|
|
free(daemon);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2003-01-27 05:02:20 +03:00
|
|
|
mutex_unlock(&gDaemonMutex);
|
2003-01-26 20:31:33 +03:00
|
|
|
|
|
|
|
// if we've iterated through the whole list, we didn't
|
|
|
|
// find the daemon, and "daemon" is NULL
|
|
|
|
return daemon != NULL ? B_OK : B_ENTRY_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
2003-01-27 16:53:10 +03:00
|
|
|
register_kernel_daemon(daemon_hook function, void *arg, int frequency)
|
2003-01-26 20:31:33 +03:00
|
|
|
{
|
|
|
|
struct daemon *daemon;
|
|
|
|
|
|
|
|
if (function == NULL || frequency < 1)
|
|
|
|
return B_BAD_VALUE;
|
|
|
|
|
2004-06-25 03:48:44 +04:00
|
|
|
daemon = malloc(sizeof(struct daemon));
|
2003-01-26 20:31:33 +03:00
|
|
|
if (daemon == NULL)
|
|
|
|
return B_NO_MEMORY;
|
|
|
|
|
|
|
|
daemon->function = function;
|
|
|
|
daemon->arg = arg;
|
|
|
|
daemon->frequency = frequency;
|
|
|
|
|
|
|
|
mutex_lock(&gDaemonMutex);
|
|
|
|
|
|
|
|
if (frequency > 1) {
|
|
|
|
// we try to balance the work-load for each daemon run
|
|
|
|
// (beware, it's a very simple algorithm, yet effective)
|
|
|
|
|
|
|
|
struct daemon *d = NULL;
|
|
|
|
int32 num = 0;
|
|
|
|
|
|
|
|
while ((d = list_get_next_item(&gDaemons, d)) != NULL) {
|
|
|
|
if (d->frequency == frequency)
|
|
|
|
num++;
|
|
|
|
}
|
|
|
|
|
|
|
|
daemon->offset = num % frequency;
|
|
|
|
} else
|
|
|
|
daemon->offset = 0;
|
|
|
|
|
2003-01-27 02:31:38 +03:00
|
|
|
list_add_item(&gDaemons, daemon);
|
2003-01-27 05:02:20 +03:00
|
|
|
mutex_unlock(&gDaemonMutex);
|
2003-01-26 20:31:33 +03:00
|
|
|
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
|
|
|
kernel_daemon_init(void)
|
|
|
|
{
|
2003-01-27 05:02:20 +03:00
|
|
|
thread_id thread;
|
|
|
|
|
2003-01-26 20:31:33 +03:00
|
|
|
if (mutex_init(&gDaemonMutex, "kernel daemon") < B_OK)
|
|
|
|
return B_ERROR;
|
|
|
|
|
|
|
|
list_init(&gDaemons);
|
|
|
|
|
2003-01-27 05:02:20 +03:00
|
|
|
thread = spawn_kernel_thread(&kernel_daemon, "kernel daemon", B_LOW_PRIORITY, NULL);
|
|
|
|
return resume_thread(thread);
|
2003-01-26 20:31:33 +03:00
|
|
|
}
|