1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/hal/fallback/FallbackAlarm.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,80 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include "Hal.h" 1.9 + 1.10 +#include <algorithm> 1.11 + 1.12 +#include "mozilla/ClearOnShutdown.h" 1.13 +#include "mozilla/StaticPtr.h" 1.14 +#include "nsComponentManagerUtils.h" 1.15 +#include "nsITimer.h" 1.16 +#include "nsThreadUtils.h" 1.17 + 1.18 +namespace mozilla { 1.19 +namespace hal_impl { 1.20 + 1.21 +static void 1.22 +TimerCallbackFunc(nsITimer *aTimer, void *aClosure) 1.23 +{ 1.24 + hal::NotifyAlarmFired(); 1.25 +} 1.26 + 1.27 +static StaticRefPtr<nsITimer> sTimer; 1.28 + 1.29 +bool 1.30 +EnableAlarm() 1.31 +{ 1.32 + static bool initialized = false; 1.33 + if (!initialized) { 1.34 + initialized = true; 1.35 + ClearOnShutdown(&sTimer); 1.36 + } 1.37 + 1.38 + nsCOMPtr<nsITimer> timer = do_CreateInstance("@mozilla.org/timer;1"); 1.39 + sTimer = timer; 1.40 + MOZ_ASSERT(sTimer); 1.41 + return true; 1.42 +} 1.43 + 1.44 +void 1.45 +DisableAlarm() 1.46 +{ 1.47 + /* 1.48 + * DisableAlarm() may be called after sTimer has been set to null by 1.49 + * ClearOnShutdown(). 1.50 + */ 1.51 + if (sTimer) { 1.52 + sTimer->Cancel(); 1.53 + } 1.54 +} 1.55 + 1.56 +bool 1.57 +SetAlarm(int32_t aSeconds, int32_t aNanoseconds) 1.58 +{ 1.59 + if (!sTimer) { 1.60 + HAL_LOG(("We should have enabled the alarm")); 1.61 + MOZ_ASSERT(false); 1.62 + return false; 1.63 + } 1.64 + 1.65 + // Do the math to convert aSeconds and aNanoseconds into milliseconds since 1.66 + // the epoch. 1.67 + int64_t milliseconds = static_cast<int64_t>(aSeconds) * 1000 + 1.68 + static_cast<int64_t>(aNanoseconds) / 1000000; 1.69 + 1.70 + // nsITimer expects relative milliseconds. 1.71 + int64_t relMilliseconds = milliseconds - PR_Now() / 1000; 1.72 + 1.73 + // If the alarm time is in the past relative to PR_Now(), 1.74 + // we choose to immediately fire the alarm. Passing 0 means nsITimer will 1.75 + // queue a timeout event immediately. 1.76 + sTimer->InitWithFuncCallback(TimerCallbackFunc, nullptr, 1.77 + clamped<int64_t>(relMilliseconds, 0, INT32_MAX), 1.78 + nsITimer::TYPE_ONE_SHOT); 1.79 + return true; 1.80 +} 1.81 + 1.82 +} // hal_impl 1.83 +} // namespace mozilla