Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / resource / csdk / connectivity / samples / linux / threadpool / main.c
1 /******************************************************************
2  *
3  * Copyright 2014 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
21 #include <stdio.h>
22 #include <stdlib.h>
23
24 #include "cathreadpool.h"
25 #include "camutex.h"
26
27 ca_thread_pool_t g_threadPoolHandle = NULL;
28
29 ca_mutex g_mutex = NULL;
30 ca_cond g_cond = NULL;
31 bool g_condFlag = false;
32
33 void task(void *data)
34 {
35     printf("[TASK] Task is executing: data: %s\n", (char *) data);
36
37     //Signal the condition that task has been completed
38     printf("[TASK] Signaling the condition\n");
39     ca_mutex_lock(g_mutex);
40     g_condFlag = true;
41     ca_cond_signal(g_cond);
42     ca_mutex_unlock(g_mutex);
43 }
44
45 void testThreadPool(void)
46 {
47     char *string = "Test thread pool";
48
49     //Initialize the mutex
50     printf("[testThreadPool] Initializing mutex\n");
51
52     //Initialize the thread pool
53     printf("[testThreadPool] Initializing thread pool\n");
54     if (CA_STATUS_OK != ca_thread_pool_init(2, &g_threadPoolHandle))
55     {
56         printf("thread_pool_init failed!\n");
57         return;
58     }
59
60     //Create the mutex
61     printf("[testThreadPool] Creating mutex\n");
62     g_mutex = ca_mutex_new();
63     if (NULL == g_mutex)
64     {
65         printf("[testThreadPool] Failed to create mutex!\n");
66         ca_thread_pool_free(g_threadPoolHandle);
67         return;
68     }
69
70     //Create the condition
71     printf("[testThreadPool] Creating Condition\n");
72     g_cond = ca_cond_new();
73     if (NULL == g_cond)
74     {
75         printf("[testThreadPool] Failed to create condition!\n");
76         ca_mutex_free(g_mutex);
77         ca_thread_pool_free(g_threadPoolHandle);
78         return;
79     }
80
81     //Lock the mutex
82     printf("[testThreadPool] Locking the mutex\n");
83     ca_mutex_lock(g_mutex);
84
85     g_condFlag = false;
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))
89     {
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);
94         ca_cond_free(g_cond);
95         return;
96     }
97
98     //Wait for the task to be executed
99     printf("[testThreadPool] Waiting for the task to be completed\n");
100
101     while (!g_condFlag)
102     {
103         ca_cond_wait(g_cond, g_mutex);
104     }
105
106     //Unlock the mutex
107     printf("[testThreadPool] Got the signal and unlock the mutex\n");
108     ca_mutex_unlock(g_mutex);
109
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);
114
115     printf("Exiting from testThreadPool\n");
116 }
117
118 static void menu()
119 {
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");
126 }
127
128 static void startTesting(void)
129 {
130     while (1)
131     {
132         int choice = -1;
133         if(scanf("%d", &choice) == 1)
134         {
135             switch (choice)
136             {
137                 case 0:
138                     printf("Terminating test.\n");
139                     return;
140                 case 1:
141                     testThreadPool();
142                     break;
143                 default:
144                     printf("Invalid input.\n");
145                     menu();
146                     break;
147             }
148         }
149         else
150         {
151             printf("Invalid input.\n");
152             menu();
153         }
154
155         // clear input buffer
156         while (getchar() != '\n');
157     }
158 }
159
160 int main()
161 {
162     menu();
163     startTesting();
164     return 0;
165 }
166