From bd149a18f166dab41f16cb2dc539cb2d70b2ca79 Mon Sep 17 00:00:00 2001 From: Giacomo Tesio Date: Fri, 25 Aug 2017 00:53:10 +0200 Subject: [PATCH] libposix: implement gettimeofday --- qa/lib/newlib/040-gettimeofday.c | 24 ++++++++++ qa/lib/newlib/build.json | 2 + sys/src/lib/posix/build.json | 3 +- sys/src/lib/posix/others.c | 7 --- sys/src/lib/posix/processes.c | 2 +- sys/src/lib/posix/signals.c | 5 ++- sys/src/lib/posix/timers.c | 76 ++++++++++++++++++++++++++++++++ 7 files changed, 108 insertions(+), 11 deletions(-) create mode 100644 qa/lib/newlib/040-gettimeofday.c create mode 100644 sys/src/lib/posix/timers.c diff --git a/qa/lib/newlib/040-gettimeofday.c b/qa/lib/newlib/040-gettimeofday.c new file mode 100644 index 0000000..769d958 --- /dev/null +++ b/qa/lib/newlib/040-gettimeofday.c @@ -0,0 +1,24 @@ +#include +#include + +int main (int argc, char** argv) { + struct timeval tvalBefore, tvalAfter; + long delta; + + gettimeofday (&tvalBefore, NULL); + int i =0; + while (i < 10000) + i++; + + gettimeofday (&tvalAfter, NULL); + + // Changed format to long int (%ld), changed time calculation + + delta = ((tvalAfter.tv_sec - tvalBefore.tv_sec)*1000000L + +tvalAfter.tv_usec) - tvalBefore.tv_usec; + printf("Time in microseconds: %ld microseconds\n", delta); + + if(delta > 0) + return 0; + return 1; +} diff --git a/qa/lib/newlib/build.json b/qa/lib/newlib/build.json index 14e8018..ea341f4 100644 --- a/qa/lib/newlib/build.json +++ b/qa/lib/newlib/build.json @@ -35,6 +35,7 @@ "010-fork.c", "020-waitpid.c", "030-pause.c", + "040-gettimeofday.c", "100-files.c", "101-files.c", "102-files.c", @@ -84,6 +85,7 @@ "010-fork.c", "020-waitpid.c", "030-pause.c", + "040-gettimeofday.c", "100-files.c", "101-files.c", "102-files.c", diff --git a/sys/src/lib/posix/build.json b/sys/src/lib/posix/build.json index 6075ef1..fe2da9d 100644 --- a/sys/src/lib/posix/build.json +++ b/sys/src/lib/posix/build.json @@ -18,7 +18,8 @@ "others.c", "processes.c", "sigchlds.c", - "signals.c" + "signals.c", + "timers.c" ] } } diff --git a/sys/src/lib/posix/others.c b/sys/src/lib/posix/others.c index 94c29cb..1088c7c 100644 --- a/sys/src/lib/posix/others.c +++ b/sys/src/lib/posix/others.c @@ -75,13 +75,6 @@ POSIX_times(int *errnop, void *buf) return -1; } -int -POSIX_gettimeofday(int *errnop, void *timeval, void *timezone) -{ - *errnop = __libposix_get_errno(PosixEINVAL); - return -1; -} - char* POSIX_getlogin(int *errnop) { diff --git a/sys/src/lib/posix/processes.c b/sys/src/lib/posix/processes.c index b66761f..31d0efa 100644 --- a/sys/src/lib/posix/processes.c +++ b/sys/src/lib/posix/processes.c @@ -46,7 +46,7 @@ __libposix_free_wait_list(void) { WaitList *wl, *c; - /* free the wait list as the memory is shared */ + /* free the wait list as the memory is NOT shared */ wl = *__libposix_wait_list; if(wl != nil){ *__libposix_wait_list = nil; diff --git a/sys/src/lib/posix/signals.c b/sys/src/lib/posix/signals.c index 7e758fa..4edfe18 100644 --- a/sys/src/lib/posix/signals.c +++ b/sys/src/lib/posix/signals.c @@ -38,7 +38,8 @@ * * Since notes in Jehanne are not reentrant, signals translated to * notes will be enqueued in kernel. A special machinery is implemented - * for timers, so that they can be used in signal handlers. + * for timers, so that they can be used in signal handlers to wakeup + * the calling process. * * For all the signals except SIGCONT, the burden of interpreting the * signal is on the receiver: the sender just send the signal. @@ -98,7 +99,7 @@ * * TIMERS * ------ - * The functions alarm() and settimer() generate SIGALRM, SIGPROF + * The functions alarm() and setitimer() generate SIGALRM, SIGPROF * or SIGVTALRM for the current process. We want timers to be able to * expire in a signal handler (interrupting a blocking syscall) but * without giving up the simplicity of notes. diff --git a/sys/src/lib/posix/timers.c b/sys/src/lib/posix/timers.c new file mode 100644 index 0000000..ea1ad9d --- /dev/null +++ b/sys/src/lib/posix/timers.c @@ -0,0 +1,76 @@ +/* + * This file is part of Jehanne. + * + * Copyright (C) 2017 Giacomo Tesio + * + * This is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, version 3 of the License. + * + * Jehanne 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. + * + * You should have received a copy of the GNU Affero General Public License + * along with Jehanne. If not, see . + */ +#include +#include +#include +#include "internal.h" + +static PosixTimevalReader __libposix_timeval_reader; +static PosixTimezoneReader __libposix_timezone_reader; + +int +POSIX_gettimeofday(int *errnop, void *timeval, void *timezone) +{ + Tm *t; + PosixError e = 0; + + if(timeval == nil && timezone == nil){ + e = PosixEFAULT; + goto FailWithError; + } + + t = localtime(time(nil)); + + if(timeval != nil){ + e = __libposix_timeval_reader(timeval, t); + if(e != 0) + goto FailWithError; + } + if(timezone != nil){ + e = __libposix_timezone_reader(timezone, t); + if(e != 0) + goto FailWithError; + } + return 0; + +FailWithError: + *errnop = __libposix_get_errno(e); + return -1; +} + +int +libposix_set_timeval_reader(PosixTimevalReader reader) +{ + if(__libposix_initialized()) + return 0; + if(reader == nil) + return 0; + __libposix_timeval_reader = reader; + return 1; +} + +int +libposix_set_timezone_reader(PosixTimezoneReader reader) +{ + if(__libposix_initialized()) + return 0; + if(reader == nil) + return 0; + __libposix_timezone_reader = reader; + return 1; +}