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: posix/gcc */
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 // include 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
179
180
181
182// Path-seperators (see cfl_utils.cpp)
183// * the first separator is the one used to prepend directories etc
184// * all other separators are used to extract filenames, i.e., to strip the path
185// * as of libFAUDES 2.32 we internally treat "/" is our separator; we set
186// path seperator to "/" for POSIX and "/\\:" for Windows.
187// * up to libFAUDES 2.31 we used "/" for POSIX and "\\:/" for Windows; this was
188// asking for trouble.
189extern FAUDES_API const std::string& faudes_pathseps(void);
190// Path-seperator (first char of above, see cfl_utils.cpp)
191extern FAUDES_API const std::string& faudes_pathsep(void);
192
193
194// Extanal vs internal paths conversion -- we are so bored
195//
196// Internal is as of v2.32 posix style, i.e., '/' is the only separtor, no
197// drive letters whatsowever. Is need be, they are converted along the
198// pattern "C:\ ==> /c/". libFAUDES should not operate on absolute oaths anyway.
199// Occasionally (when a libFAUDES tool needs to invoke a shell), we need to
200// convery back to external representaion. Likewise, libFAUDES tools may be
201// invoked with posix style argumetns and may need to convert. Henve the following
202// two conversion functons (which should be identity on posix systems)
203extern FAUDES_API std::string faudes_normpath(const std::string& rExtPath);
204extern FAUDES_API std::string faudes_extpath(const std::string& rNormIntPath);
205
206
207
208
209// uniform get/set dir (use posix style interface)
210extern FAUDES_API std::string faudes_getwd(void);
211extern FAUDES_API int faudes_chdir(const std::string& nwd);
212
213// Uniform exit-signalhandler for POSIX/Windows (see e.g. simfaudes.cpp)
214extern FAUDES_API void faudes_termsignal(void (*sighandler)(int));
215
216// Uniform signal names for POSIX/Windows (see e.g. simfaudes.cpp)
217extern FAUDES_API const char* faudes_strsignal(int sig);
218
219// Uniform sleep for POSIX/Windows (see e.g. iodevice plug-in)
220extern FAUDES_API void faudes_sleep(long int sec);
221extern FAUDES_API void faudes_usleep(long int usec);
222
223
224// have time
225#ifdef FAUDES_SYSTIME
226
227// Uniform system time using POSIX pthreads semantics
228#ifdef FAUDES_POSIX
229typedef timespec faudes_systime_t;
230typedef long int faudes_mstime_t;
231#endif
232#ifdef FAUDES_WINDOWS
233typedef struct {
234 long int tv_sec;
235 long int tv_nsec;
236} faudes_systime_t;
237typedef long int faudes_mstime_t;
238#endif
239#ifdef FAUDES_GENERIC
240#error option systime not available on generic platform
241#endif
242
243// Uniform system time definitions
244extern FAUDES_API void faudes_gettimeofday(faudes_systime_t* now);
245extern FAUDES_API void faudes_diffsystime(const faudes_systime_t& end, const faudes_systime_t& begin, faudes_systime_t* res);
246extern FAUDES_API void faudes_diffsystime(const faudes_systime_t& end, const faudes_systime_t& begin, faudes_mstime_t* res);
247extern FAUDES_API void faudes_sumsystime(const faudes_systime_t& begin, const faudes_systime_t& duration, faudes_systime_t* res);
248extern FAUDES_API void faudes_msdelay(faudes_mstime_t msecs,faudes_systime_t* end);
249extern FAUDES_API void faudes_usdelay(faudes_mstime_t usecs,faudes_systime_t* end);
250
251// global performance times
252extern FAUDES_API faudes_systime_t gPerfTimer1;
253
254#endif // systime
255
256
257// have IP network
258#ifdef FAUDES_NETWORK
259
260// Uniform POSIX sockets (see iop_modbus.cpp and iop_simplenet.cpp)
261extern FAUDES_API int faudes_closesocket(int fd);
262extern FAUDES_API int faudes_setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen);
263extern FAUDES_API int faudes_getsockopt(int fd, int level, int optname, void *optval, socklen_t *optlen);
264extern FAUDES_API int faudes_setsocket_nonblocking(int fd, bool noblo);
265extern FAUDES_API int faudes_getsocket_error(int fd);
266
267// POSIX sockets to have BSD style REUSEPORT option (see iop_modbus.cpp and iop_simplenet.cpp)
268#ifndef SO_REUSEPORT
269#define SO_REUSEPORT SO_REUSEADDR
270#endif
271
272#endif
273
274
275#ifdef FAUDES_THREADS
276
277/*
278The remaining section of this file provides elementary support for threads,
279using a minimalistic subset of the POSIX threads interface. It is tailored for
280the use of edge-detection and networking as required by the iodevice plug-in.
281In general, libFAUDES is not threadsafe due to global variables, e.g.
282symoltables. This may change in a future revision.
283*/
284
285
286// Common return codes
287#define FAUDES_THREAD_SUCCESS 0
288#define FAUDES_THREAD_ERROR 1
289#define FAUDES_THREAD_TIMEOUT 2
290
291// Thread data type (use plain POSIX thread)
292#ifdef FAUDES_POSIX
293typedef pthread_t faudes_thread_t;
294#endif
295
296// Thread data type
297// We wrap the client function to provide pointer-typed
298// return values (as opposed to Windows int-typed return values)
299#ifdef FAUDES_WINDOWS
300typedef struct {
301 HANDLE mHandle; // Windows thread handle
302 void *(*mFnct)(void *); // client function
303 void *mArg; // argument
304 void *mRes; // result
305} faudes_thread_record_t;
306typedef faudes_thread_record_t* faudes_thread_t;
307#endif
308
309// not supported on generic platform
310#ifdef FAUDES_GENERIC
311#error option threads not available on generic platform
312#endif
313
314
315// Thread functions
316extern FAUDES_API int faudes_thread_create(faudes_thread_t *thr, void *(*fnct)(void *), void *arg);
317extern FAUDES_API faudes_thread_t faudes_thread_current(void);
318extern FAUDES_API int faudes_thread_detach(faudes_thread_t thr);
319extern FAUDES_API int faudes_thread_equal(faudes_thread_t thr0, faudes_thread_t thr1);
320extern FAUDES_API void faudes_thread_exit(void* res);
321extern int faudes_thread_join(faudes_thread_t thr, void **res);
322
323// Mutex data type (use plain POSIX mutex)
324#ifdef FAUDES_POSIX
325typedef pthread_mutex_t faudes_mutex_t;
326#endif
327
328// Mutex data type (use Windows "critical section")
329#ifdef FAUDES_WINDOWS
330typedef CRITICAL_SECTION faudes_mutex_t;
331#endif
332
333// Mutex functions
334extern FAUDES_API int faudes_mutex_init(faudes_mutex_t* mtx);
335extern FAUDES_API void faudes_mutex_destroy(faudes_mutex_t* mtx);
336extern FAUDES_API int faudes_mutex_lock(faudes_mutex_t *mtx);
337extern FAUDES_API int faudes_mutex_trylock(faudes_mutex_t *mtx);
338extern FAUDES_API int faudes_mutex_unlock(faudes_mutex_t *mtx);
339
340// Condition variables (use plain POSIX cond vars)
341#ifdef FAUDES_POSIX
342typedef pthread_cond_t faudes_cond_t;
343#endif
344
345// Condition variables for Windows
346// The approach is taken from "Strategies for Implementing POSIX Condition Variables
347// on Win32" by Douglas C. Schmidt and Irfan Pyarali, Department of Computer
348// Science, Washington University.
349#ifdef FAUDES_WINDOWS
350typedef struct {
351 HANDLE mEvents[2]; // signal and broadcast event handles
352 unsigned int mWaitersCount; // count the number of waiters
353 CRITICAL_SECTION mWaitersCountMutex; // mutex access to waiterscount
354} faudes_cond_t;
355#endif
356
357// Condition functions
358extern FAUDES_API int faudes_cond_init(faudes_cond_t* cond);
359extern FAUDES_API void faudes_cond_destroy(faudes_cond_t* cond);
360extern FAUDES_API int faudes_cond_signal(faudes_cond_t *cond);
361extern FAUDES_API int faudes_cond_broadcast(faudes_cond_t *cond);
362extern FAUDES_API int faudes_cond_wait(faudes_cond_t *cond, faudes_mutex_t *mtx);
363extern FAUDES_API int faudes_cond_timedwait(faudes_cond_t *cond, faudes_mutex_t *mtx, const faudes_systime_t *abstime);
364extern FAUDES_API int faudes_cond_reltimedwait(faudes_cond_t *cond, faudes_mutex_t *mtx, faudes_mstime_t duration);
365
366
367
368#endif // threads
369
370#endif // header
371
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_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.33k --- 2025.09.16 --- c++ api documentaion by doxygen