Changeset 7f9d97f3 in mainline
- Timestamp:
- 2015-03-14T21:48:01Z (10 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 270bf4f
- Parents:
- c0c38c7c
- Location:
- uspace
- Files:
-
- 14 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/barber/barber.c
rc0c38c7c r7f9d97f3 233 233 getuptime(&cur); 234 234 235 plan_frame_timer(tv_sub (&cur, &prev));235 plan_frame_timer(tv_sub_diff(&cur, &prev)); 236 236 } 237 237 -
uspace/app/nettest1/nettest1.c
rc0c38c7c r7f9d97f3 412 412 gettimeofday(&time_after, NULL); 413 413 414 printf("Tested in %ld microseconds\n", tv_sub (&time_after,414 printf("Tested in %ld microseconds\n", tv_sub_diff(&time_after, 415 415 &time_before)); 416 416 -
uspace/app/nettest2/nettest2.c
rc0c38c7c r7f9d97f3 370 370 371 371 printf("sendto + recvfrom tested in %ld microseconds\n", 372 tv_sub (&time_after, &time_before));372 tv_sub_diff(&time_after, &time_before)); 373 373 374 374 gettimeofday(&time_before, NULL); … … 390 390 391 391 printf("sendto, recvfrom tested in %ld microseconds\n", 392 tv_sub (&time_after, &time_before));392 tv_sub_diff(&time_after, &time_before)); 393 393 394 394 rc = sockets_close(verbose, socket_ids, sockets); -
uspace/app/rcubench/rcubench.c
rc0c38c7c r7f9d97f3 264 264 265 265 getuptime(&end); 266 int64_t duration = tv_sub (&end, &start);266 int64_t duration = tv_sub_diff(&end, &start); 267 267 268 268 uint64_t secs = (uint64_t)duration / 1000 / 1000; -
uspace/app/tester/ipc/ping_pong.c
rc0c38c7c r7f9d97f3 50 50 gettimeofday(&now, NULL); 51 51 52 if (tv_sub (&now, &start) >= DURATION_SECS * 1000000L)52 if (tv_sub_diff(&now, &start) >= DURATION_SECS * 1000000L) 53 53 break; 54 54 -
uspace/app/tester/ipc/starve.c
rc0c38c7c r7f9d97f3 52 52 gettimeofday(&now, NULL); 53 53 54 if (tv_sub (&now, &start) >= DURATION_SECS * 1000000L)54 if (tv_sub_diff(&now, &start) >= DURATION_SECS * 1000000L) 55 55 break; 56 56 -
uspace/drv/time/cmos-rtc/cmos-rtc.c
rc0c38c7c r7f9d97f3 76 76 int clients_connected; 77 77 /** time at which the system booted */ 78 time_t boottime;78 struct timeval boot_time; 79 79 } rtc_t; 80 80 … … 97 97 static int rtc_dev_remove(ddf_dev_t *dev); 98 98 static void rtc_register_write(rtc_t *rtc, int reg, int data); 99 static time_t uptime_get(void);100 99 static bool is_battery_ok(rtc_t *rtc); 101 100 static int rtc_fun_online(ddf_fun_t *fun); … … 205 204 ddf_msg(LVL_DEBUG, "rtc_dev_initialize %s", ddf_dev_get_name(rtc->dev)); 206 205 207 rtc->boottime = 0; 206 rtc->boot_time.tv_sec = 0; 207 rtc->boot_time.tv_usec = 0; 208 208 rtc->clients_connected = 0; 209 209 … … 326 326 fibril_mutex_lock(&rtc->mutex); 327 327 328 if (rtc->boot time != 0) {328 if (rtc->boot_time.tv_sec) { 329 329 /* There is no need to read the current time from the 330 330 * device because it has already been cached. 331 331 */ 332 332 333 time_t cur_time = rtc->boottime + uptime_get(); 334 333 struct timeval curtime; 334 335 getuptime(&curtime); 336 tv_add(&curtime, &rtc->boot_time); 335 337 fibril_mutex_unlock(&rtc->mutex); 336 338 337 return time_ local2tm(cur_time, t);339 return time_tv2tm(&curtime, t); 338 340 } 339 341 … … 344 346 } 345 347 348 /* Microseconds are below RTC's resolution, assume 0. */ 349 t->tm_usec = 0; 350 346 351 /* now read the registers */ 347 352 do { 348 353 /* Suspend until the update process has finished */ 349 while (rtc_update_in_progress(rtc)); 350 351 t->tm_sec = rtc_register_read(rtc, RTC_SEC); 352 t->tm_min = rtc_register_read(rtc, RTC_MIN); 354 while (rtc_update_in_progress(rtc)) 355 ; 356 357 t->tm_sec = rtc_register_read(rtc, RTC_SEC); 358 t->tm_min = rtc_register_read(rtc, RTC_MIN); 353 359 t->tm_hour = rtc_register_read(rtc, RTC_HOUR); 354 360 t->tm_mday = rtc_register_read(rtc, RTC_DAY); 355 t->tm_mon 361 t->tm_mon = rtc_register_read(rtc, RTC_MON); 356 362 t->tm_year = rtc_register_read(rtc, RTC_YEAR); 357 363 358 364 /* Now check if it is stable */ 359 } while(t->tm_sec 360 t->tm_min 365 } while(t->tm_sec != rtc_register_read(rtc, RTC_SEC) || 366 t->tm_min != rtc_register_read(rtc, RTC_MIN) || 361 367 t->tm_mday != rtc_register_read(rtc, RTC_DAY) || 362 t->tm_mon 368 t->tm_mon != rtc_register_read(rtc, RTC_MON) || 363 369 t->tm_year != rtc_register_read(rtc, RTC_YEAR)); 364 370 … … 366 372 bool _12h_mode = !(rtc_register_read(rtc, RTC_STATUS_B) & 367 373 RTC_B_24H); 368 369 374 if (_12h_mode) { 370 375 /* The RTC is working in 12h mode, check if it is AM or PM */ … … 378 383 /* Check if the RTC is working in BCD mode */ 379 384 bcd_mode = !(rtc_register_read(rtc, RTC_STATUS_B) & RTC_B_BCD); 380 381 385 if (bcd_mode) { 382 t->tm_sec 383 t->tm_min 386 t->tm_sec = bcd2bin(t->tm_sec); 387 t->tm_min = bcd2bin(t->tm_min); 384 388 t->tm_hour = bcd2bin(t->tm_hour); 385 389 t->tm_mday = bcd2bin(t->tm_mday); 386 t->tm_mon 390 t->tm_mon = bcd2bin(t->tm_mon); 387 391 t->tm_year = bcd2bin(t->tm_year); 388 392 } … … 414 418 result = EINVAL; 415 419 else { 416 rtc->boottime = r - uptime_get(); 420 struct timeval uptime; 421 422 getuptime(&uptime); 423 rtc->boot_time.tv_sec = r; 424 rtc->boot_time.tv_usec = t->tm_usec; /* normalized */ 425 tv_sub(&rtc->boot_time, &uptime); 417 426 result = EOK; 418 427 } … … 435 444 bool bcd_mode; 436 445 time_t norm_time; 437 time_t uptime; 446 struct timeval uptime; 447 struct timeval ntv; 438 448 int reg_b; 439 449 int reg_a; … … 445 455 return EINVAL; 446 456 447 uptime = uptime_get(); 448 if (norm_time <= uptime) { 457 ntv.tv_sec = norm_time; 458 ntv.tv_usec = t->tm_usec; 459 getuptime(&uptime); 460 461 if (tv_gteq(&uptime, &ntv)) { 449 462 /* This is not acceptable */ 450 463 return EINVAL; … … 458 471 } 459 472 460 /* boottime must be recomputed */ 461 rtc->boottime = 0; 473 /* boot_time must be recomputed */ 474 rtc->boot_time.tv_sec = 0; 475 rtc->boot_time.tv_usec = 0; 462 476 463 477 /* Detect the RTC epoch */ … … 731 745 } 732 746 733 static time_t734 uptime_get(void)735 {736 struct timeval tv;737 738 getuptime(&tv);739 740 return tv.tv_sec;741 }742 743 747 static int 744 748 rtc_fun_online(ddf_fun_t *fun) -
uspace/lib/c/generic/async.c
rc0c38c7c r7f9d97f3 820 820 if (usecs) { 821 821 getuptime(&conn->wdata.to_event.expires); 822 tv_add (&conn->wdata.to_event.expires, usecs);822 tv_add_diff(&conn->wdata.to_event.expires, usecs); 823 823 } else 824 824 conn->wdata.to_event.inlist = false; … … 1214 1214 1215 1215 } else { 1216 timeout = tv_sub(&waiter->to_event.expires, &tv); 1216 timeout = tv_sub_diff(&waiter->to_event.expires, 1217 &tv); 1217 1218 futex_up(&async_futex); 1218 1219 } … … 1505 1506 1506 1507 getuptime(&msg->wdata.to_event.expires); 1507 tv_add (&msg->wdata.to_event.expires, timeout);1508 tv_add_diff(&msg->wdata.to_event.expires, timeout); 1508 1509 1509 1510 /* … … 1587 1588 1588 1589 getuptime(&msg->wdata.to_event.expires); 1589 tv_add (&msg->wdata.to_event.expires, timeout);1590 tv_add_diff(&msg->wdata.to_event.expires, timeout); 1590 1591 1591 1592 futex_down(&async_futex); -
uspace/lib/c/generic/fibril_synch.c
rc0c38c7c r7f9d97f3 380 380 if (timeout) { 381 381 getuptime(&wdata.to_event.expires); 382 tv_add (&wdata.to_event.expires, timeout);382 tv_add_diff(&wdata.to_event.expires, timeout); 383 383 async_insert_timeout(&wdata); 384 384 } -
uspace/lib/c/generic/io/console.c
rc0c38c7c r7f9d97f3 259 259 struct timeval t1; 260 260 gettimeofday(&t1, NULL); 261 *timeout -= tv_sub (&t1, &t0);261 *timeout -= tv_sub_diff(&t1, &t0); 262 262 263 263 return true; -
uspace/lib/c/generic/time.c
rc0c38c7c r7f9d97f3 59 59 #define MINS_PER_HOUR 60 60 60 #define SECS_PER_MIN 60 61 #define USECS_PER_SEC 1000000 61 62 #define MINS_PER_DAY (MINS_PER_HOUR * HOURS_PER_DAY) 62 63 #define SECS_PER_HOUR (SECS_PER_MIN * MINS_PER_HOUR) … … 252 253 * Optionally add specified amount of seconds. 253 254 * 254 * @param tm 255 * @param sec_add Secondsto add.255 * @param tm Broken-down time to normalize. 256 * @param tv Timeval to add. 256 257 * 257 258 * @return 0 on success, -1 on overflow 258 259 * 259 260 */ 260 static int normalize_t ime(struct tm *tm, time_t sec_add)261 static int normalize_tm_tv(struct tm *tm, const struct timeval *tv) 261 262 { 262 263 // TODO: DST correction 263 264 264 265 /* Set initial values. */ 265 time_t sec = tm->tm_sec + sec_add; 266 time_t usec = tm->tm_usec + tv->tv_usec; 267 time_t sec = tm->tm_sec + tv->tv_sec; 266 268 time_t min = tm->tm_min; 267 269 time_t hour = tm->tm_hour; … … 271 273 272 274 /* Adjust time. */ 275 sec += floor_div(usec, USECS_PER_SEC); 276 usec = floor_mod(usec, USECS_PER_SEC); 273 277 min += floor_div(sec, SECS_PER_MIN); 274 278 sec = floor_mod(sec, SECS_PER_MIN); … … 319 323 320 324 /* And put the values back to the struct. */ 325 tm->tm_usec = (int) usec; 321 326 tm->tm_sec = (int) sec; 322 327 tm->tm_min = (int) min; … … 335 340 } 336 341 342 static int normalize_tm_time(struct tm *tm, time_t time) 343 { 344 struct timeval tv = { 345 .tv_sec = time, 346 .tv_usec = 0 347 }; 348 349 return normalize_tm_tv(tm, &tv); 350 } 351 352 337 353 /** Which day the week-based year starts on. 338 354 * … … 442 458 } 443 459 460 static void tv_normalize(struct timeval *tv) 461 { 462 while (tv->tv_usec > USECS_PER_SEC) { 463 tv->tv_sec++; 464 tv->tv_usec -= USECS_PER_SEC; 465 } 466 while (tv->tv_usec < 0) { 467 tv->tv_sec--; 468 tv->tv_usec += USECS_PER_SEC; 469 } 470 } 471 444 472 /** Add microseconds to given timeval. 445 473 * … … 448 476 * 449 477 */ 450 void tv_add(struct timeval *tv, suseconds_t usecs) 451 { 452 tv->tv_sec += usecs / 1000000; 453 tv->tv_usec += usecs % 1000000; 454 455 if (tv->tv_usec > 1000000) { 456 tv->tv_sec++; 457 tv->tv_usec -= 1000000; 458 } 459 } 460 461 /** Subtract two timevals. 478 void tv_add_diff(struct timeval *tv, suseconds_t usecs) 479 { 480 tv->tv_sec += usecs / USECS_PER_SEC; 481 tv->tv_usec += usecs % USECS_PER_SEC; 482 tv_normalize(tv); 483 } 484 485 /** Add two timevals. 462 486 * 463 487 * @param tv1 First timeval. 464 488 * @param tv2 Second timeval. 489 */ 490 void tv_add(struct timeval *tv1, struct timeval *tv2) 491 { 492 tv1->tv_sec += tv2->tv_sec; 493 tv1->tv_usec += tv2->tv_usec; 494 tv_normalize(tv1); 495 } 496 497 /** Subtract two timevals. 498 * 499 * @param tv1 First timeval. 500 * @param tv2 Second timeval. 465 501 * 466 502 * @return Difference between tv1 and tv2 (tv1 - tv2) in … … 468 504 * 469 505 */ 470 suseconds_t tv_sub (struct timeval *tv1, struct timeval *tv2)506 suseconds_t tv_sub_diff(struct timeval *tv1, struct timeval *tv2) 471 507 { 472 508 return (tv1->tv_usec - tv2->tv_usec) + 473 ((tv1->tv_sec - tv2->tv_sec) * 1000000); 509 ((tv1->tv_sec - tv2->tv_sec) * USECS_PER_SEC); 510 } 511 512 /** Subtract two timevals. 513 * 514 * @param tv1 First timeval. 515 * @param tv2 Second timeval. 516 * 517 */ 518 void tv_sub(struct timeval *tv1, struct timeval *tv2) 519 { 520 tv1->tv_sec -= tv2->tv_sec; 521 tv1->tv_usec -= tv2->tv_usec; 522 tv_normalize(tv1); 474 523 } 475 524 … … 573 622 goto fallback; 574 623 575 tv->tv_usec = 0;624 tv->tv_usec = time.tm_usec; 576 625 tv->tv_sec = mktime(&time); 577 626 … … 689 738 // TODO: detect overflow 690 739 691 normalize_t ime(tm, 0);740 normalize_tm_time(tm, 0); 692 741 return secs_since_epoch(tm); 693 742 } … … 944 993 945 994 /* Set result to epoch. */ 995 result->tm_usec = 0; 946 996 result->tm_sec = 0; 947 997 result->tm_min = 0; … … 951 1001 result->tm_year = 70; /* 1970 */ 952 1002 953 if (normalize_t ime(result, time) == -1)1003 if (normalize_tm_time(result, time) == -1) 954 1004 return EOVERFLOW; 955 1005 … … 1014 1064 * Time is expressed relative to the user's specified timezone. 1015 1065 * 1016 * @param t imer Timeto convert.1066 * @param tv Timeval to convert. 1017 1067 * @param result Structure to store the result to. 1018 1068 * … … 1020 1070 * 1021 1071 */ 1022 int time_ local2tm(const time_t time, struct tm *restrict result)1072 int time_tv2tm(const struct timeval *tv, struct tm *restrict result) 1023 1073 { 1024 1074 // TODO: Deal with timezones. … … 1026 1076 1027 1077 /* Set result to epoch. */ 1078 result->tm_usec = 0; 1028 1079 result->tm_sec = 0; 1029 1080 result->tm_min = 0; … … 1033 1084 result->tm_year = 70; /* 1970 */ 1034 1085 1035 if (normalize_t ime(result, time) == -1)1086 if (normalize_tm_tv(result, tv) == -1) 1036 1087 return EOVERFLOW; 1037 1088 1038 1089 return EOK; 1090 } 1091 1092 /** Converts a time value to a broken-down local time. 1093 * 1094 * Time is expressed relative to the user's specified timezone. 1095 * 1096 * @param timer Time to convert. 1097 * @param result Structure to store the result to. 1098 * 1099 * @return EOK on success or a negative error code. 1100 * 1101 */ 1102 int time_local2tm(const time_t time, struct tm *restrict result) 1103 { 1104 struct timeval tv = { 1105 .tv_sec = time, 1106 .tv_usec = 0 1107 }; 1108 1109 return time_tv2tm(&tv, result); 1039 1110 } 1040 1111 -
uspace/lib/c/include/sys/time.h
rc0c38c7c r7f9d97f3 50 50 51 51 struct tm { 52 int tm_usec; /* Microseconds [0,999999]. */ 52 53 int tm_sec; /* Seconds [0,60]. */ 53 54 int tm_min; /* Minutes [0,59]. */ … … 71 72 }; 72 73 73 extern void tv_add(struct timeval *, suseconds_t); 74 extern suseconds_t tv_sub(struct timeval *, struct timeval *); 74 extern void tv_add_diff(struct timeval *, suseconds_t); 75 extern void tv_add(struct timeval *, struct timeval *); 76 extern suseconds_t tv_sub_diff(struct timeval *, struct timeval *); 77 extern void tv_sub(struct timeval *, struct timeval *); 75 78 extern int tv_gt(struct timeval *, struct timeval *); 76 79 extern int tv_gteq(struct timeval *, struct timeval *); … … 85 88 extern int time_utc2str(const time_t, char *); 86 89 extern void time_tm2str(const struct tm *, char *); 90 extern int time_tv2tm(const struct timeval *, struct tm *); 87 91 extern int time_local2tm(const time_t, struct tm *); 88 92 extern int time_local2str(const time_t, char *); -
uspace/lib/usbdev/src/hub.c
rc0c38c7c r7f9d97f3 229 229 * above might use much of this time so we should only wait to fill 230 230 * up the 100ms quota*/ 231 const suseconds_t elapsed = tv_sub (&end_time, &start_time);231 const suseconds_t elapsed = tv_sub_diff(&end_time, &start_time); 232 232 if (elapsed < 100000) { 233 233 async_usleep(100000 - elapsed); -
uspace/srv/audio/hound/audio_device.c
rc0c38c7c r7f9d97f3 279 279 getuptime(&time2); 280 280 log_verbose("Time to mix sources: %li\n", 281 tv_sub (&time2, &time1));281 tv_sub_diff(&time2, &time1)); 282 282 break; 283 283 }
Note:
See TracChangeset
for help on using the changeset viewer.