eARSTREAM_ERROR ARSTREAM_Reader_Delete (ARSTREAM_Reader_t **reader) { eARSTREAM_ERROR retVal = ARSTREAM_ERROR_BAD_PARAMETERS; if ((reader != NULL) && (*reader != NULL)) { int canDelete = 0; if (((*reader)->dataThreadStarted == 0) && ((*reader)->ackThreadStarted == 0)) { canDelete = 1; } if (canDelete == 1) { ARSAL_Mutex_Destroy (&((*reader)->ackPacketMutex)); ARSAL_Mutex_Destroy (&((*reader)->ackSendMutex)); ARSAL_Cond_Destroy (&((*reader)->ackSendCond)); free (*reader); *reader = NULL; retVal = ARSTREAM_OK; } else { ARSAL_PRINT (ARSAL_PRINT_ERROR, ARSTREAM_READER_TAG, "Call ARSTREAM_Reader_StopReader before calling this function"); retVal = ARSTREAM_ERROR_BUSY; } } return retVal; }
int ARSAL_Sem_Destroy(ARSAL_Sem_t *sem) { int result = -1; if (NULL == sem || NULL == *sem) { errno = EINVAL; return result; } /* No else. It's kinda obvious why. */ #if __SAL_USE_POSIX_SEM result = sem_destroy((sem_t *)*sem); free(*sem); *sem = NULL; #else /* * Custom destroy algo: * Broadcast signal to condition, destroy it * Destroy mutex * Free memory */ ARSAL_Sem_CustomImpl_t *psem = (ARSAL_Sem_CustomImpl_t *)*sem; result = ARSAL_Cond_Broadcast (&(psem->cond)); ARSAL_SEM_ERRNO_TRANSFORM (result); if (0 == result) { result = ARSAL_Cond_Destroy (&(psem->cond)); ARSAL_SEM_ERRNO_TRANSFORM (result); } /* No else. */ if (0 == result) { result = ARSAL_Mutex_Destroy (&(psem->lock)); ARSAL_SEM_ERRNO_TRANSFORM (result); } /* No else. */ free (*sem); *sem = NULL; #endif return result; }
void ARNETWORK_Sender_Delete (ARNETWORK_Sender_t **senderPtrAddr) { /** -- Delete the sender -- */ /** local declarations */ ARNETWORK_Sender_t *senderPtr = NULL; if (senderPtrAddr != NULL) { senderPtr = *senderPtrAddr; if (senderPtr != NULL) { ARSAL_Cond_Destroy (&(senderPtr->nextSendCond)); ARSAL_Mutex_Destroy (&(senderPtr->nextSendMutex)); ARSAL_Mutex_Destroy (&(senderPtr->pingMutex)); free (senderPtr); senderPtr = NULL; } *senderPtrAddr = NULL; } }
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; }