Skip to content

Instantly share code, notes, and snippets.

@rzezeski
Created January 11, 2016 00:13
Show Gist options
  • Save rzezeski/75ba1ccd9783f4f028ba to your computer and use it in GitHub Desktop.
Save rzezeski/75ba1ccd9783f4f028ba to your computer and use it in GitHub Desktop.
spin sleep
uint64_t
sl_getnsecs()
{
struct timespec ts;
(void) clock_gettime(CLOCK_MONOTONIC, &ts);
return (((uint64_t)ts.tv_sec * 1000000000L) + ts.tv_nsec);
}
/*
* Spin until approximately ns nanoseconds have passed. The ns value
* must be at least 1 nanosecond.
*/
static void
sl_spin(uint64_t ns)
{
uint64_t passed = 0;
uint64_t start = sl_getnsecs();
assert(ns > 0);
while (passed < ns)
passed = (sl_getnsecs()) - start;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment