• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //   https://www.apache.org/licenses/LICENSE-2.0
8 //
9 //   Unless required by applicable law or agreed to in writing, software
10 //   distributed under the License is distributed on an "AS IS" BASIS,
11 //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 //   See the License for the specific language governing permissions and
13 //   limitations under the License.
14 
15 #include "time_zone_impl.h"
16 
17 #include <deque>
18 #include <mutex>
19 #include <string>
20 #include <unordered_map>
21 #include <utility>
22 
23 #include "absl/base/config.h"
24 #include "time_zone_fixed.h"
25 
26 namespace absl {
27 ABSL_NAMESPACE_BEGIN
28 namespace time_internal {
29 namespace cctz {
30 
31 namespace {
32 
33 // time_zone::Impls are linked into a map to support fast lookup by name.
34 using TimeZoneImplByName =
35     std::unordered_map<std::string, const time_zone::Impl*>;
36 TimeZoneImplByName* time_zone_map = nullptr;
37 
38 // Mutual exclusion for time_zone_map.
TimeZoneMutex()39 std::mutex& TimeZoneMutex() {
40   // This mutex is intentionally "leaked" to avoid the static deinitialization
41   // order fiasco (std::mutex's destructor is not trivial on many platforms).
42   static std::mutex* time_zone_mutex = new std::mutex;
43   return *time_zone_mutex;
44 }
45 
46 }  // namespace
47 
UTC()48 time_zone time_zone::Impl::UTC() { return time_zone(UTCImpl()); }
49 
LoadTimeZone(const std::string & name,time_zone * tz)50 bool time_zone::Impl::LoadTimeZone(const std::string& name, time_zone* tz) {
51   const time_zone::Impl* const utc_impl = UTCImpl();
52 
53   // First check for UTC (which is never a key in time_zone_map).
54   auto offset = seconds::zero();
55   if (FixedOffsetFromName(name, &offset) && offset == seconds::zero()) {
56     *tz = time_zone(utc_impl);
57     return true;
58   }
59 
60   // Then check, under a shared lock, whether the time zone has already
61   // been loaded. This is the common path. TODO: Move to shared_mutex.
62   {
63     std::lock_guard<std::mutex> lock(TimeZoneMutex());
64     if (time_zone_map != nullptr) {
65       TimeZoneImplByName::const_iterator itr = time_zone_map->find(name);
66       if (itr != time_zone_map->end()) {
67         *tz = time_zone(itr->second);
68         return itr->second != utc_impl;
69       }
70     }
71   }
72 
73   // Now check again, under an exclusive lock.
74   std::lock_guard<std::mutex> lock(TimeZoneMutex());
75   if (time_zone_map == nullptr) time_zone_map = new TimeZoneImplByName;
76   const Impl*& impl = (*time_zone_map)[name];
77   if (impl == nullptr) {
78     // The first thread in loads the new time zone.
79     Impl* new_impl = new Impl(name);
80     new_impl->zone_ = TimeZoneIf::Load(new_impl->name_);
81     if (new_impl->zone_ == nullptr) {
82       delete new_impl;  // free the nascent Impl
83       impl = utc_impl;  // and fallback to UTC
84     } else {
85       impl = new_impl;  // install new time zone
86     }
87   }
88   *tz = time_zone(impl);
89   return impl != utc_impl;
90 }
91 
ClearTimeZoneMapTestOnly()92 void time_zone::Impl::ClearTimeZoneMapTestOnly() {
93   std::lock_guard<std::mutex> lock(TimeZoneMutex());
94   if (time_zone_map != nullptr) {
95     // Existing time_zone::Impl* entries are in the wild, so we can't delete
96     // them. Instead, we move them to a private container, where they are
97     // logically unreachable but not "leaked".  Future requests will result
98     // in reloading the data.
99     static auto* cleared = new std::deque<const time_zone::Impl*>;
100     for (const auto& element : *time_zone_map) {
101       cleared->push_back(element.second);
102     }
103     time_zone_map->clear();
104   }
105 }
106 
Impl(const std::string & name)107 time_zone::Impl::Impl(const std::string& name) : name_(name) {}
108 
UTCImpl()109 const time_zone::Impl* time_zone::Impl::UTCImpl() {
110   static Impl* utc_impl = [] {
111     Impl* impl = new Impl("UTC");
112     impl->zone_ = TimeZoneIf::Load(impl->name_);  // never fails
113     return impl;
114   }();
115   return utc_impl;
116 }
117 
118 }  // namespace cctz
119 }  // namespace time_internal
120 ABSL_NAMESPACE_END
121 }  // namespace absl
122