phattaradanai / libxbee

Automatically exported from code.google.com/p/libxbee
0 stars 0 forks source link

xsys_sem_timedwait not setting errno correctly on Win32 #17

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Attempting to use the Win32 library results in error -7 semaphore errors. The 
reason is that xsys_sem_timedwait returns the result from WaitForSingleObject 
directly, without checking for the error code.

I have pasted below a corrected version:

int xsys_sem_timedwait(xsys_sem *sem, struct timespec *timeout) {
    DWORD dwMiliseconds;
    int ret;
    if (timeout) {
        struct timespec now;

        /* calculate epoch miliseconds */
        dwMiliseconds  = timeout->tv_sec * 1000;
        dwMiliseconds += timeout->tv_nsec / 1000000;

        /* offset that from 'now' */
        clock_gettime(CLOCK_REALTIME, &now);
        dwMiliseconds -= now.tv_sec * 1000;
        dwMiliseconds -= now.tv_nsec / 1000000;

        /* dwMiliseconds should now hold the number of ms from NOW */
    } else {
        dwMiliseconds = 0;
    }
    DWORD rr = WaitForSingleObject(*sem, dwMiliseconds);
    if(0 != rr){
        ret = -1;
        if(WAIT_TIMEOUT == rr){
            errno = ETIMEDOUT;
        }
    }
    return ret;
}

Original issue reported on code.google.com by victor....@gmail.com on 15 Jul 2014 at 6:42

GoogleCodeExporter commented 8 years ago
Hi Victor,

Thanks very much for providing the corrected code.
I'm afraid I don't use windows much, so I've not noticed this problem.

I've committed the changes, see 2df447e36df95b97c95def763c9b84d6be3e66b6

Regards,
Attie

Original comment by attie@attie.co.uk on 15 Jul 2014 at 9:16