Revert "Remove TPCS and TWPServer features"
[platform/upstream/csf-framework.git] / framework / IpcThrdPool.c
1 /*
2     Copyright (c) 2014, McAfee, Inc.
3
4     All rights reserved.
5
6     Redistribution and use in source and binary forms, with or without modification,
7     are permitted provided that the following conditions are met:
8
9     Redistributions of source code must retain the above copyright notice, this list
10     of conditions and the following disclaimer.
11
12     Redistributions in binary form must reproduce the above copyright notice, this
13     list of conditions and the following disclaimer in the documentation and/or other
14     materials provided with the distribution.
15
16     Neither the name of McAfee, Inc. nor the names of its contributors may be used
17     to endorse or promote products derived from this software without specific prior
18     written permission.
19
20     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21     ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23     IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24     INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25     BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27     LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
28     OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29     OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /**
33  * \file IpcThrdPool.c
34  * \brief Ipc Thread pool Ipc Source File
35  *
36  * This file implements the thread pool used by IPC Server in Security framework.
37  */
38
39 #include <stdlib.h>
40 #include <stdio.h>
41 #include <unistd.h>
42 #include <pthread.h>
43 #include "Debug.h"
44 #include "IpcThrdPool.h"
45
46 static void IpcThrPoolReset(IpcHandlePool *pHPool)
47 {
48     IpcHandles *pHandle;
49
50     if (!pHPool)
51         return;
52
53     while ((pHandle = pHPool->pHList) != NULL)
54     {
55         pHPool->pHList = pHPool->pHList->pNext;
56         free(pHandle);
57     }
58
59     pthread_cond_destroy(&pHPool->Cond);
60     pthread_mutex_destroy(&pHPool->Lock);
61     pHPool->iIdleCount = 0;
62 }
63
64 /**
65  * Frees the resources and nullifies the thread pool object.
66  */
67 void IpcThrPoolFree(IpcHandlePool **pHPool)
68 {
69     IpcThrPoolReset(*pHPool);
70     free(*pHPool);
71     *pHPool = NULL;
72 }
73
74 /**
75  * Creates a new handle and makes it head of the pool list.
76  * Return 0 on success and -1 on failure.
77  */
78 static int AddHandleToThrPool(IpcHandlePool *pHPool)
79 {
80     IpcHandles *pHandle = NULL;
81     if ((pHandle = (IpcHandles *) calloc(1, sizeof(IpcHandles))) == NULL)
82     {
83         DDBG("%s\n", "calloc IpcHandles");
84         return -1;
85     }
86
87
88     pHandle->pNext = pHPool->pHList;
89     pHPool->pHList = pHandle;
90     return 0;
91 }
92
93 /**
94  * Initializes thread pool. Returns 0 on success and -1 on failure.
95  */
96 int IpcThrPoolInit(IpcHandlePool *pHPool, int iNumHandles)
97 {
98     int i;
99     int result = 0;
100
101     if (!pHPool)
102         return -1;
103
104     if (pthread_mutex_init(&pHPool->Lock, NULL))
105     {
106         DDBG("%s\n", "mutex init");
107         return -1;
108     }
109
110
111     if (pthread_cond_init(&pHPool->Cond, NULL))
112     {
113         pthread_mutex_destroy(&pHPool->Lock);
114         DDBG("%s\n", "cond_init");
115         return -1;
116     }
117
118     pHPool->iIdleCount = iNumHandles;
119     pHPool->pHList = NULL;
120
121     for (i = 0; i < iNumHandles; i++)
122     {
123         if (0 != AddHandleToThrPool(pHPool))
124         {
125             DDBG("%s\n", "add to thrpool");
126             result = -1;
127             break;
128         }
129     }
130
131     return result;
132 }
133
134 /**
135  * Synchronized method to returns a thread handle from the pool. The method is blocked till a
136  * handle is available.
137  */
138 IpcHandles *IpcThrPoolGet(IpcHandlePool *pHPool)
139 {
140     IpcHandles *pHandle;
141     pthread_mutex_lock(&pHPool->Lock);
142     while ((pHandle = pHPool->pHList) == NULL)
143     {
144         pthread_cond_wait(&pHPool->Cond, &pHPool->Lock);
145     }
146     pHPool->pHList = pHandle->pNext;
147     (pHPool->iIdleCount)--;
148     pthread_mutex_unlock(&pHPool->Lock);
149
150     return pHandle;
151 }
152
153 void IpcThrPoolPut(IpcHandlePool *pHPool, IpcHandles *pHandle)
154 {
155     pthread_mutex_lock(&pHPool->Lock);
156     pHandle->pNext = pHPool->pHList;
157     pHPool->pHList = pHandle;
158     (pHPool->iIdleCount)++;
159
160     pthread_cond_broadcast(&pHPool->Cond);
161     pthread_mutex_unlock(&pHPool->Lock);
162 }
163
164 /**
165  * Returns number of handles available in thread pool.
166  * Returns -1 when pool is invalid.
167  */
168 int IpcThrPoolIdleCount(IpcHandlePool *pHPool)
169 {
170
171     return pHPool? pHPool->iIdleCount : -1;
172 }
173