This is the mail archive of the
ecos-discuss@sourceware.org
mailing list for the eCos project.
Re: ecos and gettimeofday()
- From: Alexander Neundorf <neundorf at kde dot org>
- To: ecos-discuss at ecos dot sourceware dot org
- Date: Wed, 12 Oct 2005 20:12:01 +0200
- Subject: Re: [ECOS] ecos and gettimeofday()
- References: <200509292331.26058.neundorf@kde.org> <87achvdsn2.fsf@p4.48ers.dk> <200510041926.27347.neundorf@kde.org>
- Reply-to: neundorf at kde dot org
Hi,
attached you can find two patches which add gettimeofday(). This patch is not
intended to be commited as is, it's just a first attempt, in order to get
some comments how to do it right.
It does the following:
it adds a very simple sys/time.h to the isoinfra/ package, with the contents
more or less according to
http://www.opengroup.org/onlinepubs/000095399/toc.htm.
A simple version of gettimeofday() is included in the header as inline
function.
Additionally it adds the sys/time.h which currently lives in the tcp/ip
package to the compat/posix/ package, which overrides the contents of
isoinfra/include/sys/time.h.
What do you think about this approach ?
If it is ok, I'll prepare a complete patch.
If you think a gettimeofday() implementation doesn't belong in a header in
isoinfra/ (I guess that will be the case), please suggest a better approach.
If gettimeofday() would be implemented only in the posix package, then e.g.
the freebsd tcp/ip stack would require the posix compat. package. I don't
think this would be a good idea, adding the dependency to posix just for a
single tiny function.
Where would be a better place for gettimeofday(), maybe
language/c/libc/time/ ? Please let me know.
Bye
Alex
--
Work: alexander.neundorf@jenoptik.com - http://www.jenoptik-los.de
Home: neundorf@kde.org - http://www.kde.org
alex@neundorf.net - http://www.neundorf.net
diff -rbupN /usr/src/ecos-stuff/ecos-unchanged/packages/isoinfra/current/include/sys/time.h isoinfra/current/include/sys/time.h
--- /usr/src/ecos-stuff/ecos-unchanged/packages/isoinfra/current/include/sys/time.h 1970-01-01 01:00:00.000000000 +0100
+++ isoinfra/current/include/sys/time.h 2005-10-12 18:58:14.000000000 +0200
@@ -0,0 +1,103 @@
+#ifndef CYGONCE_ISO_SYS_TIME_H
+#define CYGONCE_ISO_SYS_TIME_H
+/*========================================================================
+//
+// sys/time.h
+//
+// ISO time functions
+//
+//========================================================================
+//####ECOSGPLCOPYRIGHTBEGIN####
+// -------------------------------------------
+// This file is part of eCos, the Embedded Configurable Operating System.
+// Copyright (C) 2005 Alexander Neundorf
+//
+// eCos is free software; you can redistribute it and/or modify it under
+// the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 2 or (at your option) any later version.
+//
+// eCos 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 General Public License along
+// with eCos; if not, write to the Free Software Foundation, Inc.,
+// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+//
+// As a special exception, if other files instantiate templates or use macros
+// or inline functions from this file, or you compile this file and link it
+// with other works to produce a work based on this file, this file does not
+// by itself cause the resulting work to be covered by the GNU General Public
+// License. However the source code for this file must still be made available
+// in accordance with section (3) of the GNU General Public License.
+//
+// This exception does not invalidate any other reasons why a work based on
+// this file might be covered by the GNU General Public License.
+//
+// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
+// at http://sources.redhat.com/ecos/ecos-license/
+// -------------------------------------------
+//####ECOSGPLCOPYRIGHTEND####
+//========================================================================
+//#####DESCRIPTIONBEGIN####
+//
+// Author(s): Alexander Neundorf <neundorf@kde.org>
+// Contributors:
+// Date: 2005-10-04
+// Purpose: This file provides the time macros, types and functions
+// required by ISO C and POSIX 1003.1.
+// Description: The real contents of this file get set from the
+// configuration (set by the implementation)
+// Usage: #include <time.h>
+//
+//####DESCRIPTIONEND####
+//
+//======================================================================
+*/
+
+/* CONFIGURATION */
+
+#include <pkgconf/isoinfra.h> /* Configuration header */
+
+/* INCLUDES */
+
+/* This is the "standard" way to get NULL and size_t from stddef.h,
+ * which is the canonical location of the definitions.
+ */
+#define __need_NULL
+#define __need_size_t
+#include <stddef.h>
+
+#include <time.h>
+#ifdef CYGBLD_ISO_STRUCTTIMEVAL_HEADER
+# include CYGBLD_ISO_STRUCTTIMEVAL_HEADER
+#else
+
+/* a very minimalistic sys/time.h */
+
+#include <cyg/kernel/kapi.h>
+
+/*
+ * Structure returned by gettimeofday(2),
+ * and used in other calls such as select(2).
+ */
+struct timeval {
+ time_t tv_sec; /* seconds */
+ time_t tv_usec; /* and microseconds */
+};
+
+static __inline int gettimeofday(struct timeval *tv, void *restrict)
+{
+ cyg_tick_count_t time_us = cyg_current_time() * (CYGNUM_HAL_RTC_NUMERATOR/CYGNUM_HAL_RTC_DENOMINATOR/1000);
+ tv->tv_sec = time_us / 1000000;
+ tv->tv_usec = time_us % 1000000;
+ return 0;
+}
+
+#endif
+
+
+#endif /* CYGONCE_ISO_SYS_TIME_H multiple inclusion protection */
+
+/* EOF sys/time.h */
diff -rbupN /usr/src/ecos-stuff/ecos-unchanged/packages/isoinfra/current/include/time.h isoinfra/current/include/time.h
--- /usr/src/ecos-stuff/ecos-unchanged/packages/isoinfra/current/include/time.h 2002-05-24 01:06:43.000000000 +0200
+++ isoinfra/current/include/time.h 2005-10-12 18:21:15.000000000 +0200
@@ -99,23 +99,7 @@ typedef long clock_t;
# define __clock_t_defined
#endif
-#ifdef CYGBLD_ISO_STRUCTTIMEVAL_HEADER
-# include CYGBLD_ISO_STRUCTTIMEVAL_HEADER
-#else
-# ifndef _POSIX_SOURCE
-
-/*
- * Structure returned by gettimeofday(2),
- * and used in other calls such as select(2).
- */
-struct timeval {
- long tv_sec; /* seconds */
- long tv_usec; /* and microseconds */
-};
-
-# endif /* _POSIX_SOURCE */
-#endif
-
+#include <sys/time.h>
#ifdef CYGINT_ISO_POSIX_CLOCK_TYPES
# include CYGBLD_ISO_POSIX_CLOCK_TYPES_HEADER
diff -rbupN /usr/src/ecos-stuff/ecos-unchanged/packages/compat/posix/current/cdl/posix.cdl compat/posix/current/cdl/posix.cdl
--- /usr/src/ecos-stuff/ecos-unchanged/packages/compat/posix/current/cdl/posix.cdl 2003-02-24 15:08:21.000000000 +0100
+++ compat/posix/current/cdl/posix.cdl 2005-10-12 18:49:53.000000000 +0200
@@ -145,6 +145,8 @@ cdl_package CYGPKG_POSIX {
"<cyg/posix/time.h>" }
requires { CYGBLD_ISO_POSIX_CLOCKS_HEADER == \
"<cyg/posix/time.h>" }
+ requires { CYGBLD_ISO_STRUCTTIMEVAL_HEADER == \
+ "<cyg/posix/sys/time.h>" }
requires CYGPKG_KERNEL
requires CYGVAR_KERNEL_COUNTERS_CLOCK
compile time.cxx
diff -rbupN /usr/src/ecos-stuff/ecos-unchanged/packages/compat/posix/current/include/sys/time.h compat/posix/current/include/sys/time.h
--- /usr/src/ecos-stuff/ecos-unchanged/packages/compat/posix/current/include/sys/time.h 1970-01-01 01:00:00.000000000 +0100
+++ compat/posix/current/include/sys/time.h 2005-10-12 19:04:41.000000000 +0200
@@ -0,0 +1,219 @@
+//==========================================================================
+//
+// include/sys/time.h
+//
+//
+//
+//==========================================================================
+//####BSDCOPYRIGHTBEGIN####
+//
+// -------------------------------------------
+//
+// Portions of this software may have been derived from OpenBSD or other sources,
+// and are covered by the appropriate copyright disclaimers included herein.
+//
+// -------------------------------------------
+//
+//####BSDCOPYRIGHTEND####
+//==========================================================================
+//#####DESCRIPTIONBEGIN####
+//
+// Author(s): gthomas
+// Contributors: gthomas
+// Date: 2000-01-10
+// Purpose:
+// Description:
+//
+//
+//####DESCRIPTIONEND####
+//
+//==========================================================================
+
+
+/* $OpenBSD: time.h,v 1.9 1999/12/06 19:36:42 aaron Exp $ */
+/* $NetBSD: time.h,v 1.18 1996/04/23 10:29:33 mycroft Exp $ */
+
+/*
+ * Copyright (c) 1982, 1986, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the University of
+ * California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * @(#)time.h 8.2 (Berkeley) 7/10/94
+ */
+
+#ifndef _SYS_TIME_H_
+#define _SYS_TIME_H_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <sys/types.h>
+#include <sys/cdefs.h>
+#include <time.h>
+
+/*
+ * Structure returned by gettimeofday(2) system call,
+ * and used in other calls.
+ */
+struct timeval {
+ long tv_sec; /* seconds */
+ long tv_usec; /* and microseconds */
+};
+
+
+#define TIMEVAL_TO_TIMESPEC(tv, ts) { \
+ (ts)->tv_sec = (tv)->tv_sec; \
+ (ts)->tv_nsec = (tv)->tv_usec * 1000; \
+}
+#define TIMESPEC_TO_TIMEVAL(tv, ts) { \
+ (tv)->tv_sec = (ts)->tv_sec; \
+ (tv)->tv_usec = (ts)->tv_nsec / 1000; \
+}
+
+struct timezone {
+ int tz_minuteswest; /* minutes west of Greenwich */
+ int tz_dsttime; /* type of dst correction */
+};
+
+#define DST_NONE 0 /* not on dst */
+#define DST_USA 1 /* USA style dst */
+#define DST_AUST 2 /* Australian style dst */
+#define DST_WET 3 /* Western European dst */
+#define DST_MET 4 /* Middle European dst */
+#define DST_EET 5 /* Eastern European dst */
+#define DST_CAN 6 /* Canada */
+
+/* Operations on timevals. */
+#define timerclear(tvp) (tvp)->tv_sec = (tvp)->tv_usec = 0
+#define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
+#define timercmp(tvp, uvp, cmp) \
+ (((tvp)->tv_sec == (uvp)->tv_sec) ? \
+ ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
+ ((tvp)->tv_sec cmp (uvp)->tv_sec))
+#define timeradd(tvp, uvp, vvp) \
+ do { \
+ (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
+ (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
+ if ((vvp)->tv_usec >= 1000000) { \
+ (vvp)->tv_sec++; \
+ (vvp)->tv_usec -= 1000000; \
+ } \
+ } while (0)
+#define timersub(tvp, uvp, vvp) \
+ do { \
+ (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
+ (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
+ if ((vvp)->tv_usec < 0) { \
+ (vvp)->tv_sec--; \
+ (vvp)->tv_usec += 1000000; \
+ } \
+ } while (0)
+
+/* Operations on timespecs. */
+#define timespecclear(tsp) (tsp)->tv_sec = (tsp)->tv_nsec = 0
+#define timespecisset(tsp) ((tsp)->tv_sec || (tsp)->tv_nsec)
+#define timespeccmp(tsp, usp, cmp) \
+ (((tsp)->tv_sec == (usp)->tv_sec) ? \
+ ((tsp)->tv_nsec cmp (usp)->tv_nsec) : \
+ ((tsp)->tv_sec cmp (usp)->tv_sec))
+#define timespecadd(tsp, usp, vsp) \
+ do { \
+ (vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec; \
+ (vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec; \
+ if ((vsp)->tv_nsec >= 1000000000L) { \
+ (vsp)->tv_sec++; \
+ (vsp)->tv_nsec -= 1000000000L; \
+ } \
+ } while (0)
+#define timespecsub(tsp, usp, vsp) \
+ do { \
+ (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
+ (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
+ if ((vsp)->tv_nsec < 0) { \
+ (vsp)->tv_sec--; \
+ (vsp)->tv_nsec += 1000000000L; \
+ } \
+ } while (0)
+
+
+//#if defined(_KERNEL) || defined(_STANDALONE)
+void microtime __P((struct timeval *tv));
+int gettimeofday __P((struct timeval *, struct timezone *));
+
+//#endif /* !_KERNEL */
+
+//#endif /* !_SYS_TIME_H_ */
+
+/* the following functions are not supported by eCos */
+#if 0
+
+int adjtime __P((const struct timeval *, struct timeval *));
+int clock_getres __P((clockid_t, struct timespec *));
+int clock_gettime __P((clockid_t, struct timespec *));
+int clock_settime __P((clockid_t, const struct timespec *));
+int futimes __P((int, const struct timeval *));
+int getitimer __P((int, struct itimerval *));
+int nanosleep __P((const struct timespec *, struct timespec *));
+int setitimer __P((int, const struct itimerval *, struct itimerval *));
+int settimeofday __P((const struct timeval *, const struct timezone *));
+int utimes __P((const char *, const struct timeval *));
+
+
+int itimerdecr __P((struct itimerval *itp, int usec));
+int itimerfix __P((struct timeval *tv));
+void settime __P((struct timeval *tv));
+
+int adjtime __P((const struct timeval *, struct timeval *));
+int clock_getres __P((clockid_t, struct timespec *));
+int clock_gettime __P((clockid_t, struct timespec *));
+int clock_settime __P((clockid_t, const struct timespec *));
+int futimes __P((int, const struct timeval *));
+int getitimer __P((int, struct itimerval *));
+int nanosleep __P((const struct timespec *, struct timespec *));
+int setitimer __P((int, const struct itimerval *, struct itimerval *));
+int settimeofday __P((const struct timeval *, const struct timezone *));
+int utimes __P((const char *, const struct timeval *));
+
+/*
+ * Structure defined by POSIX.1b to be like a timeval.
+ */
+struct timespec {
+ time_t tv_sec; /* seconds */
+ long tv_nsec; /* and nanoseconds */
+};
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+
+#endif
diff -rbupN /usr/src/ecos-stuff/ecos-unchanged/packages/compat/posix/current/src/time.cxx compat/posix/current/src/time.cxx
--- /usr/src/ecos-stuff/ecos-unchanged/packages/compat/posix/current/src/time.cxx 2003-01-31 12:53:14.000000000 +0100
+++ compat/posix/current/src/time.cxx 2005-10-12 18:55:46.000000000 +0200
@@ -67,6 +67,7 @@
#include "pprivate.h" // POSIX private header
#include <time.h> // our header
+#include <sys/time.h> // our header
#include <cyg/kernel/thread.hxx>
#include <cyg/kernel/clock.hxx>
@@ -311,6 +312,17 @@ externC void cyg_posix_timer_asr( pthrea
#endif // ifdef CYGPKG_POSIX_TIMERS
+// Get the current time in a struct timeval
+externC int gettimeofday(struct timeval* tv, struct timezone* tz)
+{
+ struct timespec ts;
+ TIME_ENTRY();
+ cyg_ticks_to_timespec( Cyg_Clock::real_time_clock->current_value(), &ts);
+ tv->tv_sec=ts.tv_sec;
+ tv->tv_usec=ts.tv_nsec/1000;
+ TIME_RETURN(0);
+}
+
//==========================================================================
// Clock functions
--
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss