replicode
utils.h
Go to the documentation of this file.
1 /*
2  * HUMANOBS - mBrane
3  *
4  * Eric Nivel
5  * Center for Analysis and Design of Intelligent Agents
6  * Reykjavik University, Menntavegur 1, 101 Reykjavik, Iceland
7  * http://cadia.ru.is
8  * Copyright(c)2012
9  *
10  * This software was developed by the above copyright holder as part of
11  * the HUMANOBS EU research project, in collaboration with the
12  * following parties:
13  *
14  * Autonomous Systems Laboratory
15  * Technical University of Madrid, Spain
16  * http://www.aslab.org/
17  *
18  * Communicative Machines
19  * Edinburgh, United Kingdom
20  * http://www.cmlabs.com/
21  *
22  * Istituto Dalle Molle di Studi sull'Intelligenza Artificiale
23  * University of Lugano and SUPSI, Switzerland
24  * http://www.idsia.ch/
25  *
26  * Institute of Cognitive Sciences and Technologies
27  * Consiglio Nazionale delle Ricerche, Italy
28  * http://www.istc.cnr.it/
29  *
30  * Dipartimento di Ingegneria Informatica
31  * University of Palermo, Italy
32  * http://roboticslab.dinfo.unipa.it/index.php/Main/HomePage
33  *
34  *
35  * --- HUMANOBS Open-Source BSD License, with CADIA Clause v 1.0 ---
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, is permitted provided that the following conditions
39  * are met:
40  *
41  * - Redistributions of source code must retain the above copyright
42  * and collaboration notice, this list of conditions and the
43  * following disclaimer.
44  *
45  * - Redistributions in binary form must reproduce the above copyright
46  * notice, this list of conditions and the following
47  * disclaimer in the documentation and/or other materials provided
48  * with the distribution.
49  *
50  * - Neither the name of its copyright holders nor the names of its
51  * contributors may be used to endorse or promote products
52  * derived from this software without specific prior written permission.
53  *
54  * - CADIA Clause: The license granted in and to the software under this
55  * agreement is a limited-use license. The software may not be used in
56  * furtherance of:
57  * (i) intentionally causing bodily injury or severe emotional distress
58  * to any person;
59  * (ii) invading the personal privacy or violating the human rights of
60  * any person; or
61  * (iii) committing or preparing for any act of war.
62  *
63  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
64  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
65  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
66  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
67  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
68  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
69  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
70  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
71  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
72  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
73  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
74  */
75 
76 #ifndef core_utils_h
77 #define core_utils_h
78 #define LINUX
79 #include "types.h"
80 #include <stdio.h>
81 #include <semaphore.h>
82 #include <iostream>
83 #include <string>
84 #include <functional>
85 #if defined WINDOWS
86 #include <sys/timeb.h>
87 #include <time.h>
88 #elif defined LINUX
89 #include <dlfcn.h>
90 #include <errno.h>
91 #include <sys/utsname.h>
92 #include <sys/time.h>
93 #include <cstring>
94 #include <cstdlib>
95 #include <pthread.h>
96 #include <signal.h>
97 #include <sys/time.h>
98 #include <unistd.h>
99 #include <time.h>
100 // #undef HANDLE
101 // #define HANDLE pthread_cond_t*
102 #endif
103 
104 #include <chrono>
105 #include <atomic>
106 #include <mutex>
107 #include <condition_variable>
108 
109 #ifdef WINDOWS
110 #define SOCKETWOULDBLOCK WSAEWOULDBLOCK
111 #else
112 #define SOCKETWOULDBLOCK EWOULDBLOCK
113 #endif
114 
115 #ifndef SD_BOTH
116 #define SD_BOTH 2
117 #endif
118 
119 #define R250_LEN 250
120 #define R521_LEN 521
121 
122 // Wrapping of OS-dependent functions
123 namespace core
124 {
125 
126 bool core_dll WaitForSocketReadability(socket s, int32_t timeout);
127 bool core_dll WaitForSocketWriteability(socket s, int32_t timeout);
128 
129 class core_dll Error
130 {
131 public:
132  static int32_t GetLastOSErrorNumber();
133  static bool GetOSErrorMessage(char *buffer, uint32_t buflen, int32_t err = -1);
134  static bool PrintLastOSErrorMessage(const char *title);
135  static void PrintBinary(void *p, uint32_t size, bool asInt, const char *title = NULL);
136 };
137 
138 class core_dll SharedLibrary
139 {
140 private:
141  shared_object library;
142 public:
143  static SharedLibrary *New(const char *fileName);
144  SharedLibrary();
145  ~SharedLibrary();
146  SharedLibrary *load(const char *fileName);
147  template<typename T> T getFunction(const char *functionName);
148 };
149 
150 class core_dll Time // TODO: make sure time stamps are consistent when computed by different cores
151 {
152 public:
154  static uint64_t Get() {
155  using namespace std::chrono;
156  return duration_cast<microseconds>(high_resolution_clock::now().time_since_epoch()).count();
157  }
158 };
159 
160 class core_dll Host
161 {
162 public:
163  typedef char host_name[255];
164  static uint8_t Name(char *name); // name size=255; return the actual size
165 };
166 
167 class core_dll Semaphore
168 {
169 private:
170  int count;
171  std::mutex mutex;
172  std::condition_variable_any condition;
173 
174 public:
175  Semaphore(uint32_t initialCount = 0) : count(initialCount) {}
176  void acquire(){
177  std::lock_guard<std::mutex> guard(mutex);
178 
179  while (count == 0) {
180  condition.wait(mutex);
181  }
182  count--;
183  }
184 
185  void release(){
186  std::lock_guard<std::mutex> guard(mutex);
187  count++;
188  condition.notify_one();
189  }
190 
191  void reset() {
192  std::lock_guard<std::mutex> guard(mutex);
193  if (count > 0) {
194  count = 0;
195  }
196  }
197 };
198 
199 class core_dll CriticalSection
200 {
201 private:
202  pthread_mutex_t cs;
203 public:
204  CriticalSection();
205  ~CriticalSection();
206  void enter();
207  void leave();
208 };
209 
210 class core_dll SignalHandler
211 {
212 public:
213  static void Add(sighandler_t h);
214  static void Remove(sighandler_t h);
215 };
216 
217 uint8_t core_dll BSR(uint32_t data); // BitScanReverse
218 
219 }
220 
221 #include "utils.tpl.cpp"
222 
223 
224 #endif
Definition: utils.h:129
Definition: utils.h:167
void reset()
Definition: utils.h:191
Definition: base.cpp:81
Semaphore(uint32_t initialCount=0)
Definition: utils.h:175
uint8_t BSR(uint32_t data)
Definition: utils.cpp:338
shared_object library
Definition: utils.h:141
std::mutex mutex
Definition: utils.h:171
Definition: utils.h:150
Definition: utils.h:138
Definition: utils.h:199
Definition: utils.h:210
bool WaitForSocketReadability(socket s, int32_t timeout)
Definition: utils.cpp:469
std::condition_variable_any condition
Definition: utils.h:172
static uint64_t Get()
in us since 01/01/1970
Definition: utils.h:154
void release()
Definition: utils.h:185
pthread_mutex_t cs
Definition: utils.h:202
Definition: utils.h:160
int count
Definition: utils.h:170
void acquire()
Definition: utils.h:176
bool WaitForSocketWriteability(socket s, int32_t timeout)
Definition: utils.cpp:496