|
|
Thread Tools | Search this Thread |
11-03-2013, 06:42 PM | #16 | |
Guru
Posts: 916
Karma: 13928438
Join Date: Jan 2013
Location: Ely, Cambridgeshire, UK
Device: Kindle Oasis 3, Kindle Oasis 1
|
Quote:
So, basically, sleeping is as close to nil overhead as anything ever is. |
|
11-03-2013, 06:54 PM | #17 | |
Guru
Posts: 916
Karma: 13928438
Join Date: Jan 2013
Location: Ely, Cambridgeshire, UK
Device: Kindle Oasis 3, Kindle Oasis 1
|
Quote:
The original Unix way of sleeping was, indeed, sleep(3). This was implemented by setting an alarm with alarm(2) and then waiting for the SIGALRM via pause(2). Over time, signals got more and more complex in a desperate attempt to make them reliable, so in the end this was more of a block/alarm/unblock dance. But still it was restricted to 1s increments. And then people looked at this and thought, this is ridiculous, why can't we sleep for less than one second? So the BSD people implemented usleep(), which worked in much the same way but allowed shorter sleeps: internally this was implemented via the new ualarm() syscall. Then POSIX came along, and now usleep() is obsolete and we have nanosleep() instead. This is a syscall in its own right, so we no longer need to futz around with signals and worry about multiple threads going to sleep at the same time and colliding with each other and generally go into spasms of hair-tearing, as one does whenever signals are involved. This was back in 1993, and it was good enough for a while. POSIX.1-2001 improved on this, specifying a bunch of clock_*() calls to get and set the time with far more flexibility than good old gettimeofday(), which while useful (it is the most commonly called syscall on Linux systems by a wide margin) is not ideal, because gettimeofday() only gives you wall clock time, which can jump back and forward and the like: clock_gettime() can give you a monotonically increasing clock, an uptime clock, and many others. They added a clock_nanosleep() too, which can sleep for a given time in realtime, in monotonic nanoseconds (so not thrown off if the system clock is changed) and even in process-wide CPU time. We also now have a timer_*() API, which lets you set up arbitrarily many one-shot or multi-shot timers, which can signal their firing in numerous ways. This is really, really useful, and so complicated that I haven't yet encountered anyone who uses it unless forced. (Obviously, there are manpages for all of these.) As for why gettimeofday() is so frequently called -- because if you need to impose an ordering on something, the easiest way to do it is to attach a timestamp to it. And gettimeofday() is *fast*, and will always be fast, because X attaches a timestamp to every single X event, and you get a long stream of those whenever you move your mouse across the screen. |
|
Advert | |
|
11-03-2013, 06:57 PM | #18 |
Evangelist
Posts: 456
Karma: 1287375
Join Date: Jan 2013
Location: West Gardiner, Maine
Device: Touch (5.3.7)
|
wonderful reading! thanks!
|
11-04-2013, 08:52 AM | #19 |
Going Viral
Posts: 17,212
Karma: 18210809
Join Date: Feb 2012
Location: Central Texas
Device: No K1, PW2, KV, KOA
|
Quote: "gettimeofday() is **fast**"
You better believe it. That statement has an interesting story all its own. Far too long for a forum post but can probably be found somewhere in the development history of *nix or computer OSs in general. For performance critical code paths, it is (still) common for those to be hand-coded and cycle counted - - And then there is "gettimeofday()" - This one is where both software implementors and cpu hardware implementors start doing crazy dances and hand-springs. And they start considering hardware delay paths within a single clock cycle. Where referencing an off-chip memory location or off-chip register location is: "too much overhead". Yes, "gettimeofday() is **fast**" |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
PRS-505 Power button not shutting off | Atagahi | Sony Reader | 2 | 11-16-2012 05:48 PM |
PRS-T1 How can I use back button as power button? | younghere | Sony Reader | 1 | 03-12-2012 06:37 PM |
PRS-T1 Does Screensaver use power or not ? | blupaul | Sony Reader | 13 | 11-06-2011 04:59 AM |
Dead K3 showing a non-OEM screensaver | Francesco | Amazon Kindle | 5 | 07-10-2011 11:57 PM |
Keylock keeping current page - not showing screensaver? | guiyoforward | Amazon Kindle | 8 | 10-13-2010 06:44 PM |