Relicense Sortix to the ISC license.
I hereby relicense all my work on Sortix under the ISC license as below.
All Sortix contributions by other people are already under this license,
are not substantial enough to be copyrightable, or have been removed.
All imported code from other projects is compatible with this license.
All GPL licensed code from other projects had previously been removed.
Copyright 2011-2016 Jonas 'Sortie' Termansen and contributors.
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.
2016-03-02 17:38:16 -05:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
2013-01-09 10:07:27 -05: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>
|
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-26 20:42:10 -04:00
|
|
|
#include <sortix/kernel/kernel.h>
|
2013-05-14 18:45:04 -04:00
|
|
|
#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>
|
2016-01-08 19:17:08 -05:00
|
|
|
#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
|
|
|
|
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;
|
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;
|
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;
|
2016-01-08 19:17:08 -05:00
|
|
|
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-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);
|
2016-01-08 19:17:08 -05:00
|
|
|
Thread* thread = CurrentThread();
|
|
|
|
Process* process = thread->process;
|
|
|
|
thread->execute_clock.Advance(tick_period);
|
2013-05-14 18:45:04 -04:00
|
|
|
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 )
|
2016-01-08 19:17:08 -05:00
|
|
|
{
|
|
|
|
thread->system_clock.Advance(tick_period);
|
|
|
|
process->system_clock.Advance(tick_period);
|
2013-05-14 18:58:54 -04:00
|
|
|
process->child_system_clock.Advance(tick_period);
|
2016-01-08 19:17:08 -05:00
|
|
|
}
|
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
|