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
76 #include <sys/socket.h>
77 #include <arpa/inet.h>
92 #ifndef WIN32_LEAN_AND_MEAN
93 #define WIN32_LEAN_AND_MEAN
94 #define FAUDES_LEAN_AND_MEAN
97 #ifdef FAUDES_LEAN_AND_MEAN
98 #undef FAUDES_LEAN_AND_MEAN
99 #undef WIN32_LEAN_AND_MEAN
102 #ifdef FAUDES_SYSTIME
105 #include <mmsystem.h>
108 #ifdef FAUDES_NETWORK
109 #include <winsock2.h>
113 #ifdef FAUDES_THREADS
160 #ifdef FAUDES_WINDOWS
161 inline void faudes_sleep(
long int sec) {Sleep((sec) * 1000);}
162 inline void faudes_usleep(
long int usec) {Sleep((usec) / 1000);}
164 #ifdef FAUDES_GENERIC
171 #ifdef FAUDES_SYSTIME
175 typedef timespec faudes_systime_t;
176 typedef long int faudes_mstime_t;
178 #ifdef FAUDES_WINDOWS
183 typedef long int faudes_mstime_t;
185 #ifdef FAUDES_GENERIC
186 #error option systime not available on generic platform
191 void faudes_gettimeofday(faudes_systime_t* now);
192 void faudes_diffsystime(
const faudes_systime_t& end,
const faudes_systime_t& begin, faudes_systime_t* res);
193 void faudes_diffsystime(
const faudes_systime_t& end,
const faudes_systime_t& begin, faudes_mstime_t* res);
194 void faudes_sumsystime(
const faudes_systime_t& begin,
const faudes_systime_t& duration, faudes_systime_t* res);
205 #ifdef FAUDES_NETWORK
209 inline int faudes_closesocket(
int fd) {
return close(fd);}
210 inline int faudes_setsockopt(
int fd,
int level,
int optname,
const void *optval, socklen_t optlen) {
211 return setsockopt(fd,level,optname,optval,optlen);}
212 inline int faudes_getsockopt(
int fd,
int level,
int optname,
void *optval, socklen_t *optlen) {
213 return getsockopt(fd,level,optname,optval,optlen);}
217 #ifdef FAUDES_WINDOWS
218 typedef int socklen_t;
219 inline int faudes_closesocket(
int fd) {
return closesocket(fd);}
220 inline int faudes_setsockopt(
int fd,
int level,
int optname,
const void *optval, socklen_t optlen) {
221 return setsockopt(fd,level,optname,(
char*) optval,optlen);}
222 inline int faudes_getsockopt(
int fd,
int level,
int optname,
void *optval, socklen_t *optlen) {
223 return getsockopt(fd,level,optname,(
char*) optval,optlen);}
227 #ifdef FAUDES_GENERIC
228 #error option network not available on generic platform
233 #define SO_REUSEPORT SO_REUSEADDR
239 #ifdef FAUDES_THREADS
251 #define FAUDES_THREAD_SUCCESS 0
252 #define FAUDES_THREAD_ERROR 1
253 #define FAUDES_THREAD_TIMEOUT 2
259 typedef pthread_t faudes_thread_t;
261 inline int faudes_thread_create(faudes_thread_t *thr,
void *(*fnct)(
void *),
void *arg){
264 pthread_attr_init(&attr);
265 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
267 int ret = pthread_create(thr, &attr, fnct, arg);
269 pthread_attr_destroy(&attr);
270 return ret == 0 ? FAUDES_THREAD_SUCCESS : FAUDES_THREAD_ERROR;
272 inline faudes_thread_t faudes_thread_current(
void) {
273 return pthread_self();
275 inline int faudes_thread_detach(faudes_thread_t thr) {
276 return pthread_detach(thr)==0 ? FAUDES_THREAD_SUCCESS : FAUDES_THREAD_ERROR;
278 inline int faudes_thread_equal(faudes_thread_t thr0, faudes_thread_t thr1) {
279 return pthread_equal(thr0, thr1);
281 inline void faudes_thread_exit(
void* res) {
284 inline int faudes_thread_join(faudes_thread_t thr,
void **res) {
285 return pthread_join(thr, res) == 0 ? FAUDES_THREAD_ERROR : FAUDES_THREAD_SUCCESS;
290 #ifdef FAUDES_WINDOWS
296 void *(*mFnct)(
void *);
299 } faudes_thread_record_t;
300 typedef faudes_thread_record_t* faudes_thread_t;
302 int faudes_thread_create(faudes_thread_t *thr,
void *(*fnct)(
void *),
void *arg);
303 faudes_thread_t faudes_thread_current(
void);
304 int faudes_thread_detach(faudes_thread_t thr);
305 int faudes_thread_equal(faudes_thread_t thr0, faudes_thread_t thr1);
306 void faudes_thread_exit(
void* res);
307 int faudes_thread_join(faudes_thread_t thr,
void **res);
313 typedef pthread_mutex_t faudes_mutex_t;
315 inline int faudes_mutex_init(faudes_mutex_t* mtx){
316 return pthread_mutex_init(mtx, NULL)==0 ? FAUDES_THREAD_SUCCESS : FAUDES_THREAD_ERROR;
318 inline void faudes_mutex_destroy(faudes_mutex_t* mtx){
319 pthread_mutex_destroy(mtx);
321 inline int faudes_mutex_lock(faudes_mutex_t *mtx) {
322 return pthread_mutex_lock(mtx) == 0 ? FAUDES_THREAD_SUCCESS : FAUDES_THREAD_ERROR;
324 inline int faudes_mutex_trylock(faudes_mutex_t *mtx){
325 return (pthread_mutex_trylock(mtx) == 0) ? FAUDES_THREAD_SUCCESS : FAUDES_THREAD_ERROR;
327 inline int faudes_mutex_unlock(faudes_mutex_t *mtx){
328 return pthread_mutex_unlock(mtx) == 0 ? FAUDES_THREAD_SUCCESS : FAUDES_THREAD_ERROR;
333 #ifdef FAUDES_WINDOWS
335 typedef CRITICAL_SECTION faudes_mutex_t;
337 inline int faudes_mutex_init(faudes_mutex_t *mtx){
338 InitializeCriticalSection(mtx);
339 return FAUDES_THREAD_SUCCESS;
341 inline void faudes_mutex_destroy(faudes_mutex_t *mtx){
342 DeleteCriticalSection(mtx);
344 inline int faudes_mutex_lock(faudes_mutex_t *mtx) {
345 EnterCriticalSection(mtx);
346 return FAUDES_THREAD_SUCCESS;
348 inline int faudes_mutex_trylock(faudes_mutex_t *mtx){
349 return TryEnterCriticalSection(mtx) ? FAUDES_THREAD_SUCCESS : FAUDES_THREAD_ERROR;
351 inline int faudes_mutex_unlock(faudes_mutex_t *mtx){
352 LeaveCriticalSection(mtx);
353 return FAUDES_THREAD_SUCCESS;
360 typedef pthread_cond_t faudes_cond_t;
362 inline int faudes_cond_init(faudes_cond_t* cond) {
363 return pthread_cond_init(cond, NULL) == 0 ? FAUDES_THREAD_SUCCESS : FAUDES_THREAD_ERROR;
365 inline void faudes_cond_destroy(faudes_cond_t* cond) {
366 pthread_cond_destroy(cond);
368 inline int faudes_cond_signal(faudes_cond_t *cond){
369 return pthread_cond_signal(cond) == 0 ? FAUDES_THREAD_SUCCESS : FAUDES_THREAD_ERROR;
371 inline int faudes_cond_broadcast(faudes_cond_t *cond) {
372 return pthread_cond_signal(cond) == 0 ? FAUDES_THREAD_SUCCESS : FAUDES_THREAD_ERROR;
374 inline int faudes_cond_wait(faudes_cond_t *cond, faudes_mutex_t *mtx) {
375 return pthread_cond_wait(cond, mtx) == 0 ? FAUDES_THREAD_SUCCESS : FAUDES_THREAD_ERROR;
377 inline int faudes_cond_timedwait(faudes_cond_t *cond, faudes_mutex_t *mtx,
const faudes_systime_t *abstime) {
378 int ret = pthread_cond_timedwait(cond, mtx, abstime);
379 if(ret == ETIMEDOUT)
return FAUDES_THREAD_TIMEOUT;
380 return ret == 0 ? FAUDES_THREAD_SUCCESS : FAUDES_THREAD_ERROR;
383 inline int faudes_cond_reltimedwait(faudes_cond_t *cond, faudes_mutex_t *mtx, faudes_mstime_t duration) {
384 faudes_systime_t abstime;
386 return faudes_cond_timedwait(cond,mtx,&abstime);
391 #ifdef FAUDES_WINDOWS
398 unsigned int mWaitersCount;
399 CRITICAL_SECTION mWaitersCountMutex;
402 int faudes_cond_init(faudes_cond_t* cond);
403 void faudes_cond_destroy(faudes_cond_t* cond);
404 int faudes_cond_signal(faudes_cond_t *cond);
405 int faudes_cond_broadcast(faudes_cond_t *cond);
406 int faudes_cond_wait(faudes_cond_t *cond, faudes_mutex_t *mtx);
407 int faudes_cond_timedwait(faudes_cond_t *cond, faudes_mutex_t *mtx,
const faudes_systime_t *abstime);
408 int faudes_cond_reltimedwait(faudes_cond_t *cond, faudes_mutex_t *mtx, faudes_mstime_t duration);
413 #ifdef FAUDES_GENERIC
414 #error option threads not available on generic platform