[Cbe-oss-dev] [PATCH 18/22]MARS/task: remove memmove usage

Yuji Mano yuji.mano at am.sony.com
Fri Mar 20 07:54:46 EST 2009


This patch removes the use of memmove from task semaphore and queue
implementations and uses a circular buffer for storing the wait id's to improve
performance and reduce code size.

Signed-off-by: Yuji Mano <yuji.mano at am.sony.com>
---
 task/include/common/mars/task_semaphore_types.h |    2 -
 task/src/common/task_queue_internal_types.h     |    4 ++-
 task/src/common/task_semaphore_internal_types.h |    2 +
 task/src/host/lib/task_queue.c                  |   20 ++++++++--------
 task/src/host/lib/task_semaphore.c              |    3 --
 task/src/mpu/lib/task_queue.c                   |   30 ++++++++++++++----------
 task/src/mpu/lib/task_semaphore.c               |   16 ++++++------
 7 files changed, 43 insertions(+), 34 deletions(-)

--- a/task/include/common/mars/task_semaphore_types.h
+++ b/task/include/common/mars/task_semaphore_types.h
@@ -48,7 +48,7 @@
  * \ingroup group_mars_task_semaphore
  * \brief Maximum task accesses allowed for single semaphore
  */
-#define MARS_TASK_SEMAPHORE_WAIT_MAX		55
+#define MARS_TASK_SEMAPHORE_WAIT_MAX		54
 
 /**
  * \ingroup group_mars_task_semaphore
--- a/task/src/common/task_queue_internal_types.h
+++ b/task/src/common/task_queue_internal_types.h
@@ -57,8 +57,10 @@ struct mars_task_queue {
 	uint64_t buffer_ea;
 	uint64_t push_ea;
 	uint64_t pop_ea;
+	uint8_t pad;
 	uint8_t direction;
-	uint8_t pad[3];
+	uint8_t push_wait_head;
+	uint8_t pop_wait_head;
 	uint16_t push_wait_count;
 	uint16_t pop_wait_count;
 	uint16_t push_wait_id[MARS_TASK_QUEUE_WAIT_MAX];
--- a/task/src/common/task_semaphore_internal_types.h
+++ b/task/src/common/task_semaphore_internal_types.h
@@ -49,6 +49,8 @@ struct mars_task_semaphore {
 	int32_t count;
 	uint16_t wait_count;
 	uint16_t wait_id[MARS_TASK_SEMAPHORE_WAIT_MAX];
+	uint8_t wait_head;
+	uint8_t pad;
 	uint64_t mars_context_ea;
 } __attribute__((aligned(MARS_TASK_SEMAPHORE_ALIGN)));
 
--- a/task/src/host/lib/task_queue.c
+++ b/task/src/host/lib/task_queue.c
@@ -35,8 +35,6 @@
  * LIBRARY OR THE USE OR OTHER DEALINGS IN THE LIBRARY.
  */
 
-#include <string.h>
-
 #include "config.h"
 
 #include <mars/base.h>
@@ -108,6 +106,8 @@ int mars_task_queue_create(struct mars_c
 	queue->count = 0;
 	queue->push_wait_count = 0;
 	queue->pop_wait_count = 0;
+	queue->push_wait_head = 0;
+	queue->pop_wait_head = 0;
 
 	/* update queue on EA */
 	mars_ea_put(queue_ea, queue, MARS_TASK_QUEUE_SIZE);
@@ -210,13 +210,13 @@ static void push_update(struct mars_task
 	/* signal waiting task that queue is ready for pop */
 	if (queue->pop_wait_count) {
 		/* signal waiting task */
-		mars_workload_queue_signal_send(mars, queue->pop_wait_id[0]);
+		mars_workload_queue_signal_send(mars,
+			queue->pop_wait_id[queue->pop_wait_head]);
 
 		/* flush id from pop wait list */
 		queue->pop_wait_count--;
-		memmove(&queue->pop_wait_id[0],
-			&queue->pop_wait_id[1],
-			sizeof(uint16_t) * queue->pop_wait_count);
+		queue->pop_wait_head++;
+		queue->pop_wait_head %= MARS_TASK_QUEUE_WAIT_MAX;
 	}
 
 	/* increment queue count */
@@ -307,13 +307,13 @@ static void pop_update(struct mars_task_
 	/* signal waiting task that queue is ready for push */
 	if (queue->push_wait_count) {
 		/* signal waiting task */
-		mars_workload_queue_signal_send(mars, queue->push_wait_id[0]);
+		mars_workload_queue_signal_send(mars,
+			queue->push_wait_id[queue->push_wait_head]);
 
 		/* flush id from push wait list */
 		queue->push_wait_count--;
-		memmove(&queue->push_wait_id[0],
-			&queue->push_wait_id[1],
-			sizeof(uint16_t) * queue->push_wait_count);
+		queue->push_wait_head++;
+		queue->push_wait_head %= MARS_TASK_QUEUE_WAIT_MAX;
 	}
 
 	/* decrement queue count */
--- a/task/src/host/lib/task_semaphore.c
+++ b/task/src/host/lib/task_semaphore.c
@@ -35,8 +35,6 @@
  * LIBRARY OR THE USE OR OTHER DEALINGS IN THE LIBRARY.
  */
 
-#include <stdlib.h>
-
 #include "config.h"
 
 #include <mars/base.h>
@@ -75,6 +73,7 @@ int mars_task_semaphore_create(struct ma
 	semaphore->mars_context_ea = mars_ptr_to_ea(mars);
 	semaphore->count = count;
 	semaphore->wait_count = 0;
+	semaphore->wait_head = 0;
 
 	/* update semaphore on EA */
 	mars_ea_put(semaphore_ea, semaphore, MARS_TASK_SEMAPHORE_SIZE);
--- a/task/src/mpu/lib/task_queue.c
+++ b/task/src/mpu/lib/task_queue.c
@@ -36,7 +36,6 @@
  */
 
 #include <stddef.h>
-#include <string.h>
 
 #include "config.h"
 
@@ -125,13 +124,13 @@ static int push_update(void)
 	/* signal waiting task that queue is ready for pop */
 	if (queue.pop_wait_count) {
 		/* signal waiting task */
-		mars_task_module_signal_send(queue.pop_wait_id[0]);
+		mars_task_module_signal_send(
+			queue.pop_wait_id[queue.pop_wait_head]);
 
 		/* flush id from pop wait list */
 		queue.pop_wait_count--;
-		memmove(&queue.pop_wait_id[0],
-			&queue.pop_wait_id[1],
-			sizeof(uint16_t) * queue.pop_wait_count);
+		queue.pop_wait_head++;
+		queue.pop_wait_head %= MARS_TASK_QUEUE_WAIT_MAX;
 	}
 
 	/* increment queue count */
@@ -181,6 +180,8 @@ static int push(uint64_t queue_ea, const
 
 	/* queue is full so wait */
 	while (queue.count == queue.depth) {
+		uint8_t push_wait_tail;
+
 		/* only try so return busy */
 		if (try) {
 			mars_mutex_unlock_put(queue_ea,
@@ -196,8 +197,9 @@ static int push(uint64_t queue_ea, const
 		}
 
 		/* add id to push wait list */
-		queue.push_wait_id[queue.push_wait_count] =
-			task->id.workload_id;
+		push_wait_tail = (queue.push_wait_head + queue.push_wait_count)
+				 % MARS_TASK_QUEUE_WAIT_MAX;
+		queue.push_wait_id[push_wait_tail] = task->id.workload_id;
 		queue.push_wait_count++;
 
 		mars_mutex_unlock_put(queue_ea, (struct mars_mutex *)&queue);
@@ -294,13 +296,13 @@ static int pop_update(void)
 	/* signal waiting task that queue is ready for push */
 	if (queue.push_wait_count) {
 		/* signal waiting task */
-		mars_task_module_signal_send(queue.push_wait_id[0]);
+		mars_task_module_signal_send(
+			queue.push_wait_id[queue.push_wait_head]);
 
 		/* flush id from push wait list */
 		queue.push_wait_count--;
-		memmove(&queue.push_wait_id[0],
-			&queue.push_wait_id[1],
-			sizeof(uint16_t) * queue.push_wait_count);
+		queue.push_wait_head++;
+		queue.push_wait_head %= MARS_TASK_QUEUE_WAIT_MAX;
 	}
 
 	/* decrement queue count */
@@ -350,6 +352,8 @@ static int pop(uint64_t queue_ea, void *
 
 	/* queue is empty so wait */
 	while (!queue.count) {
+		uint8_t pop_wait_tail;
+
 		/* only try so return busy */
 		if (try) {
 			mars_mutex_unlock_put(queue_ea,
@@ -365,7 +369,9 @@ static int pop(uint64_t queue_ea, void *
 		}
 
 		/* add id to pop wait list */
-		queue.pop_wait_id[queue.pop_wait_count] = task->id.workload_id;
+		pop_wait_tail = (queue.pop_wait_head + queue.pop_wait_count)
+				% MARS_TASK_QUEUE_WAIT_MAX;
+		queue.pop_wait_id[pop_wait_tail] = task->id.workload_id;
 		queue.pop_wait_count++;
 
 		mars_mutex_unlock_put(queue_ea, (struct mars_mutex *)&queue);
--- a/task/src/mpu/lib/task_semaphore.c
+++ b/task/src/mpu/lib/task_semaphore.c
@@ -35,9 +35,6 @@
  * LIBRARY OR THE USE OR OTHER DEALINGS IN THE LIBRARY.
  */
 
-#include <stdlib.h>
-#include <string.h>
-
 #include "config.h"
 
 #include <mars/error.h>
@@ -78,8 +75,11 @@ int mars_task_semaphore_acquire(uint64_t
 	}
 
 	if (semaphore.count <= 0) {
+		uint8_t wait_tail = (semaphore.wait_head + semaphore.wait_count)
+				    % MARS_TASK_SEMAPHORE_WAIT_MAX;
+
 		/* add id to wait list */
-		semaphore.wait_id[semaphore.wait_count] = task->id.workload_id;
+		semaphore.wait_id[wait_tail] = task->id.workload_id;
 		semaphore.wait_count++;
 
 		mars_mutex_unlock_put(semaphore_ea,
@@ -118,13 +118,13 @@ int mars_task_semaphore_release(uint64_t
 		semaphore.count--;
 
 		/* signal waiting task */
-		mars_task_module_signal_send(semaphore.wait_id[0]);
+		mars_task_module_signal_send(
+			semaphore.wait_id[semaphore.wait_head]);
 
 		/* flush id from wait list */
 		semaphore.wait_count--;
-		memmove(&semaphore.wait_id[0],
-			&semaphore.wait_id[1],
-			sizeof(uint16_t) * semaphore.wait_count);
+		semaphore.wait_head++;
+		semaphore.wait_head %= MARS_TASK_SEMAPHORE_WAIT_MAX;
 	}
 
 	mars_mutex_unlock_put(semaphore_ea, (struct mars_mutex *)&semaphore);






More information about the cbe-oss-dev mailing list