Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / lldb / test / API / api / multithreaded / common.h
blobdad8bba07a3f3a5454541a8bd42b28a00a60e24a
1 #ifndef LLDB_TEST_API_COMMON_H
2 #define LLDB_TEST_API_COMMON_H
4 #include <condition_variable>
5 #include <chrono>
6 #include <exception>
7 #include <iostream>
8 #include <mutex>
9 #include <string>
10 #include <queue>
12 #include <unistd.h>
14 /// Simple exception class with a message
15 struct Exception : public std::exception
17 std::string s;
18 Exception(std::string ss) : s(ss) {}
19 virtual ~Exception() throw () { }
20 const char* what() const throw() { return s.c_str(); }
23 // Synchronized data structure for listener to send events through
24 template<typename T>
25 class multithreaded_queue {
26 std::condition_variable m_condition;
27 std::mutex m_mutex;
28 std::queue<T> m_data;
29 bool m_notified;
31 public:
33 void push(T e) {
34 std::lock_guard<std::mutex> lock(m_mutex);
35 m_data.push(e);
36 m_notified = true;
37 m_condition.notify_all();
40 T pop(int timeout_seconds, bool &success) {
41 int count = 0;
42 while (count < timeout_seconds) {
43 std::unique_lock<std::mutex> lock(m_mutex);
44 if (!m_data.empty()) {
45 m_notified = false;
46 T ret = m_data.front();
47 m_data.pop();
48 success = true;
49 return ret;
50 } else if (!m_notified)
51 m_condition.wait_for(lock, std::chrono::seconds(1));
52 count ++;
54 success = false;
55 return T();
59 /// Allocates a char buffer with the current working directory
60 inline char* get_working_dir() {
61 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
62 return getwd(0);
63 #else
64 return get_current_dir_name();
65 #endif
68 #endif // LLDB_TEST_API_COMMON_H