LLVM  4.0.0
RawByteChannel.h
Go to the documentation of this file.
1 //===- llvm/ExecutionEngine/Orc/RawByteChannel.h ----------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_EXECUTIONENGINE_ORC_RAWBYTECHANNEL_H
11 #define LLVM_EXECUTIONENGINE_ORC_RAWBYTECHANNEL_H
12 
13 #include "OrcError.h"
14 #include "RPCSerialization.h"
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Support/Endian.h"
19 #include "llvm/Support/Error.h"
20 #include <cstddef>
21 #include <cstdint>
22 #include <mutex>
23 #include <string>
24 #include <tuple>
25 #include <type_traits>
26 #include <vector>
27 
28 namespace llvm {
29 namespace orc {
30 namespace rpc {
31 
32 /// Interface for byte-streams to be used with RPC.
34 public:
35  virtual ~RawByteChannel() {}
36 
37  /// Read Size bytes from the stream into *Dst.
38  virtual Error readBytes(char *Dst, unsigned Size) = 0;
39 
40  /// Read size bytes from *Src and append them to the stream.
41  virtual Error appendBytes(const char *Src, unsigned Size) = 0;
42 
43  /// Flush the stream if possible.
44  virtual Error send() = 0;
45 
46  /// Notify the channel that we're starting a message send.
47  /// Locks the channel for writing.
48  template <typename FunctionIdT, typename SequenceIdT>
49  Error startSendMessage(const FunctionIdT &FnId, const SequenceIdT &SeqNo) {
50  writeLock.lock();
51  return serializeSeq(*this, FnId, SeqNo);
52  }
53 
54  /// Notify the channel that we're ending a message send.
55  /// Unlocks the channel for writing.
57  writeLock.unlock();
58  return Error::success();
59  }
60 
61  /// Notify the channel that we're starting a message receive.
62  /// Locks the channel for reading.
63  template <typename FunctionIdT, typename SequenceNumberT>
64  Error startReceiveMessage(FunctionIdT &FnId, SequenceNumberT &SeqNo) {
65  readLock.lock();
66  return deserializeSeq(*this, FnId, SeqNo);
67  }
68 
69  /// Notify the channel that we're ending a message receive.
70  /// Unlocks the channel for reading.
72  readLock.unlock();
73  return Error::success();
74  }
75 
76  /// Get the lock for stream reading.
77  std::mutex &getReadLock() { return readLock; }
78 
79  /// Get the lock for stream writing.
80  std::mutex &getWriteLock() { return writeLock; }
81 
82 private:
83  std::mutex readLock, writeLock;
84 };
85 
86 template <typename ChannelT, typename T>
88  ChannelT, T, T,
89  typename std::enable_if<
90  std::is_base_of<RawByteChannel, ChannelT>::value &&
91  (std::is_same<T, uint8_t>::value || std::is_same<T, int8_t>::value ||
92  std::is_same<T, uint16_t>::value || std::is_same<T, int16_t>::value ||
93  std::is_same<T, uint32_t>::value || std::is_same<T, int32_t>::value ||
94  std::is_same<T, uint64_t>::value || std::is_same<T, int64_t>::value ||
95  std::is_same<T, char>::value)>::type> {
96 public:
97  static Error serialize(ChannelT &C, T V) {
98  support::endian::byte_swap<T, support::big>(V);
99  return C.appendBytes(reinterpret_cast<const char *>(&V), sizeof(T));
100  };
101 
102  static Error deserialize(ChannelT &C, T &V) {
103  if (auto Err = C.readBytes(reinterpret_cast<char *>(&V), sizeof(T)))
104  return Err;
105  support::endian::byte_swap<T, support::big>(V);
106  return Error::success();
107  };
108 };
109 
110 template <typename ChannelT>
111 class SerializationTraits<ChannelT, bool, bool,
112  typename std::enable_if<std::is_base_of<
113  RawByteChannel, ChannelT>::value>::type> {
114 public:
115  static Error serialize(ChannelT &C, bool V) {
116  return C.appendBytes(reinterpret_cast<const char *>(&V), 1);
117  }
118 
119  static Error deserialize(ChannelT &C, bool &V) {
120  return C.readBytes(reinterpret_cast<char *>(&V), 1);
121  }
122 };
123 
124 template <typename ChannelT>
125 class SerializationTraits<ChannelT, std::string, StringRef,
126  typename std::enable_if<std::is_base_of<
127  RawByteChannel, ChannelT>::value>::type> {
128 public:
129  /// RPC channel serialization for std::strings.
131  if (auto Err = serializeSeq(C, static_cast<uint64_t>(S.size())))
132  return Err;
133  return C.appendBytes((const char *)S.data(), S.size());
134  }
135 };
136 
137 template <typename ChannelT>
138 class SerializationTraits<ChannelT, std::string, const char *,
139  typename std::enable_if<std::is_base_of<
140  RawByteChannel, ChannelT>::value>::type> {
141 public:
142  static Error serialize(RawByteChannel &C, const char *S) {
144  S);
145  }
146 };
147 
148 template <typename ChannelT>
149 class SerializationTraits<ChannelT, std::string, std::string,
150  typename std::enable_if<std::is_base_of<
151  RawByteChannel, ChannelT>::value>::type> {
152 public:
153  /// RPC channel serialization for std::strings.
154  static Error serialize(RawByteChannel &C, const std::string &S) {
156  S);
157  }
158 
159  /// RPC channel deserialization for std::strings.
160  static Error deserialize(RawByteChannel &C, std::string &S) {
161  uint64_t Count = 0;
162  if (auto Err = deserializeSeq(C, Count))
163  return Err;
164  S.resize(Count);
165  return C.readBytes(&S[0], Count);
166  }
167 };
168 
169 } // end namespace rpc
170 } // end namespace orc
171 } // end namespace llvm
172 
173 #endif // LLVM_EXECUTIONENGINE_ORC_RAWBYTECHANNEL_H
std::mutex & getReadLock()
Get the lock for stream reading.
Error startReceiveMessage(FunctionIdT &FnId, SequenceNumberT &SeqNo)
Notify the channel that we're starting a message receive.
Interface for byte-streams to be used with RPC.
Error startSendMessage(const FunctionIdT &FnId, const SequenceIdT &SeqNo)
Notify the channel that we're starting a message send.
virtual Error send()=0
Flush the stream if possible.
Error endSendMessage()
Notify the channel that we're ending a message send.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:135
virtual Error readBytes(char *Dst, unsigned Size)=0
Read Size bytes from the stream into *Dst.
The SerializationTraits<ChannelT, T> class describes how to serialize and deserialize an instance of ...
static ErrorSuccess success()
Create a success value.
Error endReceiveMessage()
Notify the channel that we're ending a message receive.
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
Error deserializeSeq(ChannelT &C, ArgTs &...Args)
virtual Error appendBytes(const char *Src, unsigned Size)=0
Read size bytes from *Src and append them to the stream.
Error serializeSeq(ChannelT &C, const ArgTs &...Args)
std::mutex & getWriteLock()
Get the lock for stream writing.
aarch64 promote const
Lightweight error class with error context and mandatory checking.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:125
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47