transportflow.cpp (2075B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 // Original author: ekr@rtfm.com 8 #include "transportflow.h" 9 10 #include <deque> 11 12 #include "transportlayer.h" 13 14 namespace mozilla { 15 16 NS_IMPL_ISUPPORTS0(TransportFlow) 17 18 // There are some hacks here to allow destruction off of 19 // the main thread. 20 TransportFlow::~TransportFlow() { 21 // Push the destruction onto the STS thread. Note that there 22 // is still some possibility that someone is accessing this 23 // object simultaneously, but as long as smart pointer discipline 24 // is maintained, it shouldn't be possible to access and 25 // destroy it simultaneously. The conversion to a UniquePtr 26 // ensures automatic destruction of the queue at exit of 27 // DestroyFinal. 28 CheckThread(); 29 ClearLayers(layers_.get()); 30 } 31 32 void TransportFlow::DestroyFinal( 33 UniquePtr<std::deque<TransportLayer*>> layers) { 34 ClearLayers(layers.get()); 35 } 36 37 void TransportFlow::ClearLayers(std::deque<TransportLayer*>* layers) { 38 while (!layers->empty()) { 39 delete layers->front(); 40 layers->pop_front(); 41 } 42 } 43 44 void TransportFlow::PushLayer(TransportLayer* layer) { 45 CheckThread(); 46 layers_->push_front(layer); 47 EnsureSameThread(layer); 48 layer->SetFlowId(id_); 49 } 50 51 TransportLayer* TransportFlow::GetLayer(const std::string& id) const { 52 CheckThread(); 53 54 if (layers_) { 55 for (TransportLayer* layer : *layers_) { 56 if (layer->id() == id) return layer; 57 } 58 } 59 60 return nullptr; 61 } 62 63 void TransportFlow::EnsureSameThread(TransportLayer* layer) { 64 // Enforce that if any of the layers have a thread binding, 65 // they all have the same binding. 66 if (target_) { 67 const nsCOMPtr<nsIEventTarget>& lthread = layer->GetThread(); 68 69 if (lthread && (lthread != target_)) MOZ_CRASH(); 70 } else { 71 target_ = layer->GetThread(); 72 } 73 } 74 75 } // namespace mozilla