OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2009 The Native Client Authors. All rights reserved. | 2 * Copyright 2009 The Native Client Authors. All rights reserved. |
3 * Use of this source code is governed by a BSD-style license that can | 3 * Use of this source code is governed by a BSD-style license that can |
4 * be found in the LICENSE file. | 4 * be found in the LICENSE file. |
5 */ | 5 */ |
6 /* | 6 /* |
7 * Mac OSX thread priority support. | 7 * Mac OSX thread priority support. |
8 */ | 8 */ |
9 | 9 |
10 #include <mach/mach_init.h> | 10 #include <mach/mach_init.h> |
11 #include <mach/mach_time.h> | 11 #include <mach/mach_time.h> |
12 #include <mach/thread_policy.h> | 12 #include <mach/thread_policy.h> |
13 #include <mach/thread_act.h> | 13 #include <mach/thread_act.h> |
| 14 #include <pthread.h> |
14 | 15 |
15 #include "native_client/src/shared/platform/nacl_log.h" | 16 #include "native_client/src/shared/platform/nacl_log.h" |
16 #include "native_client/src/trusted/service_runtime/include/sys/nacl_nice.h" | 17 #include "native_client/src/trusted/service_runtime/include/sys/nacl_nice.h" |
17 | 18 |
18 /* MyConvertToHostTime converts nanoseconds to the time base used by MacOS. | 19 /* MyConvertToHostTime converts nanoseconds to the time base used by MacOS. |
19 * Typically the time base is nanoseconds, and the conversion is trivial. | 20 * Typically the time base is nanoseconds, and the conversion is trivial. |
20 * To avoid having to drag in the entire CoreAudio framework just for this | 21 * To avoid having to drag in the entire CoreAudio framework just for this |
21 * subroutine, it is reimplemented here in terms of mach_timebase_info which | 22 * subroutine, it is reimplemented here in terms of mach_timebase_info which |
22 * is in the MacOS core. | 23 * is in the MacOS core. |
23 * To understand that this implementation is correct, have a look at | 24 * To understand that this implementation is correct, have a look at |
(...skipping 17 matching lines...) Expand all Loading... |
41 double math_tmp = 0.0; | 42 double math_tmp = 0.0; |
42 | 43 |
43 math_tmp = gTimeBase.numer; | 44 math_tmp = gTimeBase.numer; |
44 math_tmp *= nanos; | 45 math_tmp *= nanos; |
45 math_tmp /= gTimeBase.denom; | 46 math_tmp /= gTimeBase.denom; |
46 return (uint64_t)math_tmp; | 47 return (uint64_t)math_tmp; |
47 } | 48 } |
48 | 49 |
49 int nacl_thread_nice(int nacl_nice) { | 50 int nacl_thread_nice(int nacl_nice) { |
50 kern_return_t kr; | 51 kern_return_t kr; |
51 thread_act_t mthread = mach_thread_self(); | 52 |
| 53 /* |
| 54 * Don't use mach_thread_self() because it requires a separate |
| 55 * mach_port_deallocate() system call to release it. Instead, rely on |
| 56 * pthread's cached copy of the port. |
| 57 */ |
| 58 thread_act_t mthread = pthread_mach_thread_np(pthread_self()); |
52 | 59 |
53 switch (nacl_nice) { | 60 switch (nacl_nice) { |
54 case NICE_REALTIME: { | 61 case NICE_REALTIME: { |
55 struct thread_time_constraint_policy tcpolicy; | 62 struct thread_time_constraint_policy tcpolicy; |
56 const int kPeriodInNanoseconds = 2902490; | 63 const int kPeriodInNanoseconds = 2902490; |
57 const float kDutyCycle = 0.5; | 64 const float kDutyCycle = 0.5; |
58 const float kDutyMax = 0.85; | 65 const float kDutyMax = 0.85; |
59 tcpolicy.period = MyConvertToHostTime(kPeriodInNanoseconds); | 66 tcpolicy.period = MyConvertToHostTime(kPeriodInNanoseconds); |
60 tcpolicy.computation = kDutyCycle * tcpolicy.period; | 67 tcpolicy.computation = kDutyCycle * tcpolicy.period; |
61 tcpolicy.constraint = kDutyMax * tcpolicy.period; | 68 tcpolicy.constraint = kDutyMax * tcpolicy.period; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 return -1; | 100 return -1; |
94 break; | 101 break; |
95 } | 102 } |
96 if (kr != KERN_SUCCESS) { | 103 if (kr != KERN_SUCCESS) { |
97 NaClLog(LOG_WARNING, "nacl_thread_nice() failed.\n"); | 104 NaClLog(LOG_WARNING, "nacl_thread_nice() failed.\n"); |
98 return -1; | 105 return -1; |
99 } else { | 106 } else { |
100 return 0; | 107 return 0; |
101 } | 108 } |
102 } | 109 } |
OLD | NEW |