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(_WIN64) || defined(__WIN32__) || defined(__WINDOWS__)
32#define FAUDES_WINDOWS
33#else
34#if defined(__MINGW32__) || defined(__MINGW64__)
35#define FAUDES_WINDOWS
36#else
37#if defined(__unix__) || defined(__linus__) || defined(__linux__)
38#define FAUDES_POSIX
39#else
40#if defined(__APPLE__) && defined(__MACH__)
41#define FAUDES_POSIX
42#else
43#define FAUDES_GENERIC
44#endif
45#endif
46#endif
47#endif
48
49#endif
50#endif
51#endif
52
53
54/* Interface export/import symbols: windows */
55#ifdef FAUDES_WINDOWS
56 #ifdef __GNUC__
57 #ifdef FAUDES_BUILD_DSO
58 #define FAUDES_API __attribute__ ((dllexport))
59 #endif
60 #ifdef FAUDES_BUILD_APP
61 #define FAUDES_API __attribute__ ((dllimport))
62 #endif
63 #else
64 #ifdef FAUDES_BUILD_DSO
65 #define FAUDES_API __declspec(dllexport)
66 #endif
67 #ifdef FAUDES_BUILD_APP
68 #define FAUDES_API __declspec(dllimport)
69 #endif
70 #endif
71#endif
72
73/* Interface export/import symbols: gcc/clang */
74#ifdef FAUDES_POSIX
75#ifdef FAUDES_BUILD_DSO
76 #if __GNUC__ >= 4
77 #define FAUDES_API __attribute__ ((visibility ("default")))
78 #define FAUDES_TAPI __attribute__ ((visibility ("default")))
79 #endif
80#endif
81#endif
82
83/* Interface export/import symbols: default */
84#ifndef FAUDES_API
85 #define FAUDES_API
86#endif
87#ifndef FAUDES_TAPI
88 #define FAUDES_TAPI
89#endif
90
91
92
93/* Std headers */
94#include <cstdlib>
95#include <cstring>
96#include <inttypes.h>
97#include <limits>
98#include <string>
99#include <iostream>
100#include <sstream>
101#include <fstream>
102#include <iomanip>
103#include <map>
104#include <set>
105#include <vector>
106#include <stack>
107#include <iterator>
108
109
110// Extra POSIX headers
111#ifdef FAUDES_POSIX
112
113#include <stdlib.h>
114#include <errno.h>
115#include <fcntl.h>
116#include <unistd.h>
117#include <string.h>
118#include <dirent.h>
119
120#ifdef FAUDES_SYSTIME
121#include <time.h>
122#include <sys/time.h>
123#endif
124
125#ifdef FAUDES_NETWORK
126#include <sys/socket.h>
127#include <arpa/inet.h>
128#include <netdb.h>
129#endif
130
131#ifdef FAUDES_THREADS
132#include <pthread.h>
133#endif
134
135#endif // Extra POSIX headers
136
137
138// Extra Windows headers
139#ifdef FAUDES_WINDOWS
140
141#ifdef FAUDES_NETWORK
142#include <winsock2.h>
143#include <ws2tcpip.h> // MS VC 2017
144#include <fcntl.h>
145#endif
146
147#ifndef WIN32_LEAN_AND_MEAN
148#define WIN32_LEAN_AND_MEAN
149#define FAUDES_LEAN_AND_MEAN
150#endif
151#include <windows.h>
152#ifdef FAUDES_LEAN_AND_MEAN
153#undef FAUDES_LEAN_AND_MEAN
154#undef WIN32_LEAN_AND_MEAN
155#endif
156
157#ifdef FAUDES_SYSTIME
158#include <time.h> // ok for cl
159//#include <sys/time.h> //mingw only
160#include <mmsystem.h>
161#endif
162
163
164#ifdef FAUDES_THREADS
165#include <process.h>
166#endif
167
168#include <io.h>
169
170// dislike min/max msvc macros
171#ifdef max
172#undef max
173#endif
174#ifdef min
175#undef min
176#endif
177
178#endif // End extra Windows headers
179
180
181// Path to dot executable
182#ifndef FAUDES_DOTPATH
183#ifdef FAUDES_WINDOWS
184#define FAUDES_DOTPATH "C:\\Program Files\\Graphviz\\bin\\dot.exe"
185#endif
186#endif
187#ifndef FAUDES_DOTPATH
188#define FAUDES_DOTPATH "dot"
189#endif
190
191
192
193// Path-seperators (see cfl_utils.cpp)
194// * the first separator is the one used to prepend directories etc
195// * all other separators are used to extract filenames, i.e., to strip the path
196// * as of libFAUDES 2.32 we internally treat "/" is our separator; we set
197// path seperator to "/" for POSIX and "/\\:" for Windows.
198// * up to libFAUDES 2.31 we used "/" for POSIX and "\\:/" for Windows; this was
199// asking for trouble.
200extern FAUDES_API const std::string& faudes_pathseps(void);
201// Path-seperator (first char of above, see cfl_utils.cpp)
202extern FAUDES_API const std::string& faudes_pathsep(void);
203
204
205// External vs internal paths conversion -- we are so bored
206//
207// Internal is as of v2.32 posix style, i.e., '/' is the only separtor, no
208// drive letters whatsowever. Is need be, they are converted along the
209// pattern "C:\ ==> /c/". libFAUDES should not operate on absolute paths anyway.
210// Occasionally (when a libFAUDES tool needs to invoke a shell), we need to
211// convert back to external representaion. Likewise, libFAUDES tools may be
212// invoked with posix style argumetns and may need to convert. Henve the following
213// two conversion functons (which should be identity on posix systems)
214extern FAUDES_API std::string faudes_normpath(const std::string& rExtPath);
215extern FAUDES_API std::string faudes_extpath(const std::string& rNormIntPath);
216
217// uniform get/set dir (use posix style interface)
218extern FAUDES_API std::string faudes_getwd(void);
219extern FAUDES_API int faudes_chdir(const std::string& nwd);
220
221// uniform sytem call
222extern FAUDES_API int faudes_system(const std::string& cmd, const std::string& args);
223
224// Uniform exit-signalhandler for POSIX/Windows (see e.g. simfaudes.cpp)
225extern FAUDES_API void faudes_termsignal(void (*sighandler)(int));
226
227// Uniform signal names for POSIX/Windows (see e.g. simfaudes.cpp)
228extern FAUDES_API const char* faudes_strsignal(int sig);
229
230// Uniform sleep for POSIX/Windows (see e.g. iodevice plug-in)
231extern FAUDES_API void faudes_sleep(long int sec);
232extern FAUDES_API void faudes_usleep(long int usec);
233
234
235// have time
236#ifdef FAUDES_SYSTIME
237
238// Uniform system time using POSIX pthreads semantics
239#ifdef FAUDES_POSIX
240typedef timespec faudes_systime_t;
241typedef long int faudes_mstime_t;
242#endif
243#ifdef FAUDES_WINDOWS
244typedef struct {
245 long int tv_sec;
246 long int tv_nsec;
247} faudes_systime_t;
248typedef long int faudes_mstime_t;
249#endif
250#ifdef FAUDES_GENERIC
251#error option systime not available on generic platform
252#endif
253
254// Uniform system time definitions
255extern FAUDES_API void faudes_gettimeofday(faudes_systime_t* now);
256extern FAUDES_API void faudes_diffsystime(const faudes_systime_t& end, const faudes_systime_t& begin, faudes_systime_t* res);
257extern FAUDES_API void faudes_diffsystime(const faudes_systime_t& end, const faudes_systime_t& begin, faudes_mstime_t* res);
258extern FAUDES_API void faudes_sumsystime(const faudes_systime_t& begin, const faudes_systime_t& duration, faudes_systime_t* res);
259extern FAUDES_API void faudes_msdelay(faudes_mstime_t msecs,faudes_systime_t* end);
260extern FAUDES_API void faudes_usdelay(faudes_mstime_t usecs,faudes_systime_t* end);
261
262// global performance times
263extern FAUDES_API faudes_systime_t gPerfTimer1;
264
265#endif // systime
266
267
268// have IP network
269#ifdef FAUDES_NETWORK
270
271// Uniform POSIX sockets (see iop_modbus.cpp and iop_simplenet.cpp)
272extern FAUDES_API int faudes_closesocket(int fd);
273extern FAUDES_API int faudes_setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen);
274extern FAUDES_API int faudes_getsockopt(int fd, int level, int optname, void *optval, socklen_t *optlen);
275extern FAUDES_API int faudes_setsocket_nonblocking(int fd, bool noblo);
276extern FAUDES_API int faudes_getsocket_error(int fd);
277
278// POSIX sockets to have BSD style REUSEPORT option (see iop_modbus.cpp and iop_simplenet.cpp)
279#ifndef SO_REUSEPORT
280#define SO_REUSEPORT SO_REUSEADDR
281#endif
282
283#endif
284
285
286#ifdef FAUDES_THREADS
287
288/*
289The remaining section of this file provides elementary support for threads,
290using a minimalistic subset of the POSIX threads interface. It is tailored for
291the use of edge-detection and networking as required by the iodevice plug-in.
292In general, libFAUDES is not threadsafe due to global variables, e.g.
293symoltables. This may change in a future revision.
294*/
295
296
297// Common return codes
298#define FAUDES_THREAD_SUCCESS 0
299#define FAUDES_THREAD_ERROR 1
300#define FAUDES_THREAD_TIMEOUT 2
301
302// Thread data type (use plain POSIX thread)
303#ifdef FAUDES_POSIX
304typedef pthread_t faudes_thread_t;
305#endif
306
307// Thread data type
308// We wrap the client function to provide pointer-typed
309// return values (as opposed to Windows int-typed return values)
310#ifdef FAUDES_WINDOWS
311typedef struct {
312 HANDLE mHandle; // Windows thread handle
313 void *(*mFnct)(void *); // client function
314 void *mArg; // argument
315 void *mRes; // result
316} faudes_thread_record_t;
317typedef faudes_thread_record_t* faudes_thread_t;
318#endif
319
320// not supported on generic platform
321#ifdef FAUDES_GENERIC
322#error option threads not available on generic platform
323#endif
324
325
326// Thread functions
327extern FAUDES_API int faudes_thread_create(faudes_thread_t *thr, void *(*fnct)(void *), void *arg);
328extern FAUDES_API faudes_thread_t faudes_thread_current(void);
329extern FAUDES_API int faudes_thread_detach(faudes_thread_t thr);
330extern FAUDES_API int faudes_thread_equal(faudes_thread_t thr0, faudes_thread_t thr1);
331extern FAUDES_API void faudes_thread_exit(void* res);
332extern int faudes_thread_join(faudes_thread_t thr, void **res);
333
334// Mutex data type (use plain POSIX mutex)
335#ifdef FAUDES_POSIX
336typedef pthread_mutex_t faudes_mutex_t;
337#endif
338
339// Mutex data type (use Windows "critical section")
340#ifdef FAUDES_WINDOWS
341typedef CRITICAL_SECTION faudes_mutex_t;
342#endif
343
344// Mutex functions
345extern FAUDES_API int faudes_mutex_init(faudes_mutex_t* mtx);
346extern FAUDES_API void faudes_mutex_destroy(faudes_mutex_t* mtx);
347extern FAUDES_API int faudes_mutex_lock(faudes_mutex_t *mtx);
348extern FAUDES_API int faudes_mutex_trylock(faudes_mutex_t *mtx);
349extern FAUDES_API int faudes_mutex_unlock(faudes_mutex_t *mtx);
350
351// Condition variables (use plain POSIX cond vars)
352#ifdef FAUDES_POSIX
353typedef pthread_cond_t faudes_cond_t;
354#endif
355
356// Condition variables for Windows
357// The approach is taken from "Strategies for Implementing POSIX Condition Variables
358// on Win32" by Douglas C. Schmidt and Irfan Pyarali, Department of Computer
359// Science, Washington University.
360#ifdef FAUDES_WINDOWS
361typedef struct {
362 HANDLE mEvents[2]; // signal and broadcast event handles
363 unsigned int mWaitersCount; // count the number of waiters
364 CRITICAL_SECTION mWaitersCountMutex; // mutex access to waiterscount
365} faudes_cond_t;
366#endif
367
368// Condition functions
369extern FAUDES_API int faudes_cond_init(faudes_cond_t* cond);
370extern FAUDES_API void faudes_cond_destroy(faudes_cond_t* cond);
371extern FAUDES_API int faudes_cond_signal(faudes_cond_t *cond);
372extern FAUDES_API int faudes_cond_broadcast(faudes_cond_t *cond);
373extern FAUDES_API int faudes_cond_wait(faudes_cond_t *cond, faudes_mutex_t *mtx);
374extern FAUDES_API int faudes_cond_timedwait(faudes_cond_t *cond, faudes_mutex_t *mtx, const faudes_systime_t *abstime);
375extern FAUDES_API int faudes_cond_reltimedwait(faudes_cond_t *cond, faudes_mutex_t *mtx, faudes_mstime_t duration);
376
377
378
379#endif // threads
380
381#endif // header
382
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)
FAUDES_API const std::string & faudes_pathseps(void)
#define FAUDES_API
FAUDES_API const std::string & faudes_pathsep(void)
FAUDES_API int faudes_system(const std::string &cmd, const std::string &args)
FAUDES_API int faudes_chdir(const std::string &nwd)
FAUDES_API std::string faudes_normpath(const std::string &rExtPath)
FAUDES_API const char * faudes_strsignal(int sig)
FAUDES_API void faudes_termsignal(void(*sighandler)(int))
FAUDES_API void faudes_usleep(long int usec)
FAUDES_API std::string faudes_getwd(void)
FAUDES_API std::string faudes_extpath(const std::string &rNormIntPath)
FAUDES_API void faudes_sleep(long int sec)

libFAUDES 2.34g --- 2026.04.09 --- c++ api documentaion by doxygen