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
53typedef 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 */
60typedef uint16_t PoolThreadId;
61
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 */
68PoolThread *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 */
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 */
86void *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 */
92void PoolThreadReturn(PoolThread *pt, void *data);
93
95void 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 */
102
103#endif /* SURICATA_UTIL_POOL_THREAD_H */
104
105/**
106 * @}
107 */
void PoolThreadUnlock(PoolThread *pt, PoolThreadId id)
void PoolThreadFree(PoolThread *pt)
destroy the thread pool
int PoolThreadExpand(PoolThread *pt)
grow a thread pool by one
void PoolThreadRegisterTests(void)
void * PoolThreadGetById(PoolThread *pt, uint16_t id)
get data from thread pool by thread id
void PoolThreadReturn(PoolThread *pt, void *data)
return data to thread pool
int PoolThreadSize(PoolThread *pt)
get size of PoolThread (number of 'threads', so array elements)
void PoolThreadReturnRaw(PoolThread *pt, PoolThreadId id, void *data)
void PoolThreadLock(PoolThread *pt, PoolThreadId id)
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
PoolThreadElement * array
#define SCMutex
struct PoolThread_ PoolThread
uint16_t PoolThreadId