sleep wake without runloop


Gerriet M. Denkmann
 

macOS 13.1

Is there a way to ascertain whether a process is sleeping or awake?

There is IORegisterForSystemPower which sends seep/wake notifications but needs a runloop.

Anything else?

Background: I want to modify chronyd, a LaunchDaemon, which being multi-platform does not have a runloop.
The problem: between NSWorkspaceWillSleepNotification and NSWorkspaceDidWakeNotification this (as any app) will be active from time to time (every few minutes it will be active for almost a minute), which messes up the internals of chronyd.


Gerriet.


 



On Nov 23, 2017, at 1:45 AM, Gerriet M. Denkmann <g@...> wrote:

Is there a way to ascertain whether a process is sleeping or awake?

If a process is doing anything, it’s awake. Asking “is my process asleep?” is kind of like asking a person “are you asleep?” :)

There is IORegisterForSystemPower which sends seep/wake notifications but needs a runloop.

You can start a background thread and run a runloop on it.

The problem: between NSWorkspaceWillSleepNotification and NSWorkspaceDidWakeNotification this (as any app) will be active from time to time (every few minutes it will be active for almost a minute), which messes up the internals of chronyd.

Are you talking about Power Nap mode, where the computer can wake up periodically when it’s asleep to fetch email, etc?

—Jens


Chris Hanson
 

Create your own thread and give it a run loop, then use IORegisterForSystemPower on that run loop.

-- Chris

On Nov 23, 2017, at 1:45 AM, Gerriet M. Denkmann <g@...> wrote:

macOS 13.1

Is there a way to ascertain whether a process is sleeping or awake?

There is IORegisterForSystemPower which sends seep/wake notifications but needs a runloop.

Anything else?

Background: I want to modify chronyd, a LaunchDaemon, which being multi-platform does not have a runloop.
The problem: between NSWorkspaceWillSleepNotification and NSWorkspaceDidWakeNotification this (as any app) will be active from time to time (every few minutes it will be active for almost a minute), which messes up the internals of chronyd.


Gerriet.




Gerriet M. Denkmann
 

On 2 Dec 2017, at 06:51, Jens Alfke <jens@...> wrote:

On Nov 23, 2017, at 1:45 AM, Gerriet M. Denkmann <g@...> wrote:

Is there a way to ascertain whether a process is sleeping or awake?
If a process is doing anything, it’s awake. Asking “is my process asleep?” is kind of like asking a person “are you asleep?” :)
Let’s define sleeping = time between NSWorkspaceWillSleepNotification and NSWorkspaceDidWakeNotification (or the equivalent kIOMessageSystemWillSleep and kIOMessageSystemHasPoweredOn);
and awake = not sleeping.

Active is defined as: CPU does something, e.g. runs my app.

So whenever my app is active, it makes sense for it to ask whether it is sleeping.


There is IORegisterForSystemPower which sends seep/wake notifications but needs a runloop.
You can start a background thread and run a runloop on it.
Yes; this is what I did: make a SleepThread (subclass of NSThread) with a CFRunLoop which registers for IORegisterForSystemPower.
Now my app can ask this SleepThread whether it is sleeping or not.


The problem: between NSWorkspaceWillSleepNotification and NSWorkspaceDidWakeNotification this (as any app) will be active from time to time (every few minutes it will be active for almost a minute), which messes up the internals of chronyd.
Are you talking about Power Nap mode, where the computer can wake up periodically when it’s asleep to fetch email, etc?
Yes, this seems to be the correct term.
When the computer is active in Power Nap mode (while still sleeping according to my definition above) all apps will run for a short period - not only the ones fetching email etc.


Gerriet.


Jon Gotow
 

On Dec 1, 2017, at 5:43 PM, Gerriet M. Denkmann <g@...> wrote:

Yes; this is what I did: make a SleepThread (subclass of NSThread) with a CFRunLoop which registers for IORegisterForSystemPower.
Now my app can ask this SleepThread whether it is sleeping or not.


The problem: between NSWorkspaceWillSleepNotification and NSWorkspaceDidWakeNotification this (as any app) will be active from time to time (every few minutes it will be active for almost a minute), which messes up the internals of chronyd.
Are you talking about Power Nap mode, where the computer can wake up periodically when it’s asleep to fetch email, etc?
Yes, this seems to be the correct term.
When the computer is active in Power Nap mode (while still sleeping according to my definition above) all apps will run for a short period - not only the ones fetching email etc.
Note that the IORegisterForSystemPower callback doesn't get called for wake/sleep cycles in Power Nap mode. That doesn't make sense to me, but when I filed a bug about it, Apple closed it with "behaves as intended".

- Jon