1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
24 #include "cathreadpool.h"
27 ca_thread_pool_t g_threadPoolHandle = NULL;
29 ca_mutex g_mutex = NULL;
30 ca_cond g_cond = NULL;
31 bool g_condFlag = false;
35 printf("[TASK] Task is executing: data: %s\n", (char *) data);
37 //Signal the condition that task has been completed
38 printf("[TASK] Signaling the condition\n");
39 ca_mutex_lock(g_mutex);
41 ca_cond_signal(g_cond);
42 ca_mutex_unlock(g_mutex);
45 void testThreadPool(void)
47 char *string = "Test thread pool";
49 //Initialize the mutex
50 printf("[testThreadPool] Initializing mutex\n");
52 //Initialize the thread pool
53 printf("[testThreadPool] Initializing thread pool\n");
54 if (CA_STATUS_OK != ca_thread_pool_init(2, &g_threadPoolHandle))
56 printf("thread_pool_init failed!\n");
61 printf("[testThreadPool] Creating mutex\n");
62 g_mutex = ca_mutex_new();
65 printf("[testThreadPool] Failed to create mutex!\n");
66 ca_thread_pool_free(g_threadPoolHandle);
70 //Create the condition
71 printf("[testThreadPool] Creating Condition\n");
72 g_cond = ca_cond_new();
75 printf("[testThreadPool] Failed to create condition!\n");
76 ca_mutex_free(g_mutex);
77 ca_thread_pool_free(g_threadPoolHandle);
82 printf("[testThreadPool] Locking the mutex\n");
83 ca_mutex_lock(g_mutex);
86 //Add task to thread pool
87 printf("[testThreadPool] Adding the task to thread pool\n");
88 if (CA_STATUS_OK != ca_thread_pool_add_task(g_threadPoolHandle, task, (void *) string))
90 printf("[testThreadPool] thread_pool_add_task failed!\n");
91 ca_thread_pool_free(g_threadPoolHandle);
92 ca_mutex_unlock(g_mutex);
93 ca_mutex_free(g_mutex);
98 //Wait for the task to be executed
99 printf("[testThreadPool] Waiting for the task to be completed\n");
103 ca_cond_wait(g_cond, g_mutex);
107 printf("[testThreadPool] Got the signal and unlock the mutex\n");
108 ca_mutex_unlock(g_mutex);
110 printf("[testThreadPool] Task is completed and terminating threadpool\n");
111 ca_cond_free(g_cond);
112 ca_mutex_free(g_mutex);
113 ca_thread_pool_free(g_threadPoolHandle);
115 printf("Exiting from testThreadPool\n");
120 printf(" =====================================================================\n");
121 printf("| Welcome to Theadpool testing |\n");
122 printf("|---------------------------------------------------------------------|\n");
123 printf("| ** Options ** |\n");
124 printf("| 1 - Test Threadpool functionality |\n");
125 printf("| 0 - Terminate test |\n");
128 static void startTesting(void)
133 if(scanf("%d", &choice) == 1)
138 printf("Terminating test.\n");
144 printf("Invalid input.\n");
151 printf("Invalid input.\n");
155 // clear input buffer
156 while (getchar() != '\n');