ifw-core  2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
system.hpp
Go to the documentation of this file.
1 
9 #ifndef IFW_CTD_SYSTEM_HPP_
10 #define IFW_CTD_SYSTEM_HPP_
11 
12 #include <string>
13 #include <sys/sem.h>
14 #include <pthread.h>
15 #include <mutex>
16 
17 #include "rad/logger.hpp"
18 
19 #include "ctd/defines/defines.hpp"
20 
21 
22 namespace ctd::system {
23 
24  double _Time();
25 
27  void Assert(const bool condition,
28  const std::string& message,
29  std::array<std::string, 3>& location);
30 
32  void AssertPtr(const void* ptr,
33  const std::string& object,
34  std::array<std::string, 3>& location);
35 
37  std::string Platform();
38 
40  int64_t Random(const int64_t min_val,
41  const int64_t max_val);
42 
44  //int32_t ExecuteCommand(const std::string& command,
45  // std::string& std_out,
46  // std::string& std_err);
47 
49  std::string ExecCommand(const std::string& command);
50 
72  class Mutex {
73  public:
75  static void Clear();
76 
77  explicit Mutex(const std::string& id);
78 
79  ~Mutex();
80 
81  const std::string& Id() const;
82 
83  void Lock();
84 
85  void Unlock();
86 
87  protected:
88 
89  private:
90  static bool s_initialized;
91  static bool s_debug_mode;
92  static std::recursive_mutex s_gen_mutex;
93  static void _lock_gen_mutex();
94  static void _unlock_gen_mutex();
95 
96  static std::map<std::string, std::shared_ptr<std::recursive_mutex> > s_mutexes;
97 
99  std::recursive_mutex* m_cur_sem_ptr;
100  std::string m_id;
101  double m_timeout;
102  };
103 
104 
105  namespace testUtils {
106 
109  std::string GetResDir(const std::string& module_path,
110  const std::string& res_dir = "test/resource");
111 
113  void SetRootEnvVars(const std::string& module_path,
114  const std::string& res_dir = "test/resource");
115 
116  } // namespace testUtils
117 
118 
120 #define BEGIN_CRIT_SEC(id) { \
121  ctd::system::Mutex tmp_mutex(id); \
122 
123 #define END_CRIT_SEC(id) }
125 
126 
127 }
128 
129 #endif // IFW_CTD_SYSTEM_HPP_
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:72
Common definitions.
void AssertPtr(const void *ptr, const std::string &object, std::array< std::string, 3 > &location)
Check if pointer is NULL. If yes, throw rad::Exception.
Definition: system.cpp:26
Mutex(const std::string &id)
Definition: system.cpp:137
void Assert(const bool condition, const std::string &message, std::array< std::string, 3 > &location)
Check if pointer is NULL. If yes, throw rad::Exception.
void Lock()
Definition: system.cpp:192
std::string GetResDir(const std::string &module_path, const std::string &res_dir="test/resource")
Derives the &quot;resource&quot; directory from the current working point.
Definition: system.cpp:222
void SetRootEnvVars(const std::string &module_path, const std::string &res_dir="test/resource")
Set the root environment variables: CFGROOT, INTROOT, DATAROOT.
Definition: system.cpp:257
double _Time()
Definition: system.cpp:40
static void Clear()
Releases (deletes) all semaphores allocated. Use with caution!!
Definition: system.cpp:185
std::string ExecCommand(const std::string &command)
Execute a shell command (synchroneously).
Definition: system.cpp:101
std::string Platform()
Get the name of the platform (OS).
Definition: system.cpp:50
void Unlock()
Definition: system.cpp:203
const std::string & Id() const
Definition: system.cpp:214
The Mutex class: Scope based mutex semaphore. The ctd::system::Mutex semaphore is used to implement a...
Definition: system.hpp:72
~Mutex()
Definition: system.cpp:171