Beispiel #1
0
CAResult_t CAInitializeMessageHandler()
{
    CASetPacketReceivedCallback(CAReceivedPacketCallback);

    CASetNetworkChangeCallback(CANetworkChangedCallback);
    CASetErrorHandleCallback(CAErrorHandler);

#ifndef SINGLE_THREAD
    // create thread pool
    CAResult_t res = ca_thread_pool_init(MAX_THREAD_POOL_SIZE, &g_threadPoolHandle);

    if (CA_STATUS_OK != res)
    {
        OIC_LOG(ERROR, TAG, "thread pool initialize error.");
        return res;
    }

    // send thread initialize
    if (CA_STATUS_OK != CAQueueingThreadInitialize(&g_sendThread, g_threadPoolHandle,
                                                   CASendThreadProcess, CADestroyData))
    {
        OIC_LOG(ERROR, TAG, "Failed to Initialize send queue thread");
        return CA_STATUS_FAILED;
    }

    // start send thread
    res = CAQueueingThreadStart(&g_sendThread);

    if (CA_STATUS_OK != res)
    {
        OIC_LOG(ERROR, TAG, "thread start error(send thread).");
        ca_thread_pool_free(g_threadPoolHandle);
        g_threadPoolHandle = NULL;
        return res;
    }

    // receive thread initialize
    if (CA_STATUS_OK != CAQueueingThreadInitialize(&g_receiveThread, g_threadPoolHandle,
                                                   CAReceiveThreadProcess, CADestroyData))
    {
        OIC_LOG(ERROR, TAG, "Failed to Initialize receive queue thread");
        return CA_STATUS_FAILED;
    }

#ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
    // start receive thread
    res = CAQueueingThreadStart(&g_receiveThread);

    if (res != CA_STATUS_OK)
    {
        OIC_LOG(ERROR, TAG, "thread start error(receive thread).");
        return res;
    }
#endif /* SINGLE_HANDLE */

    // retransmission initialize
    CARetransmissionInitialize(&g_retransmissionContext, g_threadPoolHandle, CASendUnicastData,
                               CATimeoutCallback, NULL);

#ifdef WITH_BWT
    // block-wise transfer initialize
    CAInitializeBlockWiseTransfer(CAAddDataToSendThread, CAAddDataToReceiveThread);
#endif

    // start retransmission
    res = CARetransmissionStart(&g_retransmissionContext);

    if (CA_STATUS_OK != res)
    {
        OIC_LOG(ERROR, TAG, "thread start error(retransmission thread).");
        return res;
    }

    // initialize interface adapters by controller
    CAInitializeAdapters(g_threadPoolHandle);
#else
    // retransmission initialize
    CARetransmissionInitialize(&g_retransmissionContext, NULL, CASendUnicastData,
                               CATimeoutCallback, NULL);
    CAInitializeAdapters();
#endif

    return CA_STATUS_OK;
}
Beispiel #2
0
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");
}