⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pa_unix_util.c

📁 一个开源的sip源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * $Id: pa_unix_util.c 1182 2007-03-16 19:48:30Z aknudsen $ * Portable Audio I/O Library * UNIX platform-specific support functions * * Based on the Open Source API proposed by Ross Bencina * Copyright (c) 1999-2000 Ross Bencina * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files * (the "Software"), to deal in the Software without restriction, * including without limitation the rights to use, copy, modify, merge, * publish, distribute, sublicense, and/or sell copies of the Software, * and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *//* * 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  * license above. *//** @file @ingroup unix_src*/ #include <pthread.h>#include <unistd.h>#include <stdlib.h>#include <time.h>#include <sys/time.h>#include <assert.h>#include <string.h> /* For memset */#include <math.h>#include <errno.h>#include "pa_util.h"#include "pa_unix_util.h"/*   Track memory allocations to avoid leaks. */#if PA_TRACK_MEMORYstatic int numAllocations_ = 0;#endifvoid *PaUtil_AllocateMemory( long size ){    void *result = malloc( size );#if PA_TRACK_MEMORY    if( result != NULL ) numAllocations_ += 1;#endif    return result;}void PaUtil_FreeMemory( void *block ){    if( block != NULL )    {        free( block );#if PA_TRACK_MEMORY        numAllocations_ -= 1;#endif    }}int PaUtil_CountCurrentlyAllocatedBlocks( void ){#if PA_TRACK_MEMORY    return numAllocations_;#else    return 0;#endif}void Pa_Sleep( long msec ){#ifdef HAVE_NANOSLEEP    struct timespec req = {0}, rem = {0};    PaTime time = msec / 1.e3;    req.tv_sec = (time_t)time;    assert(time - req.tv_sec < 1.0);    req.tv_nsec = (long)((time - req.tv_sec) * 1.e9);    nanosleep(&req, &rem);    /* XXX: Try sleeping the remaining time (contained in rem) if interrupted by a signal? */#else    while( msec > 999 )     /* For OpenBSD and IRIX, argument */        {                   /* to usleep must be < 1000000.   */        usleep( 999000 );        msec -= 999;        }    usleep( msec * 1000 );#endif}/*            *** NOT USED YET: ***static int usePerformanceCounter_;static double microsecondsPerTick_;*/void PaUtil_InitializeClock( void ){    /* TODO */}PaTime PaUtil_GetTime( void ){#ifdef HAVE_CLOCK_GETTIME    struct timespec tp;    clock_gettime(CLOCK_REALTIME, &tp);    return (PaTime)(tp.tv_sec + tp.tv_nsec / 1.e9);#else    struct timeval tv;    gettimeofday( &tv, NULL );    return (PaTime) tv.tv_usec / 1000000. + tv.tv_sec;#endif}PaError PaUtil_InitializeThreading( PaUtilThreading *threading ){    (void) paUtilErr_;    return paNoError;}void PaUtil_TerminateThreading( PaUtilThreading *threading ){}PaError PaUtil_StartThreading( PaUtilThreading *threading, void *(*threadRoutine)(void *), void *data ){    pthread_create( &threading->callbackThread, NULL, threadRoutine, data );    return paNoError;}PaError PaUtil_CancelThreading( PaUtilThreading *threading, int wait, PaError *exitResult ){    PaError result = paNoError;    void *pret;    if( exitResult )        *exitResult = paNoError;    /* Only kill the thread if it isn't in the process of stopping (flushing adaptation buffers) */    if( !wait )        pthread_cancel( threading->callbackThread );   /* XXX: Safe to call this if the thread has exited on its own? */    pthread_join( threading->callbackThread, &pret );#ifdef PTHREAD_CANCELED    if( pret && PTHREAD_CANCELED != pret )#else    /* !wait means the thread may have been canceled */    if( pret && wait )#endif    {        if( exitResult )            *exitResult = *(PaError *) pret;        free( pret );    }    return result;}/* Threading *//* paUnixMainThread  * We have to be a bit careful with defining this global variable, * as explained below. */#ifdef __apple__/* apple/gcc has a "problem" with global vars and dynamic libs.   Initializing it seems to fix the problem.   Described a bit in this thread:   http://gcc.gnu.org/ml/gcc/2005-06/msg00179.html*/pthread_t paUnixMainThread = 0;#else/*pthreads are opaque. We don't know that asigning it an int value  always makes sense, so we don't initialize it unless we have to.*/pthread_t paUnixMainThread = 0;#endifPaError PaUnixThreading_Initialize(){    paUnixMainThread = pthread_self();    return paNoError;}static PaError BoostPriority( PaUnixThread* self ){    PaError result = paNoError;    struct sched_param spm = { 0 };    /* Priority should only matter between contending FIFO threads? */    spm.sched_priority = 1;    assert( self );    if( pthread_setschedparam( self->thread, SCHED_FIFO, &spm ) != 0 )    {        PA_UNLESS( errno == EPERM, paInternalError );  /* Lack permission to raise priority */        PA_DEBUG(( "Failed bumping priority\n" ));        result = 0;    }    else    {        result = 1; /* Success */    }error:    return result;}PaError PaUnixThread_New( PaUnixThread* self, void* (*threadFunc)( void* ), void* threadArg, PaTime waitForChild,        int rtSched ){    PaError result = paNoError;    pthread_attr_t attr;    int started = 0;    memset( self, 0, sizeof (PaUnixThread) );    PaUnixMutex_Initialize( &self->mtx );    PA_ASSERT_CALL( pthread_cond_init( &self->cond, NULL ), 0 );    self->parentWaiting = 0 != waitForChild;    /* Spawn thread *//* Temporarily disabled since we should test during configuration for presence of required mman.h header */#if 0#if defined _POSIX_MEMLOCK && (_POSIX_MEMLOCK != -1)    if( rtSched )    {        if( mlockall( MCL_CURRENT | MCL_FUTURE ) < 0 )        {            int savedErrno = errno;             /* In case errno gets overwritten */            assert( savedErrno != EINVAL );     /* Most likely a programmer error */            PA_UNLESS( (savedErrno == EPERM), paInternalError );            PA_DEBUG(( "%s: Failed locking memory\n", __FUNCTION__ ));        }        else            PA_DEBUG(( "%s: Successfully locked memory\n", __FUNCTION__ ));    }#endif#endif    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_create( &self->thread, &attr, threadFunc, threadArg ), paInternalError );    started = 1;    if( rtSched )    {#if 0        if( self->useWatchdog )        {            int err;            struct sched_param wdSpm = { 0 };            /* Launch watchdog, watchdog sets callback thread priority */            int prio = PA_MIN( self->rtPrio + 4, sched_get_priority_max( SCHED_FIFO ) );            wdSpm.sched_priority = prio;            PA_UNLESS( !pthread_attr_init( &attr ), paInternalError );            PA_UNLESS( !pthread_attr_setinheritsched( &attr, PTHREAD_EXPLICIT_SCHED ), paInternalError );            PA_UNLESS( !pthread_attr_setscope( &attr, PTHREAD_SCOPE_SYSTEM ), paInternalError );            PA_UNLESS( !pthread_attr_setschedpolicy( &attr, SCHED_FIFO ), paInternalError );            PA_UNLESS( !pthread_attr_setschedparam( &attr, &wdSpm ), paInternalError );            if( (err = pthread_create( &self->watchdogThread, &attr, &WatchdogFunc, self )) )            {                PA_UNLESS( err == EPERM, paInternalError );                /* Permission error, go on without realtime privileges */                PA_DEBUG(( "Failed bumping priority\n" ));            }            else            {                int policy;                self->watchdogRunning = 1;                PA_ENSURE_SYSTEM( pthread_getschedparam( self->watchdogThread, &policy, &wdSpm ), 0 );                /* Check if priority is right, policy could potentially differ from SCHED_FIFO (but that's alright) */                if( wdSpm.sched_priority != prio )                {                    PA_DEBUG(( "Watchdog priority not set correctly (%d)\n", wdSpm.sched_priority ));                    PA_ENSURE( paInternalError );                }            }        }        else#endif            PA_ENSURE( BoostPriority( self ) );        {            int policy;            struct sched_param spm;            pthread_getschedparam(self->thread, &policy, &spm);        }    }        if( self->parentWaiting )    {        PaTime till;        struct timespec ts;        int res = 0;        PaTime now;        PA_ENSURE( PaUnixMutex_Lock( &self->mtx ) );        /* Wait for stream to be started */        now = PaUtil_GetTime();        till = now + waitForChild;        while( self->parentWaiting && !res )

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -