35 std::cerr << msg << std:endl;
60 signal(SIGTERM, sighandler);
63 signal(SIGINT, sighandler);
66 signal(SIGQUIT, sighandler);
69 signal(SIGHUP, sighandler);
72 signal(SIGABRT, sighandler);
79 return strsignal(sig);
93 #if defined(FAUDES_POSIX)
94 void faudes_gettimeofday(faudes_systime_t* now) {
96 gettimeofday(&nowval,0);
97 now->tv_sec=nowval.tv_sec;
98 now->tv_nsec=nowval.tv_usec * 1000;
102 #if defined(FAUDES_WINDOWS)
103 void faudes_gettimeofday(faudes_systime_t* now) {
105 nowval= timeGetTime();
106 now->tv_sec=nowval / 1000;
107 now->tv_nsec=(nowval % 1000) *1000000;
114 void faudes_diffsystime(
const faudes_systime_t& end,
const faudes_systime_t& begin, faudes_systime_t* res) {
115 res->tv_sec = end.tv_sec-begin.tv_sec;
116 res->tv_nsec = end.tv_nsec-begin.tv_nsec;
117 if(res->tv_nsec <= 0){
119 res->tv_nsec += 1000000000;
124 void faudes_diffsystime(
const faudes_systime_t& end,
const faudes_systime_t& begin, faudes_mstime_t* res) {
125 *res = 1000*( end.tv_sec-begin.tv_sec);
126 *res += (end.tv_nsec-begin.tv_nsec) / 1000000;
130 void faudes_sumsystime(
const faudes_systime_t& begin,
const faudes_systime_t& duration, faudes_systime_t* res) {
131 res->tv_sec = begin.tv_sec + duration.tv_sec;
132 res->tv_nsec = begin.tv_nsec + duration.tv_nsec;
133 if(res->tv_nsec >= 1000000000) {
135 res->tv_nsec-=1000000000;
141 faudes_systime_t now;
142 faudes_gettimeofday(&now);
143 faudes_systime_t delta;
144 delta.tv_sec = msecs/1000;
145 delta.tv_nsec = (msecs % 1000) *1000000;
151 faudes_systime_t now;
152 faudes_gettimeofday(&now);
153 faudes_systime_t delta;
154 delta.tv_sec = usecs/1000000;
155 delta.tv_nsec = (usecs % 1000000) * 1000;
163 #ifdef FAUDES_NETWORK
170 socklen_t len =
sizeof(opt);
171 int res = getsockopt(fd, SOL_SOCKET, SO_ERROR, &opt, &len) < 0 ? -1 : 0;
175 #ifdef FAUDES_WINDOWS
177 socklen_t len =
sizeof(opt);
178 int res = getsockopt(fd, SOL_SOCKET, SO_ERROR, (
char*) &opt, &len) < 0 ? -1 : 0;
187 int sopt=fcntl(fd, F_GETFL, NULL);
188 if(sopt<0)
return -1;
190 int rc=fcntl(fd, F_SETFL, sopt|O_NONBLOCK);
191 return rc < 0 ? -1 : 0;
193 int rc=fcntl(fd, F_SETFL, sopt& (~O_NONBLOCK));
194 return rc < 0 ? -1 : 0;
197 #ifdef FAUDES_WINDOWS
198 unsigned long onoff=0;
200 return ioctlsocket(fd, FIONBIO, &onoff) == SOCKET_ERROR ? -1 : 0;
212 #ifdef FAUDES_THREADS
216 #ifdef FAUDES_WINDOWS
217 DWORD faudes_thread_tlsidx=TLS_OUT_OF_INDEXES;
221 #ifdef FAUDES_WINDOWS
222 static unsigned WINAPI faudes_thread_wrapper(
void *argfth) {
224 faudes_thread_t fthread = (faudes_thread_t) argfth;
226 TlsSetValue(faudes_thread_tlsidx,fthread);
228 void *(*fnct)(
void*) = fthread->mFnct;
229 void *arg = fthread->mArg;
231 void* res = fnct(arg);
233 faudes_thread_exit(res);
240 #ifdef FAUDES_WINDOWS
241 int faudes_thread_create(faudes_thread_t *thr,
void *(*fnct)(
void *),
void *arg){
243 if(faudes_thread_tlsidx==TLS_OUT_OF_INDEXES)
244 faudes_thread_tlsidx=TlsAlloc();
245 if(faudes_thread_tlsidx==TLS_OUT_OF_INDEXES)
246 return FAUDES_THREAD_ERROR;
248 *thr = (faudes_thread_t) malloc(
sizeof(faudes_thread_record_t));
249 if(!*thr)
return FAUDES_THREAD_ERROR;
251 (*thr)->mFnct = fnct;
255 (*thr)->mHandle = (HANDLE) _beginthreadex(NULL, 0, faudes_thread_wrapper, (
void*) (*thr), 0, NULL);
257 return (*thr)->mHandle ? FAUDES_THREAD_SUCCESS : FAUDES_THREAD_ERROR;
261 #ifdef FAUDES_WINDOWS
263 faudes_thread_t faudes_thread_current(
void) {
265 faudes_thread_t fthread = (faudes_thread_t) TlsGetValue(faudes_thread_tlsidx);
271 #ifdef FAUDES_WINDOWS
273 int faudes_thread_detach(faudes_thread_t thr) {
274 CloseHandle(thr->mHandle);
277 return FAUDES_THREAD_SUCCESS;
281 #ifdef FAUDES_WINDOWS
283 int faudes_thread_equal(faudes_thread_t thr0, faudes_thread_t thr1) {
285 if( (thr0==NULL) && (thr1=NULL) )
return true;
286 if( (thr0==NULL) || (thr1=NULL) )
return false;
288 return thr0->mHandle == thr1->mHandle;
292 #ifdef FAUDES_WINDOWS
294 void faudes_thread_exit(
void *res) {
296 faudes_thread_t fthread = (faudes_thread_t) TlsGetValue(faudes_thread_tlsidx);
297 if(fthread) fthread->mRes=res;
303 #ifdef FAUDES_WINDOWS
305 int faudes_thread_join(faudes_thread_t thr,
void **res) {
309 DWORD rc = WaitForSingleObject(thr->mHandle, INFINITE);
311 if( (rc!=WAIT_FAILED) && (res)) *res = thr->mRes;
315 if(rc == WAIT_FAILED)
return FAUDES_THREAD_ERROR;
316 return FAUDES_THREAD_SUCCESS;
326 #ifdef FAUDES_WINDOWS
327 #define EVENT_SIGNAL 0
328 #define EVENT_BROADCAST 1
332 #ifdef FAUDES_WINDOWS
333 int faudes_cond_init(faudes_cond_t* cond) {
335 cond->mWaitersCount = 0;
336 InitializeCriticalSection(&cond->mWaitersCountMutex);
338 cond->mEvents[EVENT_SIGNAL]=CreateEvent(NULL, FALSE, FALSE, NULL);
340 cond->mEvents[EVENT_BROADCAST] = CreateEvent(NULL, TRUE, FALSE, NULL);
342 if(cond->mEvents[EVENT_SIGNAL] != NULL)
343 if(cond->mEvents[EVENT_BROADCAST] != NULL)
344 return FAUDES_THREAD_SUCCESS;
346 if(cond->mEvents[EVENT_SIGNAL] != NULL)
347 CloseHandle(cond->mEvents[EVENT_SIGNAL]);
348 if(cond->mEvents[EVENT_BROADCAST] != NULL)
349 CloseHandle(cond->mEvents[EVENT_BROADCAST]);
350 cond->mEvents[EVENT_BROADCAST] = NULL;
351 cond->mEvents[EVENT_SIGNAL]=NULL;
352 return FAUDES_THREAD_ERROR;
356 #ifdef FAUDES_WINDOWS
358 void faudes_cond_destroy(faudes_cond_t* cond) {
359 if(cond->mEvents[EVENT_SIGNAL] != NULL)
360 CloseHandle(cond->mEvents[EVENT_SIGNAL]);
361 if(cond->mEvents[EVENT_BROADCAST] != NULL)
362 CloseHandle(cond->mEvents[EVENT_BROADCAST]);
363 DeleteCriticalSection(&cond->mWaitersCountMutex);
367 #ifdef FAUDES_WINDOWS
369 int faudes_cond_signal(faudes_cond_t *cond){
372 EnterCriticalSection(&cond->mWaitersCountMutex);
373 waiters = (cond->mWaitersCount > 0);
374 LeaveCriticalSection(&cond->mWaitersCountMutex);
377 if(SetEvent(cond->mEvents[EVENT_SIGNAL]) == 0)
378 return FAUDES_THREAD_ERROR;
380 return FAUDES_THREAD_SUCCESS;
384 #ifdef FAUDES_WINDOWS
386 int faudes_cond_broadcast(faudes_cond_t *cond) {
389 EnterCriticalSection(&cond->mWaitersCountMutex);
390 waiters = (cond->mWaitersCount > 0);
391 LeaveCriticalSection(&cond->mWaitersCountMutex);
394 if(SetEvent(cond->mEvents[EVENT_BROADCAST]) == 0)
395 return FAUDES_THREAD_ERROR;
397 return FAUDES_THREAD_SUCCESS;
401 #ifdef FAUDES_WINDOWS
403 int faudes_cond_reltimedwait(faudes_cond_t *cond, faudes_mutex_t *mtx, faudes_mstime_t duration) {
405 EnterCriticalSection(&cond->mWaitersCountMutex);
406 ++ cond->mWaitersCount;
407 LeaveCriticalSection(&cond->mWaitersCountMutex);
409 LeaveCriticalSection(mtx);
411 int res = WaitForMultipleObjects(2, cond->mEvents, FALSE, (DWORD) duration);
413 EnterCriticalSection(&cond->mWaitersCountMutex);
415 -- cond->mWaitersCount;
418 (res == (WAIT_OBJECT_0 + EVENT_BROADCAST)) &&
419 (cond->mWaitersCount == 0);
420 LeaveCriticalSection(&cond->mWaitersCountMutex);
422 if(last) ResetEvent(cond->mEvents[EVENT_BROADCAST]);
424 EnterCriticalSection(mtx);
426 if(res == (
int) WAIT_TIMEOUT)
427 return FAUDES_THREAD_TIMEOUT;
428 if(res == (
int) WAIT_FAILED)
429 return FAUDES_THREAD_ERROR;
430 return FAUDES_THREAD_SUCCESS;
434 #ifdef FAUDES_WINDOWS
436 int faudes_cond_wait(faudes_cond_t *cond, faudes_mutex_t *mtx) {
437 return faudes_cond_reltimedwait(cond, mtx, INFINITE);
441 #ifdef FAUDES_WINDOWS
443 int faudes_cond_timedwait(faudes_cond_t *cond, faudes_mutex_t *mtx,
const faudes_systime_t *abstime) {
445 faudes_systime_t now;
446 faudes_gettimeofday(&now);
448 DWORD rels = abstime->tv_sec - now.tv_sec;
450 if(rels > (4294967295u/1000)-1) rels = 4294967295u/1000-1;
452 DWORD relms = rels*1000 + (abstime->tv_nsec - now.tv_nsec + 500000) / 1000000;
454 return faudes_cond_reltimedwait(cond, mtx, relms);