hal/fallback/FallbackAlarm.cpp

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #include "Hal.h"
michael@0 6
michael@0 7 #include <algorithm>
michael@0 8
michael@0 9 #include "mozilla/ClearOnShutdown.h"
michael@0 10 #include "mozilla/StaticPtr.h"
michael@0 11 #include "nsComponentManagerUtils.h"
michael@0 12 #include "nsITimer.h"
michael@0 13 #include "nsThreadUtils.h"
michael@0 14
michael@0 15 namespace mozilla {
michael@0 16 namespace hal_impl {
michael@0 17
michael@0 18 static void
michael@0 19 TimerCallbackFunc(nsITimer *aTimer, void *aClosure)
michael@0 20 {
michael@0 21 hal::NotifyAlarmFired();
michael@0 22 }
michael@0 23
michael@0 24 static StaticRefPtr<nsITimer> sTimer;
michael@0 25
michael@0 26 bool
michael@0 27 EnableAlarm()
michael@0 28 {
michael@0 29 static bool initialized = false;
michael@0 30 if (!initialized) {
michael@0 31 initialized = true;
michael@0 32 ClearOnShutdown(&sTimer);
michael@0 33 }
michael@0 34
michael@0 35 nsCOMPtr<nsITimer> timer = do_CreateInstance("@mozilla.org/timer;1");
michael@0 36 sTimer = timer;
michael@0 37 MOZ_ASSERT(sTimer);
michael@0 38 return true;
michael@0 39 }
michael@0 40
michael@0 41 void
michael@0 42 DisableAlarm()
michael@0 43 {
michael@0 44 /*
michael@0 45 * DisableAlarm() may be called after sTimer has been set to null by
michael@0 46 * ClearOnShutdown().
michael@0 47 */
michael@0 48 if (sTimer) {
michael@0 49 sTimer->Cancel();
michael@0 50 }
michael@0 51 }
michael@0 52
michael@0 53 bool
michael@0 54 SetAlarm(int32_t aSeconds, int32_t aNanoseconds)
michael@0 55 {
michael@0 56 if (!sTimer) {
michael@0 57 HAL_LOG(("We should have enabled the alarm"));
michael@0 58 MOZ_ASSERT(false);
michael@0 59 return false;
michael@0 60 }
michael@0 61
michael@0 62 // Do the math to convert aSeconds and aNanoseconds into milliseconds since
michael@0 63 // the epoch.
michael@0 64 int64_t milliseconds = static_cast<int64_t>(aSeconds) * 1000 +
michael@0 65 static_cast<int64_t>(aNanoseconds) / 1000000;
michael@0 66
michael@0 67 // nsITimer expects relative milliseconds.
michael@0 68 int64_t relMilliseconds = milliseconds - PR_Now() / 1000;
michael@0 69
michael@0 70 // If the alarm time is in the past relative to PR_Now(),
michael@0 71 // we choose to immediately fire the alarm. Passing 0 means nsITimer will
michael@0 72 // queue a timeout event immediately.
michael@0 73 sTimer->InitWithFuncCallback(TimerCallbackFunc, nullptr,
michael@0 74 clamped<int64_t>(relMilliseconds, 0, INT32_MAX),
michael@0 75 nsITimer::TYPE_ONE_SHOT);
michael@0 76 return true;
michael@0 77 }
michael@0 78
michael@0 79 } // hal_impl
michael@0 80 } // namespace mozilla

mercurial