time_zone_impl.cc
Go to the documentation of this file.
00001 // Copyright 2016 Google Inc. All Rights Reserved.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //   https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 //   Unless required by applicable law or agreed to in writing, software
00010 //   distributed under the License is distributed on an "AS IS" BASIS,
00011 //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 //   See the License for the specific language governing permissions and
00013 //   limitations under the License.
00014 
00015 #include "time_zone_impl.h"
00016 
00017 #include <mutex>
00018 #include <string>
00019 #include <unordered_map>
00020 #include <utility>
00021 
00022 #include "time_zone_fixed.h"
00023 
00024 namespace absl {
00025 namespace time_internal {
00026 namespace cctz {
00027 
00028 namespace {
00029 
00030 // time_zone::Impls are linked into a map to support fast lookup by name.
00031 using TimeZoneImplByName =
00032     std::unordered_map<std::string, const time_zone::Impl*>;
00033 TimeZoneImplByName* time_zone_map = nullptr;
00034 
00035 // Mutual exclusion for time_zone_map.
00036 std::mutex time_zone_mutex;
00037 
00038 }  // namespace
00039 
00040 time_zone time_zone::Impl::UTC() {
00041   return time_zone(UTCImpl());
00042 }
00043 
00044 bool time_zone::Impl::LoadTimeZone(const std::string& name, time_zone* tz) {
00045   const time_zone::Impl* const utc_impl = UTCImpl();
00046 
00047   // First check for UTC (which is never a key in time_zone_map).
00048   auto offset = seconds::zero();
00049   if (FixedOffsetFromName(name, &offset) && offset == seconds::zero()) {
00050     *tz = time_zone(utc_impl);
00051     return true;
00052   }
00053 
00054   // Then check, under a shared lock, whether the time zone has already
00055   // been loaded. This is the common path. TODO: Move to shared_mutex.
00056   {
00057     std::lock_guard<std::mutex> lock(time_zone_mutex);
00058     if (time_zone_map != nullptr) {
00059       TimeZoneImplByName::const_iterator itr = time_zone_map->find(name);
00060       if (itr != time_zone_map->end()) {
00061         *tz = time_zone(itr->second);
00062         return itr->second != utc_impl;
00063       }
00064     }
00065   }
00066 
00067   // Now check again, under an exclusive lock.
00068   std::lock_guard<std::mutex> lock(time_zone_mutex);
00069   if (time_zone_map == nullptr) time_zone_map = new TimeZoneImplByName;
00070   const Impl*& impl = (*time_zone_map)[name];
00071   if (impl == nullptr) {
00072     // The first thread in loads the new time zone.
00073     Impl* new_impl = new Impl(name);
00074     new_impl->zone_ = TimeZoneIf::Load(new_impl->name_);
00075     if (new_impl->zone_ == nullptr) {
00076       delete new_impl;  // free the nascent Impl
00077       impl = utc_impl;  // and fallback to UTC
00078     } else {
00079       impl = new_impl;  // install new time zone
00080     }
00081   }
00082   *tz = time_zone(impl);
00083   return impl != utc_impl;
00084 }
00085 
00086 void time_zone::Impl::ClearTimeZoneMapTestOnly() {
00087   std::lock_guard<std::mutex> lock(time_zone_mutex);
00088   if (time_zone_map != nullptr) {
00089     // Existing time_zone::Impl* entries are in the wild, so we simply
00090     // leak them.  Future requests will result in reloading the data.
00091     time_zone_map->clear();
00092   }
00093 }
00094 
00095 time_zone::Impl::Impl(const std::string& name) : name_(name) {}
00096 
00097 const time_zone::Impl* time_zone::Impl::UTCImpl() {
00098   static Impl* utc_impl = [] {
00099     Impl* impl = new Impl("UTC");
00100     impl->zone_ = TimeZoneIf::Load(impl->name_);  // never fails
00101     return impl;
00102   }();
00103   return utc_impl;
00104 }
00105 
00106 }  // namespace cctz
00107 }  // namespace time_internal
00108 }  // namespace absl


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:15