/* Drop the capabilities for this thread */
SCDropCaps(tv);
+ PacketPoolInit();
+
if (s->SlotThreadInit != NULL) {
void *slot_data = NULL;
r = s->SlotThreadInit(tv, s->slot_initdata, &slot_data);
/* Drop the capabilities for this thread */
SCDropCaps(tv);
+ PacketPoolInit();
+
if (s->SlotThreadInit != NULL) {
void *slot_data = NULL;
r = s->SlotThreadInit(tv, s->slot_initdata, &slot_data);
/* Drop the capabilities for this thread */
SCDropCaps(tv);
+ PacketPoolInit();
+
SCLogDebug("%s starting", tv->name);
if (s->SlotThreadInit != NULL) {
/* Drop the capabilities for this thread */
SCDropCaps(tv);
+ PacketPoolInit();
+
SCLogDebug("%s starting", tv->name);
if (s->SlotThreadInit != NULL) {
/* Drop the capabilities for this thread */
SCDropCaps(tv);
+ PacketPoolInit();
+
/* check if we are setup properly */
if (s == NULL || s->PktAcqLoop == NULL || tv->tmqh_in == NULL || tv->tmqh_out == NULL) {
SCLogError(SC_ERR_FATAL, "TmSlot or ThreadVars badly setup: s=%p,"
#include "util-profiling.h"
#include "util-device.h"
+/* Number of freed packet to save for one pool before freeing them. */
+#define MAX_PENDING_RETURN_PACKETS 32
+
#ifdef TLS
__thread PktPool thread_pkt_pool;
static inline PktPool *ThreadPacketPoolCreate(void)
{
- /* Nothing to do since __thread allocates the memory. */
+ /* Nothing to do since __thread statically allocates the memory. */
return GetThreadPacketPool();
}
-
-
#else
/* __thread not supported. */
static pthread_key_t pkt_pool_thread_key;
+static SCMutex pkt_pool_thread_key_mutex = SCMUTEX_INITIALIZER;
+static int pkt_pool_thread_key_initialized = 0;
static inline PktPool *GetThreadPacketPool(void)
{
return (PktPool*)pthread_getspecific(pkt_pool_thread_key);
}
+static void PktPoolThreadDestroy(void * buf)
+{
+ free(buf);
+}
+
+static void TmqhPacketpoolInit(void)
+{
+ SCMutexLock(&pkt_pool_thread_key_mutex);
+ if (pkt_pool_thread_key_initialized) {
+ /* Key has already been created. */
+ SCMutexUnlock(&pkt_pool_thread_key_mutex);
+ return;
+ }
+
+ /* Create the pthread Key that is used to look up thread specific
+ * data buffer. Needs to be created only once.
+ */
+ int r = pthread_key_create(&pkt_pool_thread_key, PktPoolThreadDestroy);
+ if (r != 0) {
+ SCLogError(SC_ERR_MEM_ALLOC, "pthread_key_create failed with %d", r);
+ exit(EXIT_FAILURE);
+ }
+
+ pkt_pool_thread_key_initialized = 1;
+ SCMutexUnlock(&pkt_pool_thread_key_mutex);
+}
+
static inline PktPool *ThreadPacketPoolCreate(void)
{
+ TmqhPacketpoolInit();
+
/* Check that the pool is not already created */
PktPool *pool = GetThreadPacketPool();
if (pool)
SCLogError(SC_ERR_MEM_ALLOC, "malloc failed");
exit(EXIT_FAILURE);
}
- pthread_setspecific(pkt_pool_thread_key, pool);
+ int r = pthread_setspecific(pkt_pool_thread_key, pool);
+ if (r != 0) {
+ SCLogError(SC_ERR_MEM_ALLOC, "pthread_setspecific failed with %d", r);
+ exit(EXIT_FAILURE);
+ }
return pool;
}
-
-static void PktPoolThreadDestroy(void * buf)
-{
- free(buf);
-}
-
-#endif
-
-/* Number of freed packet to save for one pool before freeing them. */
-#define MAX_PENDING_RETURN_PACKETS 32
-
-void TmqhPacketpoolInit(void)
-{
-#ifndef TLS
- /* Create the pthread Key that is used to look up thread specific
- * data buffer. Needs to be created only once.
- */
- pthread_key_create(&pkt_pool_thread_key, PktPoolThreadDestroy);
#endif
-}
/**
* \brief TmqhPacketpoolRegister
tmqh_table[TMQH_PACKETPOOL].name = "packetpool";
tmqh_table[TMQH_PACKETPOOL].InHandler = TmqhInputPacketpool;
tmqh_table[TMQH_PACKETPOOL].OutHandler = TmqhOutputPacketpool;
-
- TmqhPacketpoolInit();
}
static int PacketPoolIsEmpty(PktPool *pool)