Changed time representation to posix style struct timeval/ gettimeofday.

This commit is contained in:
Bahadir Balban
2008-04-18 13:58:37 +01:00
parent 5563cc1c6b
commit cff7a505e8
5 changed files with 40 additions and 40 deletions

View File

@@ -29,20 +29,23 @@ static inline void increase_jiffies(void)
}
/* Represents time since epoch */
/* Internal representation of time since epoch */
struct time_info {
int reader;
u32 thz; /* Ticks in this hertz so far */
u32 sec;
u32 min;
u32 hour;
u64 day;
u64 sec; /* Seconds so far */
};
/* Used by posix systems */
struct timeval {
int tv_sec;
int tv_usec;
};
static struct time_info systime = { 0 };
/*
* A terribly basic (probably erroneous)
* A very basic (probably erroneous)
* rule-of-thumb time calculation.
*/
void update_system_time(void)
@@ -54,41 +57,31 @@ void update_system_time(void)
/* Increase just like jiffies, but reset every HZ */
systime.thz++;
/* On every HZ increase seconds */
if (systime.thz == HZ) {
systime.thz = 0;
systime.sec++;
}
if (systime.sec == 60) {
systime.sec = 0;
systime.min++;
}
if (systime.min == 60) {
systime.min = 0;
systime.hour++;
}
if (systime.hour == 24) {
systime.hour = 0;
systime.day++;
}
}
/* Read system time */
int sys_time(struct syscall_args *args)
{
struct time_info *ti = (struct time_info *)args->r0;
struct timeval *tv = (struct timeval *)args->r0;
int set = (int)args->r1;
int retries = 20;
if (check_access((unsigned long)ti, sizeof(*ti), MAP_USR_RW_FLAGS) < 0)
if (check_access((unsigned long)tv, sizeof(*tv), MAP_USR_RW_FLAGS) < 0)
return -EINVAL;
/* Get time */
if (!set) {
while(retries > 0) {
systime.reader = 1;
memcpy(ti, &systime, sizeof(*ti));
retries--;
tv->tv_sec = systime.sec;
tv->tv_usec = 1000000 * systime.thz / HZ;
retries--;
if (systime.reader)
break;
}

View File

@@ -15,6 +15,7 @@
#include <kdata.h>
#include <syscalls.h>
#include <task.h>
#include <posix/sys/time.h>
/*
* TODO:
@@ -99,7 +100,7 @@ void handle_fs_requests(void)
void main(void)
{
struct time_info ti;
struct timeval tv;
printf("\n%s: Started with tid: %d\n", __TASKNAME__, self_tid());
@@ -107,12 +108,11 @@ void main(void)
wait_pager(PAGER_TID);
if (l4_time(&ti, 0) < 0) {
if (gettimeofday(&tv, 0) < 0) {
printf("Reading the time has failed.\n");
} else {
printf("Current time since system started: %u ticks, "
"%u seconds, %u minutes, %u hours, %llu days.\n",
ti.thz, ti.sec, ti.min, ti.hour, ti.day);
printf("Current time since system started: %u useconds, "
"%u seconds.\n", tv.tv_usec, tv.tv_sec);
}
printf("%s: Listening requests.\n", __TASKNAME__);

View File

@@ -68,20 +68,9 @@ typedef int (*__l4_kmem_control_t)(unsigned long pfn, int npages, int grant);
extern __l4_kmem_control_t __l4_kmem_control;
int l4_kmem_control(unsigned long pfn, int npages, int grant);
/* Represents time since epoch, a c0 specific structure. */
struct time_info {
int reader;
u32 thz; /* Ticks in this hertz so far */
u32 sec;
u32 min;
u32 hour;
u64 day;
};
typedef int (*__l4_time_t)(struct time_info *ti, int set);
typedef int (*__l4_time_t)(void *timeval, int set);
extern __l4_time_t __l4_time;
int l4_time(void *time_info, int set);
int l4_time(void *timeval, int set);
/* To be supplied by server tasks. */

View File

@@ -16,6 +16,7 @@ config_h = join(project_root, "include/l4/config.h")
env = Environment(CC = 'arm-none-linux-gnueabi-gcc',
CCFLAGS = ['-g', '-std=gnu99', '-nostdlib', '-ffreestanding'],
LINKFLAGS = ['-nostdlib'],
CPPPATH = ['#include'],
ENV = {'PATH' : os.environ['PATH']},
LIBS = 'gcc')

17
tasks/libposix/time.c Normal file
View File

@@ -0,0 +1,17 @@
#include <l4lib/arch/syscalls.h>
#include <sys/time.h>
#include <errno.h>
int gettimeofday(struct timeval *tv, struct timezone *tz)
{
int ret = l4_time(tv, 0);
/* If error, return positive error code */
if (ret < 0) {
errno = -ret;
return -1;
}
/* else return value */
return ret;
}