PortAudio  2.0
pa_unix_util.h
Go to the documentation of this file.
1 /*
2  * $Id$
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 /*
30  * The text above constitutes the entire PortAudio license; however,
31  * the PortAudio community also makes the following non-binding requests:
32  *
33  * Any person wishing to distribute modifications to the Software is
34  * requested to send the modifications to the original developer so that
35  * they can be incorporated into the canonical version. It is also
36  * requested that these non-binding requests be included along with the
37  * license above.
38  */
39 
44 #ifndef PA_UNIX_UTIL_H
45 #define PA_UNIX_UTIL_H
46 
47 #include "pa_util.h"
48 #include "pa_pthread_util.h"
49 #include "pa_cpuload.h"
50 #include <assert.h>
51 #include <pthread.h>
52 #include <signal.h>
53 
54 #ifdef __cplusplus
55 extern "C"
56 {
57 #endif /* __cplusplus */
58 
59 #define PA_MIN(x,y) ( (x) < (y) ? (x) : (y) )
60 #define PA_MAX(x,y) ( (x) > (y) ? (x) : (y) )
61 
62 /* Utilize GCC branch prediction for error tests */
63 #if defined __GNUC__ && __GNUC__ >= 3
64 #define UNLIKELY(expr) __builtin_expect( (expr), 0 )
65 #else
66 #define UNLIKELY(expr) (expr)
67 #endif
68 
69 #define PA_UNLESS_ON_ERROR(expr, code, _on_error) \
70  do { \
71  if( UNLIKELY( (expr) == 0 ) ) \
72  { \
73  PaUtil_DebugPrint(( "Expression '" #expr "' failed in '" __FILE__ "', line: " PA_STRINGIZE( __LINE__ ) "\n" )); \
74  result = (code); \
75  _on_error; \
76  } \
77  } while (0);
78 
79 /* Do not call this after an "error:" label because it will loop forever! */
80 #define PA_UNLESS(expr, code) PA_UNLESS_ON_ERROR(expr, code, goto error)
81 
82 /* This is safe to call after an "error:" label. */
83 #define PA_UNLESS_NO_GOTO(expr, code) PA_UNLESS_ON_ERROR(expr, code, (void)0)
84 
85 static PaError paUtilErr_; /* Used with PA_ENSURE */
86 
87 /* Check PaError */
88 #define PA_ENSURE_ON_ERROR(expr, _on_error) \
89  do { \
90  if( UNLIKELY( (paUtilErr_ = (expr)) < paNoError ) ) \
91  { \
92  PaUtil_DebugPrint(( "Expression '" #expr "' failed in '" __FILE__ "', line: " PA_STRINGIZE( __LINE__ ) "\n" )); \
93  result = paUtilErr_; \
94  _on_error; \
95  } \
96  } while (0);
97 
98 /* Do not call this after an "error:" label because it will loop forever! */
99 #define PA_ENSURE(expr) PA_ENSURE_ON_ERROR(expr, goto error)
100 
101 /* This is safe to call after an "error:" label. */
102 #define PA_ENSURE_NO_GOTO(expr) PA_ENSURE_ON_ERROR(expr, (void)0)
103 
104 #define PA_ASSERT_CALL(expr, success) \
105  paUtilErr_ = (expr); \
106  assert( success == paUtilErr_ );
107 
108 #define PA_ENSURE_SYSTEM(expr, success) \
109  do { \
110  if( UNLIKELY( (paUtilErr_ = (expr)) != success ) ) \
111  { \
112  /* PaUtil_SetLastHostErrorInfo should only be used in the main thread */ \
113  if( pthread_equal(pthread_self(), paUnixMainThread) ) \
114  { \
115  PaUtil_SetLastHostErrorInfo( paALSA, paUtilErr_, strerror( paUtilErr_ ) ); \
116  } \
117  PaUtil_DebugPrint( "Expression '" #expr "' failed in '" __FILE__ "', line: " PA_STRINGIZE( __LINE__ ) "\n" ); \
118  result = paUnanticipatedHostError; \
119  goto error; \
120  } \
121  } while( 0 );
122 
123 typedef struct {
124  pthread_t callbackThread;
126 
128 void PaUtil_TerminateThreading( PaUtilThreading *threading );
129 PaError PaUtil_StartThreading( PaUtilThreading *threading, void *(*threadRoutine)(void *), void *data );
130 PaError PaUtil_CancelThreading( PaUtilThreading *threading, int wait, PaError *exitResult );
131 
132 /* State accessed by utility functions */
133 
134 /*
135 void PaUnix_SetRealtimeScheduling( int rt );
136 
137 void PaUtil_InitializeThreading( PaUtilThreading *th, PaUtilCpuLoadMeasurer *clm );
138 
139 PaError PaUtil_CreateCallbackThread( PaUtilThreading *th, void *(*CallbackThreadFunc)( void * ), PaStream *s );
140 
141 PaError PaUtil_KillCallbackThread( PaUtilThreading *th, PaError *exitResult );
142 
143 void PaUtil_CallbackUpdate( PaUtilThreading *th );
144 */
145 
146 extern pthread_t paUnixMainThread;
147 
148 typedef struct
149 {
150  pthread_mutex_t mtx;
151 } PaUnixMutex;
152 
157 
158 typedef struct
159 {
160  pthread_t thread;
163  int locked;
165  pthread_cond_t cond;
167  volatile sig_atomic_t stopRequest;
168 } PaUnixThread;
169 
173 
183 #define PaUnixThreading_EXIT(result) \
184  do { \
185  PaError* pres = NULL; \
186  if( paNoError != (result) ) \
187  { \
188  pres = malloc( sizeof (PaError) ); \
189  *pres = (result); \
190  } \
191  pthread_exit( pres ); \
192  } while (0);
193 
205 PaError PaUnixThread_New( PaUnixThread* self, void* (*threadFunc)( void* ), void* threadArg, PaTime waitForChild,
206  int rtSched );
207 
213 PaError PaUnixThread_Terminate( PaUnixThread* self, int wait, PaError* exitResult );
214 
222 
228 
232 
233 #ifdef __cplusplus
234 }
235 #endif /* __cplusplus */
236 #endif
void PaUtil_TerminateThreading(PaUtilThreading *threading)
Definition: pa_unix_util.c:186
PaError PaUnixThread_Terminate(PaUnixThread *self, int wait, PaError *exitResult)
Definition: pa_unix_util.c:418
PaError PaUnixThread_NotifyParent(PaUnixThread *self)
Definition: pa_unix_util.c:492
PaError PaUnixMutex_Initialize(PaUnixMutex *self)
Definition: pa_unix_util.c:516
PaError PaUnixThreading_Initialize(void)
Definition: pa_unix_util.c:247
Definition: pa_unix_util.h:123
PaError PaUtil_InitializeThreading(PaUtilThreading *threading)
Definition: pa_unix_util.c:180
int locked
Definition: pa_unix_util.h:163
Definition: pa_unix_util.h:148
int parentWaiting
Definition: pa_unix_util.h:161
pthread_t thread
Definition: pa_unix_util.h:160
int stopRequested
Definition: pa_unix_util.h:162
PaUnixMutex mtx
Definition: pa_unix_util.h:164
int PaUnixThread_StopRequested(PaUnixThread *self)
Definition: pa_unix_util.c:511
pthread_t paUnixMainThread
Definition: pa_unix_util.c:244
#define PaUtilClockId
Definition: pa_pthread_util.h:85
pthread_cond_t cond
Definition: pa_unix_util.h:165
pthread_mutex_t mtx
Definition: pa_unix_util.h:150
int PaError
Definition: portaudio.h:122
PaError PaUnixMutex_Terminate(PaUnixMutex *self)
Definition: pa_unix_util.c:523
PaError PaUtil_StartThreading(PaUtilThreading *threading, void *(*threadRoutine)(void *), void *data)
Definition: pa_unix_util.c:190
PaError PaUnixThread_New(PaUnixThread *self, void *(*threadFunc)(void *), void *threadArg, PaTime waitForChild, int rtSched)
Definition: pa_unix_util.c:276
PaError PaUnixMutex_Unlock(PaUnixMutex *self)
Definition: pa_unix_util.c:553
PaError PaUtil_CancelThreading(PaUtilThreading *threading, int wait, PaError *exitResult)
Definition: pa_unix_util.c:196
pthread_t callbackThread
Definition: pa_unix_util.h:124
PaError PaUnixThread_PrepareNotify(PaUnixThread *self)
Definition: pa_unix_util.c:480
Prototypes for utility functions used by PortAudio implementations.
PaError PaUnixMutex_Lock(PaUnixMutex *self)
Definition: pa_unix_util.c:535
Functions to assist in measuring the CPU utilization of a callback stream. Used to implement the Pa_G...
double PaTime
Definition: portaudio.h:465
volatile sig_atomic_t stopRequest
Definition: pa_unix_util.h:167
Definition: pa_unix_util.h:158
PaUtilClockId condClockId
Definition: pa_unix_util.h:166