suricata
util-pool-thread.h
Go to the documentation of this file.
1 /* Copyright (C) 2013 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \ingroup utilpool
20  *
21  * @{
22  */
23 
24 /**
25  * \file
26  *
27  * \author Victor Julien <victor@inliniac.net>
28  */
29 
30 /**
31  * Consumers of this API MUST add PoolThreadReserved as the first
32  * member in the data structure. They also MUST ignore that data
33  * completely. It's managed by this API.
34  *
35  * It's purpose is to make sure thread X can return data to a pool
36  * from thread Y.
37  */
38 
39 #ifndef __UTIL_POOL_THREAD_H__
40 #define __UTIL_POOL_THREAD_H__
41 
43  SCMutex lock; /**< lock, should have low contention */
44  Pool *pool; /**< actual pool */
45 };
46 // __attribute__((aligned(CLS))); <- VJ: breaks on clang 32bit, segv in PoolThreadTestGrow01
47 
49 
50 typedef struct PoolThread_ {
51  size_t size; /**< size of the array */
52  PoolThreadElement *array; /**< array of elements */
53 } PoolThread;
54 
55 /** per data item reserved data containing the
56  * thread pool id */
57 typedef uint16_t PoolThreadReserved;
58 
59 void PoolThreadRegisterTests(void);
60 
61 /** \brief initialize a thread pool
62  * \note same as PoolInit() except for "threads"
63  * \param threads number of threads to use this
64  * \retval pt thread pool or NULL on error */
65 PoolThread *PoolThreadInit(int threads, uint32_t size, uint32_t prealloc_size, uint32_t elt_size, void *(*Alloc)(void), int (*Init)(void *, void *), void *InitData, void (*Cleanup)(void *), void (*Free)(void *));
66 
67 /** \brief grow a thread pool by one
68  * \note calls PoolInit so all args but 'pt' are the same
69  * \param pt thread pool to grow
70  * \retval r id of new entry on succes, -1 on error */
71 int PoolThreadGrow(PoolThread *pt, uint32_t size, uint32_t prealloc_size, uint32_t elt_size, void *(*Alloc)(void), int (*Init)(void *, void *), void *InitData, void (*Cleanup)(void *), void (*Free)(void *));
72 
73 /** \brief destroy the thread pool
74  * \note wrapper around PoolFree()
75  * \param pt thread pool */
76 void PoolThreadFree(PoolThread *pt);
77 
78 /** \brief get data from thread pool by thread id
79  * \note wrapper around PoolGet()
80  * \param pt thread pool
81  * \param id thread id
82  * \retval ptr data or NULL */
83 void *PoolThreadGetById(PoolThread *pt, uint16_t id);
84 
85 /** \brief return data to thread pool
86  * \note wrapper around PoolReturn()
87  * \param pt thread pool
88  * \param data memory block to return, with PoolThreadReserved as it's first member */
89 void PoolThreadReturn(PoolThread *pt, void *data);
90 
91 /** \brief get size of PoolThread (number of 'threads', so array elements)
92  * \param pt thread pool
93  * \retval size or -1 on error */
94 int PoolThreadSize(PoolThread *pt);
95 
96 #endif /* __UTIL_POOL_THREAD_H__ */
97 
98 /**
99  * @}
100  */
PoolThreadElement * array
void PoolThreadFree(PoolThread *pt)
destroy the thread pool
struct PoolThread_ PoolThread
void PoolThreadReturn(PoolThread *pt, void *data)
return data to thread pool
void PoolThreadRegisterTests(void)
int PoolThreadGrow(PoolThread *pt, uint32_t size, uint32_t prealloc_size, uint32_t elt_size, void *(*Alloc)(void), int(*Init)(void *, void *), void *InitData, void(*Cleanup)(void *), void(*Free)(void *))
grow a thread pool by one
#define SCMutex
void * PoolThreadGetById(PoolThread *pt, uint16_t id)
get data from thread pool by thread id
uint16_t PoolThreadReserved
int PoolThreadSize(PoolThread *pt)
get size of PoolThread (number of &#39;threads&#39;, so array elements)
PoolThread * PoolThreadInit(int threads, uint32_t size, uint32_t prealloc_size, uint32_t elt_size, void *(*Alloc)(void), int(*Init)(void *, void *), void *InitData, void(*Cleanup)(void *), void(*Free)(void *))
initialize a thread pool