Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(118)

Side by Side Diff: base/time/time_posix.cc

Issue 444473002: Add lock to prevent race on SysTimeFromTimeStruct (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/time/time.h" 5 #include "base/time/time.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 #include <sys/time.h> 8 #include <sys/time.h>
9 #include <time.h> 9 #include <time.h>
10 #if defined(OS_ANDROID) && !defined(__LP64__) 10 #if defined(OS_ANDROID) && !defined(__LP64__)
11 #include <time64.h> 11 #include <time64.h>
12 #endif 12 #endif
13 #include <unistd.h> 13 #include <unistd.h>
14 14
15 #include <limits> 15 #include <limits>
16 #include <ostream> 16 #include <ostream>
17 17
18 #include "base/basictypes.h" 18 #include "base/basictypes.h"
19 #include "base/logging.h" 19 #include "base/logging.h"
20 #include "base/port.h" 20 #include "base/port.h"
21 #include "build/build_config.h" 21 #include "build/build_config.h"
22 22
23 #if defined(OS_ANDROID) 23 #if defined(OS_ANDROID)
24 #include "base/lazy_instance.h"
24 #include "base/os_compat_android.h" 25 #include "base/os_compat_android.h"
26 #include "base/synchronization/lock.h"
25 #elif defined(OS_NACL) 27 #elif defined(OS_NACL)
26 #include "base/os_compat_nacl.h" 28 #include "base/os_compat_nacl.h"
27 #endif 29 #endif
28 30
29 namespace { 31 namespace {
30 32
33 #if defined(OS_ANDROID)
willchan no longer on Chromium 2014/08/05 23:14:48 No need for the OS_ANDROID stuff. This bug isn't A
bengr 2014/08/05 23:54:58 Done.
34 // This prevents a crash on traversing the environment global and looking up
35 // the 'TZ' variable in libc.
36 base::LazyInstance<base::Lock>::Leaky
37 g_sys_time_to_time_struct_lock = LAZY_INSTANCE_INITIALIZER;
38 #endif
39
31 #if !defined(OS_MACOSX) 40 #if !defined(OS_MACOSX)
32 // Define a system-specific SysTime that wraps either to a time_t or 41 // Define a system-specific SysTime that wraps either to a time_t or
33 // a time64_t depending on the host system, and associated convertion. 42 // a time64_t depending on the host system, and associated convertion.
34 // See crbug.com/162007 43 // See crbug.com/162007
35 #if defined(OS_ANDROID) && !defined(__LP64__) 44 #if defined(OS_ANDROID) && !defined(__LP64__)
36 typedef time64_t SysTime; 45 typedef time64_t SysTime;
37 46
38 SysTime SysTimeFromTimeStruct(struct tm* timestruct, bool is_local) { 47 SysTime SysTimeFromTimeStruct(struct tm* timestruct, bool is_local) {
48 #if defined(OS_ANDROID)
49 base::AutoLock locked(g_sys_time_to_time_struct_lock.Get());
50 #endif
39 if (is_local) 51 if (is_local)
40 return mktime64(timestruct); 52 return mktime64(timestruct);
41 else 53 else
42 return timegm64(timestruct); 54 return timegm64(timestruct);
43 } 55 }
44 56
45 void SysTimeToTimeStruct(SysTime t, struct tm* timestruct, bool is_local) { 57 void SysTimeToTimeStruct(SysTime t, struct tm* timestruct, bool is_local) {
58 #if defined(OS_ANDROID)
59 base::AutoLock locked(g_sys_time_to_time_struct_lock.Get());
60 #endif
46 if (is_local) 61 if (is_local)
47 localtime64_r(&t, timestruct); 62 localtime64_r(&t, timestruct);
48 else 63 else
49 gmtime64_r(&t, timestruct); 64 gmtime64_r(&t, timestruct);
50 } 65 }
51 66
52 #else // OS_ANDROID && !__LP64__ 67 #else // OS_ANDROID && !__LP64__
53 typedef time_t SysTime; 68 typedef time_t SysTime;
54 69
55 SysTime SysTimeFromTimeStruct(struct tm* timestruct, bool is_local) { 70 SysTime SysTimeFromTimeStruct(struct tm* timestruct, bool is_local) {
(...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after
377 result.tv_usec = static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1; 392 result.tv_usec = static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1;
378 return result; 393 return result;
379 } 394 }
380 int64 us = us_ - kTimeTToMicrosecondsOffset; 395 int64 us = us_ - kTimeTToMicrosecondsOffset;
381 result.tv_sec = us / Time::kMicrosecondsPerSecond; 396 result.tv_sec = us / Time::kMicrosecondsPerSecond;
382 result.tv_usec = us % Time::kMicrosecondsPerSecond; 397 result.tv_usec = us % Time::kMicrosecondsPerSecond;
383 return result; 398 return result;
384 } 399 }
385 400
386 } // namespace base 401 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698