1 /* 2 * Copyright 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "Settings.h" 18 19 #define LOG_TAG "Settings" 20 21 #include <memory> 22 23 #include "Log.h" 24 25 namespace swappy { 26 getInstance()27Settings *Settings::getInstance() { 28 static auto settings = std::make_unique<Settings>(ConstructorTag{}); 29 return settings.get(); 30 } 31 addListener(Listener listener)32void Settings::addListener(Listener listener) { 33 std::lock_guard<std::mutex> lock(mMutex); 34 mListeners.emplace_back(std::move(listener)); 35 } 36 setRefreshPeriod(std::chrono::nanoseconds period)37void Settings::setRefreshPeriod(std::chrono::nanoseconds period) { 38 { 39 std::lock_guard<std::mutex> lock(mMutex); 40 mRefreshPeriod = period; 41 } 42 // Notify the listeners without the lock held 43 notifyListeners(); 44 } setSwapIntervalNS(uint64_t swap_ns)45void Settings::setSwapIntervalNS(uint64_t swap_ns) { 46 { 47 std::lock_guard<std::mutex> lock(mMutex); 48 mSwapIntervalNS = swap_ns; 49 } 50 // Notify the listeners without the lock held 51 notifyListeners(); 52 } 53 setUseAffinity(bool tf)54void Settings::setUseAffinity(bool tf) { 55 { 56 std::lock_guard<std::mutex> lock(mMutex); 57 mUseAffinity = tf; 58 } 59 // Notify the listeners without the lock held 60 notifyListeners(); 61 } 62 63 getRefreshPeriod() const64std::chrono::nanoseconds Settings::getRefreshPeriod() const { 65 std::lock_guard<std::mutex> lock(mMutex); 66 return mRefreshPeriod; 67 } 68 getSwapIntervalNS() const69uint64_t Settings::getSwapIntervalNS() const { 70 std::lock_guard<std::mutex> lock(mMutex); 71 return mSwapIntervalNS; 72 } 73 getUseAffinity() const74bool Settings::getUseAffinity() const { 75 std::lock_guard<std::mutex> lock(mMutex); 76 return mUseAffinity; 77 } 78 notifyListeners()79void Settings::notifyListeners() { 80 // Grab a local copy of the listeners 81 std::vector<Listener> listeners; 82 { 83 std::lock_guard<std::mutex> lock(mMutex); 84 listeners = mListeners; 85 } 86 87 // Call the listeners without the lock held 88 for (const auto &listener : listeners) { 89 listener(); 90 } 91 } 92 93 } // namespace swappy 94