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,
69  void *(*Alloc)(void), int (*Init)(void *), void (*Cleanup)(void *));
70 
71 /** \brief grow a thread pool by one
72  * \note copies settings from initial PoolThreadInit() call
73  * \param pt thread pool to grow
74  * \retval r id of new entry on succes, -1 on error */
76 
77 /** \brief destroy the thread pool
78  * \note wrapper around PoolFree()
79  * \param pt thread pool */
80 void PoolThreadFree(PoolThread *pt);
81 
82 /** \brief get data from thread pool by thread id
83  * \note wrapper around PoolGet()
84  * \param pt thread pool
85  * \param id thread id
86  * \retval ptr data or NULL */
87 void *PoolThreadGetById(PoolThread *pt, uint16_t id);
88 
89 /** \brief return data to thread pool
90  * \note wrapper around PoolReturn()
91  * \param pt thread pool
92  * \param data memory block to return, with PoolThreadReserved as it's first member */
93 void PoolThreadReturn(PoolThread *pt, void *data);
94 
96 void PoolThreadReturnRaw(PoolThread *pt, PoolThreadId id, void *data);
98 
99 /** \brief get size of PoolThread (number of 'threads', so array elements)
100  * \param pt thread pool
101  * \retval size or -1 on error */
102 int PoolThreadSize(PoolThread *pt);
103 
104 #endif /* SURICATA_UTIL_POOL_THREAD_H */
105 
106 /**
107  * @}
108  */
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:211
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:152
PoolThread
struct PoolThread_ PoolThread
PoolThreadReturn
void PoolThreadReturn(PoolThread *pt, void *data)
return data to thread pool
Definition: util-pool-thread.c:189
PoolThreadLock
void PoolThreadLock(PoolThread *pt, PoolThreadId id)
Definition: util-pool-thread.c:204
PoolThreadInit
PoolThread * PoolThreadInit(int threads, uint32_t size, uint32_t prealloc_size, uint32_t elt_size, void *(*Alloc)(void), int(*Init)(void *), void(*Cleanup)(void *))
initialize a thread pool
Definition: util-pool-thread.c:44
PoolThreadRegisterTests
void PoolThreadRegisterTests(void)
Definition: util-pool-thread.c:361
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:170
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:145
util-pool.h
SCMutex
#define SCMutex
Definition: threads-debug.h:114
PoolThreadUnlock
void PoolThreadUnlock(PoolThread *pt, PoolThreadId id)
Definition: util-pool-thread.c:218