Improve cpu_frequency retry

Add retry logic related to cpu frequency manament locking.
Some general code improvements also
Improve some timeouts

Improve the precision of several pthread_cond_timedwait() function
calls to be precise to the millisecond rather than rounding to the
neareast second. Applies to most scheduling functions.