brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.6 KiB · 2ef1cb7 Raw
70 lines · c
1#ifndef LLDB_TEST_API_COMMON_H2#define LLDB_TEST_API_COMMON_H3 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 message15struct Exception : public std::exception16{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 through24template<typename T>25class multithreaded_queue {26  std::condition_variable m_condition;27  std::mutex m_mutex;28  std::queue<T> m_data;29  bool m_notified;30 31public: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 directory60inline char* get_working_dir() {61#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) ||       \62    defined(__OpenBSD__)63    return getwd(0);64#else65    return get_current_dir_name();66#endif67}68 69#endif // LLDB_TEST_API_COMMON_H70