[PATCH ulogd2 v2 v2 21/34] db: refactor ring-buffer initialization

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Move the code to initialize and start the thread that manages the ring-buffer
into a separate function.

No functional changes.

Signed-off-by: Jeremy Sowden <jeremy@xxxxxxxxxx>
---
 util/db.c | 87 +++++++++++++++++++++++++++++++++----------------------
 1 file changed, 53 insertions(+), 34 deletions(-)

diff --git a/util/db.c b/util/db.c
index a633257b5929..ee6dfb6b5a2a 100644
--- a/util/db.c
+++ b/util/db.c
@@ -62,6 +62,7 @@ static int _add_to_backlog(struct ulogd_pluginstance *upi,
 static int _process_backlog(struct ulogd_pluginstance *upi);
 
 static int _configure_ring(struct ulogd_pluginstance *upi);
+static int _start_ring(struct ulogd_pluginstance *upi);
 static int _add_to_ring(struct ulogd_pluginstance *upi, struct db_instance *di);
 static void *_process_ring(void *arg);
 
@@ -132,7 +133,6 @@ ulogd_db_start(struct ulogd_pluginstance *upi)
 {
 	struct db_instance *di = (struct db_instance *) upi->private;
 	int ret;
-	unsigned int i;
 
 	ulogd_log(ULOGD_NOTICE, "starting\n");
 
@@ -144,43 +144,13 @@ ulogd_db_start(struct ulogd_pluginstance *upi)
 	if (ret < 0)
 		goto db_error;
 
-	if (di->ring.size > 0) {
-		/* allocate */
-		di->ring.ring = calloc(di->ring.size, sizeof(char) * di->ring.length);
-		if (di->ring.ring == NULL)
-			goto db_error;
-		di->ring.wr_place = di->ring.ring;
-		ulogd_log(ULOGD_NOTICE,
-			  "Allocating %d elements of size %d for ring\n",
-			  di->ring.size, di->ring.length);
-		/* init start of query for each element */
-		for(i = 0; i < di->ring.size; i++) {
-			strcpy(di->ring.ring + di->ring.length * i + 1,
-			       di->stmt);
-		}
-		/* init cond & mutex */
-		ret = pthread_cond_init(&di->ring.cond, NULL);
-		if (ret != 0)
-			goto alloc_error;
-		ret = pthread_mutex_init(&di->ring.mutex, NULL);
-		if (ret != 0)
-			goto cond_error;
-		/* create thread */
-		ret = pthread_create(&di->db_thread_id, NULL, _process_ring, upi);
-		if (ret != 0)
-			goto mutex_error;
-	}
+	ret = _start_ring(upi);
+	if (ret < 0)
+		goto db_error;
 
 	di->interp = &_interp_db_init;
-
 	return 0;
 
-mutex_error:
-	pthread_mutex_destroy(&di->ring.mutex);
-cond_error:
-	pthread_cond_destroy(&di->ring.cond);
-alloc_error:
-	free(di->ring.ring);
 db_error:
 	di->driver->close_db(upi);
 	return -1;
@@ -762,6 +732,55 @@ _configure_ring(struct ulogd_pluginstance *upi)
 	return 0;
 }
 
+static int
+_start_ring(struct ulogd_pluginstance *upi)
+{
+	struct db_instance *di = (struct db_instance *) &upi->private;
+	unsigned int i;
+	int ret;
+
+	if (di->ring.size == 0)
+		return 0;
+
+	/* allocate */
+	di->ring.ring = calloc(di->ring.size, sizeof(char) * di->ring.length);
+	if (di->ring.ring == NULL)
+		return -1;
+	di->ring.wr_place = di->ring.ring;
+	ulogd_log(ULOGD_NOTICE,
+		  "Allocating %d elements of size %d for ring\n",
+		  di->ring.size, di->ring.length);
+
+	/* init start of query for each element */
+	for(i = 0; i < di->ring.size; i++)
+		strcpy(di->ring.ring + di->ring.length * i + 1,
+		       di->stmt);
+
+	/* init cond & mutex */
+	ret = pthread_cond_init(&di->ring.cond, NULL);
+	if (ret != 0)
+		goto alloc_error;
+	ret = pthread_mutex_init(&di->ring.mutex, NULL);
+	if (ret != 0)
+		goto cond_error;
+
+	/* create thread */
+	ret = pthread_create(&di->db_thread_id, NULL, _process_ring, upi);
+	if (ret != 0)
+		goto mutex_error;
+
+	return 0;
+
+mutex_error:
+	pthread_mutex_destroy(&di->ring.mutex);
+cond_error:
+	pthread_cond_destroy(&di->ring.cond);
+alloc_error:
+	free(di->ring.ring);
+
+	return -1;
+}
+
 static int
 _add_to_ring(struct ulogd_pluginstance *upi, struct db_instance *di)
 {
-- 
2.35.1




[Index of Archives]     [Netfitler Users]     [Berkeley Packet Filter]     [LARTC]     [Bugtraq]     [Yosemite Forum]

  Powered by Linux