catalinii / minisatip

minisatip is an SATIP server for linux using local DVB-S2, DVB-C, DVB-T or ATSC cards
https://minisatip.org
327 stars 81 forks source link

RFE: suggestion replace obsolete usleep with nanosleep #528

Closed 9000h closed 2 years ago

9000h commented 5 years ago

I like to suggest replaceing usleep with nonosleep and use a util function like:

void myusleep(uint64_t usec)
{
    struct timespec ts = { .tv_sec = usec / 1000000,
                           .tv_nsec = (usec % 1000000) * 1000};
#if defined(HAVE_SELECT)
    select(0, NULL, NULL, NULL, &ts);
#else
    nanosleep(&ts, (struct timespec *)NULL);
#endif
}

According to usleep() manpage: POSIX.1-2001 declares this function obsolete; use nanosleep(2) instead. POSIX.1-2008 removes the specification of usleep().

///////////// select(0, NULL, NULL, NULL, &ts); is the lazy method wine is using :-) and it's the same on windows (req. winsocket) //////////// the other option would be

#include <ctime>
void myusleep(int ms)
{
    clock_t starting = clock();
    while( clock() - starting < ms ) {}
}
catalinii commented 5 years ago

Sounds good to me, PR?

9000h commented 5 years ago

I did found some alternatives what do you think is better? or a more complete one like http://www.streamboard.tv/oscam/browser/trunk/oscam-time.c#L198

catalinii commented 5 years ago

I think the one from oscam is more complete and handling corner cases

catalinii commented 5 years ago

@9000h feel free to create a PR