2 Copyright (c) 2014, McAfee, Inc.
6 Redistribution and use in source and binary forms, with or without modification,
7 are permitted provided that the following conditions are met:
9 Redistributions of source code must retain the above copyright notice, this list
10 of conditions and the following disclaimer.
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.
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
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.
34 * \brief Ipc Thread pool Ipc Source File
36 * This file implements the thread pool used by IPC Server in Security framework.
44 #include "IpcThrdPool.h"
46 static void IpcThrPoolReset(IpcHandlePool *pHPool)
53 while ((pHandle = pHPool->pHList) != NULL)
55 pHPool->pHList = pHPool->pHList->pNext;
59 pthread_cond_destroy(&pHPool->Cond);
60 pthread_mutex_destroy(&pHPool->Lock);
61 pHPool->iIdleCount = 0;
65 * Frees the resources and nullifies the thread pool object.
67 void IpcThrPoolFree(IpcHandlePool **pHPool)
69 IpcThrPoolReset(*pHPool);
75 * Creates a new handle and makes it head of the pool list.
76 * Return 0 on success and -1 on failure.
78 static int AddHandleToThrPool(IpcHandlePool *pHPool)
80 IpcHandles *pHandle = NULL;
81 if ((pHandle = (IpcHandles *) calloc(1, sizeof(IpcHandles))) == NULL)
83 DDBG("%s\n", "calloc IpcHandles");
88 pHandle->pNext = pHPool->pHList;
89 pHPool->pHList = pHandle;
94 * Initializes thread pool. Returns 0 on success and -1 on failure.
96 int IpcThrPoolInit(IpcHandlePool *pHPool, int iNumHandles)
104 if (pthread_mutex_init(&pHPool->Lock, NULL))
106 DDBG("%s\n", "mutex init");
111 if (pthread_cond_init(&pHPool->Cond, NULL))
113 pthread_mutex_destroy(&pHPool->Lock);
114 DDBG("%s\n", "cond_init");
118 pHPool->iIdleCount = iNumHandles;
119 pHPool->pHList = NULL;
121 for (i = 0; i < iNumHandles; i++)
123 if (0 != AddHandleToThrPool(pHPool))
125 DDBG("%s\n", "add to thrpool");
135 * Synchronized method to returns a thread handle from the pool. The method is blocked till a
136 * handle is available.
138 IpcHandles *IpcThrPoolGet(IpcHandlePool *pHPool)
141 pthread_mutex_lock(&pHPool->Lock);
142 while ((pHandle = pHPool->pHList) == NULL)
144 pthread_cond_wait(&pHPool->Cond, &pHPool->Lock);
146 pHPool->pHList = pHandle->pNext;
147 (pHPool->iIdleCount)--;
148 pthread_mutex_unlock(&pHPool->Lock);
153 void IpcThrPoolPut(IpcHandlePool *pHPool, IpcHandles *pHandle)
155 pthread_mutex_lock(&pHPool->Lock);
156 pHandle->pNext = pHPool->pHList;
157 pHPool->pHList = pHandle;
158 (pHPool->iIdleCount)++;
160 pthread_cond_broadcast(&pHPool->Cond);
161 pthread_mutex_unlock(&pHPool->Lock);
165 * Returns number of handles available in thread pool.
166 * Returns -1 when pool is invalid.
168 int IpcThrPoolIdleCount(IpcHandlePool *pHPool)
171 return pHPool? pHPool->iIdleCount : -1;