OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "vm/os.h" | 5 #include "vm/os.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <limits.h> | 8 #include <limits.h> |
9 #include <mach/mach.h> | 9 #include <mach/mach.h> |
10 #include <mach/clock.h> | 10 #include <mach/clock.h> |
11 #include <mach/mach_time.h> | 11 #include <mach/mach_time.h> |
12 #include <sys/time.h> | 12 #include <sys/time.h> |
13 #include <sys/resource.h> | 13 #include <sys/resource.h> |
14 #include <unistd.h> | 14 #include <unistd.h> |
15 | 15 |
16 #include "platform/utils.h" | 16 #include "platform/utils.h" |
17 #include "vm/isolate.h" | 17 #include "vm/isolate.h" |
| 18 #include "vm/thread.h" |
18 | 19 |
19 namespace dart { | 20 namespace dart { |
20 | 21 |
| 22 static Mutex timezone_mutex; |
| 23 |
21 bool OS::BreakDownSecondsSinceEpoch(time_t seconds_since_epoch, | 24 bool OS::BreakDownSecondsSinceEpoch(time_t seconds_since_epoch, |
22 bool in_utc, | 25 bool in_utc, |
23 BrokenDownDate* result) { | 26 BrokenDownDate* result) { |
24 struct tm tm_result; | 27 struct tm tm_result; |
25 struct tm* error_code; | 28 struct tm* error_code; |
26 if (in_utc) { | 29 if (in_utc) { |
27 error_code = gmtime_r(&seconds_since_epoch, &tm_result); | 30 error_code = gmtime_r(&seconds_since_epoch, &tm_result); |
28 } else { | 31 } else { |
29 // TODO(floitsch): we should be able to call tzset only once during | 32 MutexLocker ml(&timezone_mutex); |
| 33 // TODO(floitsch): we should not need the tzset. According to the man-page |
| 34 // the function already acts as if it invoked tzset. In any case we |
| 35 // shouldn't need to invoke it every time, but only once during |
30 // initialization. | 36 // initialization. |
31 tzset(); // Make sure the libc knows about the local zone. | 37 tzset(); // Make sure the libc knows about the local zone. |
32 error_code = localtime_r(&seconds_since_epoch, &tm_result); | 38 error_code = localtime_r(&seconds_since_epoch, &tm_result); |
33 } | 39 } |
34 result->year = tm_result.tm_year; | 40 result->year = tm_result.tm_year; |
35 result->month= tm_result.tm_mon; | 41 result->month= tm_result.tm_mon; |
36 result->day = tm_result.tm_mday; | 42 result->day = tm_result.tm_mday; |
37 result->hours = tm_result.tm_hour; | 43 result->hours = tm_result.tm_hour; |
38 result->minutes = tm_result.tm_min; | 44 result->minutes = tm_result.tm_min; |
39 result->seconds = tm_result.tm_sec; | 45 result->seconds = tm_result.tm_sec; |
40 return error_code != NULL; | 46 return error_code != NULL; |
41 } | 47 } |
42 | 48 |
43 | 49 |
44 bool OS::BrokenDownToSecondsSinceEpoch( | 50 bool OS::BrokenDownToSecondsSinceEpoch( |
45 const BrokenDownDate& broken_down, bool in_utc, time_t* result) { | 51 const BrokenDownDate& broken_down, bool in_utc, time_t* result) { |
| 52 // Changing the timezone environment is not thread-safe. |
| 53 MutexLocker ml(&timezone_mutex); |
46 struct tm tm_broken_down; | 54 struct tm tm_broken_down; |
47 // mktime takes the years since 1900. | 55 // mktime takes the years since 1900. |
48 tm_broken_down.tm_year = broken_down.year; | 56 tm_broken_down.tm_year = broken_down.year; |
49 tm_broken_down.tm_mon = broken_down.month; | 57 tm_broken_down.tm_mon = broken_down.month; |
50 tm_broken_down.tm_mday = broken_down.day; | 58 tm_broken_down.tm_mday = broken_down.day; |
51 tm_broken_down.tm_hour = broken_down.hours; | 59 tm_broken_down.tm_hour = broken_down.hours; |
52 tm_broken_down.tm_min = broken_down.minutes; | 60 tm_broken_down.tm_min = broken_down.minutes; |
53 tm_broken_down.tm_sec = broken_down.seconds; | 61 tm_broken_down.tm_sec = broken_down.seconds; |
54 // Set wday to an impossible day, so that we can catch bad input. | 62 // Set wday to an impossible day, so that we can catch bad input. |
55 tm_broken_down.tm_wday = -1; | 63 tm_broken_down.tm_wday = -1; |
56 // Make sure the libc knows about the local zone. | |
57 // In case of 'in_utc' this call is mainly for multi-threading issues. If | |
58 // another thread uses a time-function it will set the timezone. The timezone | |
59 // adjustement below would then not work anymore. | |
60 // TODO(floitsch): we should be able to call tzset only once during | |
61 // initialization. | |
62 tzset(); | |
63 if (in_utc) { | 64 if (in_utc) { |
| 65 char* saved_timezone = getenv("TZ"); |
| 66 // Set to UTC. |
| 67 setenv("TZ", "", 1); |
| 68 tzset(); |
64 // Disable daylight saving in utc mode. | 69 // Disable daylight saving in utc mode. |
65 tm_broken_down.tm_isdst = 0; | 70 tm_broken_down.tm_isdst = 0; |
66 // mktime assumes that the given date is local time zone. | |
67 *result = mktime(&tm_broken_down); | 71 *result = mktime(&tm_broken_down); |
68 // Remove the timezone. | 72 // Restore old value of the timezone. |
69 *result -= timezone; | 73 if (saved_timezone) { |
| 74 setenv("TZ", saved_timezone, 1); |
| 75 } else { |
| 76 unsetenv("TZ"); |
| 77 } |
| 78 tzset(); |
70 } else { | 79 } else { |
| 80 // Make sure the libc knows about the local zone. |
| 81 // In case of 'in_utc' this call is mainly for multi-threading issues. If |
| 82 // another thread uses a time-function it will set the timezone. The |
| 83 // timezone adjustement below would then not work anymore. |
| 84 // TODO(floitsch): we should be able to call tzset only once during |
| 85 // initialization. |
| 86 tzset(); |
71 // Let libc figure out if daylight saving is active. | 87 // Let libc figure out if daylight saving is active. |
72 tm_broken_down.tm_isdst = -1; | 88 tm_broken_down.tm_isdst = -1; |
73 *result = mktime(&tm_broken_down); | 89 *result = mktime(&tm_broken_down); |
74 } | 90 } |
75 if ((*result == -1) && (tm_broken_down.tm_wday == -1)) { | 91 if ((*result == -1) && (tm_broken_down.tm_wday == -1)) { |
76 return false; | 92 return false; |
77 } | 93 } |
78 return true; | 94 return true; |
79 } | 95 } |
80 | 96 |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 void OS::Abort() { | 209 void OS::Abort() { |
194 abort(); | 210 abort(); |
195 } | 211 } |
196 | 212 |
197 | 213 |
198 void OS::Exit(int code) { | 214 void OS::Exit(int code) { |
199 exit(code); | 215 exit(code); |
200 } | 216 } |
201 | 217 |
202 } // namespace dart | 218 } // namespace dart |
OLD | NEW |