| 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 |
| 13 |
|
| 14 |
#define PA_MIN(x,y) ( (x) < (y) ? (x) : (y) ) |
| 15 |
#define PA_MAX(x,y) ( (x) > (y) ? (x) : (y) ) |
| 16 |
|
| 17 |
|
| 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_; |
| 38 |
|
| 39 |
|
| 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 |
\ |
| 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 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 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 |
|
| 116 |
|
| 117 |
PaError PaUnixThreading_Initialize(); |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 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 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
PaError PaUnixThread_New( PaUnixThread* self, void* (*threadFunc)( void* ), void* threadArg, PaTime waitForChild ); |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
PaError PaUnixThread_Terminate( PaUnixThread* self, int wait, PaError* exitResult ); |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
PaError PaUnixThread_PrepareNotify( PaUnixThread* self ); |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
PaError PaUnixThread_NotifyParent( PaUnixThread* self ); |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
int PaUnixThread_StopRequested( PaUnixThread* self ); |
| 175 |
|
| 176 |
#ifdef __cplusplus |
| 177 |
} |
| 178 |
#endif |
| 179 |
#endif |