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_cpuload.h"
48 #include <assert.h>
49 #include <pthread.h>
50 #include <signal.h>
51 
52 #ifdef __cplusplus
53 extern "C"
54 {
55 #endif /* __cplusplus */
56 
57 #define PA_MIN(x,y) ( (x) < (y) ? (x) : (y) )
58 #define PA_MAX(x,y) ( (x) > (y) ? (x) : (y) )
59 
60 /* Utilize GCC branch prediction for error tests */
61 #if defined __GNUC__ && __GNUC__ >= 3
62 #define UNLIKELY(expr) __builtin_expect( (expr), 0 )
63 #else
64 #define UNLIKELY(expr) (expr)
65 #endif
66 
67 #define STRINGIZE_HELPER(expr) #expr
68 #define STRINGIZE(expr) STRINGIZE_HELPER(expr)
69 
70 #define PA_UNLESS(expr, code) \
71  do { \
72  if( UNLIKELY( (expr) == 0 ) ) \
73  { \
74  PaUtil_DebugPrint(( "Expression '" #expr "' failed in '" __FILE__ "', line: " STRINGIZE( __LINE__ ) "\n" )); \
75  result = (code); \
76  goto error; \
77  } \
78  } while (0);
79 
80 static PaError paUtilErr_; /* Used with PA_ENSURE */
81 
82 /* Check PaError */
83 #define PA_ENSURE(expr) \
84  do { \
85  if( UNLIKELY( (paUtilErr_ = (expr)) < paNoError ) ) \
86  { \
87  PaUtil_DebugPrint(( "Expression '" #expr "' failed in '" __FILE__ "', line: " STRINGIZE( __LINE__ ) "\n" )); \
88  result = paUtilErr_; \
89  goto error; \
90  } \
91  } while (0);
92 
93 #define PA_ASSERT_CALL(expr, success) \
94  paUtilErr_ = (expr); \
95  assert( success == paUtilErr_ );
96 
97 #define PA_ENSURE_SYSTEM(expr, success) \
98  do { \
99  if( UNLIKELY( (paUtilErr_ = (expr)) != success ) ) \
100  { \
101  /* PaUtil_SetLastHostErrorInfo should only be used in the main thread */ \
102  if( pthread_equal(pthread_self(), paUnixMainThread) ) \
103  { \
104  PaUtil_SetLastHostErrorInfo( paALSA, paUtilErr_, strerror( paUtilErr_ ) ); \
105  } \
106  PaUtil_DebugPrint( "Expression '" #expr "' failed in '" __FILE__ "', line: " STRINGIZE( __LINE__ ) "\n" ); \
107  result = paUnanticipatedHostError; \
108  goto error; \
109  } \
110  } while( 0 );
111 
112 typedef struct {
113  pthread_t callbackThread;
115 
117 void PaUtil_TerminateThreading( PaUtilThreading *threading );
118 PaError PaUtil_StartThreading( PaUtilThreading *threading, void *(*threadRoutine)(void *), void *data );
119 PaError PaUtil_CancelThreading( PaUtilThreading *threading, int wait, PaError *exitResult );
120 
121 /* State accessed by utility functions */
122 
123 /*
124 void PaUnix_SetRealtimeScheduling( int rt );
125 
126 void PaUtil_InitializeThreading( PaUtilThreading *th, PaUtilCpuLoadMeasurer *clm );
127 
128 PaError PaUtil_CreateCallbackThread( PaUtilThreading *th, void *(*CallbackThreadFunc)( void * ), PaStream *s );
129 
130 PaError PaUtil_KillCallbackThread( PaUtilThreading *th, PaError *exitResult );
131 
132 void PaUtil_CallbackUpdate( PaUtilThreading *th );
133 */
134 
135 extern pthread_t paUnixMainThread;
136 
137 typedef struct
138 {
139  pthread_mutex_t mtx;
140 } PaUnixMutex;
141 
146 
147 typedef struct
148 {
149  pthread_t thread;
152  int locked;
154  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:178
PaError PaUnixThread_Terminate(PaUnixThread *self, int wait, PaError *exitResult)
Definition: pa_unix_util.c:402
PaError PaUnixThread_NotifyParent(PaUnixThread *self)
Definition: pa_unix_util.c:476
PaError PaUnixMutex_Initialize(PaUnixMutex *self)
Definition: pa_unix_util.c:500
Definition: pa_unix_util.h:112
PaError PaUtil_InitializeThreading(PaUtilThreading *threading)
Definition: pa_unix_util.c:172
int locked
Definition: pa_unix_util.h:152
Definition: pa_unix_util.h:137
PaError PaUnixThreading_Initialize()
Definition: pa_unix_util.c:239
int parentWaiting
Definition: pa_unix_util.h:150
pthread_t thread
Definition: pa_unix_util.h:149
int stopRequested
Definition: pa_unix_util.h:151
PaUnixMutex mtx
Definition: pa_unix_util.h:153
int PaUnixThread_StopRequested(PaUnixThread *self)
Definition: pa_unix_util.c:495
pthread_t paUnixMainThread
Definition: pa_unix_util.c:236
pthread_cond_t cond
Definition: pa_unix_util.h:154
pthread_mutex_t mtx
Definition: pa_unix_util.h:139
int PaError
Definition: portaudio.h:121
PaError PaUnixMutex_Terminate(PaUnixMutex *self)
Definition: pa_unix_util.c:507
PaError PaUtil_StartThreading(PaUtilThreading *threading, void *(*threadRoutine)(void *), void *data)
Definition: pa_unix_util.c:182
PaError PaUnixThread_New(PaUnixThread *self, void *(*threadFunc)(void *), void *threadArg, PaTime waitForChild, int rtSched)
Definition: pa_unix_util.c:268
PaError PaUnixMutex_Unlock(PaUnixMutex *self)
Definition: pa_unix_util.c:537
PaError PaUtil_CancelThreading(PaUtilThreading *threading, int wait, PaError *exitResult)
Definition: pa_unix_util.c:188
pthread_t callbackThread
Definition: pa_unix_util.h:113
PaError PaUnixThread_PrepareNotify(PaUnixThread *self)
Definition: pa_unix_util.c:464
PaError PaUnixMutex_Lock(PaUnixMutex *self)
Definition: pa_unix_util.c:519
Functions to assist in measuring the CPU utilization of a callback stream. Used to implement the Pa_G...
double PaTime
Definition: portaudio.h:460
volatile sig_atomic_t stopRequest
Definition: pa_unix_util.h:155
Definition: pa_unix_util.h:147