From d41cda32a94ccdebe2566deef9c27ff4a33a991b Mon Sep 17 00:00:00 2001
From: Konstantin Osipov <kostja@tarantool.org>
Date: Sat, 10 Oct 2015 13:38:50 +0300
Subject: [PATCH] arm: coro.cc -> coro.c, begin transition of fiber.cc

---
 src/CMakeLists.txt      |  2 +-
 src/{coro.cc => coro.c} | 11 +++++------
 src/coro.h              |  9 ++++++++-
 src/exception.h         |  2 --
 src/fiber.cc            | 43 ++++++++---------------------------------
 src/fiber.h             | 35 ++++++++++++++++++++++++---------
 src/lua/fiber.cc        |  8 ++++++++
 7 files changed, 56 insertions(+), 54 deletions(-)
 rename src/{coro.cc => coro.c} (94%)

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index dce1edb3b0..22e678c7d6 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -62,7 +62,7 @@ set (core_sources
      fiber.cc
      cbus.c
      exception.cc
-     coro.cc
+     coro.c
      reflection.c
      assoc.c
      rmean.c
diff --git a/src/coro.cc b/src/coro.c
similarity index 94%
rename from src/coro.cc
rename to src/coro.c
index 24dcd9632a..1e1c783e11 100644
--- a/src/coro.cc
+++ b/src/coro.c
@@ -31,15 +31,14 @@
 #include "coro.h"
 
 #include "trivia/config.h"
-#include "exception.h"
 #include <unistd.h>
 #include <string.h>
 #include <sys/mman.h>
-
+#include "small/slab_cache.h"
 #include "third_party/valgrind/memcheck.h"
-#include "fiber.h"
 
-void
+
+int
 tarantool_coro_create(struct tarantool_coro *coro,
 		      struct slab_cache *slabc,
 		      void (*f) (void *), void *data)
@@ -54,14 +53,14 @@ tarantool_coro_create(struct tarantool_coro *coro,
 					+ slab_sizeof();
 
 	if (coro->stack == NULL) {
-		tnt_raise(OutOfMemory, sizeof(coro->stack_size),
-			  "mmap", "coro stack");
+		return -1;
 	}
 
 	(void) VALGRIND_STACK_REGISTER(coro->stack, (char *)
 				       coro->stack + coro->stack_size);
 
 	coro_create(&coro->ctx, f, data, coro->stack, coro->stack_size);
+	return 0;
 }
 
 void
diff --git a/src/coro.h b/src/coro.h
index 55b7f54198..dde3c03f9c 100644
--- a/src/coro.h
+++ b/src/coro.h
@@ -34,6 +34,10 @@
 
 #include <third_party/coro/coro.h>
 
+#if defined(__cplusplus)
+extern "C" {
+#endif /* defined(__cplusplus) */
+
 struct tarantool_coro {
 	coro_context ctx;
 	void *stack;
@@ -42,12 +46,15 @@ struct tarantool_coro {
 
 struct slab_cache;
 
-void
+int
 tarantool_coro_create(struct tarantool_coro *ctx,
 		      struct slab_cache *cache,
 		      void (*f) (void *), void *data);
 void
 tarantool_coro_destroy(struct tarantool_coro *ctx,
 		       struct slab_cache *cache);
+#if defined(__cplusplus)
+} /* extern "C" */
+#endif /* defined(__cplusplus) */
 
 #endif /* TARANTOOL_CORO_H_INCLUDED */
diff --git a/src/exception.h b/src/exception.h
index c10e127e36..045f9eec88 100644
--- a/src/exception.h
+++ b/src/exception.h
@@ -122,8 +122,6 @@ class TimedOut: public SystemError {
 	virtual void raise() { throw this; }
 };
 
-/** \endcond */
-
 #define tnt_error(class, ...) ({					\
 	say_debug("%s at %s:%i", #class, __FILE__, __LINE__);		\
 	class *e = new class(__FILE__, __LINE__, ##__VA_ARGS__);	\
diff --git a/src/fiber.cc b/src/fiber.cc
index 102a286298..3eb90638fe 100644
--- a/src/fiber.cc
+++ b/src/fiber.cc
@@ -156,7 +156,6 @@ fiber_wakeup(struct fiber *f)
  * in it. For cancellation to work, this exception type should be
  * re-raised whenever (if) it is caught.
  */
-
 void
 fiber_cancel(struct fiber *f)
 {
@@ -175,40 +174,10 @@ fiber_cancel(struct fiber *f)
 			fiber_wakeup(f);
 		fiber_yield();
 	}
-	/*
-	 * Check if we're ourselves cancelled.
-	 * This also implements cancel for the case when
-	 * f == fiber().
-	 */
-	fiber_testcancel();
-}
-
-bool
-fiber_is_cancelled()
-{
-	return fiber()->flags & FIBER_IS_CANCELLED;
 }
 
-/** Test if this fiber is in a cancellable state and was indeed
- * cancelled, and raise an exception (FiberCancelException) if
- * that's the case.
- */
-void
-fiber_testcancel(void)
-{
-	/*
-	 * Fiber can catch FiberCancelException using try..catch
-	 * block in C or pcall()/xpcall() in Lua. However,
-	 * FIBER_IS_CANCELLED flag is still set and the subject
-	 * fiber will be killed by subsequent unprotected call of
-	 * this function.
-	 */
-	if (fiber_is_cancelled())
-		tnt_raise(FiberCancelException);
-}
-
-
-/** Change the current cancellation state of a fiber. This is not
+/**
+ * Change the current cancellation state of a fiber. This is not
  * a cancellation point.
  */
 bool
@@ -252,6 +221,7 @@ fiber_join(struct fiber *fiber)
 		e->raise();
 	fiber_testcancel();
 }
+
 /**
  * @note: this is not a cancellation point (@sa fiber_testcancel())
  * but it is considered good practice to call testcancel()
@@ -525,8 +495,11 @@ fiber_new(const char *name, void (*f) (va_list))
 	} else {
 		fiber = (struct fiber *) mempool_alloc0(&cord->fiber_pool);
 
-		tarantool_coro_create(&fiber->coro, &cord->slabc,
-				      fiber_loop, NULL);
+		if (tarantool_coro_create(&fiber->coro, &cord->slabc,
+					  fiber_loop, NULL)) {
+			tnt_raise(OutOfMemory, 65536,
+				  "runtime arena", "coro stack");
+		}
 
 		region_create(&fiber->gc, &cord->slabc);
 
diff --git a/src/fiber.h b/src/fiber.h
index 5211503939..707f9196e5 100644
--- a/src/fiber.h
+++ b/src/fiber.h
@@ -306,13 +306,6 @@ fiber_find(uint32_t fid);
 void
 fiber_cancel(struct fiber *f);
 
-/**
- * Check if the current fiber has been cancelled.  Raises
- * tnt_FiberCancelException
- */
-void
-fiber_testcancel(void);
-
 /**
  * Make it possible or not possible to wakeup the current
  * fiber immediately when it's cancelled.
@@ -354,8 +347,12 @@ fiber_set_key(struct fiber *fiber, enum fiber_key key, void *value)
 	fiber->fls[key] = value;
 }
 
-bool
-fiber_is_cancelled();
+static inline bool
+fiber_is_cancelled()
+{
+	return fiber()->flags & FIBER_IS_CANCELLED;
+}
+
 
 static inline bool
 fiber_is_dead(struct fiber *f)
@@ -420,6 +417,26 @@ class FiberCancelException: public Exception {
 	}
 	virtual void raise() { throw this; }
 };
+
+/*
+ * Test if this fiber is in a cancellable state and was indeed
+ * cancelled, and raise an exception (FiberCancelException) if
+ * that's the case.
+ */
+static inline void
+fiber_testcancel(void)
+{
+	/*
+	 * Fiber can catch FiberCancelException using try..catch
+	 * block in C or pcall()/xpcall() in Lua. However,
+	 * FIBER_IS_CANCELLED flag is still set and the subject
+	 * fiber will be killed by subsequent unprotected call of
+	 * this function.
+	 */
+	if (fiber_is_cancelled())
+		tnt_raise(FiberCancelException);
+}
+
 #endif /* defined(__cplusplus) */
 
 #endif /* TARANTOOL_FIBER_H_INCLUDED */
diff --git a/src/lua/fiber.cc b/src/lua/fiber.cc
index 4c4fe8020c..0645f2b1ec 100644
--- a/src/lua/fiber.cc
+++ b/src/lua/fiber.cc
@@ -479,6 +479,12 @@ lbox_fiber_cancel(struct lua_State *L)
 {
 	struct fiber *f = lbox_checkfiber(L, 1);
 	fiber_cancel(f);
+	/*
+	 * Check if we're ourselves cancelled.
+	 * This also implements cancel for the case when
+	 * f == fiber().
+	 */
+	fiber_testcancel();
 	return 0;
 }
 
@@ -493,6 +499,8 @@ lbox_fiber_kill(struct lua_State *L)
 	if (f == NULL)
 		luaL_error(L, "fiber.kill(): fiber not found");
 	fiber_cancel(f);
+	/* Check if we're ourselves cancelled. */
+	fiber_testcancel();
 	return 0;
 }
 
-- 
GitLab