pool/thread: introduce simpler way to grow thread pool

pull/3898/head
Victor Julien 6 years ago
parent 6e6c1bad7d
commit 0b02efdf01

@ -86,6 +86,60 @@ error:
return NULL;
}
/** \brief expand pool by one for a new thread
* \retval -1 or pool thread id
*/
int PoolThreadExpand(PoolThread *pt)
{
if (pt == NULL || pt->array == NULL || pt->size == 0) {
SCLogError(SC_ERR_POOL_INIT, "pool grow failed");
return -1;
}
size_t newsize = pt->size + 1;
SCLogDebug("newsize %"PRIuMAX, (uintmax_t)newsize);
void *ptmp = SCRealloc(pt->array, (newsize * sizeof(PoolThreadElement)));
if (ptmp == NULL) {
SCFree(pt->array);
pt->array = NULL;
SCLogError(SC_ERR_POOL_INIT, "pool grow failed");
return -1;
}
pt->array = ptmp;
pt->size = newsize;
/* copy settings from first thread that registered the pool */
Pool settings;
memset(&settings, 0x0, sizeof(settings));
PoolThreadElement *e = &pt->array[0];
SCMutexLock(&e->lock);
settings.max_buckets = e->pool->max_buckets;
settings.preallocated = e->pool->preallocated;
settings.elt_size = e->pool->elt_size;
settings.Alloc = e->pool->Alloc;
settings.Init = e->pool->Init;
settings.InitData = e->pool->InitData;
settings.Cleanup = e->pool->Cleanup;
settings.Free = e->pool->Free;
SCMutexUnlock(&e->lock);
e = &pt->array[newsize - 1];
memset(e, 0x00, sizeof(*e));
SCMutexInit(&e->lock, NULL);
SCMutexLock(&e->lock);
e->pool = PoolInit(settings.max_buckets, settings.preallocated,
settings.elt_size, settings.Alloc, settings.Init, settings.InitData,
settings.Cleanup, settings.Free);
SCMutexUnlock(&e->lock);
if (e->pool == NULL) {
SCLogError(SC_ERR_POOL_INIT, "pool grow failed");
return -1;
}
return (int)(newsize - 1);
}
/**
*
*/

@ -70,6 +70,12 @@ PoolThread *PoolThreadInit(int threads, uint32_t size, uint32_t prealloc_size, u
* \retval r id of new entry on succes, -1 on error */
int PoolThreadGrow(PoolThread *pt, 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 *));
/** \brief grow a thread pool by one
* \note copies settings from initial PoolThreadInit() call
* \param pt thread pool to grow
* \retval r id of new entry on succes, -1 on error */
int PoolThreadExpand(PoolThread *pt);
/** \brief destroy the thread pool
* \note wrapper around PoolFree()
* \param pt thread pool */

Loading…
Cancel
Save