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(expr, code) \
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  goto error; \
76  } \
77  } while (0);
78 
79 static PaError paUtilErr_; /* Used with PA_ENSURE */
80 
81 /* Check PaError */
82 #define PA_ENSURE(expr) \
83  do { \
84  if( UNLIKELY( (paUtilErr_ = (expr)) < paNoError ) ) \
85  { \
86  PaUtil_DebugPrint(( "Expression '" #expr "' failed in '" __FILE__ "', line: " PA_STRINGIZE( __LINE__ ) "\n" )); \
87  result = paUtilErr_; \
88  goto error; \
89  } \
90  } while (0);
91 
92 #define PA_ASSERT_CALL(expr, success) \
93  paUtilErr_ = (expr); \
94  assert( success == paUtilErr_ );
95 
96 #define PA_ENSURE_SYSTEM(expr, success) \
97  do { \
98  if( UNLIKELY( (paUtilErr_ = (expr)) != success ) ) \
99  { \
100  /* PaUtil_SetLastHostErrorInfo should only be used in the main thread */ \
101  if( pthread_equal(pthread_self(), paUnixMainThread) ) \
102  { \
103  PaUtil_SetLastHostErrorInfo( paALSA, paUtilErr_, strerror( paUtilErr_ ) ); \
104  } \
105  PaUtil_DebugPrint( "Expression '" #expr "' failed in '" __FILE__ "', line: " PA_STRINGIZE( __LINE__ ) "\n" ); \
106  result = paUnanticipatedHostError; \
107  goto error; \
108  } \
109  } while( 0 );
110 
111 typedef struct {
112  pthread_t callbackThread;
114 
116 void PaUtil_TerminateThreading( PaUtilThreading *threading );
117 PaError PaUtil_StartThreading( PaUtilThreading *threading, void *(*threadRoutine)(void *), void *data );
118 PaError PaUtil_CancelThreading( PaUtilThreading *threading, int wait, PaError *exitResult );
119 
120 /* State accessed by utility functions */
121 
122 /*
123 void PaUnix_SetRealtimeScheduling( int rt );
124 
125 void PaUtil_InitializeThreading( PaUtilThreading *th, PaUtilCpuLoadMeasurer *clm );
126 
127 PaError PaUtil_CreateCallbackThread( PaUtilThreading *th, void *(*CallbackThreadFunc)( void * ), PaStream *s );
128 
129 PaError PaUtil_KillCallbackThread( PaUtilThreading *th, PaError *exitResult );
130 
131 void PaUtil_CallbackUpdate( PaUtilThreading *th );
132 */
133 
134 extern pthread_t paUnixMainThread;
135 
136 typedef struct
137 {
138  pthread_mutex_t mtx;
139 } PaUnixMutex;
140 
145 
146 typedef struct
147 {
148  pthread_t thread;
151  int locked;
153  pthread_cond_t cond;
155  volatile sig_atomic_t stopRequest;
156 } PaUnixThread;
157 
161 
171 #define PaUnixThreading_EXIT(result) \
172  do { \
173  PaError* pres = NULL; \
174  if( paNoError != (result) ) \
175  { \
176  pres = malloc( sizeof (PaError) ); \
177  *pres = (result); \
178  } \
179  pthread_exit( pres ); \
180  } while (0);
181 
193 PaError PaUnixThread_New( PaUnixThread* self, void* (*threadFunc)( void* ), void* threadArg, PaTime waitForChild,
194  int rtSched );
195 
201 PaError PaUnixThread_Terminate( PaUnixThread* self, int wait, PaError* exitResult );
202 
210 
216 
220 
221 #ifdef __cplusplus
222 }
223 #endif /* __cplusplus */
224 #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:111
PaError PaUtil_InitializeThreading(PaUtilThreading *threading)
Definition: pa_unix_util.c:180
int locked
Definition: pa_unix_util.h:151
Definition: pa_unix_util.h:136
int parentWaiting
Definition: pa_unix_util.h:149
pthread_t thread
Definition: pa_unix_util.h:148
int stopRequested
Definition: pa_unix_util.h:150
PaUnixMutex mtx
Definition: pa_unix_util.h:152
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:153
pthread_mutex_t mtx
Definition: pa_unix_util.h:138
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:112
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:155
Definition: pa_unix_util.h:146
PaUtilClockId condClockId
Definition: pa_unix_util.h:154