sortix-mirror/kernel/time.cpp

109 lines
3.1 KiB
C++
Raw Normal View History

/*******************************************************************************
2011-08-05 12:25:00 +00:00
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013.
2011-08-05 12:25:00 +00:00
This file is part of Sortix.
2011-08-05 12:25:00 +00:00
Sortix is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
2011-08-05 12:25:00 +00:00
Sortix is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
2011-08-05 12:25:00 +00:00
You should have received a copy of the GNU General Public License along with
Sortix. If not, see <http://www.gnu.org/licenses/>.
2011-08-05 12:25:00 +00:00
time.cpp
Handles interrupts whenever some time has passed and provides useful
time-related functions to the kernel.
2011-08-05 12:25:00 +00:00
*******************************************************************************/
2011-08-05 12:25:00 +00:00
#include <sys/types.h>
#include <errno.h>
#include <timespec.h>
#include <sortix/clock.h>
2013-10-27 00:42:10 +00:00
#include <sortix/kernel/clock.h>
#include <sortix/kernel/copy.h>
2013-01-09 22:30:36 +00:00
#include <sortix/kernel/interrupt.h>
2013-10-27 00:42:10 +00:00
#include <sortix/kernel/kernel.h>
#include <sortix/kernel/process.h>
2013-01-09 09:47:22 +00:00
#include <sortix/kernel/scheduler.h>
2013-10-27 00:42:10 +00:00
#include <sortix/kernel/syscall.h>
#include <sortix/kernel/thread.h>
2013-10-27 00:42:10 +00:00
#include <sortix/kernel/time.h>
2013-01-08 23:41:35 +00:00
namespace Sortix {
namespace Time {
2011-08-05 12:25:00 +00:00
2013-10-13 21:56:58 +00:00
void CPUInit();
2011-08-05 12:25:00 +00:00
2013-10-13 21:56:58 +00:00
Clock* realtime_clock;
Clock* uptime_clock;
2013-10-13 21:56:58 +00:00
Clock* GetClock(clockid_t clock)
{
2013-10-13 21:56:58 +00:00
switch ( clock )
{
2013-10-13 21:56:58 +00:00
case CLOCK_REALTIME: return realtime_clock;
case CLOCK_BOOT: return uptime_clock;
case CLOCK_INIT: return uptime_clock;
case CLOCK_MONOTONIC: return uptime_clock;
case CLOCK_PROCESS_CPUTIME_ID: return &CurrentProcess()->execute_clock;
case CLOCK_PROCESS_SYSTIME_ID: return &CurrentProcess()->system_clock;
case CLOCK_CHILD_CPUTIME_ID: return &CurrentProcess()->child_execute_clock;
case CLOCK_CHILD_SYSTIME_ID: return &CurrentProcess()->child_system_clock;
case CLOCK_THREAD_CPUTIME_ID: return &CurrentThread()->execute_clock;
case CLOCK_THREAD_SYSTIME_ID: return &CurrentThread()->system_clock;
2013-10-13 21:56:58 +00:00
default: return errno = ENOTSUP, (Clock*) NULL;
}
}
2013-10-13 21:56:58 +00:00
struct timespec Get(clockid_t clockid)
{
2013-10-13 21:56:58 +00:00
Clock* clock = GetClock(clockid);
if ( clock )
{
2013-10-13 21:56:58 +00:00
struct timespec now;
clock->Get(&now, NULL);
return now;
}
2013-10-13 21:56:58 +00:00
return timespec_nul();
}
void OnTick(struct timespec tick_period, bool system_mode)
{
2013-10-13 21:56:58 +00:00
realtime_clock->Advance(tick_period);
uptime_clock->Advance(tick_period);
Thread* thread = CurrentThread();
Process* process = thread->process;
thread->execute_clock.Advance(tick_period);
process->execute_clock.Advance(tick_period);
process->child_execute_clock.Advance(tick_period);
if ( system_mode )
{
thread->system_clock.Advance(tick_period);
process->system_clock.Advance(tick_period);
process->child_system_clock.Advance(tick_period);
}
}
2011-08-05 12:25:00 +00:00
void Init()
{
2013-10-13 21:56:58 +00:00
if ( !(realtime_clock = new Clock()) )
Panic("Unable to allocate realtime clock");
if ( !(uptime_clock = new Clock()) )
Panic("Unable to allocate uptime clock");
CPUInit();
2011-08-05 12:25:00 +00:00
}
} // namespace Time
} // namespace Sortix