Uptime.cpp (1456B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "Uptime.h" 8 9 #include "mozilla/Now.h" 10 11 using namespace mozilla; 12 13 namespace { 14 15 static Maybe<uint64_t> mStartExcludingSuspendMs; 16 static Maybe<uint64_t> mStartIncludingSuspendMs; 17 18 }; // anonymous namespace 19 20 namespace mozilla { 21 22 void InitializeUptime() { 23 MOZ_RELEASE_ASSERT(mStartIncludingSuspendMs.isNothing() && 24 mStartExcludingSuspendMs.isNothing(), 25 "Must not be called more than once"); 26 mStartIncludingSuspendMs = NowIncludingSuspendMs(); 27 mStartExcludingSuspendMs = NowExcludingSuspendMs(); 28 } 29 30 Maybe<uint64_t> ProcessUptimeMs() { 31 if (!mStartIncludingSuspendMs) { 32 return Nothing(); 33 } 34 Maybe<uint64_t> maybeNow = NowIncludingSuspendMs(); 35 if (!maybeNow) { 36 return Nothing(); 37 } 38 return Some(maybeNow.value() - mStartIncludingSuspendMs.value()); 39 } 40 41 Maybe<uint64_t> ProcessUptimeExcludingSuspendMs() { 42 if (!mStartExcludingSuspendMs) { 43 return Nothing(); 44 } 45 Maybe<uint64_t> maybeNow = NowExcludingSuspendMs(); 46 if (!maybeNow) { 47 return Nothing(); 48 } 49 return Some(maybeNow.value() - mStartExcludingSuspendMs.value()); 50 } 51 52 }; // namespace mozilla