Live data from Hacker News

How to programmatically find out if computer is on

haiku-os.org

21–30 of 197 posts

Re: How to programmatically find out if computer is on

#22

> 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.

Seems 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.

Re: How to programmatically find out if computer is on

#24

is_computer_on_fire() seems more usable...

weird how it's returning a constant https://github.com/haiku/haiku/blob/8f16317a5b6db5c672f33181...

Interesting it seems to come from BeOS:

https://github.com/haiku/haiku/commit/bcf475ec65f1060c0a8abf...

where the same function had a hn thread about it already a couple years ago :D

https://news.ycombinator.com/item?id=8780606

Re: How to programmatically find out if computer is on

#27

Functional programming with monads is the right way to implement this.

It's obviously `IO Bool`, so it's equivalent to directly evaluating in an imperative language and you gain nothing from the monadic interface.

I'm sure some impurity "pragmatist" will try to argue you can make a perfectly good version of it as `Bool`. That kind of pragmatism is better ignored because it interferes with compiler optimization and standardization. As an example, you can look at the recent discussion around `==` and `/=`.

(And I just noticed some people may seriously argue that, so here comes the /s.)

Re: How to programmatically find out if computer is on

#29

> 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.

Hmm, 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...

Nah. 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?!

Re: How to programmatically find out if computer is on

#30
write a function whose sig is like "boolean isOn()". call it. if it returns true, the computer is on. if it returns false, the computer is also on. done. ship it!

add a // TODO in v2: handle case if it never returns, or the process crashes, or caller times out waiting

Post reply on HN