ASIO to JACK driver for WINE
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.

204 lines
7.4KB

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