From 6f8e168ae3de58300b44b3526295256ebf5dda8e Mon Sep 17 00:00:00 2001 From: leitner Date: Fri, 5 Apr 2024 17:37:38 +0000 Subject: [PATCH] proper return value handling --- io/iom_wait.c | 19 ++++++++++++++++--- test/iom.c | 9 +++++++-- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/io/iom_wait.c b/io/iom_wait.c index 5647f34..bec2c91 100644 --- a/io/iom_wait.c +++ b/io/iom_wait.c @@ -175,23 +175,36 @@ int iom_wait(iomux_t* c,int64* s,unsigned int* revents,unsigned long timeout) { gettimeofday(&tv, NULL); tv.tv_sec += timeout/1000; tv.tv_usec += timeout%1000; - if (tv.tv_usec>1000) { - tv.tv_usec-=1000; + if (tv.tv_usec>1000000) { + tv.tv_usec-=1000000; ++tv.tv_sec; } ts.tv_sec = tv.tv_sec; ts.tv_nsec = tv.tv_usec * 1000; #ifdef __dietlibc__ r=cnd_timedwait(&c->sem,&c->mtx,&ts); + switch (r) { + case thrd_success: + continue; + case thrd_timedout: + return 0; + case thrd_error: + return -1; + } #elif defined(__APPLE__) r=pthread_cond_timedwait(&c->sem,&c->mtx,&ts); + switch (r) { + case 0: continue; + case ETIMEDOUT: return 0; + default: return -1; + } #else r=sem_timedwait(&c->sem,&ts); -#endif if (r==-1) { if (errno==ETIMEDOUT) return 0; return -1; } +#endif /* fall through into next loop iteration */ } } diff --git a/test/iom.c b/test/iom.c index 8f334ae..a1c6100 100644 --- a/test/iom.c +++ b/test/iom.c @@ -14,7 +14,7 @@ int worker(void* arg) { uintptr_t i=(uintptr_t)arg; char buf[100]; int64 s; - int events; + unsigned int events; write(1,buf,sprintf(buf,"starting thread %ld\n",i)); @@ -73,7 +73,10 @@ int main() { if (thrd_join(x[i],&r)==-1) #else void* tmp; - if (pthread_join(x[i],&tmp)==-1, r=(int)(uintptr_t)tmp) + + if (pthread_join(x[i],&tmp)!=-1) + r=(int)(uintptr_t)tmp; + else #endif { perror("thrd_join"); @@ -82,5 +85,7 @@ int main() { printf("thread %d returned %d\n",i,r); } + fflush(stdout); + return 0; }