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;
|
|
|
|
|
2003-01-26 20:31:33 +03:00
|
|
|
|
2008-08-22 05:13:18 +04:00
|
|
|
class KernelDaemon {
|
|
|
|
public:
|
|
|
|
status_t Init(const char* name);
|
2003-01-26 20:31:33 +03:00
|
|
|
|
2008-08-22 05:13:18 +04:00
|
|
|
status_t Register(daemon_hook function, void* arg,
|
|
|
|
int frequency);
|
|
|
|
status_t Unregister(daemon_hook function, void* arg);
|
2003-01-26 20:31:33 +03:00
|
|
|
|
2008-08-22 05:13:18 +04:00
|
|
|
private:
|
|
|
|
static status_t _DaemonThreadEntry(void* data);
|
|
|
|
status_t _DaemonThread();
|
2003-01-26 20:31:33 +03:00
|
|
|
|
2008-08-22 05:13:18 +04:00
|
|
|
private:
|
|
|
|
mutex fLock;
|
|
|
|
DaemonList fDaemons;
|
|
|
|
thread_id fThread;
|
|
|
|
};
|
2008-05-08 19:08:14 +04:00
|
|
|
|
|
|
|
|
2008-08-22 05:13:18 +04:00
|
|
|
static KernelDaemon sKernelDaemon;
|
|
|
|
static KernelDaemon sResourceResizer;
|
2003-01-26 20:31:33 +03:00
|
|
|
|
2008-08-22 05:13:18 +04:00
|
|
|
|
|
|
|
status_t
|
|
|
|
KernelDaemon::Init(const char* name)
|
|
|
|
{
|
|
|
|
new(&fDaemons) DaemonList;
|
|
|
|
mutex_init(&fLock, name);
|
|
|
|
|
|
|
|
fThread = spawn_kernel_thread(&_DaemonThreadEntry, name, B_LOW_PRIORITY,
|
|
|
|
this);
|
|
|
|
if (fThread < 0)
|
|
|
|
return fThread;
|
|
|
|
|
|
|
|
send_signal_etc(fThread, SIGCONT, B_DO_NOT_RESCHEDULE);
|
2008-05-08 19:08:14 +04:00
|
|
|
|
|
|
|
return B_OK;
|
2003-01-26 20:31:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-22 05:13:18 +04:00
|
|
|
status_t
|
|
|
|
KernelDaemon::Register(daemon_hook function, void* arg, int frequency)
|
|
|
|
{
|
|
|
|
if (function == NULL || frequency < 1)
|
|
|
|
return B_BAD_VALUE;
|
2008-05-08 19:08:14 +04:00
|
|
|
|
2008-08-22 05:13:18 +04:00
|
|
|
struct daemon* daemon = new(std::nothrow) struct ::daemon;
|
|
|
|
if (daemon == NULL)
|
|
|
|
return B_NO_MEMORY;
|
2008-05-08 19:08:14 +04:00
|
|
|
|
2008-08-22 05:13:18 +04:00
|
|
|
daemon->function = function;
|
|
|
|
daemon->arg = arg;
|
|
|
|
daemon->frequency = frequency;
|
|
|
|
|
|
|
|
MutexLocker _(fLock);
|
|
|
|
|
|
|
|
if (frequency > 1) {
|
|
|
|
// we try to balance the work-load for each daemon run
|
|
|
|
// (beware, it's a very simple algorithm, yet effective)
|
|
|
|
|
|
|
|
DaemonList::Iterator iterator = fDaemons.GetIterator();
|
|
|
|
int32 num = 0;
|
|
|
|
|
|
|
|
while (iterator.HasNext()) {
|
|
|
|
if (iterator.Next()->frequency == frequency)
|
|
|
|
num++;
|
|
|
|
}
|
|
|
|
|
|
|
|
daemon->offset = num % frequency;
|
|
|
|
} else
|
|
|
|
daemon->offset = 0;
|
|
|
|
|
|
|
|
fDaemons.Add(daemon);
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
|
|
|
KernelDaemon::Unregister(daemon_hook function, void* arg)
|
2003-01-26 20:31:33 +03:00
|
|
|
{
|
2008-08-22 05:13:18 +04:00
|
|
|
MutexLocker _(fLock);
|
2003-01-26 20:31:33 +03:00
|
|
|
|
2008-08-22 05:13:18 +04:00
|
|
|
DaemonList::Iterator iterator = fDaemons.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-08-22 05:13:18 +04:00
|
|
|
/*static*/ status_t
|
|
|
|
KernelDaemon::_DaemonThreadEntry(void* data)
|
2003-01-26 20:31:33 +03:00
|
|
|
{
|
2008-08-22 05:13:18 +04:00
|
|
|
return ((KernelDaemon*)data)->_DaemonThread();
|
|
|
|
}
|
2003-01-26 20:31:33 +03:00
|
|
|
|
|
|
|
|
2008-08-22 05:13:18 +04:00
|
|
|
status_t
|
|
|
|
KernelDaemon::_DaemonThread()
|
|
|
|
{
|
|
|
|
int32 iteration = 0;
|
2003-01-26 20:31:33 +03:00
|
|
|
|
2008-08-22 05:13:18 +04:00
|
|
|
while (true) {
|
|
|
|
mutex_lock(&fLock);
|
2003-01-26 20:31:33 +03:00
|
|
|
|
2008-08-22 05:13:18 +04:00
|
|
|
DaemonList::Iterator iterator = fDaemons.GetIterator();
|
2003-01-26 20:31:33 +03:00
|
|
|
|
2008-08-22 05:13:18 +04:00
|
|
|
// iterate through the list and execute each daemon if needed
|
2008-05-08 19:08:14 +04:00
|
|
|
while (iterator.HasNext()) {
|
2008-08-22 05:13:18 +04:00
|
|
|
struct daemon* daemon = iterator.Next();
|
|
|
|
|
|
|
|
if (((iteration + daemon->offset) % daemon->frequency) == 0)
|
|
|
|
daemon->function(daemon->arg, iteration);
|
2003-01-26 20:31:33 +03:00
|
|
|
}
|
2008-08-22 05:13:18 +04:00
|
|
|
mutex_unlock(&fLock);
|
2003-01-26 20:31:33 +03:00
|
|
|
|
2008-08-22 05:13:18 +04:00
|
|
|
iteration++;
|
|
|
|
snooze(100000); // 0.1 seconds
|
|
|
|
}
|
2003-01-26 20:31:33 +03:00
|
|
|
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-22 05:13:18 +04:00
|
|
|
// #pragma mark -
|
|
|
|
|
|
|
|
|
|
|
|
extern "C" status_t
|
|
|
|
register_kernel_daemon(daemon_hook function, void* arg, int frequency)
|
|
|
|
{
|
|
|
|
return sKernelDaemon.Register(function, arg, frequency);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern "C" status_t
|
|
|
|
unregister_kernel_daemon(daemon_hook function, void* arg)
|
|
|
|
{
|
|
|
|
return sKernelDaemon.Unregister(function, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern "C" status_t
|
|
|
|
register_resource_resizer(daemon_hook function, void* arg, int frequency)
|
|
|
|
{
|
|
|
|
return sResourceResizer.Register(function, arg, frequency);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern "C" status_t
|
|
|
|
unregister_resource_resizer(daemon_hook function, void* arg)
|
|
|
|
{
|
|
|
|
return sResourceResizer.Unregister(function, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-08-22 05:13:18 +04:00
|
|
|
new(&sKernelDaemon) KernelDaemon;
|
|
|
|
if (sKernelDaemon.Init("kernel daemon") != B_OK)
|
|
|
|
panic("kernel_daemon_init(): failed to init kernel daemon");
|
2003-01-26 20:31:33 +03:00
|
|
|
|
2008-08-22 05:13:18 +04:00
|
|
|
new(&sResourceResizer) KernelDaemon;
|
|
|
|
if (sResourceResizer.Init("resource resizer") != B_OK)
|
|
|
|
panic("kernel_daemon_init(): failed to init resource resizer");
|
2004-11-08 17:03:19 +03:00
|
|
|
|
|
|
|
return B_OK;
|
2003-01-26 20:31:33 +03:00
|
|
|
}
|