1
0
Fork 0
mirror of https://gitlab.com/sortix/sortix.git synced 2023-02-13 20:55:38 -05:00
sortix--sortix/kernel/time.cpp

101 lines
2.8 KiB
C++
Raw Normal View History

/*******************************************************************************
2011-08-05 08:25:00 -04:00
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013.
2011-08-05 08:25:00 -04:00
This file is part of Sortix.
2011-08-05 08:25:00 -04: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 08:25:00 -04: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 08:25:00 -04: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 08:25:00 -04:00
time.cpp
Handles interrupts whenever some time has passed and provides useful
time-related functions to the kernel.
2011-08-05 08:25:00 -04:00
*******************************************************************************/
2011-08-05 08:25:00 -04:00
#include <sys/types.h>
#include <errno.h>
#include <timespec.h>
#include <sortix/clock.h>
2013-10-26 20:42:10 -04:00
#include <sortix/kernel/clock.h>
#include <sortix/kernel/copy.h>
2013-01-09 17:30:36 -05:00
#include <sortix/kernel/interrupt.h>
2013-10-26 20:42:10 -04:00
#include <sortix/kernel/kernel.h>
#include <sortix/kernel/process.h>
2013-01-09 04:47:22 -05:00
#include <sortix/kernel/scheduler.h>
2013-10-26 20:42:10 -04:00
#include <sortix/kernel/syscall.h>
#include <sortix/kernel/time.h>
2013-01-08 18:41:35 -05:00
namespace Sortix {
namespace Time {
2011-08-05 08:25:00 -04:00
2013-10-13 17:56:58 -04:00
void CPUInit();
2011-08-05 08:25:00 -04:00
2013-10-13 17:56:58 -04:00
Clock* realtime_clock;
Clock* uptime_clock;
2013-10-13 17:56:58 -04:00
Clock* GetClock(clockid_t clock)
{
2013-10-13 17:56:58 -04:00
switch ( clock )
{
2013-10-13 17:56:58 -04: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;
2013-10-13 17:56:58 -04:00
default: return errno = ENOTSUP, (Clock*) NULL;
}
}
2013-10-13 17:56:58 -04:00
struct timespec Get(clockid_t clockid)
{
2013-10-13 17:56:58 -04:00
Clock* clock = GetClock(clockid);
if ( clock )
{
2013-10-13 17:56:58 -04:00
struct timespec now;
clock->Get(&now, NULL);
return now;
}
2013-10-13 17:56:58 -04:00
return timespec_nul();
}
void OnTick(struct timespec tick_period, bool system_mode)
{
2013-10-13 17:56:58 -04:00
realtime_clock->Advance(tick_period);
uptime_clock->Advance(tick_period);
Process* process = CurrentProcess();
process->execute_clock.Advance(tick_period);
process->child_execute_clock.Advance(tick_period);
if ( system_mode )
process->system_clock.Advance(tick_period),
process->child_system_clock.Advance(tick_period);
}
2011-08-05 08:25:00 -04:00
void Init()
{
2013-10-13 17:56:58 -04: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 08:25:00 -04:00
}
} // namespace Time
} // namespace Sortix