Files
palemoon27/media/mtransport/nr_timer.cpp
T
roytam1 b383d6bc2f import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1161101 - Use JS_GetOwnPropertyDescriptor instead of GetPropertyDescriptor in dom/bindings. r=bz (918d7961c)
- Bug 1119387 part 5: Update docker images. (0d47fde65)
- Bug 1144463 - Add dolphin-512 and update docker. r=jlal,wcosta (aa6a55cab)
- Bug 1134226: Add lightsaber nightly builds. (7926eff96)
- Bug 1151981 - Properly check for MSVC (mingw fixup). (d95e17266)
- Bug 1154947 part 3: Add aries nightly user, userdebug and eng builds. (45e096ed6)
- Bug 1178932 - Implement Reflect.construct. r=Waldo. (4c5f0e72e)
- Bug 1131206 - Remove the less useful commands from taskcluster mach r=garndt (e4c8ed78a)
- Bug 1131206 - Select mozharness version from in tree r=garndt (fdab5140d)
- bug 1156816 - Fix scopes for aws-provisioner. Switch nightly builds to production balrog. r=garndt (561d5cd58)
- Bug 1166073 - change docker registry to hub r=garndt (17faea355)
- Bug 1166233: Bring taskcluster-build mach command back. (c0a719c6b)
- Bug 1142779 - Add buildbot steps for cloning and running tests r=lightsofapollo (304235375)
- Bug 1142779 - Update job tasks to include functional unit and dual sim r=lightsofapollo (09b320602)
- Bug 1147605 - Disable tests on phone builds r=me CLOSED TREE (0912de6ba)
- Bug 1142779 - Update job tasks to use new chunking logic r=lightsofapollo (70c531920)
- Bug 1144994 part 1: Update provisioner. (2447affa4)
- Bug 1144994 part 2: Switch aries nightlies from cypress to m-c. (3019a6878)
- Bug 1164939: Provide flame-kk user, userdebug and eng builds. (ab7ad22a7)
- goanna->gecko (70b49bb18)
- Bug 1166745: Reorganize tasks. (d3090e0a9)
- Bug 1170378: Create tasks timestamps in UTC. r=jonasfj a=jonasfj (60160f061)
- Bug 987902 - Add a "doctor" mach command; r=gps (be60e7df6)
- Bug 985857 - Automatically log mach output and add a command to display it. r=gps (90993b77f)
- Bug 991983 - Add a ContextDerivedTypedListWithItems type. r=gps (ad9482c7b)
- Bug 991983 - Don't pass template paths to contexts. r=gps (30a4f2038)
- Bug 991983 - Refactor SourcePath handling for moz.build, add new classes and extensive tests. r=gps (00aeb401b)
- Bug 1142494 - Only package the steeplechase tests if webrtc is enabled. r=ted, f=drno (760943034)
- Bug 1142494 - Fix OSX packaging mistake. r=glandium, a=bustage (03cd3ca35)
- Bug 1147029 - Land luciddream in-tree, r=ted (8a4d2651b)
- Bug 1147031 - Write mach command for luciddream. r=jgriffin (b913c4df4)
- Bug 1171446 - Add a description to the luciddream mach target r=ochameau (c807799b6)
- Bug 1176642 - Remove unused imports; r=glandium (b0e458f5d)
- Bug 1151080: Rewrite NR_async_timer_set(0) to use direct dispatch. r=mt (6c3dda54d)
- Bug 1152185 - Include port/generic/include also for mtransport/test and webrtc/signaling/test. r=ekr (969ce4205)
- Bug 1156621 - Don't assume --without-system-nspr. r=glandium (4cf9c3e76)
- Bug 1035468: A NAT simulator based on NrSocket, and integrate into ice_unittest. r=ekr (903d8f6c8)
- Bug 1162026 - move WrapRunnable &co over to variadic templates; r=ekr (6224de8e9)
- Bug 1163328 - Add a Tuple class to MFBT. r=froydnj (ba3276ef3)
- Bug 1163328 - Add an And<...> class to TemplateLib.h which performs logical and on a variadic number of booleans known at compile time. r=froydnj (ac3afcabd)
- Bug 1175621 - make WrapRunnable* more efficient by utilizing moves in wrapper functions; r=ekr (15cf9f55a)
2020-11-27 09:26:17 +08:00

261 lines
7.4 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
// Original code by: ekr@rtfm.com
// Implementation of the NR timer interface
// Some code here copied from nrappkit. The license was.
/**
Copyright (C) 2004, Network Resonance, Inc.
Copyright (C) 2006, Network Resonance, Inc.
All Rights Reserved
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of Network Resonance, Inc. nor the name of any
contributors to this software may be used to endorse or promote
products derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
ekr@rtfm.com Sun Feb 22 19:35:24 2004
*/
#include <string>
#include "nsCOMPtr.h"
#include "nsComponentManagerUtils.h"
#include "nsServiceManagerUtils.h"
#include "nsIEventTarget.h"
#include "nsITimer.h"
#include "nsNetCID.h"
#include "runnable_utils.h"
extern "C" {
#include "nr_api.h"
#include "async_timer.h"
}
namespace mozilla {
class nrappkitCallback {
public:
nrappkitCallback(NR_async_cb cb, void *cb_arg,
const char *function, int line)
: cb_(cb), cb_arg_(cb_arg), function_(function), line_(line) {
}
virtual ~nrappkitCallback() {}
virtual void Cancel() = 0;
protected:
/* additional members */
NR_async_cb cb_;
void *cb_arg_;
std::string function_;
int line_;
};
class nrappkitTimerCallback : public nrappkitCallback,
public nsITimerCallback {
public:
// We're going to release ourself in the callback, so we need to be threadsafe
NS_DECL_THREADSAFE_ISUPPORTS
NS_DECL_NSITIMERCALLBACK
nrappkitTimerCallback(NR_async_cb cb, void *cb_arg,
const char *function, int line,
nsITimer *timer)
: nrappkitCallback(cb, cb_arg, function, line),
timer_(timer) {}
virtual void Cancel() override {
AddRef(); // Cancelling the timer causes the callback it holds to
// be released. AddRef() keeps us alive.
timer_->Cancel();
timer_->Release();
Release(); // Will cause deletion of this object.
}
private:
nsITimer* timer_;
virtual ~nrappkitTimerCallback() {}
};
NS_IMPL_ISUPPORTS(nrappkitTimerCallback, nsITimerCallback)
NS_IMETHODIMP nrappkitTimerCallback::Notify(nsITimer *timer) {
r_log(LOG_GENERIC, LOG_DEBUG, "Timer callback fired (set in %s:%d)",
function_.c_str(), line_);
MOZ_ASSERT(timer == timer_);
cb_(0, 0, cb_arg_);
// Allow the timer to go away.
timer->Release();
return NS_OK;
}
class nrappkitScheduledCallback : public nrappkitCallback {
public:
nrappkitScheduledCallback(NR_async_cb cb, void *cb_arg,
const char *function, int line)
: nrappkitCallback(cb, cb_arg, function, line) {}
void Run() {
if (cb_) {
cb_(0, 0, cb_arg_);
}
}
virtual void Cancel() override {
cb_ = nullptr;
}
~nrappkitScheduledCallback() {}
};
} // close namespace
using namespace mozilla;
static nsCOMPtr<nsIEventTarget> GetSTSThread() {
nsresult rv;
nsCOMPtr<nsIEventTarget> sts_thread;
sts_thread = do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &rv);
MOZ_ASSERT(NS_SUCCEEDED(rv));
return sts_thread;
}
// These timers must only be used from the STS thread.
// This function is a helper that enforces that.
static void CheckSTSThread() {
nsCOMPtr<nsIEventTarget> sts_thread = GetSTSThread();
ASSERT_ON_THREAD(sts_thread);
}
static int nr_async_timer_set_zero(NR_async_cb cb, void *arg,
char *func, int l,
nrappkitCallback **handle) {
nrappkitScheduledCallback* callback(new nrappkitScheduledCallback(
cb, arg, func, l));
nsresult rv = GetSTSThread()->Dispatch(WrapRunnable(
nsAutoPtr<nrappkitScheduledCallback>(callback),
&nrappkitScheduledCallback::Run),
NS_DISPATCH_NORMAL);
if (NS_FAILED(rv))
return R_FAILED;
*handle = callback;
// On exit to this function, the only strong reference to callback is in
// the Runnable. Because we are redispatching to the same thread,
// this is always safe.
return 0;
}
static int nr_async_timer_set_nonzero(int timeout, NR_async_cb cb, void *arg,
char *func, int l,
nrappkitCallback **handle) {
nsresult rv;
CheckSTSThread();
nsCOMPtr<nsITimer> timer = do_CreateInstance(NS_TIMER_CONTRACTID, &rv);
if (NS_FAILED(rv)) {
return(R_FAILED);
}
nrappkitTimerCallback* callback =
new nrappkitTimerCallback(cb, arg, func, l, timer);
rv = timer->InitWithCallback(callback, timeout, nsITimer::TYPE_ONE_SHOT);
if (NS_FAILED(rv)) {
return R_FAILED;
}
// We need an AddRef here to keep the timer alive, per the spec.
timer->AddRef();
*handle = callback;
return 0;
}
int NR_async_timer_set(int timeout, NR_async_cb cb, void *arg,
char *func, int l, void **handle) {
CheckSTSThread();
nrappkitCallback *callback;
int r;
if (!timeout) {
r = nr_async_timer_set_zero(cb, arg, func, l, &callback);
} else {
r = nr_async_timer_set_nonzero(timeout, cb, arg, func, l, &callback);
}
if (r)
return r;
if (handle)
*handle = callback;
return 0;
}
int NR_async_schedule(NR_async_cb cb, void *arg, char *func, int l) {
// No need to check the thread because we check it next in the
// timer set.
return NR_async_timer_set(0, cb, arg, func, l, nullptr);
}
int NR_async_timer_cancel(void *handle) {
// Check for the handle being nonzero because sometimes we get
// no-op cancels that aren't on the STS thread. This can be
// non-racy as long as the upper-level code is careful.
if (!handle)
return 0;
CheckSTSThread();
nrappkitCallback* callback = static_cast<nrappkitCallback *>(handle);
callback->Cancel();
return 0;
}