[7f72217e] | 1 | /* |
---|
[eb5a7e07] | 2 | * $Id$ |
---|
[5e9b32b] | 3 | */ |
---|
| 4 | |
---|
[5600661] | 5 | #include <assert.h> |
---|
[5e9b32b] | 6 | #include <time.h> |
---|
[5600661] | 7 | #include <errno.h> |
---|
[f4719d5a] | 8 | |
---|
| 9 | #include <rtems/system.h> |
---|
[5600661] | 10 | #include <rtems/score/isr.h> |
---|
| 11 | #include <rtems/score/thread.h> |
---|
[5e9b32b] | 12 | #include <rtems/score/tod.h> |
---|
| 13 | |
---|
[05ce4e09] | 14 | #include <rtems/posix/seterr.h> |
---|
[5ff991e8] | 15 | #include <rtems/posix/time.h> |
---|
[5e9b32b] | 16 | |
---|
[41be2e8] | 17 | /*PAGE |
---|
| 18 | * |
---|
| 19 | * _POSIX_Timespec_subtract |
---|
| 20 | */ |
---|
| 21 | |
---|
| 22 | void _POSIX_Timespec_subtract( |
---|
| 23 | const struct timespec *the_start, |
---|
| 24 | const struct timespec *end, |
---|
| 25 | struct timespec *result |
---|
| 26 | ) |
---|
| 27 | { |
---|
| 28 | struct timespec start_struct = *the_start; |
---|
| 29 | struct timespec *start = &start_struct; |
---|
| 30 | unsigned int nsecs_per_sec = TOD_NANOSECONDS_PER_SECOND; |
---|
| 31 | |
---|
| 32 | if (end->tv_nsec < start->tv_nsec) { |
---|
| 33 | int seconds = (start->tv_nsec - end->tv_nsec) / nsecs_per_sec + 1; |
---|
| 34 | start->tv_nsec -= nsecs_per_sec * seconds; |
---|
| 35 | start->tv_sec += seconds; |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | if (end->tv_nsec - start->tv_nsec > nsecs_per_sec) { |
---|
| 39 | int seconds = (start->tv_nsec - end->tv_nsec) / nsecs_per_sec; |
---|
| 40 | start->tv_nsec += nsecs_per_sec * seconds; |
---|
| 41 | start->tv_sec -= seconds; |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | result->tv_sec = end->tv_sec - start->tv_sec; |
---|
| 45 | result->tv_nsec = end->tv_nsec - start->tv_nsec; |
---|
| 46 | } |
---|
| 47 | |
---|
[5b748a1] | 48 | /*PAGE |
---|
| 49 | * |
---|
| 50 | * _POSIX_Timespec_to_interval |
---|
[974ff40] | 51 | */ |
---|
| 52 | |
---|
[5b748a1] | 53 | Watchdog_Interval _POSIX_Timespec_to_interval( |
---|
[974ff40] | 54 | const struct timespec *time |
---|
| 55 | ) |
---|
| 56 | { |
---|
[5600661] | 57 | Watchdog_Interval ticks; |
---|
| 58 | |
---|
| 59 | ticks = (time->tv_sec * TOD_MICROSECONDS_PER_SECOND) / |
---|
| 60 | _TOD_Microseconds_per_tick; |
---|
| 61 | |
---|
| 62 | ticks += (time->tv_nsec / TOD_NANOSECONDS_PER_MICROSECOND) / |
---|
| 63 | _TOD_Microseconds_per_tick; |
---|
| 64 | |
---|
[e8ee2f32] | 65 | if (ticks) |
---|
| 66 | return ticks; |
---|
| 67 | |
---|
| 68 | return 1; |
---|
[974ff40] | 69 | } |
---|
| 70 | |
---|
[5b748a1] | 71 | /*PAGE |
---|
| 72 | * |
---|
| 73 | * _POSIX_Interval_to_timespec |
---|
| 74 | */ |
---|
| 75 | |
---|
| 76 | void _POSIX_Interval_to_timespec( |
---|
| 77 | Watchdog_Interval ticks, |
---|
| 78 | struct timespec *time |
---|
| 79 | ) |
---|
| 80 | { |
---|
| 81 | unsigned32 usecs; |
---|
| 82 | |
---|
| 83 | usecs = ticks * _TOD_Microseconds_per_tick; |
---|
| 84 | |
---|
| 85 | time->tv_sec = usecs / TOD_MICROSECONDS_PER_SECOND; |
---|
| 86 | time->tv_nsec = (usecs % TOD_MICROSECONDS_PER_SECOND) * |
---|
| 87 | TOD_NANOSECONDS_PER_MICROSECOND; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | /*PAGE |
---|
| 91 | * |
---|
[5e9b32b] | 92 | * 4.5.1 Get System Time, P1003.1b-1993, p. 91 |
---|
| 93 | */ |
---|
| 94 | |
---|
[52d99d4a] | 95 | /* Using the implementation in newlib */ |
---|
| 96 | #if 0 |
---|
[5e9b32b] | 97 | time_t time( |
---|
| 98 | time_t *tloc |
---|
| 99 | ) |
---|
| 100 | { |
---|
| 101 | time_t seconds_since_epoch; |
---|
| 102 | |
---|
[40cfab47] | 103 | /* |
---|
| 104 | * No error is the time of day is not set. For RTEMS the system time |
---|
| 105 | * starts out at the rtems epoch. |
---|
| 106 | */ |
---|
[5e9b32b] | 107 | |
---|
| 108 | /* |
---|
| 109 | * Internally the RTEMS epoch is 1988. This must be taken into account. |
---|
| 110 | */ |
---|
| 111 | |
---|
| 112 | seconds_since_epoch = _TOD_Seconds_since_epoch; |
---|
| 113 | |
---|
| 114 | seconds_since_epoch += POSIX_TIME_SECONDS_1970_THROUGH_1988; |
---|
| 115 | |
---|
| 116 | if ( tloc ) |
---|
| 117 | *tloc = seconds_since_epoch; |
---|
| 118 | |
---|
| 119 | return seconds_since_epoch; |
---|
| 120 | } |
---|
[52d99d4a] | 121 | #endif |
---|
[5e9b32b] | 122 | |
---|
[22b4b2e] | 123 | /*PAGE |
---|
| 124 | * |
---|
[5e9b32b] | 125 | * 14.2.1 Clocks, P1003.1b-1993, p. 263 |
---|
| 126 | */ |
---|
| 127 | |
---|
| 128 | int clock_settime( |
---|
| 129 | clockid_t clock_id, |
---|
| 130 | const struct timespec *tp |
---|
| 131 | ) |
---|
| 132 | { |
---|
[5600661] | 133 | struct tm split_time; |
---|
| 134 | TOD_Control tod; |
---|
| 135 | Watchdog_Interval seconds; |
---|
| 136 | |
---|
| 137 | assert( tp ); |
---|
| 138 | |
---|
| 139 | switch ( clock_id ) { |
---|
| 140 | |
---|
| 141 | case CLOCK_REALTIME: |
---|
| 142 | (void) gmtime_r( &tp->tv_sec, &split_time ); |
---|
| 143 | |
---|
| 144 | /* |
---|
| 145 | * Convert the tm structure format to that used by the TOD Handler |
---|
| 146 | * |
---|
| 147 | * NOTE: TOD Handler does not honor leap seconds. |
---|
| 148 | */ |
---|
| 149 | |
---|
| 150 | tod.year = split_time.tm_year + 1900; /* RHS is years since 1900 */ |
---|
| 151 | tod.month = split_time.tm_mon + 1; /* RHS uses 0-11 */ |
---|
| 152 | tod.day = split_time.tm_mday; |
---|
| 153 | tod.hour = split_time.tm_hour; |
---|
| 154 | tod.minute = split_time.tm_min; |
---|
| 155 | tod.second = split_time.tm_sec; /* RHS allows 0-61 for leap seconds */ |
---|
| 156 | |
---|
| 157 | tod.ticks = (tp->tv_nsec / TOD_NANOSECONDS_PER_MICROSECOND) / |
---|
| 158 | _TOD_Microseconds_per_tick; |
---|
| 159 | |
---|
[05ce4e09] | 160 | if ( !_TOD_Validate( &tod ) ) |
---|
| 161 | set_errno_and_return_minus_one( EINVAL ); |
---|
[5600661] | 162 | |
---|
| 163 | /* |
---|
| 164 | * We can't use the tp->tv_sec field because it is based on |
---|
| 165 | * a different EPOCH. |
---|
| 166 | */ |
---|
| 167 | |
---|
| 168 | seconds = _TOD_To_seconds( &tod ); |
---|
| 169 | _Thread_Disable_dispatch(); |
---|
| 170 | _TOD_Set( &tod, seconds ); |
---|
| 171 | _Thread_Enable_dispatch(); |
---|
| 172 | break; |
---|
| 173 | |
---|
| 174 | #ifdef _POSIX_CPUTIME |
---|
| 175 | case CLOCK_PROCESS_CPUTIME: |
---|
| 176 | return POSIX_NOT_IMPLEMENTED(); |
---|
| 177 | break; |
---|
| 178 | #endif |
---|
| 179 | |
---|
| 180 | #ifdef _POSIX_THREAD_CPUTIME |
---|
| 181 | case CLOCK_THREAD_CPUTIME: |
---|
| 182 | return POSIX_NOT_IMPLEMENTED(); |
---|
| 183 | break; |
---|
| 184 | #endif |
---|
| 185 | default: |
---|
[05ce4e09] | 186 | set_errno_and_return_minus_one( EINVAL ); |
---|
[5600661] | 187 | |
---|
| 188 | } |
---|
| 189 | return 0; |
---|
[5e9b32b] | 190 | } |
---|
| 191 | |
---|
[22b4b2e] | 192 | /*PAGE |
---|
| 193 | * |
---|
[5e9b32b] | 194 | * 14.2.1 Clocks, P1003.1b-1993, p. 263 |
---|
| 195 | */ |
---|
| 196 | |
---|
| 197 | int clock_gettime( |
---|
| 198 | clockid_t clock_id, |
---|
| 199 | struct timespec *tp |
---|
| 200 | ) |
---|
| 201 | { |
---|
[5600661] | 202 | ISR_Level level; |
---|
| 203 | time_t seconds; |
---|
| 204 | long ticks; |
---|
| 205 | |
---|
[f3e19146] | 206 | if ( !tp ) |
---|
| 207 | set_errno_and_return_minus_one( EINVAL ); |
---|
[5600661] | 208 | |
---|
| 209 | switch ( clock_id ) { |
---|
| 210 | |
---|
| 211 | case CLOCK_REALTIME: |
---|
| 212 | |
---|
| 213 | _ISR_Disable( level ); |
---|
| 214 | seconds = _TOD_Seconds_since_epoch; |
---|
| 215 | ticks = _TOD_Current.ticks; |
---|
| 216 | _ISR_Enable( level ); |
---|
| 217 | |
---|
| 218 | tp->tv_sec = seconds + POSIX_TIME_SECONDS_1970_THROUGH_1988; |
---|
| 219 | tp->tv_nsec = ticks * _TOD_Microseconds_per_tick * |
---|
| 220 | TOD_NANOSECONDS_PER_MICROSECOND; |
---|
| 221 | break; |
---|
| 222 | |
---|
| 223 | #ifdef _POSIX_CPUTIME |
---|
| 224 | case CLOCK_PROCESS_CPUTIME: |
---|
[e811d68] | 225 | /* don't base this on _Watchdog_Ticks_since_boot--duration is too short*/ |
---|
[5600661] | 226 | return POSIX_NOT_IMPLEMENTED(); |
---|
| 227 | break; |
---|
| 228 | #endif |
---|
| 229 | |
---|
| 230 | #ifdef _POSIX_THREAD_CPUTIME |
---|
| 231 | case CLOCK_THREAD_CPUTIME: |
---|
| 232 | return POSIX_NOT_IMPLEMENTED(); |
---|
| 233 | break; |
---|
| 234 | #endif |
---|
| 235 | default: |
---|
[05ce4e09] | 236 | set_errno_and_return_minus_one( EINVAL ); |
---|
[5600661] | 237 | |
---|
| 238 | } |
---|
| 239 | return 0; |
---|
[5e9b32b] | 240 | } |
---|
| 241 | |
---|
[22b4b2e] | 242 | /*PAGE |
---|
| 243 | * |
---|
[5e9b32b] | 244 | * 14.2.1 Clocks, P1003.1b-1993, p. 263 |
---|
| 245 | */ |
---|
| 246 | |
---|
| 247 | int clock_getres( |
---|
| 248 | clockid_t clock_id, |
---|
| 249 | struct timespec *res |
---|
| 250 | ) |
---|
| 251 | { |
---|
[1087564] | 252 | if ( !res ) |
---|
[f3e19146] | 253 | set_errno_and_return_minus_one( EINVAL ); |
---|
[1087564] | 254 | |
---|
[5600661] | 255 | switch ( clock_id ) { |
---|
| 256 | |
---|
| 257 | /* |
---|
| 258 | * All time in rtems is based on the same clock tick. |
---|
| 259 | */ |
---|
| 260 | |
---|
| 261 | case CLOCK_REALTIME: |
---|
| 262 | case CLOCK_PROCESS_CPUTIME: |
---|
| 263 | case CLOCK_THREAD_CPUTIME: |
---|
[5b748a1] | 264 | if ( res ) |
---|
| 265 | _POSIX_Interval_to_timespec( _TOD_Microseconds_per_tick, res ); |
---|
[5600661] | 266 | break; |
---|
| 267 | |
---|
| 268 | default: |
---|
[05ce4e09] | 269 | set_errno_and_return_minus_one( EINVAL ); |
---|
[5600661] | 270 | |
---|
| 271 | } |
---|
| 272 | return 0; |
---|
[5e9b32b] | 273 | } |
---|
| 274 | |
---|
[22b4b2e] | 275 | /*PAGE |
---|
| 276 | * |
---|
[5e9b32b] | 277 | * 14.2.5 High Resolution Sleep, P1003.1b-1993, p. 269 |
---|
| 278 | */ |
---|
| 279 | |
---|
| 280 | int nanosleep( |
---|
| 281 | const struct timespec *rqtp, |
---|
| 282 | struct timespec *rmtp |
---|
| 283 | ) |
---|
| 284 | { |
---|
[9a5cbef0] | 285 | Watchdog_Interval ticks; |
---|
| 286 | struct timespec *the_rqtp; |
---|
[5600661] | 287 | |
---|
[5f08fd6] | 288 | if ( !rqtp ) |
---|
| 289 | set_errno_and_return_minus_one( EINVAL ); |
---|
| 290 | |
---|
[9a5cbef0] | 291 | the_rqtp = (struct timespec *)rqtp; |
---|
| 292 | |
---|
[df8d1440] | 293 | /* |
---|
| 294 | * Return EAGAIN if the delay interval is negative. |
---|
| 295 | * |
---|
| 296 | * NOTE: This behavior is beyond the POSIX specification. |
---|
| 297 | * FSU pthreads shares this behavior. |
---|
| 298 | */ |
---|
| 299 | |
---|
[9a5cbef0] | 300 | if ( the_rqtp->tv_sec < 0 ) |
---|
| 301 | the_rqtp->tv_sec = 0; |
---|
| 302 | |
---|
| 303 | if ( /* the_rqtp->tv_sec < 0 || */ the_rqtp->tv_nsec < 0 ) |
---|
[df8d1440] | 304 | set_errno_and_return_minus_one( EAGAIN ); |
---|
| 305 | |
---|
[9a5cbef0] | 306 | if ( the_rqtp->tv_nsec >= TOD_NANOSECONDS_PER_SECOND ) |
---|
[05ce4e09] | 307 | set_errno_and_return_minus_one( EINVAL ); |
---|
[e72d995a] | 308 | |
---|
[9a5cbef0] | 309 | ticks = _POSIX_Timespec_to_interval( the_rqtp ); |
---|
[df8d1440] | 310 | |
---|
| 311 | /* |
---|
| 312 | * This behavior is also beyond the POSIX specification but is |
---|
| 313 | * consistent with the RTEMS api and yields desirable behavior. |
---|
| 314 | */ |
---|
| 315 | |
---|
| 316 | if ( !ticks ) { |
---|
| 317 | _Thread_Yield_processor(); |
---|
| 318 | _Thread_Dispatch(); |
---|
[5f08fd6] | 319 | if ( rmtp ) { |
---|
| 320 | rmtp->tv_sec = 0; |
---|
| 321 | rmtp->tv_nsec = 0; |
---|
| 322 | } |
---|
[df8d1440] | 323 | return 0; |
---|
| 324 | } |
---|
[5600661] | 325 | |
---|
| 326 | _Thread_Disable_dispatch(); |
---|
[98ed15e] | 327 | _Thread_Set_state( |
---|
| 328 | _Thread_Executing, |
---|
| 329 | STATES_DELAYING | STATES_INTERRUPTIBLE_BY_SIGNAL |
---|
| 330 | ); |
---|
[5600661] | 331 | _Watchdog_Initialize( |
---|
| 332 | &_Thread_Executing->Timer, |
---|
[f48c9068] | 333 | _Thread_Delay_ended, |
---|
[5600661] | 334 | _Thread_Executing->Object.id, |
---|
| 335 | NULL |
---|
| 336 | ); |
---|
| 337 | _Watchdog_Insert_ticks( &_Thread_Executing->Timer, ticks ); |
---|
| 338 | _Thread_Enable_dispatch(); |
---|
[e72d995a] | 339 | |
---|
[5b748a1] | 340 | /* calculate time remaining */ |
---|
| 341 | |
---|
[e72d995a] | 342 | if ( rmtp ) { |
---|
[a26ee01c] | 343 | ticks -= |
---|
| 344 | _Thread_Executing->Timer.stop_time - _Thread_Executing->Timer.start_time; |
---|
[5b748a1] | 345 | |
---|
[a26ee01c] | 346 | _POSIX_Interval_to_timespec( ticks, rmtp ); |
---|
[e72d995a] | 347 | |
---|
[f48c9068] | 348 | /* |
---|
| 349 | * If there is time remaining, then we were interrupted by a signal. |
---|
| 350 | */ |
---|
| 351 | |
---|
| 352 | if ( ticks ) |
---|
| 353 | set_errno_and_return_minus_one( EINTR ); |
---|
| 354 | } |
---|
[5600661] | 355 | |
---|
[f48c9068] | 356 | return 0; |
---|
[5e9b32b] | 357 | } |
---|
| 358 | |
---|
[22b4b2e] | 359 | /*PAGE |
---|
| 360 | * |
---|
[5e9b32b] | 361 | * 20.1.3 Accessing a Process CPU-time CLock, P1003.4b/D8, p. 55 |
---|
| 362 | */ |
---|
| 363 | |
---|
| 364 | int clock_getcpuclockid( |
---|
| 365 | pid_t pid, |
---|
| 366 | clockid_t *clock_id |
---|
| 367 | ) |
---|
| 368 | { |
---|
| 369 | return POSIX_NOT_IMPLEMENTED(); |
---|
| 370 | } |
---|
| 371 | |
---|
[22b4b2e] | 372 | /*PAGE |
---|
| 373 | * |
---|
[5e9b32b] | 374 | * 20.1.5 CPU-time Clock Attribute Access, P1003.4b/D8, p. 58 |
---|
| 375 | */ |
---|
| 376 | |
---|
| 377 | int clock_setenable_attr( |
---|
| 378 | clockid_t clock_id, |
---|
| 379 | int attr |
---|
| 380 | ) |
---|
| 381 | { |
---|
| 382 | return POSIX_NOT_IMPLEMENTED(); |
---|
| 383 | } |
---|
| 384 | |
---|
[22b4b2e] | 385 | /*PAGE |
---|
| 386 | * |
---|
[5e9b32b] | 387 | * 20.1.5 CPU-time Clock Attribute Access, P1003.4b/D8, p. 58 |
---|
| 388 | */ |
---|
| 389 | |
---|
| 390 | int clock_getenable_attr( |
---|
| 391 | clockid_t clock_id, |
---|
| 392 | int *attr |
---|
| 393 | ) |
---|
| 394 | { |
---|
| 395 | return POSIX_NOT_IMPLEMENTED(); |
---|
| 396 | } |
---|