Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:13

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup RTEMSScoreTimespec
0007  *
0008  * @brief This header file provides the interfaces of the
0009  * @ref RTEMSScoreTimespec.
0010  */
0011 
0012 /*
0013  * COPYRIGHT (C) 1989, 2021 On-Line Applications Research Corporation (OAR).
0014  *
0015  * Redistribution and use in source and binary forms, with or without
0016  * modification, are permitted provided that the following conditions
0017  * are met:
0018  * 1. Redistributions of source code must retain the above copyright
0019  *    notice, this list of conditions and the following disclaimer.
0020  * 2. Redistributions in binary form must reproduce the above copyright
0021  *    notice, this list of conditions and the following disclaimer in the
0022  *    documentation and/or other materials provided with the distribution.
0023  *
0024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0025  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0027  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0028  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0029  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0030  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0031  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0032  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0033  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0034  * POSSIBILITY OF SUCH DAMAGE.
0035  */
0036 
0037 #ifndef _RTEMS_SCORE_TIMESPEC_H
0038 #define _RTEMS_SCORE_TIMESPEC_H
0039 
0040 /**
0041  * @defgroup RTEMSScoreTimespec Timespec Helpers
0042  *
0043  * @ingroup RTEMSScore
0044  *
0045  * @brief This group contains the implementation to support operations with
0046  *   variables of the POSIX defined struct timespec type.
0047  *
0048  * @{
0049  */
0050 
0051 #include <stdbool.h> /* bool */
0052 #include <stdint.h> /* uint32_t */
0053 #include <time.h> /* struct timespec */
0054 
0055 #ifdef __cplusplus
0056 extern "C" {
0057 #endif
0058 
0059 /**
0060  * @brief Set timespec to seconds nanosecond.
0061  *
0062  *  This method sets the timespec to the specified seconds and nanoseconds
0063  *  value.
0064  *
0065  *  @param[in] _time points to the timespec instance to validate.
0066  *  @param[in] _seconds is the seconds portion of the timespec
0067  *  @param[in] _nanoseconds is the nanoseconds portion of the timespec
0068  */
0069 #define _Timespec_Set( _time, _seconds, _nanoseconds ) \
0070     do { \
0071        (_time)->tv_sec = (_seconds); \
0072        (_time)->tv_nsec = (_nanoseconds); \
0073     } while (0)
0074 
0075 /**
0076  * @brief Sets the Timespec to Zero
0077  *
0078  *  This method sets the timespec to zero.
0079  *  value.
0080  *
0081  *  @param[in] _time points to the timespec instance to zero.
0082  */
0083 #define _Timespec_Set_to_zero( _time ) \
0084     do { \
0085        (_time)->tv_sec = 0; \
0086        (_time)->tv_nsec = 0; \
0087     } while (0)
0088 
0089 /**
0090  * @brief Get seconds portion of timespec.
0091  *
0092  *  This method returns the seconds portion of the specified timespec
0093  *
0094  *  @param[in] _time points to the timespec
0095  *
0096  *  @retval The seconds portion of @a _time.
0097  */
0098 #define _Timespec_Get_seconds( _time ) \
0099     ((_time)->tv_sec)
0100 
0101 /**
0102  * @brief Get nanoseconds portion of timespec.
0103  *
0104  *  This method returns the nanoseconds portion of the specified timespec
0105  *
0106  *  @param[in] _time points to the timespec
0107  *
0108  *  @retval The nanoseconds portion of @a _time.
0109  */
0110 #define _Timespec_Get_nanoseconds( _time ) \
0111     ((_time)->tv_nsec)
0112 
0113 /**
0114  *  @brief Gets the timestamp as nanoseconds.
0115  *
0116  *  This method returns the timestamp as nanoseconds.
0117  *
0118  *  @param time points to the timestamp.
0119  *
0120  *  @return The time in nanoseconds.
0121  */
0122 uint64_t _Timespec_Get_as_nanoseconds(
0123   const struct timespec *time
0124 );
0125 
0126 /**
0127  * @brief Checks if the values in @a time are non-negative.
0128  *
0129  * @param[in] time is the timespec instance to validate.
0130  *
0131  * @retval true If @a time is filled with non-negative values.
0132  * @retval false If @a time is not filled with non-negative values.
0133  */
0134 bool _Timespec_Is_non_negative(
0135   const struct timespec *time
0136 );
0137 
0138 /**
0139  * @brief Checks if timespec is valid.
0140  *
0141  * This method determines the validity of a timespec.
0142  *
0143  * @param time is the timespec instance to validate.
0144  *
0145  * @retval true The timespec is valid.
0146  * @retval false The timespec is invalid.
0147  */
0148 bool _Timespec_Is_valid(
0149   const struct timespec *time
0150 );
0151 
0152 /**
0153  * @brief Checks if the left hand side timespec is less than the right one.
0154  *
0155  * This method is the less than operator for timespecs.
0156  *
0157  * @param lhs is the left hand side timespec.
0158  * @param rhs is the right hand side timespec.
0159  *
0160  * @retval true @a lhs is less than @a rhs.
0161  * @retval false @a lhs is greater than @a rhs.
0162  */
0163 bool _Timespec_Less_than(
0164   const struct timespec *lhs,
0165   const struct timespec *rhs
0166 );
0167 
0168 /**
0169  * @brief The Timespec "greater than" operator.
0170  *
0171  *  This method is the greater than operator for timespecs.
0172  *
0173  *  @param[in] _lhs is the left hand side timespec
0174  *  @param[in] _rhs is the right hand side timespec
0175  *
0176  *  @retval This method returns true if @a lhs is greater than the @a rhs and
0177  *          false otherwise.
0178  */
0179 #define _Timespec_Greater_than( _lhs, _rhs ) \
0180   _Timespec_Less_than( _rhs, _lhs )
0181 
0182 /**
0183  * @brief The Timespec "equal to" operator.
0184  *
0185  *  This method is the is equal to than operator for timespecs.
0186  *
0187  *  @param[in] lhs is the left hand side timespec
0188  *  @param[in] rhs is the right hand side timespec
0189  *
0190  *  @retval This method returns true if @a lhs is equal to  @a rhs and
0191  *          false otherwise.
0192  */
0193 #define _Timespec_Equal_to( lhs, rhs ) \
0194   ( ((lhs)->tv_sec  == (rhs)->tv_sec) &&   \
0195     ((lhs)->tv_nsec == (rhs)->tv_nsec)     \
0196   )
0197 
0198 /**
0199  * @brief Adds two timespecs.
0200  *
0201  * This routine adds two timespecs.  The second argument is added
0202  * to the first.
0203  *
0204  * @param time The base time to be added to.
0205  * @param add The timespec to add to the first argument.
0206  *
0207  * @return The number of seconds @a time increased by.
0208  */
0209 time_t _Timespec_Add_to(
0210   struct timespec       *time,
0211   const struct timespec *add
0212 );
0213 
0214 /**
0215  * @brief Converts timespec to number of ticks.
0216  *
0217  * This routine convert the @a time timespec to the corresponding number
0218  * of clock ticks.
0219  *
0220  * @param time The time to be converted.
0221  *
0222  * @return The number of ticks computed.
0223  */
0224 uint32_t _Timespec_To_ticks(
0225   const struct timespec *time
0226 );
0227 
0228 /**
0229  * @brief Converts ticks to timespec.
0230  *
0231  *  This routine converts the @a ticks value to the corresponding
0232  *  timespec format @a time.
0233  *
0234  *  @param ticks The number of ticks to convert.
0235  *  @param[out] time The timespec format time result.
0236  */
0237 void _Timespec_From_ticks(
0238   uint32_t         ticks,
0239   struct timespec *time
0240 );
0241 
0242 /**
0243  * @brief Subtracts two timespec.
0244  *
0245  * This routine subtracts two timespecs.  @a result is set to
0246  * @a end - @a start.
0247  *
0248  * @param start The starting time
0249  * @param end The ending time
0250  * @param[out] result The difference between starting and ending time.
0251  */
0252 void _Timespec_Subtract(
0253   const struct timespec *start,
0254   const struct timespec *end,
0255   struct timespec       *result
0256 );
0257 
0258 /**
0259  * @brief Divides timespec by an integer.
0260  *
0261  * This routine divides a timespec by an integer value.  The expected
0262  * use is to assist in benchmark calculations where you typically
0263  * divide a duration by a number of iterations.
0264  *
0265  * @param time The total.
0266  * @param iterations The number of iterations.
0267  * @param[out] result The average time.
0268  */
0269 void _Timespec_Divide_by_integer(
0270   const struct timespec *time,
0271   uint32_t               iterations,
0272   struct timespec       *result
0273 );
0274 
0275 /**
0276  * @brief Divides a timespec by another timespec.
0277  *
0278  * This routine divides a timespec by another timespec.  The
0279  * intended use is for calculating percentages to three decimal points.
0280  *
0281  * @param lhs The left hand timespec.
0282  * @param rhs The right hand timespec.
0283  * @param[out] ival_percentage The integer portion of the average.
0284  * @param[out] fval_percentage The thousandths of percentage.
0285  */
0286 void _Timespec_Divide(
0287   const struct timespec *lhs,
0288   const struct timespec *rhs,
0289   uint32_t              *ival_percentage,
0290   uint32_t              *fval_percentage
0291 );
0292 
0293 #ifdef __cplusplus
0294 }
0295 #endif
0296 
0297 /** @} */
0298 
0299 #endif
0300 /* end of include file */