CIRCT  19.0.0git
Ports.cpp
Go to the documentation of this file.
1 //===- Ports.cpp - ESI communication channels -------------------------===//
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 // DO NOT EDIT!
10 // This file is distributed as part of an ESI package. The source for this file
11 // should always be modified within CIRCT (lib/dialect/ESI/runtime/cpp/).
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "esi/Ports.h"
16 
17 #include <chrono>
18 #include <stdexcept>
19 
20 using namespace std;
21 using namespace esi;
22 
23 BundlePort::BundlePort(AppID id, map<string, ChannelPort &> channels)
24  : id(id), channels(channels) {}
25 
26 WriteChannelPort &BundlePort::getRawWrite(const string &name) const {
27  auto f = channels.find(name);
28  if (f == channels.end())
29  throw runtime_error("Channel '" + name + "' not found");
30  auto *write = dynamic_cast<WriteChannelPort *>(&f->second);
31  if (!write)
32  throw runtime_error("Channel '" + name + "' is not a write channel");
33  return *write;
34 }
35 
36 ReadChannelPort &BundlePort::getRawRead(const string &name) const {
37  auto f = channels.find(name);
38  if (f == channels.end())
39  throw runtime_error("Channel '" + name + "' not found");
40  auto *read = dynamic_cast<ReadChannelPort *>(&f->second);
41  if (!read)
42  throw runtime_error("Channel '" + name + "' is not a read channel");
43  return *read;
44 }
45 
46 std::future<MessageData> ReadChannelPort::readAsync() {
47  // TODO: running this deferred is a horrible idea considering that it blocks!
48  // It's a hack since Capnp RPC refuses to work with multiple threads.
49  return std::async(std::launch::deferred, [this]() {
50  MessageData output;
51  while (!read(output)) {
52  std::this_thread::sleep_for(std::chrono::milliseconds(1));
53  }
54  return output;
55  });
56 }
std::map< std::string, ChannelPort & > channels
Definition: Ports.h:101
ReadChannelPort & getRawRead(const std::string &name) const
Definition: Ports.cpp:36
WriteChannelPort & getRawWrite(const std::string &name) const
Get access to the raw byte streams of a channel.
Definition: Ports.cpp:26
A logical chunk of data representing serialized data.
Definition: Common.h:85
A ChannelPort which reads data from the accelerator.
Definition: Ports.h:55
virtual std::future< MessageData > readAsync()
Asynchronous read.
Definition: Ports.cpp:46
virtual bool read(MessageData &)=0
Specify a buffer to read into.
A ChannelPort which sends data to the accelerator.
Definition: Ports.h:46
Definition: esi.py:1