Changes in uspace/lib/c/generic/time.c [2c577e0b:63f8966] in mainline
- File:
-
- 1 edited
-
uspace/lib/c/generic/time.c (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/time.c
r2c577e0b r63f8966 35 35 #include <sys/time.h> 36 36 #include <unistd.h> 37 #include < bool.h>38 #include < ipc/ns.h>37 #include <ipc/ipc.h> 38 #include <stdio.h> 39 39 #include <arch/barrier.h> 40 #include <macros.h> 40 #include <unistd.h> 41 #include <atomic.h> 42 #include <sysinfo.h> 43 #include <ipc/services.h> 41 44 #include <libc.h> 45 46 #include <sysinfo.h> 47 #include <as.h> 48 #include <ddi.h> 49 42 50 #include <time.h> 43 51 44 /* * Pointer to kernel sharedvariables with time */52 /* Pointers to public variables with time */ 45 53 struct { 46 54 volatile sysarg_t seconds1; … … 51 59 /** Add microseconds to given timeval. 52 60 * 53 * @param tv Destination timeval. 54 * @param usecs Number of microseconds to add. 55 * 61 * @param tv Destination timeval. 62 * @param usecs Number of microseconds to add. 56 63 */ 57 64 void tv_add(struct timeval *tv, suseconds_t usecs) … … 59 66 tv->tv_sec += usecs / 1000000; 60 67 tv->tv_usec += usecs % 1000000; 61 62 68 if (tv->tv_usec > 1000000) { 63 69 tv->tv_sec++; … … 68 74 /** Subtract two timevals. 69 75 * 70 * @param tv1 First timeval. 71 * @param tv2 Second timeval. 72 * 73 * @return Difference between tv1 and tv2 (tv1 - tv2) in 74 * microseconds. 75 * 76 * @param tv1 First timeval. 77 * @param tv2 Second timeval. 78 * 79 * @return Return difference between tv1 and tv2 (tv1 - tv2) in 80 * microseconds. 76 81 */ 77 82 suseconds_t tv_sub(struct timeval *tv1, struct timeval *tv2) 78 83 { 79 return (tv1->tv_usec - tv2->tv_usec) + 80 ((tv1->tv_sec - tv2->tv_sec) * 1000000); 84 suseconds_t result; 85 86 result = tv1->tv_usec - tv2->tv_usec; 87 result += (tv1->tv_sec - tv2->tv_sec) * 1000000; 88 89 return result; 81 90 } 82 91 83 92 /** Decide if one timeval is greater than the other. 84 93 * 85 * @param t1 First timeval. 86 * @param t2 Second timeval. 87 * 88 * @return True if tv1 is greater than tv2. 89 * @return False otherwise. 90 * 94 * @param t1 First timeval. 95 * @param t2 Second timeval. 96 * 97 * @return Return true tv1 is greater than tv2. Otherwise return 98 * false. 91 99 */ 92 100 int tv_gt(struct timeval *tv1, struct timeval *tv2) 93 101 { 94 102 if (tv1->tv_sec > tv2->tv_sec) 95 return true; 96 97 if ((tv1->tv_sec == tv2->tv_sec) && (tv1->tv_usec > tv2->tv_usec)) 98 return true; 99 100 return false; 103 return 1; 104 if (tv1->tv_sec == tv2->tv_sec && tv1->tv_usec > tv2->tv_usec) 105 return 1; 106 return 0; 101 107 } 102 108 103 109 /** Decide if one timeval is greater than or equal to the other. 104 110 * 105 * @param tv1 First timeval. 106 * @param tv2 Second timeval. 107 * 108 * @return True if tv1 is greater than or equal to tv2. 109 * @return False otherwise. 110 * 111 * @param tv1 First timeval. 112 * @param tv2 Second timeval. 113 * 114 * @return Return true if tv1 is greater than or equal to tv2. 115 * Otherwise return false. 111 116 */ 112 117 int tv_gteq(struct timeval *tv1, struct timeval *tv2) 113 118 { 114 119 if (tv1->tv_sec > tv2->tv_sec) 115 return true; 116 117 if ((tv1->tv_sec == tv2->tv_sec) && (tv1->tv_usec >= tv2->tv_usec)) 118 return true; 119 120 return false; 121 } 122 123 /** Get time of day 124 * 125 * The time variables are memory mapped (read-only) from kernel which 126 * updates them periodically. 127 * 128 * As it is impossible to read 2 values atomically, we use a trick: 129 * First we read the seconds, then we read the microseconds, then we 130 * read the seconds again. If a second elapsed in the meantime, set 131 * the microseconds to zero. 132 * 133 * This assures that the values returned by two subsequent calls 134 * to gettimeofday() are monotonous. 135 * 120 return 1; 121 if (tv1->tv_sec == tv2->tv_sec && tv1->tv_usec >= tv2->tv_usec) 122 return 1; 123 return 0; 124 } 125 126 127 /** POSIX gettimeofday 128 * 129 * The time variables are memory mapped(RO) from kernel, which updates 130 * them periodically. As it is impossible to read 2 values atomically, we 131 * use a trick: First read a seconds, then read microseconds, then 132 * read seconds again. If a second elapsed in the meantime, set it to zero. 133 * This provides assurance, that at least the 134 * sequence of subsequent gettimeofday calls is ordered. 136 135 */ 137 136 int gettimeofday(struct timeval *tv, struct timezone *tz) 138 137 { 138 void *mapping; 139 sysarg_t s1, s2; 140 int rights; 141 int res; 142 139 143 if (!ktime) { 140 ktime = service_realtime_share_in(); 141 if (!ktime) 142 return -1; 143 } 144 144 mapping = as_get_mappable_page(PAGE_SIZE); 145 /* Get the mapping of kernel clock */ 146 res = ipc_share_in_start_1_1(PHONE_NS, mapping, PAGE_SIZE, 147 SERVICE_MEM_REALTIME, &rights); 148 if (res) { 149 printf("Failed to initialize timeofday memarea\n"); 150 _exit(1); 151 } 152 if (!(rights & AS_AREA_READ)) { 153 printf("Received bad rights on time area: %X\n", 154 rights); 155 as_area_destroy(mapping); 156 _exit(1); 157 } 158 ktime = mapping; 159 } 145 160 if (tz) { 146 161 tz->tz_minuteswest = 0; 147 162 tz->tz_dsttime = DST_NONE; 148 163 } 149 150 sysarg_t s2 = ktime->seconds2; 151 164 165 s2 = ktime->seconds2; 152 166 read_barrier(); 153 167 tv->tv_usec = ktime->useconds; 154 155 168 read_barrier(); 156 sysarg_t s1 = ktime->seconds1; 157 169 s1 = ktime->seconds1; 158 170 if (s1 != s2) { 159 tv->tv_sec = max(s1, s2);160 171 tv->tv_usec = 0; 172 tv->tv_sec = s1 > s2 ? s1 : s2; 161 173 } else 162 174 tv->tv_sec = s1; 163 175 164 176 return 0; 165 177 } … … 168 180 { 169 181 struct timeval tv; 182 170 183 if (gettimeofday(&tv, NULL)) 171 184 return (time_t) -1; 172 173 185 if (tloc) 174 186 *tloc = tv.tv_sec; 175 176 187 return tv.tv_sec; 177 188 } 178 189 179 /** Wait unconditionally for specified number of microseconds 180 * 181 */ 190 /** Wait unconditionally for specified number of microseconds */ 182 191 int usleep(useconds_t usec) 183 192 { … … 186 195 } 187 196 188 /** Wait unconditionally for specified number of seconds 189 * 190 */ 197 /** Wait unconditionally for specified number of seconds */ 191 198 unsigned int sleep(unsigned int sec) 192 199 { 193 /* 194 * Sleep in 1000 second steps to support 195 * full argument range 196 */ 197 200 /* Sleep in 1000 second steps to support 201 full argument range */ 198 202 while (sec > 0) { 199 203 unsigned int period = (sec > 1000) ? 1000 : sec; 200 204 201 205 usleep(period * 1000000); 202 206 sec -= period; 203 207 } 204 205 208 return 0; 206 209 }
Note:
See TracChangeset
for help on using the changeset viewer.
