2 * Copyright (c) 2011, Joakim Johansson <jocke@tbricks.com>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice unmodified, this list of conditions, and the following
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #if (WITEM_CACHE_TYPE == 1)
37 witem_cache_init(void)
43 witem_alloc(void (*func)(void *), void *func_arg)
47 while (!(witem = fastpath(malloc(ROUND_UP_TO_CACHELINE_SIZE(sizeof(*witem)))))) {
53 witem->item_entry.stqe_next = 0;
55 witem->func_arg = func_arg;
61 witem_free(struct work *wi)
63 dbg_printf("freed work item %p", wi);
68 witem_cache_cleanup(void *value)
73 /* libumem based object cache */
75 #elif (WITEM_CACHE_TYPE == 2)
79 static umem_cache_t *witem_cache;
82 witem_cache_init(void)
84 witem_cache = umem_cache_create((char *) "witem_cache",
97 witem_alloc(void (*func)(void *), void *func_arg)
101 while (!(witem = fastpath(umem_cache_alloc(witem_cache, UMEM_DEFAULT)))) {
107 witem->item_entry.stqe_next = 0;
109 witem->func_arg = func_arg;
115 witem_free(struct work *wi)
117 umem_cache_free(witem_cache, wi);
122 witem_cache_cleanup(void *value)
128 /* TSD based cacheing per thread */
130 #elif (WITEM_CACHE_TYPE == 3)
132 pthread_key_t witem_cache_key;
135 witem_cache_init(void)
137 pthread_key_create(&witem_cache_key, witem_cache_cleanup);
142 witem_alloc_from_heap(void)
146 while (!(witem = fastpath(malloc(ROUND_UP_TO_CACHELINE_SIZE(sizeof(*witem)))))) {
152 witem->item_entry.stqe_next = 0;
158 witem_alloc(void (*func)(void *), void *func_arg)
160 struct work *witem = fastpath(pthread_getspecific(witem_cache_key));
163 pthread_setspecific(witem_cache_key, witem->wi_next);
167 witem = witem_alloc_from_heap();
171 witem->func_arg = func_arg;
177 witem_free(struct work *witem)
179 struct work *prev_wi = pthread_getspecific(witem_cache_key);
181 witem->wi_next = prev_wi;
183 // We need to initialize here also...
186 witem->item_entry.stqe_next = 0;
188 witem->func_arg = NULL;
190 pthread_setspecific(witem_cache_key, witem);
194 witem_cache_cleanup(void *value)
196 struct work *wi, *next_wi = value;
198 while ((wi = next_wi)) {
199 next_wi = wi->wi_next;
205 #error Invalid witem cache type specified