2012-10-03 00:04:58 +04:00
|
|
|
/* -*- 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/. */
|
|
|
|
|
|
|
|
// Original author: ekr@rtfm.com
|
|
|
|
|
|
|
|
#ifndef transportflow_h__
|
|
|
|
#define transportflow_h__
|
|
|
|
|
|
|
|
#include <deque>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "nscore.h"
|
|
|
|
#include "nsISupportsImpl.h"
|
2016-03-02 23:28:27 +03:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2012-10-03 00:04:58 +04:00
|
|
|
#include "transportlayer.h"
|
|
|
|
#include "m_cpp_utils.h"
|
|
|
|
|
|
|
|
// A stack of transport layers acts as a flow.
|
|
|
|
// Generally, one reads and writes to the top layer.
|
2013-02-19 15:43:34 +04:00
|
|
|
|
|
|
|
// This code has a confusing hybrid threading model which
|
|
|
|
// probably needs some eventual refactoring.
|
|
|
|
// TODO(ekr@rtfm.com): Bug 844891
|
|
|
|
//
|
|
|
|
// TransportFlows are not inherently bound to a thread *but*
|
|
|
|
// TransportLayers can be. If any layer in a flow is bound
|
|
|
|
// to a given thread, then all layers in the flow MUST be
|
|
|
|
// bound to that thread and you can only manipulate the
|
|
|
|
// flow (push layers, write, etc.) on that thread.
|
|
|
|
//
|
|
|
|
// The sole official exception to this is that you are
|
|
|
|
// allowed to *destroy* a flow off the bound thread provided
|
|
|
|
// that there are no listeners on its signals. This exception
|
|
|
|
// is designed to allow idioms where you create the flow
|
|
|
|
// and then something goes wrong and you destroy it and
|
|
|
|
// you don't want to bother with a thread dispatch.
|
|
|
|
//
|
|
|
|
// Eventually we hope to relax the "no listeners"
|
|
|
|
// restriction by thread-locking the signals, but previous
|
|
|
|
// attempts have caused deadlocks.
|
|
|
|
//
|
|
|
|
// Most of these invariants are enforced by hard asserts
|
|
|
|
// (i.e., those which fire even in production builds).
|
|
|
|
|
2012-10-03 00:04:58 +04:00
|
|
|
namespace mozilla {
|
|
|
|
|
2018-04-27 20:14:40 +03:00
|
|
|
class TransportFlow final : public nsISupports {
|
2012-10-03 00:04:58 +04:00
|
|
|
public:
|
2013-03-04 11:10:30 +04:00
|
|
|
TransportFlow()
|
2013-02-19 15:43:34 +04:00
|
|
|
: id_("(anonymous)"), layers_(new std::deque<TransportLayer*>) {}
|
2014-08-16 01:41:29 +04:00
|
|
|
explicit TransportFlow(const std::string id)
|
2013-02-19 15:43:34 +04:00
|
|
|
: id_(id), layers_(new std::deque<TransportLayer*>) {}
|
2013-03-04 11:10:30 +04:00
|
|
|
|
2012-10-03 00:04:58 +04:00
|
|
|
const std::string& id() const { return id_; }
|
|
|
|
|
|
|
|
// Layer management. Note PushLayer() is not thread protected, so
|
|
|
|
// either:
|
|
|
|
// (a) Do it in the thread handling the I/O
|
|
|
|
// (b) Do it before you activate the I/O system
|
2012-10-19 00:01:52 +04:00
|
|
|
//
|
|
|
|
// The flow takes ownership of the layers after a successful
|
|
|
|
// push.
|
2018-04-28 00:57:28 +03:00
|
|
|
void PushLayer(TransportLayer* layer);
|
2012-10-19 00:01:52 +04:00
|
|
|
|
2012-10-03 00:04:58 +04:00
|
|
|
TransportLayer* GetLayer(const std::string& id) const;
|
|
|
|
|
2014-03-15 23:00:17 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
private:
|
2014-07-31 21:05:58 +04:00
|
|
|
~TransportFlow();
|
|
|
|
|
2012-10-03 00:04:58 +04:00
|
|
|
DISALLOW_COPY_ASSIGN(TransportFlow);
|
|
|
|
|
2013-02-19 15:43:34 +04:00
|
|
|
// Check if we are on the right thread
|
|
|
|
void CheckThread() const {
|
|
|
|
if (!CheckThreadInt()) MOZ_CRASH();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CheckThreadInt() const {
|
|
|
|
bool on;
|
|
|
|
|
|
|
|
if (!target_) // OK if no thread set.
|
|
|
|
return true;
|
|
|
|
if (NS_FAILED(target_->IsOnCurrentThread(&on))) return false;
|
|
|
|
|
|
|
|
return on;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EnsureSameThread(TransportLayer* layer);
|
|
|
|
|
2020-02-22 01:43:53 +03:00
|
|
|
static void DestroyFinal(UniquePtr<std::deque<TransportLayer*>> layers);
|
2013-02-19 15:43:34 +04:00
|
|
|
|
|
|
|
// Overload needed because we use deque internally and queue externally.
|
|
|
|
static void ClearLayers(std::deque<TransportLayer*>* layers);
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
std::string id_;
|
2016-03-02 23:28:27 +03:00
|
|
|
UniquePtr<std::deque<TransportLayer*>> layers_;
|
2013-02-19 15:43:34 +04:00
|
|
|
nsCOMPtr<nsIEventTarget> target_;
|
2012-10-03 00:04:58 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
#endif
|