23 #ifndef FAUDES_PLATFORM_H
24 #define FAUDES_PLATFORM_H
28 #ifndef FAUDES_WINDOWS
29 #ifndef FAUDES_GENERIC
31 #if defined(_WIN32) || defined(__WIN32__) || defined(__WINDOWS__)
32 #define FAUDES_WINDOWS
34 #if defined (__unix__) || (__linus__) || (__linux__) || (defined (__APPLE__) && defined (__MACH__))
39 #ifndef FAUDES_WINDOWS
40 #define FAUDES_GENERIC
51 #ifdef FAUDES_BUILD_DSO
53 #define FAUDES_API __attribute__ ((dllexport))
55 #define FAUDES_API __declspec(dllexport)
58 #ifdef FAUDES_BUILD_APP
60 #define FAUDES_API __attribute__ ((dllimport))
62 #define FAUDES_API __declspec(dllimport)
69 #ifdef FAUDES_BUILD_DSO
71 #define FAUDES_API __attribute__ ((visibility ("default")))
74 #ifdef FAUDES_BUILD_APP
112 #ifdef FAUDES_SYSTIME
114 #include <sys/time.h>
117 #ifdef FAUDES_NETWORK
118 #include <sys/socket.h>
119 #include <arpa/inet.h>
123 #ifdef FAUDES_THREADS
131 #ifdef FAUDES_WINDOWS
133 #ifndef WIN32_LEAN_AND_MEAN
134 #define WIN32_LEAN_AND_MEAN
135 #define FAUDES_LEAN_AND_MEAN
138 #ifdef FAUDES_LEAN_AND_MEAN
139 #undef FAUDES_LEAN_AND_MEAN
140 #undef WIN32_LEAN_AND_MEAN
143 #ifdef FAUDES_SYSTIME
146 #include <mmsystem.h>
149 #ifdef FAUDES_NETWORK
150 #include <winsock2.h>
154 #ifdef FAUDES_THREADS
201 #ifdef FAUDES_WINDOWS
205 #ifdef FAUDES_GENERIC
212 #ifdef FAUDES_SYSTIME
216 typedef timespec faudes_systime_t;
217 typedef long int faudes_mstime_t;
219 #ifdef FAUDES_WINDOWS
224 typedef long int faudes_mstime_t;
226 #ifdef FAUDES_GENERIC
227 #error option systime not available on generic platform
232 extern FAUDES_API void faudes_gettimeofday(faudes_systime_t* now);
246 #ifdef FAUDES_NETWORK
250 inline FAUDES_API int faudes_closesocket(
int fd) {
return close(fd);}
251 inline FAUDES_API int faudes_setsockopt(
int fd,
int level,
int optname,
const void *optval, socklen_t optlen) {
252 return setsockopt(fd,level,optname,optval,optlen);}
253 inline FAUDES_API int faudes_getsockopt(
int fd,
int level,
int optname,
void *optval, socklen_t *optlen) {
254 return getsockopt(fd,level,optname,optval,optlen);}
258 #ifdef FAUDES_WINDOWS
259 typedef int socklen_t;
260 inline FAUDES_API int faudes_closesocket(
int fd) {
return closesocket(fd);}
261 inline FAUDES_API int faudes_setsockopt(
int fd,
int level,
int optname,
const void *optval, socklen_t optlen) {
262 return setsockopt(fd,level,optname,(
char*) optval,optlen);}
263 inline FAUDES_API int faudes_getsockopt(
int fd,
int level,
int optname,
void *optval, socklen_t *optlen) {
264 return getsockopt(fd,level,optname,(
char*) optval,optlen);}
268 #ifdef FAUDES_GENERIC
269 #error option network not available on generic platform
274 #define SO_REUSEPORT SO_REUSEADDR
280 #ifdef FAUDES_THREADS
292 #define FAUDES_THREAD_SUCCESS 0
293 #define FAUDES_THREAD_ERROR 1
294 #define FAUDES_THREAD_TIMEOUT 2
300 typedef pthread_t faudes_thread_t;
302 inline FAUDES_API int faudes_thread_create(faudes_thread_t *thr,
void *(*fnct)(
void *),
void *arg){
305 pthread_attr_init(&attr);
306 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
308 int ret = pthread_create(thr, &attr, fnct, arg);
310 pthread_attr_destroy(&attr);
311 return ret == 0 ? FAUDES_THREAD_SUCCESS : FAUDES_THREAD_ERROR;
313 inline FAUDES_API faudes_thread_t faudes_thread_current(
void) {
314 return pthread_self();
316 inline FAUDES_API int faudes_thread_detach(faudes_thread_t thr) {
317 return pthread_detach(thr)==0 ? FAUDES_THREAD_SUCCESS : FAUDES_THREAD_ERROR;
319 inline FAUDES_API int faudes_thread_equal(faudes_thread_t thr0, faudes_thread_t thr1) {
320 return pthread_equal(thr0, thr1);
322 inline FAUDES_API void faudes_thread_exit(
void* res) {
325 inline int faudes_thread_join(faudes_thread_t thr,
void **res) {
326 return pthread_join(thr, res) == 0 ? FAUDES_THREAD_ERROR : FAUDES_THREAD_SUCCESS;
331 #ifdef FAUDES_WINDOWS
337 void *(*mFnct)(
void *);
340 } faudes_thread_record_t;
341 typedef faudes_thread_record_t* faudes_thread_t;
343 extern FAUDES_API int faudes_thread_create(faudes_thread_t *thr,
void *(*fnct)(
void *),
void *arg);
344 extern FAUDES_API faudes_thread_t faudes_thread_current(
void);
345 extern FAUDES_API int faudes_thread_detach(faudes_thread_t thr);
346 extern FAUDES_API int faudes_thread_equal(faudes_thread_t thr0, faudes_thread_t thr1);
347 extern FAUDES_API void faudes_thread_exit(
void* res);
348 extern FAUDES_API int faudes_thread_join(faudes_thread_t thr,
void **res);
354 typedef pthread_mutex_t faudes_mutex_t;
356 inline FAUDES_API int faudes_mutex_init(faudes_mutex_t* mtx){
357 return pthread_mutex_init(mtx, NULL)==0 ? FAUDES_THREAD_SUCCESS : FAUDES_THREAD_ERROR;
359 inline FAUDES_API void faudes_mutex_destroy(faudes_mutex_t* mtx){
360 pthread_mutex_destroy(mtx);
362 inline FAUDES_API int faudes_mutex_lock(faudes_mutex_t *mtx) {
363 return pthread_mutex_lock(mtx) == 0 ? FAUDES_THREAD_SUCCESS : FAUDES_THREAD_ERROR;
365 inline FAUDES_API int faudes_mutex_trylock(faudes_mutex_t *mtx){
366 return (pthread_mutex_trylock(mtx) == 0) ? FAUDES_THREAD_SUCCESS : FAUDES_THREAD_ERROR;
368 inline FAUDES_API int faudes_mutex_unlock(faudes_mutex_t *mtx){
369 return pthread_mutex_unlock(mtx) == 0 ? FAUDES_THREAD_SUCCESS : FAUDES_THREAD_ERROR;
374 #ifdef FAUDES_WINDOWS
376 typedef CRITICAL_SECTION faudes_mutex_t;
378 inline FAUDES_API int faudes_mutex_init(faudes_mutex_t *mtx){
379 InitializeCriticalSection(mtx);
380 return FAUDES_THREAD_SUCCESS;
382 inline FAUDES_API void faudes_mutex_destroy(faudes_mutex_t *mtx){
383 DeleteCriticalSection(mtx);
385 inline FAUDES_API int faudes_mutex_lock(faudes_mutex_t *mtx) {
386 EnterCriticalSection(mtx);
387 return FAUDES_THREAD_SUCCESS;
389 inline FAUDES_API int faudes_mutex_trylock(faudes_mutex_t *mtx){
390 return TryEnterCriticalSection(mtx) ? FAUDES_THREAD_SUCCESS : FAUDES_THREAD_ERROR;
392 inline FAUDES_API int faudes_mutex_unlock(faudes_mutex_t *mtx){
393 LeaveCriticalSection(mtx);
394 return FAUDES_THREAD_SUCCESS;
401 typedef pthread_cond_t faudes_cond_t;
403 inline FAUDES_API int faudes_cond_init(faudes_cond_t* cond) {
404 return pthread_cond_init(cond, NULL) == 0 ? FAUDES_THREAD_SUCCESS : FAUDES_THREAD_ERROR;
406 inline FAUDES_API void faudes_cond_destroy(faudes_cond_t* cond) {
407 pthread_cond_destroy(cond);
409 inline FAUDES_API int faudes_cond_signal(faudes_cond_t *cond){
410 return pthread_cond_signal(cond) == 0 ? FAUDES_THREAD_SUCCESS : FAUDES_THREAD_ERROR;
412 inline FAUDES_API int faudes_cond_broadcast(faudes_cond_t *cond) {
413 return pthread_cond_signal(cond) == 0 ? FAUDES_THREAD_SUCCESS : FAUDES_THREAD_ERROR;
415 inline FAUDES_API int faudes_cond_wait(faudes_cond_t *cond, faudes_mutex_t *mtx) {
416 return pthread_cond_wait(cond, mtx) == 0 ? FAUDES_THREAD_SUCCESS : FAUDES_THREAD_ERROR;
418 inline FAUDES_API int faudes_cond_timedwait(faudes_cond_t *cond, faudes_mutex_t *mtx,
const faudes_systime_t *abstime) {
419 int ret = pthread_cond_timedwait(cond, mtx, abstime);
420 if(ret == ETIMEDOUT)
return FAUDES_THREAD_TIMEOUT;
421 return ret == 0 ? FAUDES_THREAD_SUCCESS : FAUDES_THREAD_ERROR;
424 inline FAUDES_API int faudes_cond_reltimedwait(faudes_cond_t *cond, faudes_mutex_t *mtx, faudes_mstime_t duration) {
425 faudes_systime_t abstime;
427 return faudes_cond_timedwait(cond,mtx,&abstime);
432 #ifdef FAUDES_WINDOWS
439 unsigned int mWaitersCount;
440 CRITICAL_SECTION mWaitersCountMutex;
443 extern FAUDES_API int faudes_cond_init(faudes_cond_t* cond);
444 extern FAUDES_API void faudes_cond_destroy(faudes_cond_t* cond);
445 extern FAUDES_API int faudes_cond_signal(faudes_cond_t *cond);
446 extern FAUDES_API int faudes_cond_broadcast(faudes_cond_t *cond);
447 extern FAUDES_API int faudes_cond_wait(faudes_cond_t *cond, faudes_mutex_t *mtx);
448 extern FAUDES_API int faudes_cond_timedwait(faudes_cond_t *cond, faudes_mutex_t *mtx,
const faudes_systime_t *abstime);
449 extern FAUDES_API int faudes_cond_reltimedwait(faudes_cond_t *cond, faudes_mutex_t *mtx, faudes_mstime_t duration);
454 #ifdef FAUDES_GENERIC
455 #error option threads not available on generic platform