CIRCT  19.0.0git
Endpoint.cpp
Go to the documentation of this file.
1 //===- EndPoint.cpp - Definitions for EndPointRegistry ----------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Definitions for Cosim EndPoint and EndPointRegistry.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "cosim/Endpoint.h"
14 
15 using namespace esi::cosim;
16 
17 Endpoint::Endpoint(std::string fromHostTypeId, std::string toHostTypeId)
18  : fromHostTypeId(fromHostTypeId), toHostTypeId(toHostTypeId), inUse(false) {
19 }
21 
23  Lock g(m);
24  if (inUse)
25  return false;
26  inUse = true;
27  return true;
28 }
29 
31  Lock g(m);
32  if (!inUse)
33  fprintf(stderr, "Warning: Returning an endpoint which was not in use.\n");
34  inUse = false;
35 }
36 
37 bool EndpointRegistry::registerEndpoint(std::string epId,
38  std::string fromHostTypeId,
39  std::string toHostTypeId) {
40  Lock g(m);
41  if (endpoints.find(epId) != endpoints.end()) {
42  fprintf(stderr, "Endpoint ID already exists!\n");
43  return false;
44  }
45  // The following ugliness adds an Endpoint to the map of Endpoints. The
46  // Endpoint class has its copy constructor deleted, thus the metaprogramming.
47  endpoints.emplace(std::piecewise_construct,
48  // Map key.
49  std::forward_as_tuple(epId),
50  // Endpoint constructor args.
51  std::forward_as_tuple(fromHostTypeId, toHostTypeId));
52  return true;
53 }
54 
56  const std::function<void(std::string, const Endpoint &)> &f) const {
57  // This function is logically const, but modification is needed to obtain a
58  // lock.
59  Lock g(const_cast<EndpointRegistry *>(this)->m);
60  for (const auto &ep : endpoints) {
61  f(ep.first, ep.second);
62  }
63 }
64 
65 size_t EndpointRegistry::size() const {
66  // This function is logically const, but modification is needed to obtain a
67  // lock.
68  Lock g(const_cast<EndpointRegistry *>(this)->m);
69  return endpoints.size();
70 }
The Endpoint registry is where Endpoints report their existence (register) and they are looked up by ...
Definition: Endpoint.h:110
bool registerEndpoint(std::string epId, std::string fromHostTypeId, std::string toHostTypeId)
Register an Endpoint.
Definition: Endpoint.cpp:37
std::mutex m
This object needs to be thread-safe. An object-wide mutex is sufficient.
Definition: Endpoint.h:140
void iterateEndpoints(const std::function< void(std::string id, const Endpoint &)> &f) const
Iterate over the list of endpoints, calling the provided function for each endpoint.
Definition: Endpoint.cpp:55
size_t size() const
Return the number of endpoints.
Definition: Endpoint.cpp:65
std::lock_guard< std::mutex > Lock
Definition: Endpoint.h:137
std::map< std::string, Endpoint > endpoints
Endpoint ID to object pointer mapping.
Definition: Endpoint.h:143
Implements a bi-directional, thread-safe bridge between the RPC server and DPI functions.
Definition: Endpoint.h:35
bool setInUse()
These two are used to set and unset the inUse flag, to ensure that an open endpoint is not opened aga...
Definition: Endpoint.cpp:22
std::lock_guard< std::mutex > Lock
Definition: Endpoint.h:96
std::mutex m
This class needs to be thread-safe.
Definition: Endpoint.h:101
Endpoint(std::string fromHostTypeId, std::string toHostTypeId)
Construct an endpoint which knows and the type IDs in both directions.
Definition: Endpoint.cpp:17