1 #ifndef LLDB_TEST_API_COMMON_H 2 #define LLDB_TEST_API_COMMON_H 3 4 #include <condition_variable> 5 #include <chrono> 6 #include <exception> 7 #include <iostream> 8 #include <mutex> 9 #include <string> 10 #include <queue> 11 12 #include <unistd.h> 13 14 /// Simple exception class with a message 15 struct Exception : public std::exception 16 { 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(); } 21 }; 22 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; 30 31 public: 32 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(); 38 } 39 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 ++; 53 } 54 success = false; 55 return T(); 56 } 57 }; 58 59 /// Allocates a char buffer with the current working directory on Linux/Darwin 60 inline char* get_working_dir() { 61 #ifdef __APPLE__ 62 return getwd(0); 63 #else 64 return get_current_dir_name(); 65 #endif 66 } 67 68 #endif // LLDB_TEST_API_COMMON_H 69