diff options
Diffstat (limited to '3rdparty/portaudio/src/os/unix/pa_unix_util.c')
-rw-r--r-- | 3rdparty/portaudio/src/os/unix/pa_unix_util.c | 55 |
1 files changed, 34 insertions, 21 deletions
diff --git a/3rdparty/portaudio/src/os/unix/pa_unix_util.c b/3rdparty/portaudio/src/os/unix/pa_unix_util.c index 17664eabf0a..5ad55e41d13 100644 --- a/3rdparty/portaudio/src/os/unix/pa_unix_util.c +++ b/3rdparty/portaudio/src/os/unix/pa_unix_util.c @@ -27,20 +27,20 @@ */ /* - * The text above constitutes the entire PortAudio license; however, + * The text above constitutes the entire PortAudio license; however, * the PortAudio community also makes the following non-binding requests: * * Any person wishing to distribute modifications to the Software is * requested to send the modifications to the original developer so that - * they can be incorporated into the canonical version. It is also - * requested that these non-binding requests be included along with the + * they can be incorporated into the canonical version. It is also + * requested that these non-binding requests be included along with the * license above. */ /** @file @ingroup unix_src */ - + #include <pthread.h> #include <unistd.h> #include <stdlib.h> @@ -71,9 +71,13 @@ static int numAllocations_ = 0; #endif -void *PaUtil_AllocateMemory( long size ) +void *PaUtil_AllocateZeroInitializedMemory( long size ) { + /* use { malloc(); memset() } instead of calloc() so that we get + the same alignment guarantee as malloc(). */ void *result = malloc( size ); + if ( result ) + memset( result, 0, size ); #if PA_TRACK_MEMORY if( result != NULL ) numAllocations_ += 1; @@ -129,7 +133,7 @@ void Pa_Sleep( long msec ) /* Discussion on the CoreAudio mailing list suggests that calling gettimeofday (or anything else in the BSD layer) is not real-time - safe, so we use mach_absolute_time on OSX. This implementation is + safe, so we use mach_absolute_time on OSX. This implementation is based on these two links: Technical Q&A QA1398 - Mach Absolute Time Units @@ -140,7 +144,7 @@ void Pa_Sleep( long msec ) */ /* Scaler to convert the result of mach_absolute_time to seconds */ -static double machSecondsConversionScaler_ = 0.0; +static double machSecondsConversionScaler_ = 0.0; #endif void PaUtil_InitializeClock( void ) @@ -160,7 +164,11 @@ PaTime PaUtil_GetTime( void ) return mach_absolute_time() * machSecondsConversionScaler_; #elif defined(HAVE_CLOCK_GETTIME) struct timespec tp; +#if defined(CLOCK_MONOTONIC) + clock_gettime(CLOCK_MONOTONIC, &tp); +#else clock_gettime(CLOCK_REALTIME, &tp); +#endif return (PaTime)(tp.tv_sec + tp.tv_nsec * 1e-9); #else struct timeval tv; @@ -193,8 +201,8 @@ PaError PaUtil_CancelThreading( PaUtilThreading *threading, int wait, PaError *e if( exitResult ) *exitResult = paNoError; - /* If pthread_cancel is not supported (Android platform) whole this function can lead to indefinite waiting if - working thread (callbackThread) has'n received any stop signals from outside, please keep + /* If pthread_cancel is not supported (Android platform) whole this function can lead to indefinite waiting if + working thread (callbackThread) has'n received any stop signals from outside, please keep this in mind when considering using PaUtil_CancelThreading */ #ifdef PTHREAD_CANCELED @@ -220,7 +228,7 @@ PaError PaUtil_CancelThreading( PaUtilThreading *threading, int wait, PaError *e } /* Threading */ -/* paUnixMainThread +/* paUnixMainThread * We have to be a bit careful with defining this global variable, * as explained below. */ #ifdef __APPLE__ @@ -231,12 +239,12 @@ PaError PaUtil_CancelThreading( PaUtilThreading *threading, int wait, PaError *e */ pthread_t paUnixMainThread = 0; #else -/*pthreads are opaque. We don't know that asigning it an int value +/*pthreads are opaque. We don't know that assigning it an int value always makes sense, so we don't initialize it unless we have to.*/ pthread_t paUnixMainThread = 0; #endif -PaError PaUnixThreading_Initialize() +PaError PaUnixThreading_Initialize( void ) { paUnixMainThread = pthread_self(); return paNoError; @@ -270,11 +278,16 @@ PaError PaUnixThread_New( PaUnixThread* self, void* (*threadFunc)( void* ), void { PaError result = paNoError; pthread_attr_t attr; + pthread_condattr_t cattr; int started = 0; memset( self, 0, sizeof (PaUnixThread) ); PaUnixMutex_Initialize( &self->mtx ); - PA_ASSERT_CALL( pthread_cond_init( &self->cond, NULL ), 0 ); + PA_ASSERT_CALL( pthread_condattr_init( &cattr ), 0 ); +#if defined(CLOCK_MONOTONIC) && !defined(__APPLE__) + PA_ASSERT_CALL( pthread_condattr_setclock( &cattr, CLOCK_MONOTONIC ), 0 ); +#endif + PA_ASSERT_CALL( pthread_cond_init( &self->cond, &cattr), 0 ); self->parentWaiting = 0 != waitForChild; @@ -300,7 +313,7 @@ PaError PaUnixThread_New( PaUnixThread* self, void* (*threadFunc)( void* ), void PA_UNLESS( !pthread_attr_init( &attr ), paInternalError ); /* Priority relative to other processes */ - PA_UNLESS( !pthread_attr_setscope( &attr, PTHREAD_SCOPE_SYSTEM ), paInternalError ); + PA_UNLESS( !pthread_attr_setscope( &attr, PTHREAD_SCOPE_SYSTEM ), paInternalError ); PA_UNLESS( !pthread_create( &self->thread, &attr, threadFunc, threadArg ), paInternalError ); started = 1; @@ -350,7 +363,7 @@ PaError PaUnixThread_New( PaUnixThread* self, void* (*threadFunc)( void* ), void pthread_getschedparam(self->thread, &policy, &spm); } } - + if( self->parentWaiting ) { PaTime till; @@ -513,15 +526,15 @@ PaError PaUnixMutex_Terminate( PaUnixMutex* self ) /** Lock mutex. * - * We're disabling thread cancellation while the thread is holding a lock, so mutexes are + * We're disabling thread cancellation while the thread is holding a lock, so mutexes are * properly unlocked at termination time. */ PaError PaUnixMutex_Lock( PaUnixMutex* self ) { PaError result = paNoError; - + #ifdef PTHREAD_CANCEL - int oldState; + int oldState; PA_ENSURE_SYSTEM( pthread_setcancelstate( PTHREAD_CANCEL_DISABLE, &oldState ), 0 ); #endif PA_ENSURE_SYSTEM( pthread_mutex_lock( &self->mtx ), 0 ); @@ -540,7 +553,7 @@ PaError PaUnixMutex_Unlock( PaUnixMutex* self ) PA_ENSURE_SYSTEM( pthread_mutex_unlock( &self->mtx ), 0 ); #ifdef PTHREAD_CANCEL - int oldState; + int oldState; PA_ENSURE_SYSTEM( pthread_setcancelstate( PTHREAD_CANCEL_ENABLE, &oldState ), 0 ); #endif @@ -595,7 +608,7 @@ static void *WatchdogFunc( void *userData ) while( 1 ) { double lowpassCoeff = 0.9, lowpassCoeff1 = 0.99999 - lowpassCoeff; - + /* Test before and after in case whatever underlying sleep call isn't interrupted by pthread_cancel */ pthread_testcancel(); Pa_Sleep( intervalMsec ); @@ -680,7 +693,7 @@ error: /* Pass on error code */ pres = malloc( sizeof (PaError) ); *pres = result; - + pthread_exit( pres ); } |