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 SURICATA_UTIL_POOL_THREAD_H
40 #define SURICATA_UTIL_POOL_THREAD_H
41 
42 #include "threads.h"
43 #include "util-pool.h"
44 
46  SCMutex lock; /**< lock, should have low contention */
47  Pool *pool; /**< actual pool */
48 };
49 // __attribute__((aligned(CLS))); <- VJ: breaks on clang 32bit, segv in PoolThreadTestGrow01
50 
52 
53 typedef struct PoolThread_ {
54  size_t size; /**< size of the array */
55  PoolThreadElement *array; /**< array of elements */
57 
58 /** per data item reserved data containing the
59  * thread pool id */
60 typedef uint16_t PoolThreadId;
61 
62 void PoolThreadRegisterTests(void);
63 
64 /** \brief initialize a thread pool
65  * \note same as PoolInit() except for "threads"
66  * \param threads number of threads to use this
67  * \retval pt thread pool or NULL on error */
68 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 *));
69 
70 /** \brief grow a thread pool by one
71  * \note copies settings from initial PoolThreadInit() call
72  * \param pt thread pool to grow
73  * \retval r id of new entry on succes, -1 on error */
75 
76 /** \brief destroy the thread pool
77  * \note wrapper around PoolFree()
78  * \param pt thread pool */
79 void PoolThreadFree(PoolThread *pt);
80 
81 /** \brief get data from thread pool by thread id
82  * \note wrapper around PoolGet()
83  * \param pt thread pool
84  * \param id thread id
85  * \retval ptr data or NULL */
86 void *PoolThreadGetById(PoolThread *pt, uint16_t id);
87 
88 /** \brief return data to thread pool
89  * \note wrapper around PoolReturn()
90  * \param pt thread pool
91  * \param data memory block to return, with PoolThreadReserved as it's first member */
92 void PoolThreadReturn(PoolThread *pt, void *data);
93 
95 void PoolThreadReturnRaw(PoolThread *pt, PoolThreadId id, void *data);
97 
98 /** \brief get size of PoolThread (number of 'threads', so array elements)
99  * \param pt thread pool
100  * \retval size or -1 on error */
101 int PoolThreadSize(PoolThread *pt);
102 
103 #endif /* SURICATA_UTIL_POOL_THREAD_H */
104 
105 /**
106  * @}
107  */
PoolThreadInit
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
Definition: util-pool-thread.c:43
PoolThreadExpand
int PoolThreadExpand(PoolThread *pt)
grow a thread pool by one
Definition: util-pool-thread.c:97
PoolThreadElement_::pool
Pool * pool
Definition: util-pool-thread.h:47
PoolThread_::size
size_t size
Definition: util-pool-thread.h:54
PoolThreadReturnRaw
void PoolThreadReturnRaw(PoolThread *pt, PoolThreadId id, void *data)
Definition: util-pool-thread.c:214
PoolThreadElement_
Definition: util-pool-thread.h:45
threads.h
Pool_
Definition: util-pool.h:43
PoolThreadElement_::lock
SCMutex lock
Definition: util-pool-thread.h:46
PoolThreadFree
void PoolThreadFree(PoolThread *pt)
destroy the thread pool
Definition: util-pool-thread.c:155
PoolThread
struct PoolThread_ PoolThread
PoolThreadReturn
void PoolThreadReturn(PoolThread *pt, void *data)
return data to thread pool
Definition: util-pool-thread.c:192
PoolThreadLock
void PoolThreadLock(PoolThread *pt, PoolThreadId id)
Definition: util-pool-thread.c:207
PoolThreadRegisterTests
void PoolThreadRegisterTests(void)
Definition: util-pool-thread.c:401
PoolThread_
Definition: util-pool-thread.h:53
PoolThreadId
uint16_t PoolThreadId
Definition: util-pool-thread.h:60
PoolThreadGetById
void * PoolThreadGetById(PoolThread *pt, uint16_t id)
get data from thread pool by thread id
Definition: util-pool-thread.c:173
PoolThread_::array
PoolThreadElement * array
Definition: util-pool-thread.h:55
PoolThreadSize
int PoolThreadSize(PoolThread *pt)
get size of PoolThread (number of 'threads', so array elements)
Definition: util-pool-thread.c:148
util-pool.h
SCMutex
#define SCMutex
Definition: threads-debug.h:114
PoolThreadUnlock
void PoolThreadUnlock(PoolThread *pt, PoolThreadId id)
Definition: util-pool-thread.c:221