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 <mutex> |
18 | #include <string> |
19 | #include <unordered_map> |
20 | #include <utility> |
21 | |
22 | #include "time_zone_fixed.h" |
23 | |
24 | namespace absl { |
25 | namespace time_internal { |
26 | namespace cctz { |
27 | |
28 | namespace { |
29 | |
30 | // time_zone::Impls are linked into a map to support fast lookup by name. |
31 | using TimeZoneImplByName = |
32 | std::unordered_map<std::string, const time_zone::Impl*>; |
33 | TimeZoneImplByName* time_zone_map = nullptr; |
34 | |
35 | // Mutual exclusion for time_zone_map. |
36 | std::mutex time_zone_mutex; |
37 | |
38 | } // namespace |
39 | |
40 | time_zone time_zone::Impl::UTC() { |
41 | return time_zone(UTCImpl()); |
42 | } |
43 | |
44 | bool time_zone::Impl::LoadTimeZone(const std::string& name, time_zone* tz) { |
45 | const time_zone::Impl* const utc_impl = UTCImpl(); |
46 | |
47 | // First check for UTC (which is never a key in time_zone_map). |
48 | auto offset = seconds::zero(); |
49 | if (FixedOffsetFromName(name, &offset) && offset == seconds::zero()) { |
50 | *tz = time_zone(utc_impl); |
51 | return true; |
52 | } |
53 | |
54 | // Then check, under a shared lock, whether the time zone has already |
55 | // been loaded. This is the common path. TODO: Move to shared_mutex. |
56 | { |
57 | std::lock_guard<std::mutex> lock(time_zone_mutex); |
58 | if (time_zone_map != nullptr) { |
59 | TimeZoneImplByName::const_iterator itr = time_zone_map->find(name); |
60 | if (itr != time_zone_map->end()) { |
61 | *tz = time_zone(itr->second); |
62 | return itr->second != utc_impl; |
63 | } |
64 | } |
65 | } |
66 | |
67 | // Now check again, under an exclusive lock. |
68 | std::lock_guard<std::mutex> lock(time_zone_mutex); |
69 | if (time_zone_map == nullptr) time_zone_map = new TimeZoneImplByName; |
70 | const Impl*& impl = (*time_zone_map)[name]; |
71 | if (impl == nullptr) { |
72 | // The first thread in loads the new time zone. |
73 | Impl* new_impl = new Impl(name); |
74 | new_impl->zone_ = TimeZoneIf::Load(new_impl->name_); |
75 | if (new_impl->zone_ == nullptr) { |
76 | delete new_impl; // free the nascent Impl |
77 | impl = utc_impl; // and fallback to UTC |
78 | } else { |
79 | impl = new_impl; // install new time zone |
80 | } |
81 | } |
82 | *tz = time_zone(impl); |
83 | return impl != utc_impl; |
84 | } |
85 | |
86 | void time_zone::Impl::ClearTimeZoneMapTestOnly() { |
87 | std::lock_guard<std::mutex> lock(time_zone_mutex); |
88 | if (time_zone_map != nullptr) { |
89 | // Existing time_zone::Impl* entries are in the wild, so we simply |
90 | // leak them. Future requests will result in reloading the data. |
91 | time_zone_map->clear(); |
92 | } |
93 | } |
94 | |
95 | time_zone::Impl::Impl(const std::string& name) : name_(name) {} |
96 | |
97 | const time_zone::Impl* time_zone::Impl::UTCImpl() { |
98 | static Impl* utc_impl = [] { |
99 | Impl* impl = new Impl("UTC" ); |
100 | impl->zone_ = TimeZoneIf::Load(impl->name_); // never fails |
101 | return impl; |
102 | }(); |
103 | return utc_impl; |
104 | } |
105 | |
106 | } // namespace cctz |
107 | } // namespace time_internal |
108 | } // namespace absl |
109 | |