Replace usleep call by nanosleep

Also improve behavior in case signals are delivered to the sleeping
thread. This fixes a potential security weakness reported by codacy
(interaction of usleep with SIGALRM and other timer functions such
as sleep(), alarm(), setitimer(), and nanosleep() is unspecified).
This commit is contained in:
Martin Konrad
2020-03-04 10:56:25 -05:00
parent 41f1b0ffb5
commit f9820577c1

View File

@@ -48,8 +48,10 @@ void epicsAtomicLock ( EpicsAtomicLockKey * )
status = pthread_mutex_lock ( & mutex );
if ( status == 0 ) return;
assert ( status == EINTR );
static const useconds_t retryDelayUSec = 100000;
usleep ( retryDelayUSec );
struct timespec retryDelay = { 0, 100000000 };
struct timespec remainingDelay;
while (nanosleep(&retryDelay, &remainingDelay) == -1 && errno == EINTR)
retryDelay = remainingDelay;
countDown--;
assert ( countDown );
}