[Cbe-oss-dev] [PATCH 2/3]MARS/modules/task: Wrap wait condition loops

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


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

Wrap loops to wait for shared memory condition (task module)

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>
---
 modules/task/src/host/lib/task_event_flag.c |   29 +++++++++++++++++++++-------
 modules/task/src/host/lib/task_queue.c      |   29 ++++++++++++++++++++++------
 2 files changed, 45 insertions(+), 13 deletions(-)

--- a/modules/task/src/host/lib/task_event_flag.c
+++ b/modules/task/src/host/lib/task_event_flag.c
@@ -35,7 +35,6 @@
  * LIBRARY OR THE USE OR OTHER DEALINGS IN THE LIBRARY.
  */
 
-#include <sched.h>
 #include <string.h>
 
 #include "config.h"
@@ -217,6 +216,20 @@ end:
 	return ret;
 }
 
+static int test_any_bits(uint32_t bits, void *param)
+{
+	const uint32_t *mask = (const uint32_t *)param;
+
+	return (bits & *mask) ? MARS_SUCCESS : -1;
+}
+
+static int test_all_bits(uint32_t bits, void *param)
+{
+	const uint32_t *mask = (const uint32_t *)param;
+
+	return ((bits & *mask) == *mask) ? MARS_SUCCESS : -1;
+}
+
 static int wait(uint64_t event_flag_ea,
 		uint32_t mask, uint8_t mask_mode, uint32_t *bits, int try)
 {
@@ -256,9 +269,10 @@ static int wait(uint64_t event_flag_ea,
 			if (try)
 				return MARS_ERROR_BUSY;
 
-			/* yield until condition is met */
-			while ((mars_ea_get_uint32(bits_ea) & mask) == 0)
-				sched_yield();
+			/* wait until condition is met */
+			ret = mars_ea_cond_wait(bits_ea, test_any_bits, &mask);
+			if (ret)
+				return ret;
 
 			mars_mutex_lock_get(event_flag_ea,
 					    (struct mars_mutex *)event_flag);
@@ -273,9 +287,10 @@ static int wait(uint64_t event_flag_ea,
 			if (try)
 				return MARS_ERROR_BUSY;
 
-			/* yield until condition is met */
-			while ((mars_ea_get_uint32(bits_ea) & mask) != mask)
-				sched_yield();
+			/* wait until condition is met */
+			ret = mars_ea_cond_wait(bits_ea, test_all_bits, &mask);
+			if (ret)
+				return ret;
 
 			mars_mutex_lock_get(event_flag_ea,
 					    (struct mars_mutex *)event_flag);
--- a/modules/task/src/host/lib/task_queue.c
+++ b/modules/task/src/host/lib/task_queue.c
@@ -35,7 +35,6 @@
  * LIBRARY OR THE USE OR OTHER DEALINGS IN THE LIBRARY.
  */
 
-#include <sched.h>
 #include <string.h>
 
 #include "config.h"
@@ -226,6 +225,14 @@ static void push_update(struct mars_task
 		queue->push_ea = queue->buffer_ea;
 }
 
+static int test_not_full(uint32_t count, void *param)
+{
+	const struct mars_task_queue *queue =
+		(const struct mars_task_queue *)param;
+
+	return (count != queue->depth) ? MARS_SUCCESS : -1;
+}
+
 static int push(uint64_t queue_ea, const void *data, int try)
 {
 	int ret;
@@ -256,9 +263,10 @@ static int push(uint64_t queue_ea, const
 		if (try)
 			return MARS_ERROR_BUSY;
 
-		while (mars_ea_get_uint32(queue_count_ea(queue_ea)) ==
-		       queue->depth)
-			sched_yield();
+		ret = mars_ea_cond_wait(queue_count_ea(queue_ea),
+					test_not_full, queue);
+		if (ret)
+			return ret;
 
 		mars_mutex_lock_get(queue_ea, (struct mars_mutex *)queue);
 	}
@@ -312,6 +320,13 @@ static void pop_update(struct mars_task_
 		queue->pop_ea = queue->buffer_ea;
 }
 
+static int test_not_empty(uint32_t count, void *param)
+{
+	(void)param;
+
+	return count ? MARS_SUCCESS : -1;
+}
+
 static int pop(uint64_t queue_ea, void *data, int peek, int try)
 {
 	int ret;
@@ -341,8 +356,10 @@ static int pop(uint64_t queue_ea, void *
 		if (try)
 			return MARS_ERROR_BUSY;
 
-		while (!mars_ea_get_uint32(queue_count_ea(queue_ea)))
-			sched_yield();
+		ret = mars_ea_cond_wait(queue_count_ea(queue_ea),
+					test_not_empty, queue);
+		if (ret)
+			return ret;
 
 		mars_mutex_lock_get(queue_ea, (struct mars_mutex *)queue);
 	}






More information about the cbe-oss-dev mailing list