pa_mac_util.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * $Id: pa_unix_util.h 1097 2006-08-26 08:27:53Z rossb $
  3. * Portable Audio I/O Library
  4. * UNIX platform-specific support functions
  5. *
  6. * Based on the Open Source API proposed by Ross Bencina
  7. * Copyright (c) 1999-2000 Ross Bencina
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining
  10. * a copy of this software and associated documentation files
  11. * (the "Software"), to deal in the Software without restriction,
  12. * including without limitation the rights to use, copy, modify, merge,
  13. * publish, distribute, sublicense, and/or sell copies of the Software,
  14. * and to permit persons to whom the Software is furnished to do so,
  15. * subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be
  18. * included in all copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  23. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  24. * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  25. * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. /*
  29. * The text above constitutes the entire PortAudio license; however,
  30. * the PortAudio community also makes the following non-binding requests:
  31. *
  32. * Any person wishing to distribute modifications to the Software is
  33. * requested to send the modifications to the original developer so that
  34. * they can be incorporated into the canonical version. It is also
  35. * requested that these non-binding requests be included along with the
  36. * license above.
  37. */
  38. /** @file
  39. @ingroup unix_src
  40. */
  41. #ifndef PA_UNIX_UTIL_H
  42. #define PA_UNIX_UTIL_H
  43. #include "pa_cpuload.h"
  44. #include <assert.h>
  45. #include <pthread.h>
  46. #include <signal.h>
  47. #ifdef __cplusplus
  48. extern "C"
  49. {
  50. #endif /* __cplusplus */
  51. #define PA_MIN(x,y) ( (x) < (y) ? (x) : (y) )
  52. #define PA_MAX(x,y) ( (x) > (y) ? (x) : (y) )
  53. /* Utilize GCC branch prediction for error tests */
  54. #if defined __GNUC__ && __GNUC__ >= 3
  55. #define UNLIKELY(expr) __builtin_expect( (expr), 0 )
  56. #else
  57. #define UNLIKELY(expr) (expr)
  58. #endif
  59. #define STRINGIZE_HELPER(expr) #expr
  60. #define STRINGIZE(expr) STRINGIZE_HELPER(expr)
  61. #define PA_UNLESS(expr, code) \
  62. do { \
  63. if( UNLIKELY( (expr) == 0 ) ) \
  64. { \
  65. PaUtil_DebugPrint(( "Expression '" #expr "' failed in '" __FILE__ "', line: " STRINGIZE( __LINE__ ) "\n" )); \
  66. result = (code); \
  67. goto error; \
  68. } \
  69. } while (0);
  70. static PaError paUtilErr_; /* Used with PA_ENSURE */
  71. /* Check PaError */
  72. #define PA_ENSURE(expr) \
  73. do { \
  74. if( UNLIKELY( (paUtilErr_ = (expr)) < paNoError ) ) \
  75. { \
  76. PaUtil_DebugPrint(( "Expression '" #expr "' failed in '" __FILE__ "', line: " STRINGIZE( __LINE__ ) "\n" )); \
  77. result = paUtilErr_; \
  78. goto error; \
  79. } \
  80. } while (0);
  81. #define PA_ASSERT_CALL(expr, success) \
  82. paUtilErr_ = (expr); \
  83. assert( success == paUtilErr_ );
  84. #define PA_ENSURE_SYSTEM(expr, success) \
  85. do { \
  86. if( UNLIKELY( (paUtilErr_ = (expr)) != success ) ) \
  87. { \
  88. /* PaUtil_SetLastHostErrorInfo should only be used in the main thread */ \
  89. if( pthread_equal(pthread_self(), paUnixMainThread) ) \
  90. { \
  91. PaUtil_SetLastHostErrorInfo( paALSA, paUtilErr_, strerror( paUtilErr_ ) ); \
  92. } \
  93. PaUtil_DebugPrint( "Expression '" #expr "' failed in '" __FILE__ "', line: " STRINGIZE( __LINE__ ) "\n" ); \
  94. result = paUnanticipatedHostError; \
  95. goto error; \
  96. } \
  97. } while( 0 );
  98. typedef struct {
  99. pthread_t callbackThread;
  100. } PaUtilThreading;
  101. PaError PaUtil_InitializeThreading( PaUtilThreading *threading );
  102. void PaUtil_TerminateThreading( PaUtilThreading *threading );
  103. PaError PaUtil_StartThreading( PaUtilThreading *threading, void *(*threadRoutine)(void *), void *data );
  104. PaError PaUtil_CancelThreading( PaUtilThreading *threading, int wait, PaError *exitResult );
  105. /* State accessed by utility functions */
  106. /*
  107. void PaUnix_SetRealtimeScheduling( int rt );
  108. void PaUtil_InitializeThreading( PaUtilThreading *th, PaUtilCpuLoadMeasurer *clm );
  109. PaError PaUtil_CreateCallbackThread( PaUtilThreading *th, void *(*CallbackThreadFunc)( void * ), PaStream *s );
  110. PaError PaUtil_KillCallbackThread( PaUtilThreading *th, PaError *exitResult );
  111. void PaUtil_CallbackUpdate( PaUtilThreading *th );
  112. */
  113. extern pthread_t paUnixMainThread;
  114. typedef struct
  115. {
  116. pthread_mutex_t mtx;
  117. } PaUnixMutex;
  118. PaError PaUnixMutex_Initialize( PaUnixMutex* self );
  119. PaError PaUnixMutex_Terminate( PaUnixMutex* self );
  120. PaError PaUnixMutex_Lock( PaUnixMutex* self );
  121. PaError PaUnixMutex_Unlock( PaUnixMutex* self );
  122. typedef struct
  123. {
  124. pthread_t thread;
  125. int parentWaiting;
  126. int stopRequested;
  127. int locked;
  128. PaUnixMutex mtx;
  129. pthread_cond_t cond;
  130. volatile sig_atomic_t stopRequest;
  131. } PaUnixThread;
  132. /** Initialize global threading state.
  133. */
  134. PaError PaUnixThreading_Initialize();
  135. /** Perish, passing on eventual error code.
  136. *
  137. * A thin wrapper around pthread_exit, will automatically pass on any error code to the joining thread.
  138. * If the result indicates an error, i.e. it is not equal to paNoError, this function will automatically
  139. * allocate a pointer so the error is passed on with pthread_exit. If the result indicates that all is
  140. * well however, only a NULL pointer will be handed to pthread_exit. Thus, the joining thread should
  141. * check whether a non-NULL result pointer is obtained from pthread_join and make sure to free it.
  142. * @param result: The error code to pass on to the joining thread.
  143. */
  144. #define PaUnixThreading_EXIT(result) \
  145. do { \
  146. PaError* pres = NULL; \
  147. if( paNoError != (result) ) \
  148. { \
  149. pres = malloc( sizeof (PaError) ); \
  150. *pres = (result); \
  151. } \
  152. pthread_exit( pres ); \
  153. } while (0);
  154. /** Spawn a thread.
  155. *
  156. * Intended for spawning the callback thread from the main thread. This function can even block (for a certain
  157. * time or indefinitely) untill notified by the callback thread (using PaUnixThread_NotifyParent), which can be
  158. * useful in order to make sure that callback has commenced before returning from Pa_StartStream.
  159. * @param threadFunc: The function to be executed in the child thread.
  160. * @param waitForChild: If not 0, wait for child thread to call PaUnixThread_NotifyParent. Less than 0 means
  161. * wait for ever, greater than 0 wait for the specified time.
  162. * @return: If timed out waiting on child, paTimedOut.
  163. */
  164. PaError PaUnixThread_New( PaUnixThread* self, void* (*threadFunc)( void* ), void* threadArg, PaTime waitForChild );
  165. /** Terminate thread.
  166. *
  167. * @param wait: If true, request that background thread stop and wait untill it does, else cancel it.
  168. * @param exitResult: If non-null this will upon return contain the exit status of the thread.
  169. */
  170. PaError PaUnixThread_Terminate( PaUnixThread* self, int wait, PaError* exitResult );
  171. /** Prepare to notify waiting parent thread.
  172. *
  173. * An internal lock must be held before the parent is notified in PaUnixThread_NotifyParent, call this to
  174. * acquire it beforehand.
  175. * @return: If parent is not waiting, paInternalError.
  176. */
  177. PaError PaUnixThread_PrepareNotify( PaUnixThread* self );
  178. /** Notify waiting parent thread.
  179. *
  180. * @return: If parent timed out waiting, paTimedOut. If parent was never waiting, paInternalError.
  181. */
  182. PaError PaUnixThread_NotifyParent( PaUnixThread* self );
  183. /** Has the parent thread requested this thread to stop?
  184. */
  185. int PaUnixThread_StopRequested( PaUnixThread* self );
  186. #ifdef __cplusplus
  187. }
  188. #endif /* __cplusplus */
  189. #endif