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 <time.h> | 9 #include <time.h> |
10 #include <sys/resource.h> | 10 #include <sys/resource.h> |
11 #include <sys/time.h> | 11 #include <sys/time.h> |
12 #include <sys/types.h> | 12 #include <sys/types.h> |
13 #include <unistd.h> | 13 #include <unistd.h> |
14 | 14 |
15 #include "platform/utils.h" | 15 #include "platform/utils.h" |
16 #include "vm/isolate.h" | 16 #include "vm/isolate.h" |
17 #include "vm/thread.h" | |
17 | 18 |
18 namespace dart { | 19 namespace dart { |
19 | 20 |
21 static Mutex timezone_mutex; | |
22 | |
20 bool OS::BreakDownSecondsSinceEpoch(time_t seconds_since_epoch, | 23 bool OS::BreakDownSecondsSinceEpoch(time_t seconds_since_epoch, |
21 bool in_utc, | 24 bool in_utc, |
22 BrokenDownDate* result) { | 25 BrokenDownDate* result) { |
23 struct tm tm_result; | 26 struct tm tm_result; |
24 struct tm* error_code; | 27 struct tm* error_code; |
25 if (in_utc) { | 28 if (in_utc) { |
26 error_code = gmtime_r(&seconds_since_epoch, &tm_result); | 29 error_code = gmtime_r(&seconds_since_epoch, &tm_result); |
27 } else { | 30 } else { |
28 // TODO(floitsch): we should be able to call tzset only once during | 31 MutexLocker ml(&timezone_mutex); |
32 // TODO(floitsch): we should not need the tzset. According to the man-page | |
33 // the function already acts as if it invoked tzset. In any case we | |
34 // shouldn't need to invoke it every time, but only once during | |
29 // initialization. | 35 // initialization. |
30 tzset(); // Make sure the libc knows about the local zone. | 36 tzset(); // Make sure the libc knows about the local zone. |
31 error_code = localtime_r(&seconds_since_epoch, &tm_result); | 37 error_code = localtime_r(&seconds_since_epoch, &tm_result); |
32 } | 38 } |
33 result->year = tm_result.tm_year; | 39 result->year = tm_result.tm_year; |
34 result->month= tm_result.tm_mon; | 40 result->month= tm_result.tm_mon; |
35 result->day = tm_result.tm_mday; | 41 result->day = tm_result.tm_mday; |
36 result->hours = tm_result.tm_hour; | 42 result->hours = tm_result.tm_hour; |
37 result->minutes = tm_result.tm_min; | 43 result->minutes = tm_result.tm_min; |
38 result->seconds = tm_result.tm_sec; | 44 result->seconds = tm_result.tm_sec; |
39 return error_code != NULL; | 45 return error_code != NULL; |
40 } | 46 } |
41 | 47 |
42 | 48 |
43 bool OS::BrokenDownToSecondsSinceEpoch( | 49 bool OS::BrokenDownToSecondsSinceEpoch( |
44 const BrokenDownDate& broken_down, bool in_utc, time_t* result) { | 50 const BrokenDownDate& broken_down, bool in_utc, time_t* result) { |
51 // Changing the timezone environment is not thread-safe. | |
52 MutexLocker ml(&timezone_mutex); | |
Ivan Posva
2012/04/03 23:03:52
You might be protecting against other Dart VM base
floitsch
2012/04/04 08:27:43
I never thought I would be safe against the embedd
| |
45 struct tm tm_broken_down; | 53 struct tm tm_broken_down; |
46 // mktime takes the years since 1900. | 54 // mktime takes the years since 1900. |
47 tm_broken_down.tm_year = broken_down.year; | 55 tm_broken_down.tm_year = broken_down.year; |
48 tm_broken_down.tm_mon = broken_down.month; | 56 tm_broken_down.tm_mon = broken_down.month; |
49 tm_broken_down.tm_mday = broken_down.day; | 57 tm_broken_down.tm_mday = broken_down.day; |
50 tm_broken_down.tm_hour = broken_down.hours; | 58 tm_broken_down.tm_hour = broken_down.hours; |
51 tm_broken_down.tm_min = broken_down.minutes; | 59 tm_broken_down.tm_min = broken_down.minutes; |
52 tm_broken_down.tm_sec = broken_down.seconds; | 60 tm_broken_down.tm_sec = broken_down.seconds; |
53 // Set wday to an impossible day, so that we can catch bad input. | 61 // Set wday to an impossible day, so that we can catch bad input. |
54 tm_broken_down.tm_wday = -1; | 62 tm_broken_down.tm_wday = -1; |
55 // Make sure the libc knows about the local zone. | |
56 // In case of 'in_utc' this call is mainly for multi-threading issues. If | |
57 // another thread uses a time-function it will set the timezone. The timezone | |
58 // adjustement below would then not work anymore. | |
59 // TODO(floitsch): we should be able to call tzset only once during | |
60 // initialization. | |
61 tzset(); | |
62 if (in_utc) { | 63 if (in_utc) { |
64 char* saved_timezone = getenv("TZ"); | |
65 // Set to UTC. | |
66 setenv("TZ", "", 1); | |
67 tzset(); | |
63 // Disable daylight saving in utc mode. | 68 // Disable daylight saving in utc mode. |
64 tm_broken_down.tm_isdst = 0; | 69 tm_broken_down.tm_isdst = 0; |
65 // mktime assumes that the given date is local time zone. | |
66 *result = mktime(&tm_broken_down); | 70 *result = mktime(&tm_broken_down); |
67 // Remove the timezone. | 71 // Restore old value of the timezone. |
68 *result -= timezone; | 72 if (saved_timezone) { |
73 setenv("TZ", saved_timezone, 1); | |
74 } else { | |
75 unsetenv("TZ"); | |
76 } | |
77 tzset(); | |
69 } else { | 78 } else { |
79 // Make sure the libc knows about the local zone. | |
80 // In case of 'in_utc' this call is mainly for multi-threading issues. If | |
81 // another thread uses a time-function it will set the timezone. The | |
82 // timezone adjustement below would then not work anymore. | |
83 // TODO(floitsch): we should be able to call tzset only once during | |
84 // initialization. | |
85 tzset(); | |
70 // Let libc figure out if daylight saving is active. | 86 // Let libc figure out if daylight saving is active. |
71 tm_broken_down.tm_isdst = -1; | 87 tm_broken_down.tm_isdst = -1; |
72 *result = mktime(&tm_broken_down); | 88 *result = mktime(&tm_broken_down); |
73 } | 89 } |
74 if ((*result == -1) && (tm_broken_down.tm_wday == -1)) { | 90 if ((*result == -1) && (tm_broken_down.tm_wday == -1)) { |
75 return false; | 91 return false; |
76 } | 92 } |
77 return true; | 93 return true; |
78 } | 94 } |
79 | 95 |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
220 void OS::Abort() { | 236 void OS::Abort() { |
221 abort(); | 237 abort(); |
222 } | 238 } |
223 | 239 |
224 | 240 |
225 void OS::Exit(int code) { | 241 void OS::Exit(int code) { |
226 exit(code); | 242 exit(code); |
227 } | 243 } |
228 | 244 |
229 } // namespace dart | 245 } // namespace dart |
OLD | NEW |