OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "services/blamer/public/cpp/blamer_interface.h" |
| 6 |
| 7 #include <atomic> |
| 8 |
| 9 #include "services/blamer/public/interfaces/service_constants.mojom.h" |
| 10 #include "services/blamer/shared_memory_heap.h" |
| 11 |
| 12 namespace blamer { |
| 13 |
| 14 namespace { |
| 15 |
| 16 void OnConnectionError() { |
| 17 // The service should outlast child processes. |
| 18 NOTREACHED(); |
| 19 } |
| 20 |
| 21 // Used to indicate that the right to create the singleton instance has been |
| 22 // acquired. |
| 23 BlamerInterface* kInstanceLockSentinel = reinterpret_cast<BlamerInterface*>(1); |
| 24 |
| 25 std::atomic<BlamerInterface*> g_instance(nullptr); |
| 26 |
| 27 } // namespace |
| 28 |
| 29 BlamerInterface::~BlamerInterface() = default; |
| 30 |
| 31 BlamerInterface* BlamerInterface::CreateForProcess( |
| 32 service_manager::Connector* connector) { |
| 33 BlamerInterface* instance = g_instance.load(); |
| 34 if (instance != nullptr && instance != kInstanceLockSentinel) |
| 35 return instance; |
| 36 |
| 37 // Try to acquire the right to create the singleton. |
| 38 if (instance == nullptr && |
| 39 g_instance.compare_exchange_strong(instance, kInstanceLockSentinel)) { |
| 40 instance = new BlamerInterface(connector); |
| 41 BlamerInterface* expected = kInstanceLockSentinel; |
| 42 CHECK(g_instance.compare_exchange_strong(expected, instance)); |
| 43 return instance; |
| 44 } |
| 45 |
| 46 // Getting here means somebody else has the right to create the singleton. |
| 47 // Wait until it's available. |
| 48 while (true) { |
| 49 instance = g_instance.load(); |
| 50 if (instance != nullptr && instance != kInstanceLockSentinel) |
| 51 return instance; |
| 52 } |
| 53 } |
| 54 |
| 55 BlamerInterface* BlamerInterface::GetForProcess() { |
| 56 BlamerInterface* instance = g_instance.load(); |
| 57 if (instance == kInstanceLockSentinel) |
| 58 return nullptr; |
| 59 return instance; |
| 60 } |
| 61 |
| 62 BlamerInterface::BlamerInterface(service_manager::Connector* connector) |
| 63 : shared_memory_heap_(nullptr) { |
| 64 mojom::SharedMemoryHeapRegistryPtr heap_registry; |
| 65 connector->BindInterface(mojom::kServiceName, |
| 66 mojo::MakeRequest(&heap_registry)); |
| 67 heap_registry.set_connection_error_handler(base::Bind( |
| 68 &OnConnectionError)); |
| 69 |
| 70 // Create a shared memory heap for this process. |
| 71 shared_memory_heap_.reset(new SharedMemoryHeap(std::move(heap_registry))); |
| 72 } |
| 73 |
| 74 } // namespace blamer |
OLD | NEW |