Changeset 3f466c33 in mainline
- Timestamp:
- 2011-07-07T22:59:42Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- ec18957a
- Parents:
- 324d46b
- Location:
- uspace/lib/posix
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/posix/signal.h
r324d46b r3f466c33 284 284 #define sigset_t posix_sigset_t 285 285 #define sigval posix_sigval 286 #define sigevent posix_sigevent 286 #ifndef sigevent 287 #define sigevent posix_sigevent 288 #endif 287 289 #define sigaction posix_sigaction 288 290 #define mcontext_t posix_mcontext_t -
uspace/lib/posix/sys/types.h
r324d46b r3f466c33 52 52 typedef struct posix_thread_attr posix_thread_attr_t; 53 53 54 /* Clock types */ 55 typedef long posix_clock_t; 56 typedef int posix_clockid_t; 57 54 58 #ifndef LIBPOSIX_INTERNAL 55 59 #define ino_t posix_ino_t … … 64 68 65 69 #define pthread_attr_t posix_thread_attr_t 70 71 #define clock_t posix_clock_t 72 #define clockid_t posix_clockid_t 66 73 #endif 67 74 -
uspace/lib/posix/time.c
r324d46b r3f466c33 44 44 #include "ctype.h" 45 45 #include "errno.h" 46 #include "signal.h" 46 47 47 48 #include "libc/malloc.h" 48 49 #include "libc/task.h" 49 50 #include "libc/stats.h" 51 #include "libc/sys/time.h" 50 52 51 53 // TODO: documentation … … 212 214 /* Now the difficult part - days of month. */ 213 215 /* Slow, but simple. */ 214 // TODO: do this faster216 // FIXME: do this faster 215 217 216 218 while (tm->tm_mday < 1) { … … 253 255 static int _wbyear(const struct posix_tm *tm) 254 256 { 255 if (tm->tm_yday < _wbyear_offset(tm->tm_year)) { 257 int day = tm->tm_yday - _wbyear_offset(tm->tm_year); 258 if (day < 0) { 259 /* Last week of previous year. */ 256 260 return tm->tm_year - 1; 257 261 } 258 if ( tm->tm_yday > (364 + _is_leap_year(tm->tm_year) +259 _wbyear_offset(tm->tm_year + 1))) {262 if (day > 364 + _is_leap_year(tm->tm_year)){ 263 /* First week of next year. */ 260 264 return tm->tm_year + 1; 261 265 } 266 /* All the other days are in the calendar year. */ 262 267 return tm->tm_year; 263 268 } 264 269 265 /* Number of week in year, when week starts on sunday. 270 /** Week number of the year, assuming weeks start on sunday. 271 * The first Sunday of January is the first day of week 1; 272 * days in the new year before this are in week 0. 273 * 274 * @param tm Normalized broken-down time. 275 * @return The week number (0 - 53). 266 276 */ 267 277 static int _sun_week_number(const struct posix_tm *tm) 268 278 { 269 // TODO 270 not_implemented(); 271 } 272 273 /* Number of week in week-based year. 279 int first_day = (7 - _day_of_week(tm->tm_year, 0, 1)) % 7; 280 return (tm->tm_yday - first_day + 7) / 7; 281 } 282 283 /** Week number of the year, assuming weeks start on monday. 284 * If the week containing January 1st has four or more days in the new year, 285 * then it is considered week 1. Otherwise, it is the last week of the previous 286 * year, and the next week is week 1. Both January 4th and the first Thursday 287 * of January are always in week 1. 288 * 289 * @param tm Normalized broken-down time. 290 * @return The week number (1 - 53). 274 291 */ 275 292 static int _iso_week_number(const struct posix_tm *tm) 276 293 { 277 // TODO 278 not_implemented(); 279 } 280 281 /* Number of week in year, when week starts on monday. 294 int day = tm->tm_yday - _wbyear_offset(tm->tm_year); 295 if (day < 0) { 296 /* Last week of previous year. */ 297 return 53; 298 } 299 if (day > 364 + _is_leap_year(tm->tm_year)){ 300 /* First week of next year. */ 301 return 1; 302 } 303 /* All the other days give correct answer. */ 304 return (day / 7 + 1); 305 } 306 307 /** Week number of the year, assuming weeks start on monday. 308 * The first Monday of January is the first day of week 1; 309 * days in the new year before this are in week 0. 310 * 311 * @param tm Normalized broken-down time. 312 * @return The week number (0 - 53). 282 313 */ 283 314 static int _mon_week_number(const struct posix_tm *tm) 284 315 { 285 // TODO286 not_implemented();316 int first_day = (1 - _day_of_week(tm->tm_year, 0, 1)) % 7; 317 return (tm->tm_yday - first_day + 7) / 7; 287 318 } 288 319 … … 327 358 } 328 359 329 /** 330 * 331 * @param timep 332 * @return 333 */ 334 struct posix_tm *posix_localtime(const time_t *timep) 360 struct posix_tm *posix_gmtime(const time_t *timer) 335 361 { 336 362 static struct posix_tm result; 337 return posix_ localtime_r(timep, &result);338 } 339 340 struct posix_tm *posix_ localtime_r(const time_t *restrict timer,363 return posix_gmtime_r(timer, &result); 364 } 365 366 struct posix_tm *posix_gmtime_r(const time_t *restrict timer, 341 367 struct posix_tm *restrict result) 342 368 { 343 369 assert(timer != NULL); 344 370 assert(result != NULL); 345 346 // TODO: deal with timezone347 // currently assumes system and all times are in GMT348 371 349 372 /* Set epoch and seconds to _long_tm struct and normalize to get … … 371 394 /** 372 395 * 396 * @param timep 397 * @return 398 */ 399 struct posix_tm *posix_localtime(const time_t *timer) 400 { 401 static struct posix_tm result; 402 return posix_localtime_r(timer, &result); 403 } 404 405 struct posix_tm *posix_localtime_r(const time_t *restrict timer, 406 struct posix_tm *restrict result) 407 { 408 // TODO: deal with timezone 409 // currently assumes system and all times are in GMT 410 return posix_gmtime_r(timer, result); 411 } 412 413 /** 414 * 373 415 * @param tm 374 416 * @return … … 409 451 * @return 410 452 */ 411 char *posix_ctime(const time_t *timep) 412 { 413 return posix_asctime(posix_localtime(timep)); 453 char *posix_ctime(const time_t *timer) 454 { 455 struct posix_tm *loctime = posix_localtime(timer); 456 if (loctime == NULL) { 457 return NULL; 458 } 459 return posix_asctime(loctime); 460 } 461 462 char *posix_ctime_r(const time_t *timer, char *buf) 463 { 464 struct posix_tm loctime; 465 if (posix_localtime_r(timer, &loctime) == NULL) { 466 return NULL; 467 } 468 return posix_asctime_r(&loctime, buf); 414 469 } 415 470 … … 600 655 } 601 656 657 int posix_clock_getres(posix_clockid_t clock_id, struct posix_timespec *res) 658 { 659 assert(res != NULL); 660 661 switch (clock_id) { 662 case CLOCK_REALTIME: 663 res->tv_sec = 0; 664 res->tv_nsec = 1000; /* Microsecond resolution. */ 665 return 0; 666 default: 667 errno = EINVAL; 668 return -1; 669 } 670 } 671 672 int posix_clock_gettime(posix_clockid_t clock_id, struct posix_timespec *tp) 673 { 674 assert(tp != NULL); 675 676 switch (clock_id) { 677 case CLOCK_REALTIME: 678 ; 679 struct timeval tv; 680 gettimeofday(&tv, NULL); 681 tp->tv_sec = tv.tv_sec; 682 tp->tv_nsec = tv.tv_usec * 1000; 683 return 0; 684 default: 685 errno = EINVAL; 686 return -1; 687 } 688 } 689 690 int posix_clock_settime(posix_clockid_t clock_id, 691 const struct posix_timespec *tp) 692 { 693 assert(tp != NULL); 694 695 switch (clock_id) { 696 case CLOCK_REALTIME: 697 // TODO: setting clock 698 // FIXME: HelenOS doesn't actually support hardware 699 // clock yet 700 errno = EPERM; 701 return -1; 702 default: 703 errno = EINVAL; 704 return -1; 705 } 706 } 707 708 int posix_clock_nanosleep(posix_clockid_t clock_id, int flags, 709 const struct posix_timespec *rqtp, struct posix_timespec *rmtp) 710 { 711 assert(rqtp != NULL); 712 assert(rmtp != NULL); 713 714 switch (clock_id) { 715 case CLOCK_REALTIME: 716 // TODO: interruptible sleep 717 if (rqtp->tv_sec != 0) { 718 sleep(rqtp->tv_sec); 719 } 720 if (rqtp->tv_nsec != 0) { 721 usleep(rqtp->tv_nsec / 1000); 722 } 723 return 0; 724 default: 725 errno = EINVAL; 726 return -1; 727 } 728 } 729 730 #if 0 731 732 struct __posix_timer { 733 posix_clockid_t clockid; 734 struct posix_sigevent evp; 735 }; 736 737 int posix_timer_create(posix_clockid_t clockid, 738 struct posix_sigevent *restrict evp, 739 posix_timer_t *restrict timerid) 740 { 741 // TODO 742 not_implemented(); 743 } 744 745 int posix_timer_delete(posix_timer_t timerid) 746 { 747 // TODO 748 not_implemented(); 749 } 750 751 int posix_timer_getoverrun(posix_timer_t timerid) 752 { 753 // TODO 754 not_implemented(); 755 } 756 757 int posix_timer_gettime(posix_timer_t timerid, 758 struct posix_itimerspec *value) 759 { 760 // TODO 761 not_implemented(); 762 } 763 764 int posix_timer_settime(posix_timer_t timerid, int flags, 765 const struct posix_itimerspec *restrict value, 766 struct posix_itimerspec *restrict ovalue) 767 { 768 // TODO 769 not_implemented(); 770 } 771 772 #endif 773 602 774 /** 603 775 * Get CPU time used since the process invocation. -
uspace/lib/posix/time.h
r324d46b r3f466c33 56 56 #endif 57 57 58 #ifndef POSIX_SIGNAL_H_ 59 struct posix_sigevent; 60 #ifndef LIBPOSIX_INTERNAL 61 #define sigevent posix_sigevent 62 #endif 63 #endif 64 58 65 #undef ASCTIME_BUF_LEN 59 66 #define ASCTIME_BUF_LEN 26 … … 74 81 }; 75 82 76 // FIXME: should be in sys/types.h77 typedef long posix_clock_t;78 79 83 struct posix_timespec { 80 84 time_t tv_sec; /* Seconds. */ … … 87 91 }; 88 92 93 typedef struct __posix_timer *posix_timer_t; 94 89 95 /* Timezones */ 90 96 … … 101 107 /* Broken-down Time */ 102 108 extern time_t posix_mktime(struct posix_tm *timeptr); 103 extern struct posix_tm *posix_localtime(const time_t *timep); 109 extern struct posix_tm *posix_gmtime(const time_t *timer); 110 extern struct posix_tm *posix_gmtime_r(const time_t *restrict timer, 111 struct posix_tm *restrict result); 112 extern struct posix_tm *posix_localtime(const time_t *timer); 104 113 extern struct posix_tm *posix_localtime_r(const time_t *restrict timer, 105 114 struct posix_tm *restrict result); 115 106 116 /* Formatting Calendar Time */ 107 117 extern char *posix_asctime(const struct posix_tm *timeptr); 108 118 extern char *posix_asctime_r(const struct posix_tm *restrict timeptr, 109 119 char *restrict buf); 110 extern char *posix_ctime(const time_t *timep); 120 extern char *posix_ctime(const time_t *timer); 121 extern char *posix_ctime_r(const time_t *timer, char *buf); 122 111 123 extern size_t posix_strftime(char *restrict s, size_t maxsize, 112 124 const char *restrict format, const struct posix_tm *restrict tm); 113 125 126 extern size_t posix_strftime_l(char *restrict s, size_t maxsize, 127 const char *restrict format, const struct posix_tm *restrict tm, 128 posix_locale_t loc); 129 130 /* Clocks. */ 131 132 extern int posix_clock_getres(posix_clockid_t clock_id, 133 struct posix_timespec *res); 134 extern int posix_clock_gettime(posix_clockid_t clock_id, 135 struct posix_timespec *tp); 136 extern int posix_clock_settime(posix_clockid_t clock_id, 137 const struct posix_timespec *tp); 138 extern int posix_clock_nanosleep(posix_clockid_t clock_id, int flags, 139 const struct posix_timespec *rqtp, struct posix_timespec *rmtp); 140 141 /* Timers. */ 142 143 #if 0 144 145 extern int posix_timer_create(posix_clockid_t clockid, 146 struct posix_sigevent *restrict evp, 147 posix_timer_t *restrict timerid); 148 extern int posix_timer_delete(posix_timer_t timerid); 149 extern int posix_timer_getoverrun(posix_timer_t timerid); 150 extern int posix_timer_gettime(posix_timer_t timerid, 151 struct posix_itimerspec *value); 152 extern int posix_timer_settime(posix_timer_t timerid, int flags, 153 const struct posix_itimerspec *restrict value, 154 struct posix_itimerspec *restrict ovalue); 155 156 #endif 157 114 158 /* CPU Time */ 115 159 extern posix_clock_t posix_clock(void); … … 119 163 #define tm posix_tm 120 164 121 #define clock_t posix_clock_t122 165 #define timespec posix_timespec 123 166 #define itimerspec posix_itimerspec 167 #define timer_t posix_timer_t 124 168 125 169 #define difftime posix_difftime 126 170 #define mktime posix_mktime 171 #define gmtime posix_gmtime 172 #define gmtime_r posix_gmtime_r 127 173 #define localtime posix_localtime 128 174 #define localtime_r posix_localtime_r … … 136 182 #define asctime_r posix_asctime_r 137 183 #define ctime posix_ctime 184 #define ctime_r posix_ctime_r 138 185 #define strftime posix_strftime 139 186 187 #define clock_getres posix_clock_getres 188 #define clock_gettime posix_clock_gettime 189 #define clock_settime posix_clock_settime 190 #define clock_nanosleep posix_clock_nanosleep 191 192 #define timer_create posix_timer_create 193 #define timer_delete posix_timer_delete 194 #define timer_getoverrun posix_timer_getoverrun 195 #define timer_gettime posix_timer_gettime 196 #define timer_settime posix_timer_settime 197 140 198 #define clock posix_clock 141 199 #endif
Note:
See TracChangeset
for help on using the changeset viewer.