コード例 #1
0
ファイル: ARSAL_Sem.c プロジェクト: jquesnelle/MissionControl
int ARSAL_Sem_Init(ARSAL_Sem_t *sem, int shared, int value)
{
    int result = -1;

    if (NULL == sem)
    {
        errno = EINVAL;
        return result;
    }
    /* No else. */

#if __SAL_USE_POSIX_SEM

    sem_t *psem = (sem_t *) calloc (1, sizeof (sem_t));
    if (NULL != psem)
    {
        result = sem_init(psem, shared, value);
        if (0 == result)
        {
            *sem = (ARSAL_Sem_t)psem;
        }
        else
        {
            free (psem);
            psem = NULL;
        }
    }
    /* No else: if calloc failed, return default value -1. */

#else

    /*
     * Custom init algo:
     * Alloc memory
     * Init mutex / condition
     * Set internal counter to 'value'
     */

    int isMalloc = 0, isMutexInit = 0;
    ARSAL_Sem_CustomImpl_t *psem = (ARSAL_Sem_CustomImpl_t *) malloc (sizeof (ARSAL_Sem_CustomImpl_t));
    if (NULL != psem)
    {
        isMalloc = 1;
        result = ARSAL_Mutex_Init (&(psem->lock));
        ARSAL_SEM_ERRNO_TRANSFORM (result);
        if (0 == result)
        {
            isMutexInit = 1;
            result = ARSAL_Cond_Init (&(psem->cond));
            ARSAL_SEM_ERRNO_TRANSFORM (result);
            psem->count = value;
        }
        /* No else. */
    }
    /* No else. */

    if (0 == result)
    {
        *sem = (ARSAL_Sem_t)psem;
    }
    else
    {
        if (1 == isMutexInit)
        {
            ARSAL_Mutex_Destroy (&(psem->lock));
        }
        /* No else: no need to destroy a mutex that wasn't created. */
        if (1 == isMalloc)
        {
            free (psem);
        }
        /* No else: no need to free memory that wasn't allocated. */
    }

#endif

    return result;
}
コード例 #2
0
ファイル: ARSTREAM_Reader.c プロジェクト: wicktt/libARStream
ARSTREAM_Reader_t* ARSTREAM_Reader_New (ARNETWORK_Manager_t *manager, int dataBufferID, int ackBufferID, ARSTREAM_Reader_FrameCompleteCallback_t callback, uint8_t *frameBuffer, uint32_t frameBufferSize, uint32_t maxFragmentSize, int32_t maxAckInterval, void *custom, eARSTREAM_ERROR *error)
{
    ARSTREAM_Reader_t *retReader = NULL;
    int ackPacketMutexWasInit = 0;
    int ackSendMutexWasInit = 0;
    int ackSendCondWasInit = 0;
    eARSTREAM_ERROR internalError = ARSTREAM_OK;
    /* ARGS Check */
    if ((manager == NULL) ||
        (callback == NULL) ||
        (frameBuffer == NULL) ||
        (frameBufferSize == 0) ||
        (maxFragmentSize == 0) ||
        (maxAckInterval < -1))
    {
        SET_WITH_CHECK (error, ARSTREAM_ERROR_BAD_PARAMETERS);
        return retReader;
    }

    /* Alloc new reader */
    retReader = malloc (sizeof (ARSTREAM_Reader_t));
    if (retReader == NULL)
    {
        internalError = ARSTREAM_ERROR_ALLOC;
    }

    /* Copy parameters */
    if (internalError == ARSTREAM_OK)
    {
        retReader->manager = manager;
        retReader->dataBufferID = dataBufferID;
        retReader->ackBufferID = ackBufferID;
        retReader->maxFragmentSize = maxFragmentSize;
        retReader->maxAckInterval = maxAckInterval;
        retReader->callback = callback;
        retReader->custom = custom;
        retReader->currentFrameBufferSize = frameBufferSize;
        retReader->currentFrameBuffer = frameBuffer;
    }

    /* Setup internal mutexes/conditions */
    if (internalError == ARSTREAM_OK)
    {
        int mutexInitRet = ARSAL_Mutex_Init (&(retReader->ackPacketMutex));
        if (mutexInitRet != 0)
        {
            internalError = ARSTREAM_ERROR_ALLOC;
        }
        else
        {
            ackPacketMutexWasInit = 1;
        }
    }
    if (internalError == ARSTREAM_OK)
    {
        int mutexInitRet = ARSAL_Mutex_Init (&(retReader->ackSendMutex));
        if (mutexInitRet != 0)
        {
            internalError = ARSTREAM_ERROR_ALLOC;
        }
        else
        {
            ackSendMutexWasInit = 1;
        }
    }
    if (internalError == ARSTREAM_OK)
    {
        int condInitRet = ARSAL_Cond_Init (&(retReader->ackSendCond));
        if (condInitRet != 0)
        {
            internalError = ARSTREAM_ERROR_ALLOC;
        }
        else
        {
            ackSendCondWasInit = 1;
        }
    }

    /* Setup internal variables */
    if (internalError == ARSTREAM_OK)
    {
        int i;
        retReader->currentFrameSize = 0;
        retReader->threadsShouldStop = 0;
        retReader->dataThreadStarted = 0;
        retReader->ackThreadStarted = 0;
        retReader->efficiency_index = 0;
        for (i = 0; i < ARSTREAM_READER_EFFICIENCY_AVERAGE_NB_FRAMES; i++)
        {
            retReader->efficiency_nbTotal [i] = 0;
            retReader->efficiency_nbUseful [i] = 0;
        }
    }

    if ((internalError != ARSTREAM_OK) &&
        (retReader != NULL))
    {
        if (ackPacketMutexWasInit == 1)
        {
            ARSAL_Mutex_Destroy (&(retReader->ackPacketMutex));
        }
        if (ackSendMutexWasInit == 1)
        {
            ARSAL_Mutex_Destroy (&(retReader->ackSendMutex));
        }
        if (ackSendCondWasInit == 1)
        {
            ARSAL_Cond_Destroy (&(retReader->ackSendCond));
        }
        free (retReader);
        retReader = NULL;
    }

    SET_WITH_CHECK (error, internalError);
    return retReader;
}
コード例 #3
0
ARNETWORK_Sender_t* ARNETWORK_Sender_New (ARNETWORKAL_Manager_t *networkALManager, unsigned int numberOfInputBuffer, ARNETWORK_IOBuffer_t **inputBufferPtrArr, unsigned int numberOfInternalInputBuffer, ARNETWORK_IOBuffer_t **internalInputBufferPtrArr, ARNETWORK_IOBuffer_t **inputBufferPtrMap, int pingDelayMs)
{
    /** -- Create a new sender -- */

    /** local declarations */
    ARNETWORK_Sender_t* senderPtr =  NULL;
    eARNETWORK_ERROR error = ARNETWORK_OK;

    /** Create the sender */
    senderPtr =  malloc (sizeof (ARNETWORK_Sender_t));

    if (senderPtr)
    {
        if(networkALManager != NULL)
        {
            senderPtr->networkALManager = networkALManager;
        }
        else
        {
            error = ARNETWORK_ERROR_BAD_PARAMETER;
        }

        if(error == ARNETWORK_OK)
        {
            senderPtr->isAlive = 1;
            senderPtr->numberOfInputBuff = numberOfInputBuffer;
            senderPtr->inputBufferPtrArr = inputBufferPtrArr;
            senderPtr->numberOfInternalInputBuff = numberOfInternalInputBuffer;
            senderPtr->internalInputBufferPtrArr = internalInputBufferPtrArr;
            senderPtr->inputBufferPtrMap = inputBufferPtrMap;
            senderPtr->minimumTimeBetweenSendsMs = ARNETWORK_SENDER_MILLISECOND;
            senderPtr->isPingRunning = 0;
            senderPtr->hadARNetworkALOverflowOnPreviousRun = 0;
            if (pingDelayMs == 0)
            {
                senderPtr->minTimeBetweenPings = ARNETWORK_SENDER_MINIMUM_TIME_BETWEEN_PINGS_MS;
            }
            else
            {
                senderPtr->minTimeBetweenPings = pingDelayMs;
            }
            ARSAL_Time_GetTime(&(senderPtr->pingStartTime));
        }

        /* Create the mutex/condition */
        if ( (error == ARNETWORK_OK) &&
             (ARSAL_Mutex_Init (&(senderPtr->nextSendMutex)) != 0))
        {
            error = ARNETWORK_ERROR_NEW_BUFFER;
        }

        if ( (error == ARNETWORK_OK) &&
             (ARSAL_Cond_Init (&(senderPtr->nextSendCond)) != 0))
        {
            error = ARNETWORK_ERROR_NEW_BUFFER;
        }

        if ( (error == ARNETWORK_OK) &&
             (ARSAL_Mutex_Init (&(senderPtr->pingMutex)) != 0))
        {
            error = ARNETWORK_ERROR_NEW_BUFFER;
        }

        /** delete the sender if an error occurred */
        if (error != ARNETWORK_OK)
        {
            ARSAL_PRINT (ARSAL_PRINT_ERROR, ARNETWORK_SENDER_TAG, "error: %s", ARNETWORK_Error_ToString (error));
            ARNETWORK_Sender_Delete (&senderPtr);
        }
    }

    return senderPtr;
}