Add clock_*(2) API.

This commit is contained in:
Jonas 'Sortie' Termansen 2013-05-12 22:10:34 +02:00
parent 2d94cd1246
commit 1e2550c0d5
11 changed files with 228 additions and 19 deletions

View File

@ -242,7 +242,6 @@ getpagesize.o \
getpid.o \
getppid.o \
gettermmode.o \
gettimeofday.o \
getuid.o \
grent.o \
init.o \
@ -350,8 +349,14 @@ sys/socket/sockatmark.o \
sys/socket/socket.o \
sys/socket/socketpair.o \
system.o \
sys/time/gettimeofday.o \
tfork.o \
time.o \
time/clock_getres.o \
time/clock_gettime.o \
time/clock_gettimeres.o \
time/clock_settime.o \
time/clock_settimeres.o \
time/time.o \
time/timer_create.o \
time/timer_delete.o \
time/timer_getoverrun.o \

View File

@ -102,6 +102,12 @@ int timer_settime(timer_t, int, const struct itimerspec* __restrict,
struct itimerspec* __restrict);
void tzset(void);
#if defined(_SORTIX_SOURCE)
int clock_gettimeres(clockid_t, struct timespec*, struct timespec*);
int clock_settimeres(clockid_t, const struct timespec*, const struct timespec*);
#endif
extern int daylight;
extern long timezone;
extern char* tzname[];

View File

@ -1,6 +1,6 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012.
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013.
This file is part of the Sortix C Library.
@ -17,20 +17,22 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
gettimeofday.cpp
sys/time/gettimeofday.cpp
Get date and time.
*******************************************************************************/
#include <sys/time.h>
#include <errno.h>
#include <unistd.h>
#include <time.h>
extern "C" int gettimeofday(struct timeval* tp, void* /*tzp*/)
{
uintmax_t sinceboot;
uptime(&sinceboot);
tp->tv_sec = sinceboot / 1000000ULL;
tp->tv_usec = sinceboot % 1000000ULL;
struct timespec now;
// TODO: We should be using CLOCK_REALTIME.
if ( clock_gettime(CLOCK_MONOTONIC, &now) < 0 )
return -1;
tp->tv_sec = now.tv_sec;
tp->tv_usec = now.tv_nsec / 1000;
return 0;
}

View File

@ -0,0 +1,30 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2013.
This file is part of the Sortix C Library.
The Sortix C Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
The Sortix C Library 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 Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
time/clock_getres.cpp
Get clock resolution.
*******************************************************************************/
#include <time.h>
extern "C" int clock_getres(clockid_t clockid, struct timespec* res)
{
return clock_gettimeres(clockid, NULL, res);
}

View File

@ -0,0 +1,30 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2013.
This file is part of the Sortix C Library.
The Sortix C Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
The Sortix C Library 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 Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
time/clock_gettime.cpp
Get clock time.
*******************************************************************************/
#include <time.h>
extern "C" int clock_gettime(clockid_t clockid, struct timespec* time)
{
return clock_gettimeres(clockid, time, NULL);
}

View File

@ -0,0 +1,36 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2013.
This file is part of the Sortix C Library.
The Sortix C Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
The Sortix C Library 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 Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
time/clock_gettimeres.cpp
Get clock time and resolution.
*******************************************************************************/
#include <sys/syscall.h>
#include <time.h>
DEFN_SYSCALL3(int, sys_clock_gettimeres, SYSCALL_CLOCK_GETTIMERES, clockid_t,
struct timespec*, struct timespec*);
extern "C" int clock_gettimeres(clockid_t clockid, struct timespec* time,
struct timespec* res)
{
return sys_clock_gettimeres(clockid, time, res);
}

View File

@ -0,0 +1,30 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2013.
This file is part of the Sortix C Library.
The Sortix C Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
The Sortix C Library 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 Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
time/clock_settime.cpp
Set clock time.
*******************************************************************************/
#include <time.h>
extern "C" int clock_settime(clockid_t clockid, const struct timespec* time)
{
return clock_settimeres(clockid, time, NULL);
}

View File

@ -0,0 +1,36 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2013.
This file is part of the Sortix C Library.
The Sortix C Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
The Sortix C Library 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 Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
time/clock_settimeres.cpp
Set clock time and resolution.
*******************************************************************************/
#include <sys/syscall.h>
#include <time.h>
DEFN_SYSCALL3(int, sys_clock_settimeres, SYSCALL_CLOCK_SETTIMERES, clockid_t,
const struct timespec*, const struct timespec*);
extern "C" int clock_settimeres(clockid_t clockid, const struct timespec* time,
const struct timespec* res)
{
return sys_clock_settimeres(clockid, time, res);
}

View File

@ -1,6 +1,6 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012.
Copyright(C) Jonas 'Sortie' Termansen 2013
This file is part of the Sortix C Library.
@ -17,19 +17,17 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
time.cpp
time/time.cpp
Get time in seconds.
*******************************************************************************/
#include <sys/time.h>
#include <stddef.h>
#include <time.h>
extern "C" time_t time(time_t* t)
{
struct timeval tv;
gettimeofday(&tv, NULL);
time_t result = tv.tv_sec;
return t ? *t = result : result;
struct timespec now;
if ( clock_gettime(CLOCK_REALTIME, &now) < 0 )
return -1;
return t ? *t = now.tv_sec : now.tv_sec;
}

View File

@ -124,6 +124,8 @@
#define SYSCALL_TIMER_GETTIME 100
#define SYSCALL_TIMER_SETTIME 101
#define SYSCALL_ALARMNS 102
#define SYSCALL_MAX_NUM 103 /* index of highest constant + 1 */
#define SYSCALL_CLOCK_GETTIMERES 103
#define SYSCALL_CLOCK_SETTIMERES 104
#define SYSCALL_MAX_NUM 105 /* index of highest constant + 1 */
#endif

View File

@ -211,6 +211,38 @@ static int sys_timer_settime(timer_t timerid, int flags,
return 0;
}
static int sys_clock_gettimeres(clockid_t clockid, struct timespec* time,
struct timespec* res)
{
Clock* clock = Time::GetClock(clockid);
if ( !clock )
return -1;
struct timespec ktime, kres;
clock->Get(&ktime, &kres);
return (!time || CopyToUser(time, &ktime, sizeof(ktime))) &&
(!res || CopyToUser(res, &kres, sizeof(kres))) ? 0 : -1;
}
static int sys_clock_settimeres(clockid_t clockid, const struct timespec* time,
const struct timespec* res)
{
Clock* clock = Time::GetClock(clockid);
if ( !clock )
return -1;
struct timespec ktime, kres;
if ( (time && !CopyFromUser(&ktime, time, sizeof(ktime))) ||
(res && !CopyFromUser(&kres, res, sizeof(kres))) )
return -1;
clock->Set(time ? &ktime : NULL, res ? &kres : NULL);
return 0;
}
// TODO: Made obsolete by cloc_gettimeres.
static int sys_uptime(uintmax_t* usecssinceboot)
{
struct timespec now;
@ -226,6 +258,8 @@ static int sys_uptime(uintmax_t* usecssinceboot)
void UserTimer::Init()
{
Syscall::Register(SYSCALL_CLOCK_GETTIMERES, (void*) sys_clock_gettimeres);
Syscall::Register(SYSCALL_CLOCK_SETTIMERES, (void*) sys_clock_settimeres);
Syscall::Register(SYSCALL_TIMER_CREATE, (void*) sys_timer_create);
Syscall::Register(SYSCALL_TIMER_DELETE, (void*) sys_timer_delete);
Syscall::Register(SYSCALL_TIMER_GETOVERRUN, (void*) sys_timer_getoverrun);