From ad51b5a07004d8317396bcb90ec50f97cf818633 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Wed, 15 May 2013 15:00:48 +0200 Subject: [PATCH] Add clock(3). --- libc/Makefile | 2 +- libc/include/time.h | 2 +- libc/{ => time}/clock.cpp | 17 ++++++++++------- 3 files changed, 12 insertions(+), 9 deletions(-) rename libc/{ => time}/clock.cpp (68%) diff --git a/libc/Makefile b/libc/Makefile index ac0f20e5..b57e22ed 100644 --- a/libc/Makefile +++ b/libc/Makefile @@ -174,7 +174,6 @@ canonicalize_file_name.o \ chdir.o \ chmod.o \ chown.o \ -clock.o \ close.o \ confstr.o \ $(CPUDIR)/calltrace.o \ @@ -355,6 +354,7 @@ time/clock_getres.o \ time/clock_gettime.o \ time/clock_gettimeres.o \ time/clock_nanosleep.o \ +time/clock.o \ time/clock_settime.o \ time/clock_settimeres.o \ time/nanosleep.o \ diff --git a/libc/include/time.h b/libc/include/time.h index 2ebe8a03..42ce529c 100644 --- a/libc/include/time.h +++ b/libc/include/time.h @@ -62,7 +62,7 @@ __BEGIN_DECLS @include(NULL.h) -#define CLOCKS_PER_SEC ((clock_t) 1000000) +#define CLOCKS_PER_SEC 1000000l __END_DECLS #include diff --git a/libc/clock.cpp b/libc/time/clock.cpp similarity index 68% rename from libc/clock.cpp rename to libc/time/clock.cpp index 979ed0f7..1b417188 100644 --- a/libc/clock.cpp +++ b/libc/time/clock.cpp @@ -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,16 +17,19 @@ You should have received a copy of the GNU Lesser General Public License along with the Sortix C Library. If not, see . - clock.cpp - Determine processor time. + time/clock.cpp + Get process execution time. *******************************************************************************/ -#include #include -extern "C" clock_t clock(void) +// TODO: This function is crap and has been replaced by clock_gettime. +extern "C" clock_t clock() { - errno = ENOTSUP; - return -1; + struct timespec now; + if ( clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &now) < 0 ) + return -1; + static_assert(CLOCKS_PER_SEC == 1000000, "CLOCKS_PER_SEC == 1000000"); + return (clock_t) now.tv_sec * 1000000 + (clock_t) now.tv_sec / 1000; }