00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #ifndef PA_UNIX_UTIL_H
00041 #define PA_UNIX_UTIL_H
00042
00043 #include "pa_cpuload.h"
00044 #include <assert.h>
00045 #include <pthread.h>
00046 #include <signal.h>
00047
00048 #ifdef __cplusplus
00049 extern "C"
00050 {
00051 #endif
00052
00053 #define PA_MIN(x,y) ( (x) < (y) ? (x) : (y) )
00054 #define PA_MAX(x,y) ( (x) > (y) ? (x) : (y) )
00055
00056
00057 #if defined __GNUC__ && __GNUC__ >= 3
00058 #define UNLIKELY(expr) __builtin_expect( (expr), 0 )
00059 #else
00060 #define UNLIKELY(expr) (expr)
00061 #endif
00062
00063 #define STRINGIZE_HELPER(expr) #expr
00064 #define STRINGIZE(expr) STRINGIZE_HELPER(expr)
00065
00066 #define PA_UNLESS(expr, code) \
00067 do { \
00068 if( UNLIKELY( (expr) == 0 ) ) \
00069 { \
00070 PaUtil_DebugPrint(( "Expression '" #expr "' failed in '" __FILE__ "', line: " STRINGIZE( __LINE__ ) "\n" )); \
00071 result = (code); \
00072 goto error; \
00073 } \
00074 } while (0);
00075
00076 static PaError paUtilErr_;
00077
00078
00079 #define PA_ENSURE(expr) \
00080 do { \
00081 if( UNLIKELY( (paUtilErr_ = (expr)) < paNoError ) ) \
00082 { \
00083 PaUtil_DebugPrint(( "Expression '" #expr "' failed in '" __FILE__ "', line: " STRINGIZE( __LINE__ ) "\n" )); \
00084 result = paUtilErr_; \
00085 goto error; \
00086 } \
00087 } while (0);
00088
00089 #define PA_ASSERT_CALL(expr, success) \
00090 paUtilErr_ = (expr); \
00091 assert( success == paUtilErr_ );
00092
00093 #define PA_ENSURE_SYSTEM(expr, success) \
00094 do { \
00095 if( UNLIKELY( (paUtilErr_ = (expr)) != success ) ) \
00096 { \
00097 \
00098 if( pthread_equal(pthread_self(), paUnixMainThread) ) \
00099 { \
00100 PaUtil_SetLastHostErrorInfo( paALSA, paUtilErr_, strerror( paUtilErr_ ) ); \
00101 } \
00102 PaUtil_DebugPrint( "Expression '" #expr "' failed in '" __FILE__ "', line: " STRINGIZE( __LINE__ ) "\n" ); \
00103 result = paUnanticipatedHostError; \
00104 goto error; \
00105 } \
00106 } while( 0 );
00107
00108 typedef struct {
00109 pthread_t callbackThread;
00110 } PaUtilThreading;
00111
00112 PaError PaUtil_InitializeThreading( PaUtilThreading *threading );
00113 void PaUtil_TerminateThreading( PaUtilThreading *threading );
00114 PaError PaUtil_StartThreading( PaUtilThreading *threading, void *(*threadRoutine)(void *), void *data );
00115 PaError PaUtil_CancelThreading( PaUtilThreading *threading, int wait, PaError *exitResult );
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131 extern pthread_t paUnixMainThread;
00132
00133 typedef struct
00134 {
00135 pthread_mutex_t mtx;
00136 } PaUnixMutex;
00137
00138 PaError PaUnixMutex_Initialize( PaUnixMutex* self );
00139 PaError PaUnixMutex_Terminate( PaUnixMutex* self );
00140 PaError PaUnixMutex_Lock( PaUnixMutex* self );
00141 PaError PaUnixMutex_Unlock( PaUnixMutex* self );
00142
00143 typedef struct
00144 {
00145 pthread_t thread;
00146 int parentWaiting;
00147 int stopRequested;
00148 int locked;
00149 PaUnixMutex mtx;
00150 pthread_cond_t cond;
00151 volatile sig_atomic_t stopRequest;
00152 } PaUnixThread;
00153
00156 PaError PaUnixThreading_Initialize();
00157
00167 #define PaUnixThreading_EXIT(result) \
00168 do { \
00169 PaError* pres = NULL; \
00170 if( paNoError != (result) ) \
00171 { \
00172 pres = malloc( sizeof (PaError) ); \
00173 *pres = (result); \
00174 } \
00175 pthread_exit( pres ); \
00176 } while (0);
00177
00188 PaError PaUnixThread_New( PaUnixThread* self, void* (*threadFunc)( void* ), void* threadArg, PaTime waitForChild );
00189
00195 PaError PaUnixThread_Terminate( PaUnixThread* self, int wait, PaError* exitResult );
00196
00203 PaError PaUnixThread_PrepareNotify( PaUnixThread* self );
00204
00209 PaError PaUnixThread_NotifyParent( PaUnixThread* self );
00210
00213 int PaUnixThread_StopRequested( PaUnixThread* self );
00214
00215 #ifdef __cplusplus
00216 }
00217 #endif
00218 #endif