Debian Codegraph @

3rdparty/iaxclient/lib/portaudio/src/os/unix/pa_unix_util.h

raw symbols SCIP
1 #ifndef PA_UNIX_UTIL_H
2 #define PA_UNIX_UTIL_H
3
4 #include "pa_cpuload.h"
5 #include <assert.h>
6 #include <pthread.h>
7 #include <signal.h>
8
9 #ifdef __cplusplus
10 extern "C"
11 {
12 #endif /* __cplusplus */
13
14 #define PA_MIN(x,y) ( (x) < (y) ? (x) : (y) )
15 #define PA_MAX(x,y) ( (x) > (y) ? (x) : (y) )
16
17 /* Utilize GCC branch prediction for error tests */
18 #if defined __GNUC__ && __GNUC__ >= 3
19 #define UNLIKELY(expr) __builtin_expect( (expr), 0 )
20 #else
21 #define UNLIKELY(expr) (expr)
22 #endif
23
24 #define STRINGIZE_HELPER(expr) #expr
25 #define STRINGIZE(expr) STRINGIZE_HELPER(expr)
26
27 #define PA_UNLESS(expr, code) \
28 do { \
29 if( UNLIKELY( (expr) == 0 ) ) \
30 { \
31 PaUtil_DebugPrint(( "Expression '" #expr "' failed in '" __FILE__ "', line: " STRINGIZE( __LINE__ ) "\n" )); \
32 result = (code); \
33 goto error; \
34 } \
35 } while (0);
36
37 static PaError paUtilErr_; /* Used with PA_ENSURE */
38
39 /* Check PaError */
40 #define PA_ENSURE(expr) \
41 do { \
42 if( UNLIKELY( (paUtilErr_ = (expr)) < paNoError ) ) \
43 { \
44 PaUtil_DebugPrint(( "Expression '" #expr "' failed in '" __FILE__ "', line: " STRINGIZE( __LINE__ ) "\n" )); \
45 result = paUtilErr_; \
46 goto error; \
47 } \
48 } while (0);
49
50 #define PA_ASSERT_CALL(expr, success) \
51 paUtilErr_ = (expr); \
52 assert( success == paUtilErr_ );
53
54 #define PA_ENSURE_SYSTEM(expr, success) \
55 do { \
56 if( UNLIKELY( (paUtilErr_ = (expr)) != success ) ) \
57 { \
58 /* PaUtil_SetLastHostErrorInfo should only be used in the main thread */ \
59 if( pthread_equal(pthread_self(), paUnixMainThread) ) \
60 { \
61 PaUtil_SetLastHostErrorInfo( paALSA, paUtilErr_, strerror( paUtilErr_ ) ); \
62 } \
63 PaUtil_DebugPrint( "Expression '" #expr "' failed in '" __FILE__ "', line: " STRINGIZE( __LINE__ ) "\n" ); \
64 result = paUnanticipatedHostError; \
65 goto error; \
66 } \
67 } while( 0 );
68
69 typedef struct {
70 pthread_t callbackThread;
71 } PaUtilThreading;
72
73 PaError PaUtil_InitializeThreading( PaUtilThreading *threading );
74 void PaUtil_TerminateThreading( PaUtilThreading *threading );
75 PaError PaUtil_StartThreading( PaUtilThreading *threading, void *(*threadRoutine)(void *), void *data );
76 PaError PaUtil_CancelThreading( PaUtilThreading *threading, int wait, PaError *exitResult );
77
78 /* State accessed by utility functions */
79
80 /*
81 void PaUnix_SetRealtimeScheduling( int rt );
82
83 void PaUtil_InitializeThreading( PaUtilThreading *th, PaUtilCpuLoadMeasurer *clm );
84
85 PaError PaUtil_CreateCallbackThread( PaUtilThreading *th, void *(*CallbackThreadFunc)( void * ), PaStream *s );
86
87 PaError PaUtil_KillCallbackThread( PaUtilThreading *th, PaError *exitResult );
88
89 void PaUtil_CallbackUpdate( PaUtilThreading *th );
90 */
91
92 extern pthread_t paUnixMainThread;
93
94 typedef struct
95 {
96 pthread_mutex_t mtx;
97 } PaUnixMutex;
98
99 PaError PaUnixMutex_Initialize( PaUnixMutex* self );
100 PaError PaUnixMutex_Terminate( PaUnixMutex* self );
101 PaError PaUnixMutex_Lock( PaUnixMutex* self );
102 PaError PaUnixMutex_Unlock( PaUnixMutex* self );
103
104 typedef struct
105 {
106 pthread_t thread;
107 int parentWaiting;
108 int stopRequested;
109 int locked;
110 PaUnixMutex mtx;
111 pthread_cond_t cond;
112 volatile sig_atomic_t stopRequest;
113 } PaUnixThread;
114
115 /** Initialize global threading state.
116 */
117 PaError PaUnixThreading_Initialize();
118
119 /** Perish, passing on eventual error code.
120 *
121 * A thin wrapper around pthread_exit, will automatically pass on any error code to the joining thread.
122 * If the result indicates an error, i.e. it is not equal to paNoError, this function will automatically
123 * allocate a pointer so the error is passed on with pthread_exit. If the result indicates that all is
124 * well however, only a NULL pointer will be handed to pthread_exit. Thus, the joining thread should
125 * check whether a non-NULL result pointer is obtained from pthread_join and make sure to free it.
126 * @param result: The error code to pass on to the joining thread.
127 */
128 #define PaUnixThreading_EXIT(result) \
129 do { \
130 PaError* pres = NULL; \
131 if( paNoError != (result) ) \
132 { \
133 pres = malloc( sizeof (PaError) ); \
134 *pres = (result); \
135 } \
136 pthread_exit( pres ); \
137 } while (0);
138
139 /** Spawn a thread.
140 *
141 * Intended for spawning the callback thread from the main thread. This function can even block (for a certain
142 * time or indefinitely) untill notified by the callback thread (using PaUnixThread_NotifyParent), which can be
143 * useful in order to make sure that callback has commenced before returning from Pa_StartStream.
144 * @param threadFunc: The function to be executed in the child thread.
145 * @param waitForChild: If not 0, wait for child thread to call PaUnixThread_NotifyParent. Less than 0 means
146 * wait for ever, greater than 0 wait for the specified time.
147 * @return: If timed out waiting on child, paTimedOut.
148 */
149 PaError PaUnixThread_New( PaUnixThread* self, void* (*threadFunc)( void* ), void* threadArg, PaTime waitForChild );
150
151 /** Terminate thread.
152 *
153 * @param wait: If true, request that background thread stop and wait untill it does, else cancel it.
154 * @param exitResult: If non-null this will upon return contain the exit status of the thread.
155 */
156 PaError PaUnixThread_Terminate( PaUnixThread* self, int wait, PaError* exitResult );
157
158 /** Prepare to notify waiting parent thread.
159 *
160 * An internal lock must be held before the parent is notified in PaUnixThread_NotifyParent, call this to
161 * acquire it beforehand.
162 * @return: If parent is not waiting, paInternalError.
163 */
164 PaError PaUnixThread_PrepareNotify( PaUnixThread* self );
165
166 /** Notify waiting parent thread.
167 *
168 * @return: If parent timed out waiting, paTimedOut. If parent was never waiting, paInternalError.
169 */
170 PaError PaUnixThread_NotifyParent( PaUnixThread* self );
171
172 /** Has the parent thread requested this thread to stop?
173 */
174 int PaUnixThread_StopRequested( PaUnixThread* self );
175
176 #ifdef __cplusplus
177 }
178 #endif /* __cplusplus */
179 #endif