CIRCT  19.0.0git
Utils.h
Go to the documentation of this file.
1 //===- Utils.h - utility code for cosim -------------------------*- 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 #ifndef COSIM_UTILS_H
10 #define COSIM_UTILS_H
11 
12 #include <mutex>
13 #include <optional>
14 #include <queue>
15 
16 namespace esi {
17 namespace cosim {
18 
19 /// Thread safe queue. Just wraps std::queue protected with a lock.
20 template <typename T>
21 class TSQueue {
22  using Lock = std::lock_guard<std::mutex>;
23 
24  std::mutex m;
25  std::queue<T> q;
26 
27 public:
28  /// Push onto the queue.
29  template <typename... E>
30  void push(E... t) {
31  Lock l(m);
32  q.emplace(t...);
33  }
34 
35  /// Pop something off the queue but return nullopt if the queue is empty. Why
36  /// doesn't std::queue have anything like this?
37  std::optional<T> pop() {
38  Lock l(m);
39  if (q.size() == 0)
40  return std::nullopt;
41  auto t = q.front();
42  q.pop();
43  return t;
44  }
45 };
46 
47 } // namespace cosim
48 } // namespace esi
49 
50 #endif
Thread safe queue. Just wraps std::queue protected with a lock.
Definition: Utils.h:21
std::queue< T > q
Definition: Utils.h:25
std::lock_guard< std::mutex > Lock
Definition: Utils.h:22
void push(E... t)
Push onto the queue.
Definition: Utils.h:30
std::mutex m
Definition: Utils.h:24
std::optional< T > pop()
Pop something off the queue but return nullopt if the queue is empty.
Definition: Utils.h:37
Definition: esi.py:1