cfl_platform.h
Go to the documentation of this file.
1 /** @file cfl_platform.h Platform dependant wrappers */
2 
3 /* FAU Discrete Event Systems Library (libfaudes)
4 
5  Copyright (C) 2013, 2024 Thomas Moor
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public
9  License as published by the Free Software Foundation; either
10  version 2.1 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNES FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public
18  License along with this library; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21 
22 
23 #ifndef FAUDES_PLATFORM_H
24 #define FAUDES_PLATFORM_H
25 
26 /** Sense POSIX versus Windows */
27 #ifndef FAUDES_POSIX
28 #ifndef FAUDES_WINDOWS
29 #ifndef FAUDES_GENERIC
30 
31 #if defined(_WIN32) || defined(__WIN32__) || defined(__WINDOWS__)
32 #define FAUDES_WINDOWS
33 #endif
34 #if defined (__unix__) || (__linus__) || (__linux__) || (defined (__APPLE__) && defined (__MACH__))
35 #define FAUDES_POSIX
36 #endif
37 
38 #ifndef FAUDES_POSIX
39 #ifndef FAUDES_WINDOWS
40 #define FAUDES_GENERIC
41 #endif
42 #endif
43 
44 #endif
45 #endif
46 #endif
47 
48 
49 /** Interface export/import symbols: windows */
50 #ifdef FAUDES_WINDOWS
51  #ifdef __GNUC__
52  #ifdef FAUDES_BUILD_DSO
53  #define FAUDES_API __attribute__ ((dllexport))
54  #endif
55  #ifdef FAUDES_BUILD_APP
56  #define FAUDES_API __attribute__ ((dllimport))
57  #endif
58  #else
59  #ifdef FAUDES_BUILD_DSO
60  #define FAUDES_API __declspec(dllexport)
61  #endif
62  #ifdef FAUDES_BUILD_APP
63  #define FAUDES_API __declspec(dllimport)
64  #endif
65  #endif
66 #endif
67 
68 /** Interface export/import symbols: posix/gcc */
69 #ifdef FAUDES_POSIX
70 #ifdef FAUDES_BUILD_DSO
71  #if __GNUC__ >= 4
72  #define FAUDES_API __attribute__ ((visibility ("default")))
73  #define FAUDES_TAPI __attribute__ ((visibility ("default")))
74  #endif
75 #endif
76 #endif
77 
78 /** Interface export/import symbols: default */
79 #ifndef FAUDES_API
80  #define FAUDES_API
81 #endif
82 #ifndef FAUDES_TAPI
83  #define FAUDES_TAPI
84 #endif
85 
86 
87 
88 // Std headers
89 #include <cstdlib>
90 #include <cstring>
91 #include <inttypes.h>
92 #include <limits>
93 #include <string>
94 #include <iostream>
95 #include <sstream>
96 #include <fstream>
97 #include <iomanip>
98 #include <map>
99 #include <set>
100 #include <vector>
101 #include <stack>
102 #include <iterator>
103 
104 
105 // Extra POSIX headers
106 #ifdef FAUDES_POSIX
107 
108 #include <stdlib.h>
109 #include <cerrno>
110 #include <fcntl.h>
111 #include <unistd.h>
112 #include <dirent.h>
113 
114 #ifdef FAUDES_SYSTIME
115 #include <time.h>
116 #include <sys/time.h>
117 #endif
118 
119 #ifdef FAUDES_NETWORK
120 #include <sys/socket.h>
121 #include <arpa/inet.h>
122 #include <netdb.h>
123 #endif
124 
125 #ifdef FAUDES_THREADS
126 #include <pthread.h>
127 #endif
128 
129 #endif // include POSIX headers
130 
131 
132 // Extra Windows headers
133 #ifdef FAUDES_WINDOWS
134 
135 #ifdef FAUDES_NETWORK
136 #include <winsock2.h>
137 #include <ws2tcpip.h> // MS VC 2017
138 #include <fcntl.h>
139 #endif
140 
141 #ifndef WIN32_LEAN_AND_MEAN
142 #define WIN32_LEAN_AND_MEAN
143 #define FAUDES_LEAN_AND_MEAN
144 #endif
145 #include <windows.h>
146 #ifdef FAUDES_LEAN_AND_MEAN
147 #undef FAUDES_LEAN_AND_MEAN
148 #undef WIN32_LEAN_AND_MEAN
149 #endif
150 
151 #ifdef FAUDES_SYSTIME
152 #include <time.h> // ok for cl
153 //#include <sys/time.h> //mingw only
154 #include <mmsystem.h>
155 #endif
156 
157 
158 #ifdef FAUDES_THREADS
159 #include <process.h>
160 #endif
161 
162 #include <io.h>
163 
164 // dislike min/max msvc macros
165 #ifdef max
166 #undef max
167 #endif
168 #ifdef min
169 #undef min
170 #endif
171 
172 #endif
173 
174 
175 
176 // Path-seperators (see cfl_utils.cpp)
177 // * the first separator is the one used to prepend directories etc
178 // * all other separators are used to extract basenames
179 // * using gcc/make also on Windows, we occasionaly get Unix style files names
180 // in the build process ... thus we allways define the Unix "/" as a separator
181 // * up to libFAUDES 2.31 we used "/" for POSIX and "\\:/" for Windows.
182 // * as of libFAUDES 2.32 we used "/" for POSIX and "/\\:" for Windows.
183 extern FAUDES_API const std::string& faudes_pathseps(void);
184 // Path-seperator (first char of above, see cfl_utils.cpp)
185 extern FAUDES_API const std::string& faudes_pathsep(void);
186 
187 
188 // Uniform exit-signalhandler for POSIX/Windows (see e.g. simfaudes.cpp)
189 extern FAUDES_API void faudes_termsignal(void (*sighandler)(int));
190 
191 // Uniform signal names for POSIX/Windows (see e.g. simfaudes.cpp)
192 extern FAUDES_API const char* faudes_strsignal(int sig);
193 
194 // Uniform sleep for POSIX/Windows (see e.g. iodevice plug-in)
195 extern FAUDES_API void faudes_sleep(long int sec);
196 extern FAUDES_API void faudes_usleep(long int usec);
197 
198 
199 // have time
200 #ifdef FAUDES_SYSTIME
201 
202 // Uniform system time using POSIX pthreads semantics
203 #ifdef FAUDES_POSIX
204 typedef timespec faudes_systime_t;
205 typedef long int faudes_mstime_t;
206 #endif
207 #ifdef FAUDES_WINDOWS
208 typedef struct {
209  long int tv_sec;
210  long int tv_nsec;
211 } faudes_systime_t;
212 typedef long int faudes_mstime_t;
213 #endif
214 #ifdef FAUDES_GENERIC
215 #error option systime not available on generic platform
216 #endif
217 
218 // Uniform system time definitions
219 extern FAUDES_API void faudes_gettimeofday(faudes_systime_t* now);
220 extern FAUDES_API void faudes_diffsystime(const faudes_systime_t& end, const faudes_systime_t& begin, faudes_systime_t* res);
221 extern FAUDES_API void faudes_diffsystime(const faudes_systime_t& end, const faudes_systime_t& begin, faudes_mstime_t* res);
222 extern FAUDES_API void faudes_sumsystime(const faudes_systime_t& begin, const faudes_systime_t& duration, faudes_systime_t* res);
223 extern FAUDES_API void faudes_msdelay(faudes_mstime_t msecs,faudes_systime_t* end);
224 extern FAUDES_API void faudes_usdelay(faudes_mstime_t usecs,faudes_systime_t* end);
225 
226 // global performance times
227 extern FAUDES_API faudes_systime_t gPerfTimer1;
228 
229 #endif // systime
230 
231 
232 // have IP network
233 #ifdef FAUDES_NETWORK
234 
235 // Uniform POSIX sockets (see iop_modbus.cpp and iop_simplenet.cpp)
236 extern FAUDES_API int faudes_closesocket(int fd);
237 extern FAUDES_API int faudes_setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen);
238 extern FAUDES_API int faudes_getsockopt(int fd, int level, int optname, void *optval, socklen_t *optlen);
239 extern FAUDES_API int faudes_setsocket_nonblocking(int fd, bool noblo);
240 extern FAUDES_API int faudes_getsocket_error(int fd);
241 
242 // POSIX sockets to have BSD style REUSEPORT option (see iop_modbus.cpp and iop_simplenet.cpp)
243 #ifndef SO_REUSEPORT
244 #define SO_REUSEPORT SO_REUSEADDR
245 #endif
246 
247 #endif
248 
249 
250 #ifdef FAUDES_THREADS
251 
252 /*
253 The remaining section of this file provides elementary support for threads,
254 using a minimalistic subset of the POSIX threads interface. It is tailored for
255 the use of edge-detection and networking as required by the iodevice plug-in.
256 In general, libFAUDES is not threadsafe due to global variables, e.g.
257 symoltables. This may change in a future revision.
258 */
259 
260 
261 // Common return codes
262 #define FAUDES_THREAD_SUCCESS 0
263 #define FAUDES_THREAD_ERROR 1
264 #define FAUDES_THREAD_TIMEOUT 2
265 
266 // Thread data type (use plain POSIX thread)
267 #ifdef FAUDES_POSIX
268 typedef pthread_t faudes_thread_t;
269 #endif
270 
271 // Thread data type
272 // We wrap the client function to provide pointer-typed
273 // return values (as opposed to Windows int-typed return values)
274 #ifdef FAUDES_WINDOWS
275 typedef struct {
276  HANDLE mHandle; // Windows thread handle
277  void *(*mFnct)(void *); // client function
278  void *mArg; // argument
279  void *mRes; // result
280 } faudes_thread_record_t;
281 typedef faudes_thread_record_t* faudes_thread_t;
282 #endif
283 
284 // not supported on generic platform
285 #ifdef FAUDES_GENERIC
286 #error option threads not available on generic platform
287 #endif
288 
289 
290 // Thread functions
291 extern FAUDES_API int faudes_thread_create(faudes_thread_t *thr, void *(*fnct)(void *), void *arg);
292 extern FAUDES_API faudes_thread_t faudes_thread_current(void);
293 extern FAUDES_API int faudes_thread_detach(faudes_thread_t thr);
294 extern FAUDES_API int faudes_thread_equal(faudes_thread_t thr0, faudes_thread_t thr1);
295 extern FAUDES_API void faudes_thread_exit(void* res);
296 extern int faudes_thread_join(faudes_thread_t thr, void **res);
297 
298 // Mutex data type (use plain POSIX mutex)
299 #ifdef FAUDES_POSIX
300 typedef pthread_mutex_t faudes_mutex_t;
301 #endif
302 
303 // Mutex data type (use Windows "critical section")
304 #ifdef FAUDES_WINDOWS
305 typedef CRITICAL_SECTION faudes_mutex_t;
306 #endif
307 
308 // Mutex functions
309 extern FAUDES_API int faudes_mutex_init(faudes_mutex_t* mtx);
310 extern FAUDES_API void faudes_mutex_destroy(faudes_mutex_t* mtx);
311 extern FAUDES_API int faudes_mutex_lock(faudes_mutex_t *mtx);
312 extern FAUDES_API int faudes_mutex_trylock(faudes_mutex_t *mtx);
313 extern FAUDES_API int faudes_mutex_unlock(faudes_mutex_t *mtx);
314 
315 // Condition variables (use plain POSIX cond vars)
316 #ifdef FAUDES_POSIX
317 typedef pthread_cond_t faudes_cond_t;
318 #endif
319 
320 // Condition variables for Windows
321 // The approach is taken from "Strategies for Implementing POSIX Condition Variables
322 // on Win32" by Douglas C. Schmidt and Irfan Pyarali, Department of Computer
323 // Science, Washington University.
324 #ifdef FAUDES_WINDOWS
325 typedef struct {
326  HANDLE mEvents[2]; // signal and broadcast event handles
327  unsigned int mWaitersCount; // count the number of waiters
328  CRITICAL_SECTION mWaitersCountMutex; // mutex access to waiterscount
329 } faudes_cond_t;
330 #endif
331 
332 // Condition functions
333 extern FAUDES_API int faudes_cond_init(faudes_cond_t* cond);
334 extern FAUDES_API void faudes_cond_destroy(faudes_cond_t* cond);
335 extern FAUDES_API int faudes_cond_signal(faudes_cond_t *cond);
336 extern FAUDES_API int faudes_cond_broadcast(faudes_cond_t *cond);
337 extern FAUDES_API int faudes_cond_wait(faudes_cond_t *cond, faudes_mutex_t *mtx);
338 extern FAUDES_API int faudes_cond_timedwait(faudes_cond_t *cond, faudes_mutex_t *mtx, const faudes_systime_t *abstime);
339 extern FAUDES_API int faudes_cond_reltimedwait(faudes_cond_t *cond, faudes_mutex_t *mtx, faudes_mstime_t duration);
340 
341 
342 
343 #endif // threads
344 
345 #endif // header
346 
faudes_systime_t gPerfTimer1
void faudes_usdelay(faudes_mstime_t usecs, faudes_systime_t *end)
void faudes_sumsystime(const faudes_systime_t &begin, const faudes_systime_t &duration, faudes_systime_t *res)
void faudes_msdelay(faudes_mstime_t msecs, faudes_systime_t *end)
void faudes_diffsystime(const faudes_systime_t &end, const faudes_systime_t &begin, faudes_systime_t *res)
#define FAUDES_API
Interface export/import symbols: windows.
Definition: cfl_platform.h:80
FAUDES_API const std::string & faudes_pathsep(void)
FAUDES_API void faudes_termsignal(void(*sighandler)(int))
FAUDES_API const char * faudes_strsignal(int sig)
FAUDES_API void faudes_usleep(long int usec)
FAUDES_API void faudes_sleep(long int sec)
FAUDES_API const std::string & faudes_pathseps(void)

libFAUDES 2.32f --- 2024.12.22 --- c++ api documentaion by doxygen