Changeset 709594f0 in rtems


Ignore:
Timestamp:
Aug 1, 2016, 8:54:18 PM (3 years ago)
Author:
Gedare Bloom <gedare@…>
Branches:
master
Children:
9a4eca5
Parents:
21d73655
git-author:
Gedare Bloom <gedare@…> (08/01/16 20:54:18)
git-committer:
Gedare Bloom <gedare@…> (08/10/16 16:09:50)
Message:

posix: nanosleep: adjust elapsed time calculation

Use clock_gettime before and after sleep to calculate
the time spent asleep, and the amount of time remaining.

updates #2732

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpukit/posix/src/nanosleep.c

    r21d73655 r709594f0  
    3434
    3535static inline int nanosleep_helper(
     36  clockid_t             clock_id,
    3637  uint64_t              ticks,
    37   Watchdog_Interval     relative_interval,
     38  struct timespec      *timeout,
    3839  struct timespec      *rmtp,
    3940  Watchdog_Discipline   discipline
     
    4142{
    4243  Thread_Control  *executing;
    43 
    44   Watchdog_Interval  start;
    45   Watchdog_Interval  elapsed;
     44  struct timespec stop;
     45  int err = 0;
    4646
    4747  executing = _Thread_Get_executing();
    48 
    49   start = _Watchdog_Ticks_since_boot;
    5048
    5149  /*
     
    6260  );
    6361
    64   /*
    65    * Calculate the time that passed while we were sleeping and how
    66    * much remains from what we requested.
    67    */
    68   elapsed = _Watchdog_Ticks_since_boot - start;
    69   if ( elapsed >= relative_interval )
    70     relative_interval = 0;
    71   else
    72     relative_interval -= elapsed;
    73 
     62  clock_gettime( clock_id, &stop );
    7463  /*
    7564   * If the user wants the time remaining, do the conversion.
    7665   */
    77   if ( rmtp ) {
    78     _Timespec_From_ticks( relative_interval, rmtp );
    79   }
    80 
    81   /*
    82    *  Only when POSIX is enabled, can a sleep be interrupted.
    83    */
    84   #if defined(RTEMS_POSIX_API)
     66  if ( _Timespec_Less_than( &stop, timeout ) ) {
    8567    /*
    8668     *  If there is time remaining, then we were interrupted by a signal.
    8769     */
    88     if ( relative_interval )
    89       return EINTR;
    90   #endif
    91 
    92   return 0;
     70    err = EINTR;
     71    if ( rmtp != NULL ) {
     72      _Timespec_Subtract( &stop, timeout, rmtp );
     73    }
     74  } else if ( rmtp != NULL ) {
     75    /* no time remaining */
     76    _Timespec_Set_to_zero( rmtp );
     77  }
     78
     79  return err;
    9380}
    9481
     
    153140  _Timespec_Add_to( &now, rqtp );
    154141  ticks = _Watchdog_Ticks_from_timespec( &now );
    155   err = nanosleep_helper(ticks, relative_interval, rmtp, WATCHDOG_ABSOLUTE );
     142  err = nanosleep_helper( CLOCK_REALTIME,
     143      ticks,
     144      &now,
     145      rmtp,
     146      WATCHDOG_ABSOLUTE
     147  );
    156148  if ( err != 0 ) {
    157149    rtems_set_errno_and_return_minus_one( err );
     
    171163{
    172164  int err = 0;
    173   struct timespec now;
     165  struct timespec timeout;
    174166  uint64_t ticks;
    175167  Watchdog_Interval relative_interval;
    176   struct timespec relative_ts;
    177168  TOD_Absolute_timeout_conversion_results status;
    178169
     
    191182      return nanosleep_yield( NULL );
    192183    rmtp = NULL; /* Do not touch rmtp when using absolute time */
     184    timeout.tv_sec = 0;
     185    timeout.tv_nsec = 0;
    193186  } else {
     187    /* prepare to convert relative ticks to absolute timeout */
     188    err = clock_gettime( clock_id, &timeout );
     189    if ( err != 0 )
     190      return EINVAL;
    194191    relative_interval = _Timespec_To_ticks( rqtp );
    195192  }
     
    197194    return nanosleep_yield( rmtp );
    198195
     196  _Timespec_Add_to( &timeout, rqtp );
    199197  if ( clock_id == CLOCK_REALTIME ) {
    200     /* convert relative ticks to absolute timeout */
    201     err = clock_gettime( CLOCK_REALTIME, &now );
    202     if ( err != 0 )
    203       return EINVAL;
    204     _Timespec_Add_to( &now, rqtp );
    205     ticks = _Watchdog_Ticks_from_timespec( &now );
    206     err = nanosleep_helper( ticks, relative_interval, rmtp, WATCHDOG_ABSOLUTE );
     198    ticks = _Watchdog_Ticks_from_timespec( &timeout );
     199    err = nanosleep_helper(
     200        clock_id,
     201        ticks,
     202        &timeout,
     203        rmtp,
     204        WATCHDOG_ABSOLUTE
     205    );
    207206  } else if ( clock_id == CLOCK_MONOTONIC ) {
    208207    /* use the WATCHDOG_RELATIVE to ignore changes in wall time */
    209208    err = nanosleep_helper(
     209        clock_id,
    210210        relative_interval,
    211         relative_interval,
     211        &timeout,
    212212        rmtp,
    213213        WATCHDOG_RELATIVE
Note: See TracChangeset for help on using the changeset viewer.