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

libFAUDES 2.33h --- 2025.06.18 --- c++ api documentaion by doxygen