libposix: implement gettimeofday

This commit is contained in:
Giacomo Tesio 2017-08-25 00:53:10 +02:00
parent 2409419ba2
commit bd149a18f1
7 changed files with 108 additions and 11 deletions

View File

@ -0,0 +1,24 @@
#include <stdio.h>
#include <sys/time.h>
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;
}

View File

@ -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",

View File

@ -18,7 +18,8 @@
"others.c",
"processes.c",
"sigchlds.c",
"signals.c"
"signals.c",
"timers.c"
]
}
}

View File

@ -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)
{

View File

@ -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;

View File

@ -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.

View File

@ -0,0 +1,76 @@
/*
* This file is part of Jehanne.
*
* Copyright (C) 2017 Giacomo Tesio <giacomo@tesio.it>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <u.h>
#include <lib9.h>
#include <posix.h>
#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;
}