1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
9 #include "mozilla/ClearOnShutdown.h"
10 #include "mozilla/StaticPtr.h"
11 #include "nsComponentManagerUtils.h"
13 #include "nsThreadUtils.h"
19 TimerCallbackFunc(nsITimer
*aTimer
, void *aClosure
)
21 hal::NotifyAlarmFired();
24 static StaticRefPtr
<nsITimer
> sTimer
;
29 static bool initialized
= false;
32 ClearOnShutdown(&sTimer
);
35 nsCOMPtr
<nsITimer
> timer
= do_CreateInstance("@mozilla.org/timer;1");
45 * DisableAlarm() may be called after sTimer has been set to null by
54 SetAlarm(int32_t aSeconds
, int32_t aNanoseconds
)
57 HAL_LOG(("We should have enabled the alarm"));
62 // Do the math to convert aSeconds and aNanoseconds into milliseconds since
64 int64_t milliseconds
= static_cast<int64_t>(aSeconds
) * 1000 +
65 static_cast<int64_t>(aNanoseconds
) / 1000000;
67 // nsITimer expects relative milliseconds.
68 int64_t relMilliseconds
= milliseconds
- PR_Now() / 1000;
70 // If the alarm time is in the past relative to PR_Now(),
71 // we choose to immediately fire the alarm. Passing 0 means nsITimer will
72 // queue a timeout event immediately.
73 sTimer
->InitWithFuncCallback(TimerCallbackFunc
, nullptr,
74 clamped
<int64_t>(relMilliseconds
, 0, INT32_MAX
),
75 nsITimer::TYPE_ONE_SHOT
);
80 } // namespace mozilla