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
55extern "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
85static 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
123typedef struct {
124 pthread_t callbackThread;
126
129PaError PaUtil_StartThreading( PaUtilThreading *threading, void *(*threadRoutine)(void *), void *data );
130PaError PaUtil_CancelThreading( PaUtilThreading *threading, int wait, PaError *exitResult );
131
132/* State accessed by utility functions */
133
134/*
135void PaUnix_SetRealtimeScheduling( int rt );
136
137void PaUtil_InitializeThreading( PaUtilThreading *th, PaUtilCpuLoadMeasurer *clm );
138
139PaError PaUtil_CreateCallbackThread( PaUtilThreading *th, void *(*CallbackThreadFunc)( void * ), PaStream *s );
140
141PaError PaUtil_KillCallbackThread( PaUtilThreading *th, PaError *exitResult );
142
143void PaUtil_CallbackUpdate( PaUtilThreading *th );
144*/
145
146extern pthread_t paUnixMainThread;
147
148typedef struct
149{
150 pthread_mutex_t mtx;
152
157
158typedef struct
159{
160 pthread_t thread;
165 pthread_cond_t cond;
166 PaUtilClockId condClockId;
167 volatile sig_atomic_t stopRequest;
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
205PaError PaUnixThread_New( PaUnixThread* self, void* (*threadFunc)( void* ), void* threadArg, PaTime waitForChild,
206 int rtSched );
207
213PaError PaUnixThread_Terminate( PaUnixThread* self, int wait, PaError* exitResult );
214
222
228
232
233#ifdef __cplusplus
234}
235#endif /* __cplusplus */
236#endif
Functions to assist in measuring the CPU utilization of a callback stream. Used to implement the Pa_G...
PaError PaUtil_CancelThreading(PaUtilThreading *threading, int wait, PaError *exitResult)
Definition pa_unix_util.c:196
PaError PaUnixMutex_Terminate(PaUnixMutex *self)
Definition pa_unix_util.c:523
PaError PaUnixThread_New(PaUnixThread *self, void *(*threadFunc)(void *), void *threadArg, PaTime waitForChild, int rtSched)
Definition pa_unix_util.c:276
PaError PaUnixMutex_Lock(PaUnixMutex *self)
Definition pa_unix_util.c:535
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 PaUnixMutex_Unlock(PaUnixMutex *self)
Definition pa_unix_util.c:553
PaError PaUnixThreading_Initialize(void)
Definition pa_unix_util.c:247
PaError PaUnixThread_NotifyParent(PaUnixThread *self)
Definition pa_unix_util.c:492
pthread_t paUnixMainThread
Definition pa_unix_util.c:244
int PaUnixThread_StopRequested(PaUnixThread *self)
Definition pa_unix_util.c:511
PaError PaUnixMutex_Initialize(PaUnixMutex *self)
Definition pa_unix_util.c:516
PaError PaUtil_StartThreading(PaUtilThreading *threading, void *(*threadRoutine)(void *), void *data)
Definition pa_unix_util.c:190
PaError PaUnixThread_PrepareNotify(PaUnixThread *self)
Definition pa_unix_util.c:480
PaError PaUtil_InitializeThreading(PaUtilThreading *threading)
Definition pa_unix_util.c:180
Prototypes for utility functions used by PortAudio implementations.
int PaError
Definition portaudio.h:122
double PaTime
Definition portaudio.h:465
Definition pa_unix_util.h:149
pthread_mutex_t mtx
Definition pa_unix_util.h:150
Definition pa_unix_util.h:159
int parentWaiting
Definition pa_unix_util.h:161
pthread_cond_t cond
Definition pa_unix_util.h:165
int stopRequested
Definition pa_unix_util.h:162
volatile sig_atomic_t stopRequest
Definition pa_unix_util.h:167
PaUnixMutex mtx
Definition pa_unix_util.h:164
PaUtilClockId condClockId
Definition pa_unix_util.h:166
pthread_t thread
Definition pa_unix_util.h:160
int locked
Definition pa_unix_util.h:163
Definition pa_unix_util.h:123
pthread_t callbackThread
Definition pa_unix_util.h:124