39 #ifndef _CTHREAD_POOL_H_
40 #define _CTHREAD_POOL_H_
45 #include "cthread_int.h"
80 struct qnode_pool *pool;
89 struct qnode *fast_alloc;
92 char name[CTHREAD_NAME_SIZE];
105 static inline struct qnode_pool *
106 _qnode_pool_create(
const char *name,
int prealloc_size)
108 struct qnode_pool *p = calloc(1,
sizeof(
struct qnode_pool));
114 p->stub = calloc(1,
sizeof(
struct qnode));
117 if (p->stub == NULL) {
123 strlcpy(p->name, name,
sizeof(p->name));
124 p->name[
sizeof(p->name) - 1] = 0;
127 p->stub->next = NULL;
130 p->pre_alloc = prealloc_size;
143 static inline void __attribute__((always_inline))
144 _qnode_pool_insert(
struct qnode_pool *p,
struct qnode *n)
147 struct qnode *prev = n;
149 prev = (
struct qnode *)__sync_lock_test_and_set((uint64_t *)&p->head, (uint64_t)prev);
171 static inline struct qnode *__attribute__((always_inline)) _pool_remove(
struct qnode_pool *p)
174 struct qnode *tail = p->tail;
175 struct qnode *next = tail->next;
178 if (tail == p->stub) {
186 if (
likely(next != NULL)) {
196 _qnode_pool_insert(p, p->stub);
215 static inline struct qnode *__attribute__((always_inline)) _qnode_pool_remove(
struct qnode_pool *p)
225 }
while ((p->head != p->tail) && (p->tail != p->stub));
233 static inline struct qnode *__attribute__((always_inline)) _qnode_alloc(
void)
235 struct qnode_pool *p = (THIS_SCHED)->qnode_pool;
236 int prealloc_size = p->pre_alloc;
240 if (
likely(p->fast_alloc != NULL)) {
242 p->fast_alloc = NULL;
246 n = _qnode_pool_remove(p);
249 for (i = 0; i < prealloc_size; i++) {
250 n = calloc(1,
sizeof(
struct qnode));
255 _qnode_pool_insert(p, n);
257 n = _qnode_pool_remove(p);
266 static inline void __attribute__((always_inline)) _qnode_free(
struct qnode *n)
268 struct qnode_pool *p = n->pool;
270 if (
unlikely(p->fast_alloc != NULL) ||
unlikely(n->pool != (THIS_SCHED)->qnode_pool)) {
271 _qnode_pool_insert(p, n);
282 _qnode_pool_destroy(
struct qnode_pool *p)
#define cne_compiler_barrier()
#define __cne_cache_aligned