Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

time_util.h

Go to the documentation of this file.
00001 /*
00002  * time_util.h
00003  *
00004  * Copyright (c) 2003 The University of Utah and the Flux Group.
00005  * All rights reserved.
00006  *
00007  * This file is licensed under the terms of the GNU Public License.  
00008  * See the file "license.terms" for restrictions on redistribution 
00009  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
00010  */
00011 
00012 /**
00013  * @file time_util.h
00014  *
00015  * Inline functions for manipulating time values.
00016  */
00017 
00018 #ifndef _time_util_h
00019 #define _time_util_h
00020 
00021 #include "assert_pp.h"
00022 
00023 /**
00024  * The number of microseconds per second.
00025  */
00026 #define MICROS_PER_SECOND 1000000
00027 
00028 /**
00029  * The number of nanoseconds per microsecond.
00030  */
00031 #define NANOS_PER_MICRO 1000
00032 
00033 /**
00034  * Convert a microsecond value to a struct timespec.
00035  *
00036  * @param microseconds The microsecond value to convert.
00037  * @return A timespec that represents the given number of microseconds.
00038  */
00039 static inline
00040 struct timespec microsec_to_timespec(unsigned long long microseconds)
00041 {
00042     struct timespec retval;
00043     
00044     retval.tv_sec =  (int)(microseconds / MICROS_PER_SECOND);
00045     retval.tv_nsec = (int)(microseconds % MICROS_PER_SECOND) * NANOS_PER_MICRO;
00046     return retval;
00047 }
00048 
00049 /**
00050  * Convert a timespec to a microsecond value.  @e NOTE: There is a loss of
00051  * precision in the conversion.
00052  *
00053  * @param time_spec The timespec to convert.
00054  * @return The number of microseconds specified by the timespec.
00055  */
00056 static inline
00057 unsigned long long timespec_to_microsec(struct timespec *time_spec)
00058 {
00059     long long retval;
00060 
00061     require(time_spec != 0);
00062     
00063     retval = time_spec->tv_sec * MICROS_PER_SECOND;
00064     retval += time_spec->tv_nsec / NANOS_PER_MICRO;
00065     return retval;
00066 }
00067 
00068 /**
00069  * Compare two timespec's.
00070  *
00071  * @param tvp Pointer to the first timespec.
00072  * @param uvp Pointer to the second timespec.
00073  * @param cmp The comparison operator.
00074  */
00075 #define tscmp(tvp, uvp, cmp)                                            \
00076         (((tvp)->tv_sec == (uvp)->tv_sec) ?                             \
00077             ((tvp)->tv_nsec cmp (uvp)->tv_nsec) :                       \
00078             ((tvp)->tv_sec cmp (uvp)->tv_sec))
00079 
00080 #endif

Generated on Mon Dec 1 16:21:56 2003 for CPUBroker by doxygen 1.3.4