]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
pool/thread: introduce simpler way to grow thread pool
authorVictor Julien <victor@inliniac.net>
Tue, 28 May 2019 13:14:20 +0000 (15:14 +0200)
committerVictor Julien <victor@inliniac.net>
Wed, 29 May 2019 13:34:36 +0000 (15:34 +0200)
src/util-pool-thread.c
src/util-pool-thread.h

index e531e8935c24ba9a04cc493e7bdd869bb6049d1d..438f95aa7d8e7df66e314db41948e262f037aebd 100644 (file)
@@ -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);
+}
+
 /**
  *
  */
index d43d4e8c9fb50c8038cd8b2d6cac3f62d63e9fe6..26d1a5a18ef1c57ff909f20c217808143930ceb6 100644 (file)
@@ -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 */