2005-06-13 16:59:11 +04:00
|
|
|
/*
|
2008-05-08 19:08:14 +04:00
|
|
|
* Copyright 2003-2008, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
|
2005-06-13 16:59:11 +04:00
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*/
|
2003-01-26 20:31:33 +03:00
|
|
|
|
|
|
|
|
|
|
|
#include <kernel_daemon.h>
|
|
|
|
|
2008-05-08 19:08:14 +04:00
|
|
|
#include <new>
|
|
|
|
#include <signal.h>
|
2005-06-13 16:59:11 +04:00
|
|
|
#include <stdlib.h>
|
2003-01-26 20:31:33 +03:00
|
|
|
|
2008-05-08 19:08:14 +04:00
|
|
|
#include <KernelExport.h>
|
|
|
|
|
|
|
|
#include <lock.h>
|
|
|
|
#include <util/AutoLock.h>
|
|
|
|
#include <util/DoublyLinkedList.h>
|
|
|
|
|
2005-06-13 16:59:11 +04:00
|
|
|
|
|
|
|
// 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.
|
2003-01-26 20:31:33 +03:00
|
|
|
|
2008-05-08 19:08:14 +04:00
|
|
|
struct daemon : DoublyLinkedListLinkImpl<struct daemon> {
|
2003-01-27 16:53:10 +03:00
|
|
|
daemon_hook function;
|
2008-05-08 19:08:14 +04:00
|
|
|
void* arg;
|
2003-01-26 20:31:33 +03:00
|
|
|
int32 frequency;
|
|
|
|
int32 offset;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-05-08 19:08:14 +04:00
|
|
|
typedef DoublyLinkedList<struct daemon> DaemonList;
|
|
|
|
|
2008-06-06 03:19:27 +04:00
|
|
|
static mutex sDaemonMutex = MUTEX_INITIALIZER("kernel daemon");
|
2008-05-08 19:08:14 +04:00
|
|
|
static DaemonList sDaemons;
|
2003-01-26 20:31:33 +03:00
|
|
|
|
|
|
|
|
2008-05-08 19:08:14 +04:00
|
|
|
static status_t
|
|
|
|
kernel_daemon(void* data)
|
2003-01-26 20:31:33 +03:00
|
|
|
{
|
|
|
|
int32 iteration = 0;
|
|
|
|
|
|
|
|
while (true) {
|
2005-06-13 16:59:11 +04:00
|
|
|
mutex_lock(&sDaemonMutex);
|
2003-01-26 20:31:33 +03:00
|
|
|
|
2008-05-08 19:08:14 +04:00
|
|
|
DaemonList::Iterator iterator = sDaemons.GetIterator();
|
|
|
|
|
2003-01-26 20:31:33 +03:00
|
|
|
// iterate through the list and execute each daemon if needed
|
2008-05-08 19:08:14 +04:00
|
|
|
while (iterator.HasNext()) {
|
|
|
|
struct daemon* daemon = iterator.Next();
|
|
|
|
|
2003-01-26 20:31:33 +03:00
|
|
|
if (((iteration + daemon->offset) % daemon->frequency) == 0)
|
|
|
|
daemon->function(daemon->arg, iteration);
|
|
|
|
}
|
2005-06-13 16:59:11 +04:00
|
|
|
mutex_unlock(&sDaemonMutex);
|
2003-01-26 20:31:33 +03:00
|
|
|
|
|
|
|
iteration++;
|
|
|
|
snooze(100000); // 0.1 seconds
|
|
|
|
}
|
2008-05-08 19:08:14 +04:00
|
|
|
|
|
|
|
return B_OK;
|
2003-01-26 20:31:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-08 19:08:14 +04:00
|
|
|
// #pragma mark -
|
|
|
|
|
|
|
|
|
|
|
|
extern "C" status_t
|
|
|
|
unregister_kernel_daemon(daemon_hook function, void* arg)
|
2003-01-26 20:31:33 +03:00
|
|
|
{
|
2008-05-08 19:08:14 +04:00
|
|
|
MutexLocker _(sDaemonMutex);
|
2003-01-26 20:31:33 +03:00
|
|
|
|
2008-05-08 19:08:14 +04:00
|
|
|
DaemonList::Iterator iterator = sDaemons.GetIterator();
|
2003-01-26 20:31:33 +03:00
|
|
|
|
|
|
|
// search for the daemon and remove it from the list
|
2008-05-08 19:08:14 +04:00
|
|
|
while (iterator.HasNext()) {
|
|
|
|
struct daemon* daemon = iterator.Next();
|
|
|
|
|
2003-01-26 20:31:33 +03:00
|
|
|
if (daemon->function == function && daemon->arg == arg) {
|
|
|
|
// found it!
|
2008-05-08 19:08:14 +04:00
|
|
|
iterator.Remove();
|
2008-05-22 15:37:20 +04:00
|
|
|
delete daemon;
|
2008-05-08 19:08:14 +04:00
|
|
|
return B_OK;
|
2003-01-26 20:31:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-08 19:08:14 +04:00
|
|
|
return B_ENTRY_NOT_FOUND;
|
2003-01-26 20:31:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-08 19:08:14 +04:00
|
|
|
extern "C" status_t
|
|
|
|
register_kernel_daemon(daemon_hook function, void* arg, int frequency)
|
2003-01-26 20:31:33 +03:00
|
|
|
{
|
|
|
|
if (function == NULL || frequency < 1)
|
|
|
|
return B_BAD_VALUE;
|
|
|
|
|
2008-05-08 19:08:14 +04:00
|
|
|
struct daemon* daemon = new(std::nothrow) 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;
|
|
|
|
|
2008-05-08 19:08:14 +04:00
|
|
|
MutexLocker _(sDaemonMutex);
|
2003-01-26 20:31:33 +03:00
|
|
|
|
|
|
|
if (frequency > 1) {
|
|
|
|
// we try to balance the work-load for each daemon run
|
|
|
|
// (beware, it's a very simple algorithm, yet effective)
|
|
|
|
|
2008-05-08 19:08:14 +04:00
|
|
|
DaemonList::Iterator iterator = sDaemons.GetIterator();
|
2003-01-26 20:31:33 +03:00
|
|
|
int32 num = 0;
|
|
|
|
|
2008-05-08 19:08:14 +04:00
|
|
|
while (iterator.HasNext()) {
|
|
|
|
if (iterator.Next()->frequency == frequency)
|
2003-01-26 20:31:33 +03:00
|
|
|
num++;
|
|
|
|
}
|
|
|
|
|
|
|
|
daemon->offset = num % frequency;
|
|
|
|
} else
|
|
|
|
daemon->offset = 0;
|
|
|
|
|
2008-05-08 19:08:14 +04:00
|
|
|
sDaemons.Add(daemon);
|
2003-01-26 20:31:33 +03:00
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-08 19:08:14 +04:00
|
|
|
extern "C" status_t
|
2003-01-26 20:31:33 +03:00
|
|
|
kernel_daemon_init(void)
|
|
|
|
{
|
2008-05-08 19:08:14 +04:00
|
|
|
new(&sDaemons) DaemonList;
|
2003-01-26 20:31:33 +03:00
|
|
|
|
2008-06-06 03:19:27 +04:00
|
|
|
thread_id thread = spawn_kernel_thread(&kernel_daemon, "kernel daemon",
|
2008-05-08 19:08:14 +04:00
|
|
|
B_LOW_PRIORITY, NULL);
|
2004-11-08 17:03:19 +03:00
|
|
|
send_signal_etc(thread, SIGCONT, B_DO_NOT_RESCHEDULE);
|
|
|
|
|
|
|
|
return B_OK;
|
2003-01-26 20:31:33 +03:00
|
|
|
}
|