From ee43cd8f5de47c3ec1657c67d8f9bb140a89ae97 Mon Sep 17 00:00:00 2001
From: Roman Tsisyk <roman@tsisyk.com>
Date: Thu, 19 May 2016 09:19:41 +0300
Subject: [PATCH] Review fixes for gh-1452-cord-on-coeio

- Move start/stop hooks to coeio.c
- Don't invoke on_stop hook if on_start has failed
---
 src/box/wal.cc           |  2 +-
 src/coeio.c              | 36 +++++++++++++++++++++++++++---------
 src/coeio.h              |  3 +--
 src/main.cc              | 21 +--------------------
 third_party/libeio/etp.c | 10 ++++++++--
 5 files changed, 38 insertions(+), 34 deletions(-)

diff --git a/src/box/wal.cc b/src/box/wal.cc
index f9e4dff225..c7456cac65 100644
--- a/src/box/wal.cc
+++ b/src/box/wal.cc
@@ -661,7 +661,7 @@ wal_writer_f(va_list ap)
 {
 	struct wal_writer *writer = va_arg(ap, struct wal_writer *);
 	/** Initialize eio in this thread */
-	coeio_init();
+	coeio_enable();
 
 	writer->main_f = fiber();
 	cbus_join(&writer->tx_wal_bus, &writer->wal_pipe);
diff --git a/src/coeio.c b/src/coeio.c
index 55e8cc9322..f1a4dabe3d 100644
--- a/src/coeio.c
+++ b/src/coeio.c
@@ -108,13 +108,39 @@ coeio_done_poll_cb(void *ptr)
 	(void)ptr;
 }
 
+static int
+coeio_on_start(void *data)
+{
+	(void) data;
+	struct cord *cord = (struct cord *)calloc(sizeof(struct cord), 1);
+	if (!cord)
+		return -1;
+	cord_create(cord, "coeio");
+	return 0;
+}
+
+static int
+coeio_on_stop(void *data)
+{
+	(void) data;
+	cord_destroy(cord());
+	return 0;
+}
+
+void
+coeio_init(void)
+{
+	eio_set_thread_on_start(coeio_on_start, NULL);
+	eio_set_thread_on_stop(coeio_on_stop, NULL);
+}
+
 /**
  * Init coeio subsystem.
  *
  * Create idle and async watchers, init eio.
  */
 void
-coeio_init(void)
+coeio_enable(void)
 {
 	eio_init(&coeio_manager, coeio_want_poll_cb, coeio_done_poll_cb);
 	coeio_manager.loop = loop();
@@ -125,14 +151,6 @@ coeio_init(void)
 	ev_async_start(loop(), &coeio_manager.coeio_async);
 }
 
-void
-coeio_set_thread_cb(int (*on_start_cb)(void *),
-		    int (*on_stop_cb)(void *), void *data)
-{
-	eio_set_thread_on_start(on_start_cb, data);
-	eio_set_thread_on_stop(on_stop_cb, data);
-}
-
 static void
 coio_on_exec(eio_req *req)
 {
diff --git a/src/coeio.h b/src/coeio.h
index 5ffe74c99c..f572120cb0 100644
--- a/src/coeio.h
+++ b/src/coeio.h
@@ -49,9 +49,8 @@ extern "C" {
  */
 
 void coeio_init(void);
+void coeio_enable(void);
 
-void coeio_set_thread_cb(int (*on_start_cb)(void *),
-			 int (*on_stop_cb)(void *), void *data);
 
 struct coio_task;
 
diff --git a/src/main.cc b/src/main.cc
index e755be1e0e..2ba2fbcc10 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -517,25 +517,6 @@ tarantool_free(void)
 #endif
 }
 
-static int 
-coeio_cord_start(void *data)
-{
-	(void) data;
-	struct cord *cord = (struct cord *)calloc(sizeof(struct cord), 1);
-	if (!cord)
-		return -1;
-	cord_create(cord, "coeio");
-	return 0;
-}
-
-static int
-coeio_cord_stop(void *data)
-{
-	(void) data;
-	cord_destroy(cord());
-	return 0;
-}
-
 int
 main(int argc, char **argv)
 {
@@ -645,7 +626,7 @@ main(int argc, char **argv)
 	/* Init iobuf library with default readahead */
 	iobuf_init();
 	coeio_init();
-	coeio_set_thread_cb(coeio_cord_start, coeio_cord_stop, NULL);
+	coeio_enable();
 	signal_init();
 	tarantool_lua_init(tarantool_bin, main_argc, main_argv);
 	box_lua_init(tarantool_L);
diff --git a/third_party/libeio/etp.c b/third_party/libeio/etp.c
index 81921c85e0..42f7661ebe 100644
--- a/third_party/libeio/etp.c
+++ b/third_party/libeio/etp.c
@@ -331,8 +331,7 @@ X_THREAD_PROC (etp_proc)
 
   if (pool->on_start_cb)
     if (pool->on_start_cb(pool->on_start_data))
-      goto quit;
-
+      goto error;
 
   for (;;)
     {
@@ -393,6 +392,13 @@ X_THREAD_PROC (etp_proc)
     pool->on_stop_cb(pool->on_stop_data);
 
   return 0;
+
+error:
+  assert(pool->started > 0);
+  pool->started--;
+  X_COND_BROADCAST (pool->wrkwait);
+  X_UNLOCK (pool->lock);
+  return 0;
 }
 
 static void ecb_cold
-- 
GitLab