[Cbe-oss-dev] [PATCH 1/3]MARS/core: Wrap wait condition loops

Yuji Mano yuji.mano at am.sony.com
Tue Dec 9 17:27:36 EST 2008


From: Kazunori Asayama <asayama at sm.sony.co.jp>

Wrap loops to wait for shared memory condition (core)

This patch wraps loops to wait for shared memory condition. This
change is a preparation for hybrid system support and for elimination
of busy loops.

Signed-off-by: Kazunori Asayama <asayama at sm.sony.co.jp>
---
 core/include/host/mars/core.h      |    9 ++++++
 core/src/host/lib/Makefile.am      |    1 
 core/src/host/lib/cond.c           |   55 +++++++++++++++++++++++++++++++++++++
 core/src/host/lib/workload_queue.c |   29 ++++++++++++++-----
 4 files changed, 86 insertions(+), 8 deletions(-)

--- a/core/include/host/mars/core.h
+++ b/core/include/host/mars/core.h
@@ -265,6 +265,15 @@ static inline uint64_t mars_ptr_to_ea(co
  */
 uint32_t mars_get_ticks(void);
 
+/* test_cond:
+ *	MARS_SUCCESS: the condition is satisfied
+ *	<0: the condition is not satisfied
+ *	>0: error code
+ */
+int mars_ea_cond_wait(uint64_t watch_point_ea,
+		      int (*test_cond)(uint32_t , void *),
+		      void *test_cond_param);
+
 #if defined(__cplusplus)
 }
 #endif
--- a/core/src/host/lib/Makefile.am
+++ b/core/src/host/lib/Makefile.am
@@ -94,6 +94,7 @@ lib_LTLIBRARIES = libmars_core.la
 libmars_core_la_SOURCES = \
 	$(srcdir)/../../../src/common/*.h \
 	alloc.c \
+	cond.c \
 	context.c \
 	context_internal_types.h \
 	ea.c \
--- /dev/null
+++ b/core/src/host/lib/cond.c
@@ -0,0 +1,55 @@
+/*
+ * Copyright 2008 Sony Corporation of America
+ *
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this Library and associated documentation files (the
+ * "Library"), to deal in the Library without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Library, and to
+ * permit persons to whom the Library is furnished to do so, subject to
+ * the following conditions:
+ *
+ *  The above copyright notice and this permission notice shall be
+ *  included in all copies or substantial portions of the Library.
+ *
+ *  If you modify the Library, you may copy and distribute your modified
+ *  version of the Library in object code or as an executable provided
+ *  that you also do one of the following:
+ *
+ *   Accompany the modified version of the Library with the complete
+ *   corresponding machine-readable source code for the modified version
+ *   of the Library; or,
+ *
+ *   Accompany the modified version of the Library with a written offer
+ *   for a complete machine-readable copy of the corresponding source
+ *   code of the modified version of the Library.
+ *
+ *
+ * THE LIBRARY IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * LIBRARY OR THE USE OR OTHER DEALINGS IN THE LIBRARY.
+ */
+
+#include <sched.h>
+
+#include "config.h"
+
+#include "mars/core.h"
+
+int mars_ea_cond_wait(uint64_t watch_point_ea,
+		      int (*test_cond)(uint32_t , void *),
+		      void *test_cond_param)
+{
+	int ret;
+
+	while ((ret = (*test_cond)(mars_ea_get_uint32(watch_point_ea),
+				   test_cond_param)) < 0)
+		sched_yield();
+
+	return ret;
+}
--- a/core/src/host/lib/workload_queue.c
+++ b/core/src/host/lib/workload_queue.c
@@ -35,9 +35,7 @@
  * LIBRARY OR THE USE OR OTHER DEALINGS IN THE LIBRARY.
  */
 
-#include <sched.h>
 #include <string.h>
-#include <unistd.h>
 
 #include "config.h"
 
@@ -676,10 +674,28 @@ int mars_workload_queue_schedule_cancel(
 	return MARS_SUCCESS;
 }
 
+static int test_workload_state_finished(uint32_t upper, void *param)
+{
+	/* this function assumes 'STATE' is stored in upper 32bits */
+	uint64_t bits = (uint64_t)upper << 32;
+
+	(void)param;
+
+	switch (MARS_BITS_GET(&bits, STATE)) {
+	case MARS_WORKLOAD_STATE_FINISHED:
+		return MARS_SUCCESS;
+	case MARS_WORKLOAD_STATE_NONE:
+		return MARS_ERROR_STATE;
+	default:
+		return -1;
+	}
+}
+
 int mars_workload_queue_wait(struct mars_context *mars,
 				uint16_t id,
 				uint64_t *workload_ea)
 {
+	int ret;
 	uint64_t queue_ea;
 	int block;
 	int index;
@@ -707,12 +723,9 @@ int mars_workload_queue_wait(struct mars
 	bits_ea = workload_queue_block_bits_ea(block_ea, index);
 	bits = mars_ea_get_uint64(bits_ea);
 
-	while (MARS_BITS_GET(&bits, STATE) != MARS_WORKLOAD_STATE_FINISHED) {
-		if (MARS_BITS_GET(&bits, STATE) == MARS_WORKLOAD_STATE_NONE)
-			return MARS_ERROR_STATE;
-		sched_yield();
-		bits = mars_ea_get_uint64(bits_ea);
-	}
+	ret = mars_ea_cond_wait(bits_ea, test_workload_state_finished, NULL);
+	if (ret)
+		return ret;
 
 	/* if requested set workload context pointer to return */
 	if (workload_ea)






More information about the cbe-oss-dev mailing list