|
| 1 | +#ifdef YOSYS_ENABLE_THREADS |
| 2 | +#include <condition_variable> |
| 3 | +#include <deque> |
| 4 | +#include <mutex> |
| 5 | +#include <thread> |
| 6 | +#endif |
| 7 | + |
| 8 | +#include "kernel/yosys_common.h" |
| 9 | +#include "kernel/log.h" |
| 10 | + |
| 11 | +#ifndef YOSYS_THREADING_H |
| 12 | +#define YOSYS_THREADING_H |
| 13 | + |
| 14 | +YOSYS_NAMESPACE_BEGIN |
| 15 | + |
| 16 | +// Concurrent queue implementation. Not fast, but simple. |
| 17 | +// Multi-producer, multi-consumer, optionally bounded. |
| 18 | +// When YOSYS_ENABLE_THREADS is not defined, this is just a non-thread-safe non-blocking deque. |
| 19 | +template <typename T> |
| 20 | +class ConcurrentQueue |
| 21 | +{ |
| 22 | +public: |
| 23 | + ConcurrentQueue(int capacity = INT_MAX) |
| 24 | + : capacity(capacity) {} |
| 25 | + // Push an element into the queue. If it's at capacity, block until there is room. |
| 26 | + void push_back(T t) |
| 27 | + { |
| 28 | +#ifdef YOSYS_ENABLE_THREADS |
| 29 | + std::unique_lock<std::mutex> lock(mutex); |
| 30 | + not_full_condition.wait(lock, [this] { return static_cast<int>(contents.size()) < capacity; }); |
| 31 | + if (contents.empty()) |
| 32 | + not_empty_condition.notify_one(); |
| 33 | +#endif |
| 34 | + log_assert(!closed); |
| 35 | + contents.push_back(std::move(t)); |
| 36 | +#ifdef YOSYS_ENABLE_THREADS |
| 37 | + if (static_cast<int>(contents.size()) < capacity) |
| 38 | + not_full_condition.notify_one(); |
| 39 | +#endif |
| 40 | + } |
| 41 | + // Signal that no more elements will be produced. `pop_front()` will return nullopt. |
| 42 | + void close() |
| 43 | + { |
| 44 | +#ifdef YOSYS_ENABLE_THREADS |
| 45 | + std::unique_lock<std::mutex> lock(mutex); |
| 46 | + not_empty_condition.notify_all(); |
| 47 | +#endif |
| 48 | + closed = true; |
| 49 | + } |
| 50 | + // Pop an element from the queue. Blocks until an element is available |
| 51 | + // or the queue is closed and empty. |
| 52 | + std::optional<T> pop_front() |
| 53 | + { |
| 54 | + return pop_front_internal(true); |
| 55 | + } |
| 56 | + // Pop an element from the queue. Does not block, just returns nullopt if the |
| 57 | + // queue is empty. |
| 58 | + std::optional<T> try_pop_front() |
| 59 | + { |
| 60 | + return pop_front_internal(false); |
| 61 | + } |
| 62 | +private: |
| 63 | + std::optional<T> pop_front_internal(bool wait = false) |
| 64 | + { |
| 65 | +#ifdef YOSYS_ENABLE_THREADS |
| 66 | + std::unique_lock<std::mutex> lock(mutex); |
| 67 | + if (wait) { |
| 68 | + not_empty_condition.wait(lock, [this] { return !contents.empty() || closed; }); |
| 69 | + } |
| 70 | + if (contents.empty()) |
| 71 | + return std::nullopt; |
| 72 | + if (static_cast<int>(contents.size()) == capacity) |
| 73 | + not_full_condition.notify_one(); |
| 74 | +#endif |
| 75 | + T result = std::move(contents.front()); |
| 76 | + contents.pop_front(); |
| 77 | +#ifdef YOSYS_ENABLE_THREADS |
| 78 | + if (!contents.empty()) |
| 79 | + not_empty_condition.notify_one(); |
| 80 | +#endif |
| 81 | + return std::move(result); |
| 82 | + } |
| 83 | + |
| 84 | +#ifdef YOSYS_ENABLE_THREADS |
| 85 | + std::mutex mutex; |
| 86 | + // Signals one waiter thread when the queue changes and is not full. |
| 87 | + std::condition_variable not_full_condition; |
| 88 | + // Signals one waiter thread when the queue changes and is not empty. |
| 89 | + std::condition_variable not_empty_condition; |
| 90 | +#endif |
| 91 | + std::deque<T> contents; |
| 92 | + int capacity; |
| 93 | + bool closed = false; |
| 94 | +}; |
| 95 | + |
| 96 | +class DeferredLogs |
| 97 | +{ |
| 98 | +public: |
| 99 | + template <typename... Args> |
| 100 | + void log(FmtString<TypeIdentity<Args>...> fmt, Args... args) |
| 101 | + { |
| 102 | + logs.push_back({fmt.format(args...), false}); |
| 103 | + } |
| 104 | + template <typename... Args> |
| 105 | + void log_error(FmtString<TypeIdentity<Args>...> fmt, Args... args) |
| 106 | + { |
| 107 | + logs.push_back({fmt.format(args...), true}); |
| 108 | + } |
| 109 | + void flush(); |
| 110 | +private: |
| 111 | + struct Message |
| 112 | + { |
| 113 | + std::string text; |
| 114 | + bool error; |
| 115 | + }; |
| 116 | + std::vector<Message> logs; |
| 117 | +}; |
| 118 | + |
| 119 | +class ThreadPool |
| 120 | +{ |
| 121 | +public: |
| 122 | + // Computes the number of worker threads to use. |
| 123 | + // `reserved_cores` cores are set aside for other threads (e.g. work on the main thread). |
| 124 | + // `max_threads` --- don't return more workers than this. |
| 125 | + // The result may be 0. |
| 126 | + static int pool_size(int reserved_cores, int max_threads); |
| 127 | + |
| 128 | + // Create a pool of threads running the given closure (parameterized by thread number). |
| 129 | + // `pool_size` must be the result of a `pool_size()` call. |
| 130 | + ThreadPool(int pool_size, std::function<void(int)> body); |
| 131 | + // Waits for all threads to terminate. Make sure those closures return! |
| 132 | + ~ThreadPool(); |
| 133 | + |
| 134 | + // Return the number of threads in the pool. |
| 135 | + int num_threads() const |
| 136 | + { |
| 137 | +#ifdef YOSYS_ENABLE_THREADS |
| 138 | + return threads.size(); |
| 139 | +#else |
| 140 | + return 0; |
| 141 | +#endif |
| 142 | + } |
| 143 | +private: |
| 144 | +#ifdef YOSYS_ENABLE_THREADS |
| 145 | + std::vector<std::thread> threads; |
| 146 | +#endif |
| 147 | +}; |
| 148 | + |
| 149 | +YOSYS_NAMESPACE_END |
| 150 | + |
| 151 | +#endif // YOSYS_THREADING_H |
0 commit comments