Modifying version number for building on tizen 3.0
[platform/upstream/iotivity.git] / resource / csdk / connectivity / samples / linux / threadpool / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #include "uthreadpool.h"
5 #include "umutex.h"
6
7 u_thread_pool_t gThreadPoolHandle = NULL;
8
9 u_mutex gMutex = NULL;
10 u_cond gCond = NULL;
11
12 void task(void *data)
13 {
14     printf("[TASK] Task is executing: data: %s\n", (char *) data);
15
16     //Signal the condition that task has been completed
17     printf("[TASK] Signaling the condition\n");
18     u_cond_signal(gCond);
19 }
20
21 void testThreadPool(void)
22 {
23     char *string = "Test glib thread pool";
24
25     //Initialize the mutex
26     printf("[testThreadPool] Initializing mutex\n");
27     u_mutex_init();
28
29     //Initialize the thread pool
30     printf("[testThreadPool] Initializing thread pool\n");
31     if (CA_STATUS_OK != u_thread_pool_init(2, &gThreadPoolHandle))
32     {
33         printf("thread_pool_init failed!\n");
34         return;
35     }
36
37     //Create the mutex
38     printf("[testThreadPool] Creating mutex\n");
39     gMutex = u_mutex_new();
40     if (NULL == gMutex)
41     {
42         printf("[testThreadPool] Failed to create mutex!\n");
43         return;
44     }
45
46     //Create the condition
47     printf("[testThreadPool] Creating Condition\n");
48     gCond = u_cond_new();
49     if (NULL == gCond)
50     {
51         printf("[testThreadPool] Failed to create condition!\n");
52
53         u_mutex_free(gMutex);
54         return;
55     }
56
57     //Lock the mutex
58     printf("[testThreadPool] Locking the mutex\n");
59     u_mutex_lock(gMutex);
60
61     //Add task to thread pool
62     printf("[testThreadPool] Adding the task to thread pool\n");
63     if (CA_STATUS_OK != u_thread_pool_add_task(gThreadPoolHandle, task, (void *) string))
64     {
65         printf("[testThreadPool] thread_pool_add_task failed!\n");
66
67         u_mutex_unlock(gMutex);
68         u_mutex_free(gMutex);
69         u_cond_free(gCond);
70         return;
71     }
72
73     //Wait for the task to be executed
74     printf("[testThreadPool] Waiting for the task to be completed\n");
75     u_cond_wait(gCond, gMutex);
76
77     //Unlock the mutex
78     printf("[testThreadPool] Got the signal and unlock the mutex\n");
79     u_mutex_unlock(gMutex);
80
81     printf("[testThreadPool] Task is completed and terminating threadpool\n");
82     u_mutex_free(gMutex);
83     u_cond_free(gCond);
84     u_thread_pool_free(gThreadPoolHandle);
85
86     printf("Exiting from testThreadPool\n");
87 }
88
89 static void menu()
90 {
91     printf(" =====================================================================\n");
92     printf("|                 Welcome to Theadpool testing                        |\n");
93     printf("|---------------------------------------------------------------------|\n");
94     printf("|                           ** Options **                             |\n");
95     printf("|  1 - Test Threadpool functionality                                  |\n");
96     printf("|  0 - Terminate test                                                 |\n");
97 }
98
99 static void startTesting(void)
100 {
101     while (1)
102     {
103         int choice = -1;
104         scanf("%d", &choice);
105
106         switch (choice)
107         {
108             case 0:
109                 printf("Terminating test.....\n");
110                 return;
111             case 1:
112                 testThreadPool();
113                 break;
114             default:
115                 printf("Invalid input...\n");
116                 menu();
117                 break;
118         }
119     }
120 }
121
122 int main()
123 {
124     menu();
125     startTesting();
126     return 0;
127 }