2013-07-10 09:26:01 -04:00
|
|
|
/*******************************************************************************
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2013-01-09 10:07:27 -05:00
|
|
|
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013.
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
This file is part of Sortix.
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2013-07-10 09:26:01 -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
|
|
|
|
2013-07-10 09:26:01 -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
|
|
|
|
2013-07-10 09:26:01 -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
|
|
|
|
2013-07-10 09:26:01 -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
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
*******************************************************************************/
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2013-01-09 10:07:27 -05:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <timespec.h>
|
|
|
|
|
|
|
|
#include <sortix/clock.h>
|
|
|
|
|
2012-03-21 19:52:29 -04:00
|
|
|
#include <sortix/kernel/platform.h>
|
2013-01-08 18:41:35 -05:00
|
|
|
#include <sortix/kernel/syscall.h>
|
2013-01-09 10:07:27 -05:00
|
|
|
#include <sortix/kernel/copy.h>
|
2013-01-09 17:30:36 -05:00
|
|
|
#include <sortix/kernel/interrupt.h>
|
2013-10-13 17:56:58 -04:00
|
|
|
#include <sortix/kernel/clock.h>
|
2013-05-14 18:45:04 -04:00
|
|
|
#include <sortix/kernel/process.h>
|
2013-01-09 10:07:27 -05:00
|
|
|
#include <sortix/kernel/time.h>
|
2013-01-09 04:47:22 -05:00
|
|
|
#include <sortix/kernel/scheduler.h>
|
2013-01-08 18:41:35 -05:00
|
|
|
|
2011-09-08 05:10:41 -04:00
|
|
|
#ifdef PLATFORM_SERIAL
|
2011-11-20 09:58:42 -05:00
|
|
|
#include "serialterminal.h"
|
2011-09-08 05:10:41 -04:00
|
|
|
#endif
|
|
|
|
|
2013-01-09 10:07:27 -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;
|
2011-08-28 10:59:07 -04:00
|
|
|
|
2013-10-13 17:56:58 -04:00
|
|
|
Clock* GetClock(clockid_t clock)
|
2013-01-09 10:07:27 -05:00
|
|
|
{
|
2013-10-13 17:56:58 -04:00
|
|
|
switch ( clock )
|
2013-01-09 10:07:27 -05:00
|
|
|
{
|
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;
|
2013-05-14 18:59:37 -04:00
|
|
|
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-01-09 10:07:27 -05:00
|
|
|
}
|
|
|
|
}
|
2011-08-28 10:59:07 -04:00
|
|
|
|
2013-10-13 17:56:58 -04:00
|
|
|
struct timespec Get(clockid_t clockid)
|
2013-01-09 10:07:27 -05:00
|
|
|
{
|
2013-10-13 17:56:58 -04:00
|
|
|
Clock* clock = GetClock(clockid);
|
|
|
|
if ( clock )
|
2013-01-09 10:07:27 -05:00
|
|
|
{
|
2013-10-13 17:56:58 -04:00
|
|
|
struct timespec now;
|
|
|
|
clock->Get(&now, NULL);
|
|
|
|
return now;
|
2013-01-09 10:07:27 -05:00
|
|
|
}
|
2013-10-13 17:56:58 -04:00
|
|
|
return timespec_nul();
|
2013-01-09 10:07:27 -05:00
|
|
|
}
|
2011-09-08 05:10:41 -04:00
|
|
|
|
2013-05-14 18:45:04 -04:00
|
|
|
void OnTick(struct timespec tick_period, bool system_mode)
|
2013-01-09 10:07:27 -05:00
|
|
|
{
|
2013-10-13 17:56:58 -04:00
|
|
|
realtime_clock->Advance(tick_period);
|
|
|
|
uptime_clock->Advance(tick_period);
|
2013-05-14 18:45:04 -04:00
|
|
|
Process* process = CurrentProcess();
|
|
|
|
process->execute_clock.Advance(tick_period);
|
2013-05-14 18:58:54 -04:00
|
|
|
process->child_execute_clock.Advance(tick_period);
|
2013-05-14 18:45:04 -04:00
|
|
|
if ( system_mode )
|
2013-05-14 18:58:54 -04:00
|
|
|
process->system_clock.Advance(tick_period),
|
|
|
|
process->child_system_clock.Advance(tick_period);
|
2013-01-09 10:07:27 -05:00
|
|
|
}
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2013-01-09 10:07:27 -05: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
|
|
|
}
|
2013-01-09 10:07:27 -05:00
|
|
|
|
|
|
|
} // namespace Time
|
|
|
|
} // namespace Sortix
|