jack1 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
7.0KB

  1. /* Copyright: © Copyright 2002 Apple Computer, Inc. All rights reserved.
  2. Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
  3. ("Apple") in consideration of your agreement to the following terms, and your
  4. use, installation, modification or redistribution of this Apple software
  5. constitutes acceptance of these terms. If you do not agree with these terms,
  6. please do not use, install, modify or redistribute this Apple software.
  7. In consideration of your agreement to abide by the following terms, and subject
  8. to these terms, Apple grants you a personal, non-exclusive license, under AppleÕs
  9. copyrights in this original Apple software (the "Apple Software"), to use,
  10. reproduce, modify and redistribute the Apple Software, with or without
  11. modifications, in source and/or binary forms; provided that if you redistribute
  12. the Apple Software in its entirety and without modifications, you must retain
  13. this notice and the following text and disclaimers in all such redistributions of
  14. the Apple Software. Neither the name, trademarks, service marks or logos of
  15. Apple Computer, Inc. may be used to endorse or promote products derived from the
  16. Apple Software without specific prior written permission from Apple. Except as
  17. expressly stated in this notice, no other rights or licenses, express or implied,
  18. are granted by Apple herein, including but not limited to any patent rights that
  19. may be infringed by your derivative works or by other works in which the Apple
  20. Software may be incorporated.
  21. The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
  22. WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  23. WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  24. PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  25. COMBINATION WITH YOUR PRODUCTS.
  26. IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  27. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  28. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29. ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  30. OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  31. (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  32. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. /* pThreadUtilities.c */
  35. #define THREAD_SET_PRIORITY 0
  36. #define THREAD_SCHEDULED_PRIORITY 1
  37. #include <mach/thread_policy.h>
  38. #include <mach/thread_act.h>
  39. #include <CoreAudio/HostTime.h>
  40. #include "pThreadUtilities.h"
  41. UInt32 _getThreadPriority (pthread_t inThread, int inWhichPriority);
  42. void setThreadToPriority(pthread_t inThread, UInt32 inPriority, Boolean inIsFixed, UInt64 inHALIOProcCycleDurationInNanoseconds)
  43. {
  44. if (inPriority == 96)
  45. {
  46. // REAL-TIME / TIME-CONSTRAINT THREAD
  47. thread_time_constraint_policy_data_t theTCPolicy;
  48. UInt64 theComputeQuanta;
  49. UInt64 thePeriod;
  50. UInt64 thePeriodNanos;
  51. thePeriodNanos = inHALIOProcCycleDurationInNanoseconds;
  52. theComputeQuanta = AudioConvertNanosToHostTime ( thePeriodNanos * 0.15 );
  53. thePeriod = AudioConvertNanosToHostTime (thePeriodNanos);
  54. theTCPolicy.period = thePeriod;
  55. theTCPolicy.computation = theComputeQuanta;
  56. theTCPolicy.constraint = thePeriod;
  57. theTCPolicy.preemptible = true;
  58. thread_policy_set (pthread_mach_thread_np(inThread), THREAD_TIME_CONSTRAINT_POLICY, (thread_policy_t)&theTCPolicy, THREAD_TIME_CONSTRAINT_POLICY_COUNT);
  59. } else {
  60. // OTHER THREADS
  61. thread_extended_policy_data_t theFixedPolicy;
  62. thread_precedence_policy_data_t thePrecedencePolicy;
  63. SInt32 relativePriority;
  64. // [1] SET FIXED / NOT FIXED
  65. theFixedPolicy.timeshare = !inIsFixed;
  66. thread_policy_set (pthread_mach_thread_np(inThread), THREAD_EXTENDED_POLICY, (thread_policy_t)&theFixedPolicy, THREAD_EXTENDED_POLICY_COUNT);
  67. // [2] SET PRECEDENCE
  68. // N.B.: We expect that if thread A created thread B, and the program wishes to change
  69. // the priority of thread B, then the call to change the priority of thread B must be
  70. // made by thread A.
  71. // This assumption allows us to use pthread_self() to correctly calculate the priority
  72. // of the feeder thread (since precedency policy's importance is relative to the
  73. // spawning thread's priority.)
  74. relativePriority = inPriority - getThreadSetPriority (pthread_self());
  75. thePrecedencePolicy.importance = relativePriority;
  76. thread_policy_set (pthread_mach_thread_np(inThread), THREAD_PRECEDENCE_POLICY, (thread_policy_t)&thePrecedencePolicy, THREAD_PRECEDENCE_POLICY_COUNT);
  77. }
  78. }
  79. // returns the thread's priority as it was last set by the API
  80. UInt32 getThreadSetPriority(pthread_t inThread)
  81. {
  82. return _getThreadPriority (inThread, THREAD_SET_PRIORITY);
  83. }
  84. // returns the thread's priority as it was last scheduled by the Kernel
  85. UInt32 getThreadScheduledPriority(pthread_t inThread)
  86. {
  87. return _getThreadPriority (inThread, THREAD_SCHEDULED_PRIORITY);
  88. }
  89. UInt32 _getThreadPriority(pthread_t inThread, int inWhichPriority)
  90. {
  91. thread_basic_info_data_t threadInfo;
  92. policy_info_data_t thePolicyInfo;
  93. unsigned int count;
  94. // get basic info
  95. count = THREAD_BASIC_INFO_COUNT;
  96. thread_info (pthread_mach_thread_np (inThread), THREAD_BASIC_INFO, (thread_info_t)&threadInfo, &count);
  97. switch (threadInfo.policy) {
  98. case POLICY_TIMESHARE:
  99. count = POLICY_TIMESHARE_INFO_COUNT;
  100. thread_info(pthread_mach_thread_np (inThread), THREAD_SCHED_TIMESHARE_INFO, (thread_info_t)&(thePolicyInfo.ts), &count);
  101. if (inWhichPriority == THREAD_SCHEDULED_PRIORITY) {
  102. return thePolicyInfo.ts.cur_priority;
  103. } else {
  104. return thePolicyInfo.ts.base_priority;
  105. }
  106. break;
  107. case POLICY_FIFO:
  108. count = POLICY_FIFO_INFO_COUNT;
  109. thread_info(pthread_mach_thread_np (inThread), THREAD_SCHED_FIFO_INFO, (thread_info_t)&(thePolicyInfo.fifo), &count);
  110. if ( (thePolicyInfo.fifo.depressed) && (inWhichPriority == THREAD_SCHEDULED_PRIORITY) ) {
  111. return thePolicyInfo.fifo.depress_priority;
  112. }
  113. return thePolicyInfo.fifo.base_priority;
  114. break;
  115. case POLICY_RR:
  116. count = POLICY_RR_INFO_COUNT;
  117. thread_info(pthread_mach_thread_np (inThread), THREAD_SCHED_RR_INFO, (thread_info_t)&(thePolicyInfo.rr), &count);
  118. if ( (thePolicyInfo.rr.depressed) && (inWhichPriority == THREAD_SCHEDULED_PRIORITY) ) {
  119. return thePolicyInfo.rr.depress_priority;
  120. }
  121. return thePolicyInfo.rr.base_priority;
  122. break;
  123. }
  124. return 0;
  125. }