static void CAEDRDestroyMutex() { OIC_LOG(DEBUG, TAG, "IN"); if (g_mutexUnicastServer) { ca_mutex_free(g_mutexUnicastServer); g_mutexUnicastServer = NULL; } if (g_mutexMulticastServer) { ca_mutex_free(g_mutexMulticastServer); g_mutexMulticastServer = NULL; } if (g_mutexStateList) { ca_mutex_free(g_mutexStateList); g_mutexStateList = NULL; } if (g_mutexObjectList) { ca_mutex_free(g_mutexObjectList); g_mutexObjectList = NULL; } OIC_LOG(DEBUG, TAG, "OUT"); }
void CAManagerTerminateLEAutoConnection() { if (g_connectRetryCond) { ca_cond_signal(g_connectRetryCond); ca_cond_free(g_connectRetryCond); g_connectRetryCond = NULL; } if (g_connectRetryMutex) { ca_mutex_free(g_connectRetryMutex); g_connectRetryMutex = NULL; } if (g_recoveryCond) { ca_cond_signal(g_recoveryCond); ca_cond_free(g_recoveryCond); g_recoveryCond = NULL; } if (g_recoveryMutex) { ca_mutex_free(g_recoveryMutex); g_recoveryMutex = NULL; } }
static void CADestroyNetworkMonitorMutexes() { ca_mutex_free(g_networkMonitorContextMutex); g_networkMonitorContextMutex = NULL; ca_mutex_free(g_stopNetworkMonitorMutex); g_stopNetworkMonitorMutex = NULL; }
void CATerminateLENwkMonitorMutexVaraibles() { OIC_LOG(DEBUG, TAG, "IN"); ca_mutex_free(g_bleDeviceStateChangedCbMutex); g_bleDeviceStateChangedCbMutex = NULL; ca_mutex_free(g_bleConnectionStateChangedCbMutex); g_bleConnectionStateChangedCbMutex = NULL; OIC_LOG(DEBUG, TAG, "OUT"); }
static void CAEDRDestroyMutex() { if (g_mutexStateList) { ca_mutex_free(g_mutexStateList); g_mutexStateList = NULL; } if (g_mutexObjectList) { ca_mutex_free(g_mutexObjectList); g_mutexObjectList = NULL; } }
/** * ca_context_destroy: * @c: the context to destroy. * * Destroy a (connected or unconnected) context object. * * Returns: 0 on success, negative error code on error. */ int ca_context_destroy(ca_context *c) { int ret = CA_SUCCESS; ca_return_val_if_fail(!ca_detect_fork(), CA_ERROR_FORKED); ca_return_val_if_fail(c, CA_ERROR_INVALID); /* There's no locking necessary here, because the application is * broken anyway if it destructs this object in one thread and * still is calling a method of it in another. */ if (c->opened) ret = driver_destroy(c); if (c->props) ca_assert_se(ca_proplist_destroy(c->props) == CA_SUCCESS); if (c->mutex) ca_mutex_free(c->mutex); ca_free(c->driver); ca_free(c->device); ca_free(c); return ret; }
CAResult_t CAInitLENwkMonitorMutexVaraibles() { OIC_LOG(DEBUG, TAG, "IN"); if (NULL == g_bleDeviceStateChangedCbMutex) { g_bleDeviceStateChangedCbMutex = ca_mutex_new(); if (NULL == g_bleDeviceStateChangedCbMutex) { OIC_LOG(ERROR, TAG, "ca_mutex_new has failed"); return CA_STATUS_FAILED; } } if (NULL == g_bleConnectionStateChangedCbMutex) { g_bleConnectionStateChangedCbMutex = ca_mutex_new(); if (NULL == g_bleConnectionStateChangedCbMutex) { OIC_LOG(ERROR, TAG, "ca_mutex_new has failed"); ca_mutex_free(g_bleDeviceStateChangedCbMutex); return CA_STATUS_FAILED; } } OIC_LOG(DEBUG, TAG, "OUT"); return CA_STATUS_OK; }
void CAManagerTerminateMutexVariables() { if (g_deviceACDataListMutex) { ca_mutex_free(g_deviceACDataListMutex); g_deviceACDataListMutex = NULL; } }
void CATerminateGattServerMutexVariables() { OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN"); ca_mutex_free(g_bleServerStateMutex); g_bleServerStateMutex = NULL; g_bleServerStateMutex = NULL; ca_mutex_free(g_bleServiceMutex); g_bleServiceMutex = NULL; ca_mutex_free(g_bleCharacteristicMutex); g_bleCharacteristicMutex = NULL; ca_mutex_free(g_bleReqRespCbMutex); g_bleReqRespCbMutex = NULL; OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT"); }
static void CATCPDestroyMutex() { if (g_mutexObjectList) { ca_mutex_free(g_mutexObjectList); g_mutexObjectList = NULL; } }
static void CAIPServerDestroyMutex(void) { OIC_LOG(DEBUG, IP_SERVER_TAG, "IN"); if (g_mutexServerInfoList) { ca_mutex_free(g_mutexServerInfoList); g_mutexServerInfoList = NULL; } if (g_mutexAdapterServerContext) { ca_mutex_free(g_mutexAdapterServerContext); g_mutexAdapterServerContext = NULL; } OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT"); }
void CAEDRManagerTerminateMutex(void) { OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN"); if (g_edrDeviceListMutex) { ca_mutex_free(g_edrDeviceListMutex); g_edrDeviceListMutex = NULL; } OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT"); }
void CATerminateGattClientMutexVariables() { OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "IN"); ca_mutex_free(g_bleClientStateMutex); g_bleClientStateMutex = NULL; ca_mutex_free(g_bleServiceListMutex); g_bleServiceListMutex = NULL; ca_mutex_free(g_bleReqRespClientCbMutex); g_bleReqRespClientCbMutex = NULL; ca_mutex_free(g_bleClientConnectMutex); g_bleClientConnectMutex = NULL; ca_mutex_free(g_bleClientThreadPoolMutex); g_bleClientThreadPoolMutex = NULL; ca_mutex_free(g_bleServerBDAddressMutex); g_bleServerBDAddressMutex = NULL; ca_cond_free(g_bleClientSendCondWait); g_bleClientSendCondWait = NULL; OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "OUT"); }
static void CAIPDestroyNetworkMonitorList() { if (g_netInterfaceList) { u_arraylist_destroy(g_netInterfaceList); g_netInterfaceList = NULL; } if (g_networkMonitorContextMutex) { ca_mutex_free(g_networkMonitorContextMutex); g_networkMonitorContextMutex = NULL; } }
CAResult_t CAStopRA() { OIC_LOG(DEBUG, RA_ADAPTER_TAG, PCF("Stopping RA adapter")); xmpp_error_code_t ret = xmpp_close(g_xmppData.connection_handle); if (XMPP_ERR_OK != ret) { OIC_LOG_V(ERROR, RA_ADAPTER_TAG, "Failed to close XMPP connection, status: %d", ret); return CA_STATUS_FAILED; } xmpp_shutdown_xmpp(g_xmppData.handle); xmpp_context_destroy(&g_xmppData.context); ca_mutex_free (g_raadapterMutex); g_raadapterMutex = NULL; OIC_LOG(DEBUG, RA_ADAPTER_TAG, PCF("Stopped RA adapter successfully")); return CA_STATUS_OK; }
void ca_thread_pool_free(ca_thread_pool_t thread_pool) { OIC_LOG(DEBUG, TAG, "IN"); if(!thread_pool) { OIC_LOG(ERROR, TAG, "Invalid parameter thread_pool was NULL"); return; } ca_mutex_lock(thread_pool->details->list_lock); for(uint32_t i = 0; i<u_arraylist_length(thread_pool->details->threads_list); ++i) { pthread_t tid = (pthread_t)u_arraylist_get(thread_pool->details->threads_list, i); #if defined(_WIN32) DWORD joinres = WaitForSingleObject(tid, INFINITE); if (WAIT_OBJECT_0 != joinres) { OIC_LOG_V(ERROR, TAG, "Failed to join thread at index %u with error %d", i, joinres); } CloseHandle(tid); #else int joinres = pthread_join(tid, NULL); if(0 != joinres) { OIC_LOG_V(ERROR, TAG, "Failed to join thread at index %u with error %d", i, joinres); } #endif } u_arraylist_free(&(thread_pool->details->threads_list)); ca_mutex_unlock(thread_pool->details->list_lock); ca_mutex_free(thread_pool->details->list_lock); OICFree(thread_pool->details); OICFree(thread_pool); OIC_LOG(DEBUG, TAG, "OUT"); }
void CATerminateLENetworkMonitor() { /** * @note "Network monitor" operations are stopped in @c CALEStop() * since they are started in @c CAStartLEAdapter() rather * than @c CAInitializeLENetworkMonitor(). * * @see @c CAStartLEAdapter() for further details. */ /* Since the CA LE interface doesn't expose a CAInitializeLEGattServer() function, finalize the LE server (peripheral) here rather than in CATerminateLEGattServer() to ensure finalization is correctly paired with initialization. */ CAPeripheralFinalize(); (void) sem_destroy(&g_context.le_started); ca_mutex_lock(g_context.lock); g_context.on_device_state_changed = NULL; g_context.on_server_received_data = NULL; g_context.on_client_received_data = NULL; g_context.client_thread_pool = NULL; g_context.server_thread_pool = NULL; g_context.on_client_error = NULL; g_context.on_server_error = NULL; ca_cond_free(g_context.condition); g_context.condition = NULL; ca_mutex_unlock(g_context.lock); ca_mutex_free(g_context.lock); g_context.lock = NULL; }
static CAResult_t CAIPServerCreateMutex(void) { OIC_LOG(DEBUG, IP_SERVER_TAG, "IN"); g_mutexServerInfoList = ca_mutex_new(); if (!g_mutexServerInfoList) { OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT"); return CA_MEMORY_ALLOC_FAILED; } g_mutexAdapterServerContext = ca_mutex_new(); if (!g_mutexAdapterServerContext) { OIC_LOG(ERROR, IP_SERVER_TAG, "Failed to created mutex!"); ca_mutex_free(g_mutexServerInfoList); g_mutexServerInfoList = NULL; return CA_MEMORY_ALLOC_FAILED; } OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT"); return CA_STATUS_OK; }
static CAResult_t CAInitializeNetworkMonitorMutexes() { if (!g_networkMonitorContextMutex) { g_networkMonitorContextMutex = ca_mutex_new(); if (!g_networkMonitorContextMutex) { OIC_LOG(ERROR, IP_MONITOR_TAG, "g_networkMonitorContextMutex Malloc failed"); return CA_MEMORY_ALLOC_FAILED; } } if (!g_stopNetworkMonitorMutex) { g_stopNetworkMonitorMutex = ca_mutex_new(); if (!g_stopNetworkMonitorMutex) { OIC_LOG(ERROR, IP_MONITOR_TAG, "g_stopNetworkMonitorMutex Malloc failed"); ca_mutex_free(g_networkMonitorContextMutex); return CA_MEMORY_ALLOC_FAILED; } } return CA_STATUS_OK; }
void ca_thread_pool_free(ca_thread_pool_t thread_pool) { OIC_LOG(DEBUG, TAG, "IN"); if(!thread_pool) { OIC_LOG(ERROR, TAG, "Invalid parameter thread_pool was NULL"); return; } ca_mutex_lock(thread_pool->details->list_lock); for(uint32_t i = 0; i<u_arraylist_length(thread_pool->details->threads_list); ++i) { pthread_t tid = (pthread_t)u_arraylist_get(thread_pool->details->threads_list, i); int joinres = pthread_join(tid, NULL); if(0 != joinres) { OIC_LOG_V(ERROR, TAG, "Failed to join thread at index %u with error %d", i, joinres); } } CAResult_t freeres = u_arraylist_free(&(thread_pool->details->threads_list)); if(CA_STATUS_OK != freeres) { OIC_LOG_V(ERROR, TAG, "Failed to free array list, error was: %d", freeres); } ca_mutex_unlock(thread_pool->details->list_lock); ca_mutex_free(thread_pool->details->list_lock); OICFree(thread_pool->details); OICFree(thread_pool); OIC_LOG(DEBUG, TAG, "OUT"); }
static void CAEDRServerDestroyMutex() { if (g_mutexUnicastServer) { ca_mutex_free(g_mutexUnicastServer); g_mutexUnicastServer = NULL; } if (g_mutexMulticastServer) { ca_mutex_free(g_mutexMulticastServer); g_mutexMulticastServer = NULL; } if (g_mutexAcceptServer) { ca_mutex_free(g_mutexAcceptServer); g_mutexAcceptServer = NULL; } if (g_mutexServerSocket) { ca_mutex_free(g_mutexServerSocket); g_mutexServerSocket = NULL; } if (g_mutexStateList) { ca_mutex_free(g_mutexStateList); g_mutexStateList = NULL; } if (g_mutexObjectList) { ca_mutex_free(g_mutexObjectList); g_mutexObjectList = NULL; } }
void testThreadPool(void) { char *string = "Test thread pool"; //Initialize the mutex printf("[testThreadPool] Initializing mutex\n"); //Initialize the thread pool printf("[testThreadPool] Initializing thread pool\n"); if (CA_STATUS_OK != ca_thread_pool_init(2, &g_threadPoolHandle)) { printf("thread_pool_init failed!\n"); return; } //Create the mutex printf("[testThreadPool] Creating mutex\n"); g_mutex = ca_mutex_new(); if (NULL == g_mutex) { printf("[testThreadPool] Failed to create mutex!\n"); ca_thread_pool_free(g_threadPoolHandle); return; } //Create the condition printf("[testThreadPool] Creating Condition\n"); g_cond = ca_cond_new(); if (NULL == g_cond) { printf("[testThreadPool] Failed to create condition!\n"); ca_mutex_free(g_mutex); ca_thread_pool_free(g_threadPoolHandle); return; } //Lock the mutex printf("[testThreadPool] Locking the mutex\n"); ca_mutex_lock(g_mutex); g_condFlag = false; //Add task to thread pool printf("[testThreadPool] Adding the task to thread pool\n"); if (CA_STATUS_OK != ca_thread_pool_add_task(g_threadPoolHandle, task, (void *) string)) { printf("[testThreadPool] thread_pool_add_task failed!\n"); ca_thread_pool_free(g_threadPoolHandle); ca_mutex_unlock(g_mutex); ca_mutex_free(g_mutex); ca_cond_free(g_cond); return; } //Wait for the task to be executed printf("[testThreadPool] Waiting for the task to be completed\n"); while (!g_condFlag) { ca_cond_wait(g_cond, g_mutex); } //Unlock the mutex printf("[testThreadPool] Got the signal and unlock the mutex\n"); ca_mutex_unlock(g_mutex); printf("[testThreadPool] Task is completed and terminating threadpool\n"); ca_cond_free(g_cond); ca_mutex_free(g_mutex); ca_thread_pool_free(g_threadPoolHandle); printf("Exiting from testThreadPool\n"); }
void CAPeripheralFinalize() { ca_cond_free(g_context.condition); ca_mutex_free(g_context.lock); }
// this implementation doesn't do a thread pool, so this function is essentially // a no-op besides creating a valid ca_thread_pool_t object. It was determined after // reading through the existing implementation that the thread-pooling was unnecessary // for the posix platforms. Behavior shouldn't be changed since previously num_of_threads // was greater than the number of requested threads. CAResult_t ca_thread_pool_init(int32_t num_of_threads, ca_thread_pool_t *thread_pool) { OIC_LOG(DEBUG, TAG, "IN"); if(!thread_pool) { OIC_LOG(ERROR, TAG, "Parameter thread_pool was null!"); return CA_STATUS_INVALID_PARAM; } if(num_of_threads <= 0) { OIC_LOG(ERROR, TAG, "num_of_threads must be positive and non-zero"); return CA_STATUS_INVALID_PARAM; } *thread_pool = OICMalloc(sizeof(struct ca_thread_pool)); if(!*thread_pool) { OIC_LOG(ERROR, TAG, "Failed to allocate for thread-pool"); return CA_MEMORY_ALLOC_FAILED; } (*thread_pool)->details = OICMalloc(sizeof(struct ca_thread_pool_details_t)); if(!(*thread_pool)->details) { OIC_LOG(ERROR, TAG, "Failed to allocate for thread-pool details"); OICFree(*thread_pool); *thread_pool=NULL; return CA_MEMORY_ALLOC_FAILED; } (*thread_pool)->details->list_lock = ca_mutex_new(); if(!(*thread_pool)->details->list_lock) { OIC_LOG(ERROR, TAG, "Failed to create thread-pool mutex"); goto exit; } (*thread_pool)->details->threads_list = u_arraylist_create(); if(!(*thread_pool)->details->threads_list) { OIC_LOG(ERROR, TAG, "Failed to create thread-pool list"); if(!ca_mutex_free((*thread_pool)->details->list_lock)) { OIC_LOG(ERROR, TAG, "Failed to free thread-pool mutex"); } goto exit; } OIC_LOG(DEBUG, TAG, "OUT"); return CA_STATUS_OK; exit: OICFree((*thread_pool)->details); OICFree(*thread_pool); *thread_pool = NULL; return CA_STATUS_FAILED; }