diff options
| author | Russ Cox <rsc@golang.org> | 2011-11-09 15:17:05 -0500 |
|---|---|---|
| committer | Russ Cox <rsc@golang.org> | 2011-11-09 15:17:05 -0500 |
| commit | 3b860269eeb0b2d6176da5c972139b7c21d5251b (patch) | |
| tree | 247969f3d82d13d31928f470ec4c4cd6a7616847 /src/pkg/runtime/linux/thread.c | |
| parent | fbfed49134bca038184dbc1a427e82647fc1f12e (diff) | |
| download | go-3b860269eeb0b2d6176da5c972139b7c21d5251b.tar.xz | |
runtime: add timer support, use for package time
This looks like it is just moving some code from
time to runtime (and translating it to C), but the
runtime can do a better job managing the goroutines,
and it needs this functionality for its own maintenance
(for example, for the garbage collector to hand back
unused memory to the OS on a time delay).
Might as well have just one copy of the timer logic,
and runtime can't depend on time, so vice versa.
It also unifies Sleep, NewTicker, and NewTimer behind
one mechanism, so that there are no claims that one
is more efficient than another. (For example, today
people recommend using time.After instead of time.Sleep
to avoid blocking an OS thread.)
Fixes #1644.
Fixes #1731.
Fixes #2190.
R=golang-dev, r, hectorchu, iant, iant, jsing, alex.brainman, dvyukov
CC=golang-dev
https://golang.org/cl/5334051
Diffstat (limited to 'src/pkg/runtime/linux/thread.c')
| -rw-r--r-- | src/pkg/runtime/linux/thread.c | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/src/pkg/runtime/linux/thread.c b/src/pkg/runtime/linux/thread.c index b24aa4f453..5502bbbcab 100644 --- a/src/pkg/runtime/linux/thread.c +++ b/src/pkg/runtime/linux/thread.c @@ -34,15 +34,29 @@ enum // Atomically, // if(*addr == val) sleep // Might be woken up spuriously; that's allowed. +// Don't sleep longer than ns; ns < 0 means forever. void -runtime·futexsleep(uint32 *addr, uint32 val) +runtime·futexsleep(uint32 *addr, uint32 val, int64 ns) { + Timespec ts, *tsp; + + if(ns < 0) + tsp = nil; + else { + ts.tv_sec = ns/1000000000LL; + ts.tv_nsec = ns%1000000000LL; + // Avoid overflow + if(ts.tv_sec > 1<<30) + ts.tv_sec = 1<<30; + tsp = &ts; + } + // Some Linux kernels have a bug where futex of // FUTEX_WAIT returns an internal error code // as an errno. Libpthread ignores the return value // here, and so can we: as it says a few lines up, // spurious wakeups are allowed. - runtime·futex(addr, FUTEX_WAIT, val, nil, nil, 0); + runtime·futex(addr, FUTEX_WAIT, val, tsp, nil, 0); } // If any procs are sleeping on addr, wake up at most cnt. |
