4 ms·
> If the computer isn't on, the value returned by this function is undefined. Shouldn't it just block until the computer turns back on? Probably interferes wi
by h2odragon 5y ago
> If the computer isn't on, the value returned by this function is undefined.
Shouldn't it just block until the computer turns back on? Probably interferes with pthreads.
- eminence32 5y agoHmm, it seems like you need to make sure the computer is on before calling this function, or else you run the risk of getting back undefined data. Seems like we also need a function that can be used to tell if the computer is on...
- salawat 5y agoNah. Just bump that responsibility to the caller, and power supply manufacturer. Have the caller call power_on_computer() first always. This will ensure you always get defined state! ...What do you mean not every power supply manufacturer implements the power_on_computer() function as a NOOP if the computer is already on?!
- h2odragon 5y agoSome implement it that way, some it returns and error message or throws an NMI; then ya got the cheap clones that use it to frob a toggle without regard to the state it was in (which there's no way to read)... Then ya got the weird ones (HP) where it only works right if you call it twice with a 40ms delay between while streaming a sample from "Swan Lake" over their NDA'd 1wire thingy.
- marcodiego 5y agoI know that at least Solaris implemented SIGPWR signal to inform programs of a power failure: https://community.bmc.com/s/article/Inquira-KA334556 https://community.bmc.com/s/article/Inquira-KA334556
- marcodiego 5y agoTIL: linux also implements SIGPWR. Using "kill -PWR `pidof dd`" killed it with "power failure".
- ancode 5y agoThere's a syscall to check whether the computer is on so just call that before this and you'll be fine
- Someone 5y agoThat introduces a time-of-check to time-of-use bug (https://en.wikipedia.org/wiki/Time-of-check_to_time-of-use https://en.wikipedia.org/wiki/Time-of-check_to_time-of-use).
- nvader 5y agoAh, good ole TOCTOU
- waterhouse 5y agoAha. If only the API was "Execute a user-provided function in a context where the computer is guaranteed to be on".
- bryan_w 5y agoYou need to halt this thinking at once
- jcwayne 5y agoSeems like a reasonable compiler would inline the most likely implementation as 1. So, while technically undefined, it's safe to assume that it will 'return' 1 regardless of the actual on/off state of the computer.
- jeremyjh 5y agoI agree on the implementation, but I'm pretty sure if the computer is off you aren't getting a 1.
- yholio 5y ago{return 1;} is clearly a faulty implementation, it's entirely possible that the computer is already turned off by the time the function is executed using power stored in the decoupling capacitors of the CPU.
- azundo 5y agoSince the behavior is undefined when the computer is off this implementation seems correct in your hypothetical as well.
- josefx 5y agoI would be unreasonable. The power state appears to me as highly asynchronous to program execution. I see no explicit guarantees on how the state is retrieved or synchronized, without synchronization the state wont be properly updated. We know that the computer was of at some point, so it starts of as undefined. Given undefined as starting state and the lack of update guarantee we can assume that it will always return an undefined value. This opens up a lot more optimization possibilities than assuming a 1.