ifw  0.0.1-dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
system.hpp
Go to the documentation of this file.
1 
6 #ifndef IFW_CTD_SYSTEM_HPP_
7 #define IFW_CTD_SYSTEM_HPP_
8 
9 #include <string>
10 #include <sys/sem.h>
11 #include <pthread.h>
12 
13 #include "rad/Logger.hpp"
14 
15 #include "ctd/defines/defines.hpp"
16 
17 
18 namespace ctd {
19 namespace system {
20 
24 void Sleep(const float sleep_time);
25 
27 double Time();
28 
30 void SecondsToTimeSpec(const double time,
31  struct timespec& time_spec);
32 
34 void SecondsToTimeVal(const double time,
35  struct timeval& time_val);
36 
38 std::string IsoTime(const double time_since_epoch,
39  const uint8_t prec = 6);
40 
42 std::string IsoTimeNow(const int8_t prec = 6);
43 
45 void AssertPtr(const void* ptr,
46  const std::string& object,
47  const std::string& location);
48 
50 std::string Platform();
51 
53 std::string GenUniqueId(const std::string& prefix = "");
54 
56 int64_t Random(const int64_t min_val,
57  const int64_t max_val);
58 
60 //int32_t ExecuteCommand(const std::string& command,
61 // std::string& std_out,
62 // std::string& std_err);
63 
65 std::string ExecCommand(const std::string& command);
66 
67 class Mutex {
68 public:
70  static void Clear();
71 
72  explicit Mutex(const std::string& id);
73 
74  ~Mutex();
75 
76  const std::string& Id() const;
77 
78  void Lock(bool& timed_out,
79  const double timeout = ctd::defines::NO_TIMEOUT);
80 
81  void Unlock();
82 
83  protected:
84 
85  private:
86  static bool s_initialized;
87  static bool s_debug_mode;
88  static pthread_mutex_t s_gen_mutex;
89  static void _take_gen_mutex();
90  static void _release_gen_mutex();
91 
92  static std::map<std::string, std::shared_ptr<pthread_mutex_t> > s_mutexes;
93 
95  pthread_mutex_t* m_sem_ptr;
96  std::string m_id;
97  double m_timeout;
98 };
99 
100 namespace testUtils {
101 
104 std::string GetResDir(const std::string& module_path,
105  const std::string& res_dir = "test/resource");
106 
108 void SetRootEnvVars(const std::string& module_path,
109  const std::string& res_dir = "test/resource");
110 
111 } // namespace testUtils
112 
113 
115 #define BEGIN_CRIT_SEC(id) { \
116  ctd::system::Mutex tmpMutex(id); \
117 
118 #define END_CRIT_SEC }
120 
121 } // namespace ctd
122 } // namespace system
123 
124 #endif // IFW_CTD_SYSTEM_HPP_
void Sleep(const float sleep_time)
Sleep.
Definition: system.cpp:19
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:141
Mutex(const std::string &id)
Definition: system.cpp:199
void SecondsToTimeVal(const double time, struct timeval &time_val)
Convert time in seconds since epoch to timeval struct.
Definition: system.cpp:51
std::string GenUniqueId(const std::string &prefix="")
Generate a unique UUID based ID. A prefix may be prepended, if requested.
Definition: system.cpp:127
double Time()
Return the time in seconds since epoch.
Definition: system.cpp:32
optional bool timeout
Definition: requests.proto:13
void AssertPtr(const void *ptr, const std::string &object, const std::string &location)
Check if pointer is NULL. If yes, throw rad::Exception.
Definition: system.cpp:92
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:318
base::threading::internal::NoMutex Mutex
Definition: easylogging++.h:1066
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:353
static void Clear()
Releases (deletes) all semaphores allocated. Use with caution!!
Definition: system.cpp:261
std::string ExecCommand(const std::string &command)
Execute a shell command (synchroneously).
Definition: system.cpp:168
void SecondsToTimeSpec(const double time, struct timespec &time_spec)
Convert time in seconds since epoch to a timespec struct.
Definition: system.cpp:42
std::string Platform()
Get the name of the platform (OS).
Definition: system.cpp:105
void Unlock()
Definition: system.cpp:302
void Lock(bool &timed_out, const double timeout=ctd::defines::NO_TIMEOUT)
Definition: system.cpp:268
const std::string & Id() const
Definition: system.cpp:312
std::string IsoTime(const double time_since_epoch, const uint8_t prec=6)
Generate ISO8601 timestamp from provided time in seconds since epoch.
Definition: system.cpp:60
~Mutex()
Definition: system.cpp:247
const int64_t NO_TIMEOUT
Definition: defines.hpp:206
std::string IsoTimeNow(const int8_t prec=6)
Generate ISO8601 timestamp from time of invoking this function.
Definition: system.cpp:85