ifw-core  5.0.0-pre2
system.hpp
Go to the documentation of this file.
1 
6 #ifndef IFW_CORE_UTILS_SYSTEM_HPP_
7 #define IFW_CORE_UTILS_SYSTEM_HPP_
8 
9 #include <string>
10 #include <sys/sem.h>
11 #include <pthread.h>
12 #include <mutex>
13 
14 #include <rad/logger.hpp>
15 
16 #include <core/utils/base/base.hpp>
17 
18 
20 
21  double _Time();
22 
24  void Assert(const bool condition,
25  const std::string& message,
26  std::array<std::string, 3>& location);
27 
29  void AssertPtr(const void* ptr,
30  const std::string& object,
31  std::array<std::string, 3>& location);
32 
34  std::string Platform();
35 
37  int64_t Random(const int64_t min_val,
38  const int64_t max_val);
39 
41  //int32_t ExecuteCommand(const std::string& command,
42  // std::string& std_out,
43  // std::string& std_err);
44 
46  std::string ExecCommand(const std::string& command);
47 
69  class Mutex {
70  public:
72  static void Clear();
73 
74  explicit Mutex(const std::string& id);
75 
76  ~Mutex();
77 
78  const std::string& Id() const;
79 
80  void Lock();
81 
82  void Unlock();
83 
84  protected:
85 
86  private:
87  static bool s_initialized;
88  static bool s_debug_mode;
89  static std::recursive_mutex s_gen_mutex;
90  static void _lock_gen_mutex();
91  static void _unlock_gen_mutex();
92 
93  static std::map<std::string, std::shared_ptr<std::recursive_mutex> > s_mutexes;
94 
95  std::recursive_mutex* m_cur_sem_ptr;
96  std::string m_id;
97  double m_timeout;
98  };
99 
101 #define BEGIN_CRIT_SEC(id) { \
102  core::utils::system::Mutex tmp_mutex(id); \
103 
105 #define END_CRIT_SEC(id) }
106 
107 }
108 
109 #endif // IFW_CORE_UTILS_SYSTEM_HPP_
The Mutex class: Scope based mutex semaphore. The core::utils::system::Mutex semaphore is used to imp...
Definition: system.hpp:69
void Unlock()
Definition: system.cpp:180
Mutex(const std::string &id)
Definition: system.cpp:119
const std::string & Id() const
Definition: system.cpp:190
~Mutex()
Definition: system.cpp:151
void Lock()
Definition: system.cpp:170
static void Clear()
Releases (deletes) all semaphores allocated. Use with caution!!
Definition: system.cpp:164
Definition: system.hpp:19
void Assert(const bool condition, const std::string &message, std::array< std::string, 3 > &location)
Check if pointer is NULL. If yes, throw std::runtime_error.
int64_t Random(const int64_t min_val, const int64_t max_val)
Generate an int64_t random number in the inteval specified.
Definition: system.cpp:64
std::string Platform()
Get the name of the platform (OS).
Definition: system.cpp:44
std::string ExecCommand(const std::string &command)
Execute a shell command (synchroneously).
Definition: system.cpp:88
double _Time()
Definition: system.cpp:36
void AssertPtr(const void *ptr, const std::string &object, std::array< std::string, 3 > &location)
Check if pointer is NULL. If yes, throw std::runtime_error.
Definition: system.cpp:25