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

104 lines
3 KiB
C++
Raw Normal View History

/*
* Copyright (c) 2011, 2012, 2013 Jonas 'Sortie' Termansen.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* 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
#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/thread.h>
2013-10-26 20:42:10 -04:00
#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;
2022-02-10 13:49:36 -05:00
case CLOCK_BOOTTIME: return uptime_clock;
2013-10-13 17:56:58 -04:00
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 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);
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 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