diff options
author | Tavian Barnes <tavianator@gmail.com> | 2009-09-17 13:33:10 +0000 |
---|---|---|
committer | Tavian Barnes <tavianator@gmail.com> | 2009-09-17 13:33:10 +0000 |
commit | 500a64194f6dd1d226e0ae94873dc435284d6727 (patch) | |
tree | 275e8ddcd4abe73b19e77177f2fe69fc09f00887 | |
parent | 9a358c3cdf8eb34754cc5544e2a5e0f67c022f8d (diff) | |
download | libsandglass-500a64194f6dd1d226e0ae94873dc435284d6727.tar.xz |
Generalise handling of negative grain differences.
-rw-r--r-- | src/sandglass.c | 8 | ||||
-rw-r--r-- | src/sandglass.h | 3 |
2 files changed, 8 insertions, 3 deletions
diff --git a/src/sandglass.c b/src/sandglass.c index 3399b73..c63e2a0 100644 --- a/src/sandglass.c +++ b/src/sandglass.c @@ -186,7 +186,7 @@ sandglass_elapse(sandglass_t *sandglass) sandglass->grains -= oldgrains; /* Magical correction for timespec-based grains */ if (sandglass->grains < 0) - sandglass->grains += 2000000000L; + sandglass->grains += sandglass->adjustment; sandglass->grains /= sandglass->loops; return 0; @@ -226,7 +226,8 @@ sandglass_real_gettime(sandglass_t *sandglass) if (clock_gettime(CLOCK_REALTIME, &ts) != 0) return -1; } - sandglass->grains = sandglass_timespec_grains(&ts); + sandglass->grains = sandglass_timespec_grains(&ts); + sandglass->adjustment = 2000000000L; break; default: @@ -249,7 +250,8 @@ sandglass_real_gettime(sandglass_t *sandglass) return -1; } else return -1; - sandglass->grains = sandglass_timespec_grains(&ts); + sandglass->grains = sandglass_timespec_grains(&ts); + sandglass->adjustment = 2000000000L; break; case SANDGLASS_SYSTEM: diff --git a/src/sandglass.h b/src/sandglass.h index 3cd14ba..7073fb9 100644 --- a/src/sandglass.h +++ b/src/sandglass.h @@ -99,6 +99,9 @@ struct sandglass_t * Internal fields */ + /* Adjustment to be added for negative (i.e. overflowed) grains counts */ + long adjustment; + /* For SANDGLASS_TICKS looping support */ unsigned int i, loops; }; |