Using the add_timer() function as proposed by alexd. Thanks.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@33791 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Colin Günther 2009-10-27 17:09:53 +00:00
parent ad34440edc
commit 6c76785bef
1 changed files with 18 additions and 44 deletions

View File

@ -12,61 +12,38 @@
int ticks;
static sem_id sHardClockSem;
static thread_id sHardClockThread;
static timer sHardClockTimer;
/*!
* Implementation of FreeBSD's hardclock timer.
*/
static status_t
hardClock(timer* hardClockTimer)
{
atomic_add((vint32*)&ticks, 1);
return B_OK;
}
/*!
* Initialization of the hardclock timer.
*
* Note: We are not using the FreeBSD variable hz as the invocation frequency
* as it is the case in FreeBSD's hardclock function. This is due to lower
* system load. The hz (see compat/sys/kernel.h) variable in the compat layer is
* set to 1000000 Hz, whereas it is usually set to 1000 Hz for FreeBSD.
*/
static status_t
hard_clock_thread(void* data)
{
status_t status = B_OK;
const bigtime_t duration
= CONVERT_HZ_TO_USECS(FREEBSD_CLOCK_FREQUENCY_IN_HZ);
do {
bigtime_t timeout = system_time() + duration;
status = acquire_sem_etc(sHardClockSem, 1, B_ABSOLUTE_TIMEOUT, timeout);
if (system_time() >= timeout) {
atomic_add((vint32*)&ticks, 1);
}
} while (status != B_BAD_SEM_ID);
return status;
}
status_t
init_hard_clock()
{
status_t status = B_OK;
status_t status;
sHardClockSem = create_sem(0, "hard clock wait");
if (sHardClockSem < B_OK) {
status = sHardClockSem;
goto error1;
}
ticks = 0;
status = add_timer(&sHardClockTimer, hardClock,
CONVERT_HZ_TO_USECS(FREEBSD_CLOCK_FREQUENCY_IN_HZ),
B_PERIODIC_TIMER);
sHardClockThread = spawn_kernel_thread(hard_clock_thread, "hard clock",
B_NORMAL_PRIORITY, NULL);
if (sHardClockThread < B_OK) {
status = sHardClockThread;
goto error2;
}
return resume_thread(sHardClockThread);
error2:
delete_sem(sHardClockSem);
error1:
return status;
}
@ -74,8 +51,5 @@ error1:
void
uninit_hard_clock()
{
status_t status;
delete_sem(sHardClockSem);
wait_for_thread(sHardClockThread, &status);
cancel_timer(&sHardClockTimer);
}