]> git.saurik.com Git - apple/libc.git/blob - sys/OSThermalNotification.c
Libc-997.1.1.tar.gz
[apple/libc.git] / sys / OSThermalNotification.c
1 /*
2 * Copyright (c) 2007 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #include <dispatch/dispatch.h>
25 #include <libkern/OSThermalNotification.h>
26 #include <notify.h>
27
28 #define OSThermalAlert "com.apple.system.thermalalert"
29 #define OSThermalDecision "com.apple.system.thermaldecision"
30 #define OSThermalStatusName "com.apple.system.thermalstatus"
31 #define OSThermalPressureLevelName "com.apple.system.thermalpressurelevel"
32
33 const char * const kOSThermalNotificationAlert = OSThermalAlert;
34 const char * const kOSThermalNotificationDecision = OSThermalDecision;
35 const char * const kOSThermalNotificationName = OSThermalStatusName;
36 const char * const kOSThermalNotificationPressureLevelName = OSThermalPressureLevelName;
37
38 static const char * const kOSThermalMitigationNames[kOSThermalMitigationCount] = {
39 OSThermalStatusName,
40 "com.apple.system.thermalmitigation.70percenttorch",
41 "com.apple.system.thermalmitigation.70percentbacklight",
42 "com.apple.system.thermalmitigation.50percenttorch",
43 "com.apple.system.thermalmitigation.50percentbacklight",
44 "com.apple.system.thermalmitigation.disabletorch",
45 "com.apple.system.thermalmitigation.25percentbacklight",
46 "com.apple.system.thermalmitigation.disablemapshalo",
47 "com.apple.system.thermalmitigation.appterminate",
48 "com.apple.system.thermalmitigation.devicerestart",
49 "com.apple.system.thermalmitigation.thermaltableready"
50 };
51
52 static int tokens[kOSThermalMitigationCount];
53 static dispatch_once_t predicates[kOSThermalMitigationCount];
54 static bool thermalLevelsReady = false;
55
56 OSThermalNotificationLevel _OSThermalNotificationLevelForBehavior(int behavior)
57 {
58 uint64_t val = OSThermalNotificationLevelAny;
59 if (behavior >= 0 && behavior < kOSThermalMitigationCount) {
60 dispatch_once(&predicates[behavior], ^{
61 (void)notify_register_check(kOSThermalMitigationNames[behavior], &tokens[behavior]);
62 });
63 (void)notify_get_state(tokens[behavior], &val);
64 }
65 return (OSThermalNotificationLevel)val;
66 }
67
68 void _OSThermalNotificationSetLevelForBehavior(int level, int behavior)
69 {
70 uint64_t val = (uint64_t)level;
71 if (behavior >= 0 && behavior < kOSThermalMitigationCount) {
72 dispatch_once(&predicates[behavior], ^{
73 (void)notify_register_check(kOSThermalMitigationNames[behavior], &tokens[behavior]);
74 });
75 (void)notify_set_state(tokens[behavior], val);
76
77 // Note:
78 // - We are ready when we program in the appterminate value.
79 // - Assumes that user programs kOSThermalMitigationNone level less than
80 // kOSThermalMitigationAppTerminate & kOSThermalMitigationDeviceRestart
81 if (behavior == kOSThermalMitigationAppTerminate) {
82 dispatch_once(&predicates[kOSThermalMitigationThermalTableReady], ^{
83 (void)notify_register_check(kOSThermalMitigationNames[kOSThermalMitigationThermalTableReady], &tokens[kOSThermalMitigationThermalTableReady]);
84 });
85 (void)notify_set_state(tokens[kOSThermalMitigationThermalTableReady], kOSThermalMitigationCount);
86 }
87 }
88 }
89
90
91 OSThermalNotificationLevel OSThermalNotificationCurrentLevel(void)
92 {
93 if (thermalLevelsReady) {
94 return _OSThermalNotificationLevelForBehavior(kOSThermalMitigationNone);
95 }
96
97 uint64_t tableReady = 0;
98
99 dispatch_once(&predicates[kOSThermalMitigationThermalTableReady], ^{
100 (void)notify_register_check(kOSThermalMitigationNames[kOSThermalMitigationThermalTableReady], &tokens[kOSThermalMitigationThermalTableReady]);
101 });
102 (void)notify_get_state(tokens[kOSThermalMitigationThermalTableReady], &tableReady);
103
104 // If we are ready then optimize this so we don't call dispatch everytime.
105 if (tableReady == kOSThermalMitigationCount) {
106 thermalLevelsReady = true;
107 return _OSThermalNotificationLevelForBehavior(kOSThermalMitigationNone);
108 }
109 else {
110 // Allow reset so we can dynamically change the table without thermal trap screen appearing.
111 thermalLevelsReady = false;
112 }
113
114 // Not ready returns -1, which should not be equal or greater than any other thermal state.
115 return OSThermalNotificationLevelAny;
116 }