[Cbe-oss-dev] [PATCH 3/7]MARS/core: Task event flag remove duplicate code

Yuji Mano yuji.mano at am.sony.com
Wed Oct 29 06:05:32 EST 2008


This reuses the duplicate code used in both the wait and try wait routines of
task event flag implementation by moving out into a separate static function.

Signed-off-by: Yuji Mano <yuji.mano at am.sony.com>

---
 core/src/host/lib/task_event_flag.c |   65 ++++++++++-----------------
 core/src/mpu/lib/task_event_flag.c  |   84 ++++++++++--------------------------
 2 files changed, 49 insertions(+), 100 deletions(-)

--- a/core/src/host/lib/task_event_flag.c
+++ b/core/src/host/lib/task_event_flag.c
@@ -180,9 +180,8 @@ int mars_task_event_flag_set(struct mars
 	return MARS_SUCCESS;
 }
 
-int mars_task_event_flag_wait(struct mars_task_event_flag *event_flag,
-				uint32_t mask, uint8_t mask_mode,
-				uint32_t *bits)
+static int wait(struct mars_task_event_flag *event_flag,
+		uint32_t mask, uint8_t mask_mode, uint32_t *bits, int try)
 {
 	/* check function params */
 	if (!event_flag)
@@ -200,16 +199,30 @@ int mars_task_event_flag_wait(struct mar
 	case MARS_TASK_EVENT_FLAG_MASK_OR:
 		while ((event_flag->bits & mask) == 0) {
 			mars_mutex_unlock((struct mars_mutex *)event_flag);
+
+			/* only try so return busy */
+			if (try)
+				return MARS_ERROR_BUSY;
+
+			/* yield until condition is met */
 			while ((event_flag->bits & mask) == 0)
 				sched_yield();
+
 			mars_mutex_lock((struct mars_mutex *)event_flag);
 		}
 		break;
 	case MARS_TASK_EVENT_FLAG_MASK_AND:
 		while ((event_flag->bits & mask) != mask) {
 			mars_mutex_unlock((struct mars_mutex *)event_flag);
+
+			/* only try so return busy */
+			if (try)
+				return MARS_ERROR_BUSY;
+
+			/* yield until condition is met */
 			while ((event_flag->bits & mask) != mask)
 				sched_yield();
+
 			mars_mutex_lock((struct mars_mutex *)event_flag);
 		}
 		break;
@@ -228,46 +241,16 @@ int mars_task_event_flag_wait(struct mar
 	return MARS_SUCCESS;
 }
 
-int mars_task_event_flag_try_wait(struct mars_task_event_flag *event_flag,
+int mars_task_event_flag_wait(struct mars_task_event_flag *event_flag,
 				uint32_t mask, uint8_t mask_mode,
 				uint32_t *bits)
 {
-	/* check function params */
-	if (!event_flag)
-		return MARS_ERROR_NULL;
-	if (event_flag->direction != MARS_TASK_EVENT_FLAG_MPU_TO_HOST)
-		return MARS_ERROR_STATE;
-	if (mask_mode != MARS_TASK_EVENT_FLAG_MASK_OR &&
-		mask_mode != MARS_TASK_EVENT_FLAG_MASK_AND)
-		return MARS_ERROR_PARAMS;
-
-	mars_mutex_lock((struct mars_mutex *)event_flag);
-
-	/* check condition based on wait mode */
-	switch (mask_mode) {
-	case MARS_TASK_EVENT_FLAG_MASK_OR:
-		if ((event_flag->bits & mask) == 0) {
-			mars_mutex_unlock((struct mars_mutex *)event_flag);
-			return MARS_ERROR_BUSY;
-		}
-		break;
-	case MARS_TASK_EVENT_FLAG_MASK_AND:
-		if ((event_flag->bits & mask) != mask) {
-			mars_mutex_unlock((struct mars_mutex *)event_flag);
-			return MARS_ERROR_BUSY;
-		}
-		break;
-	}
-
-	/* return bits if requested */
-	if (bits)
-		*bits = event_flag->bits;
-
-	/* clear event if clear mode is auto */
-	if (event_flag->clear_mode == MARS_TASK_EVENT_FLAG_CLEAR_AUTO)
-		event_flag->bits &= ~mask;
-
-	mars_mutex_unlock((struct mars_mutex *)event_flag);
+	return wait(event_flag, mask, mask_mode, bits, 0);
+}
 
-	return MARS_SUCCESS;
+int mars_task_event_flag_try_wait(struct mars_task_event_flag *event_flag,
+				uint32_t mask, uint8_t mask_mode,
+				uint32_t *bits)
+{
+	return wait(event_flag, mask, mask_mode, bits, 1);
 }
--- a/core/src/mpu/lib/task_event_flag.c
+++ b/core/src/mpu/lib/task_event_flag.c
@@ -127,9 +127,8 @@ int mars_task_event_flag_set(uint64_t ev
 	return MARS_SUCCESS;
 }
 
-int mars_task_event_flag_wait(uint64_t event_flag_ea,
-				uint32_t mask, uint8_t mask_mode,
-				uint32_t *bits)
+static int wait(uint64_t event_flag_ea,uint32_t mask, uint8_t mask_mode,
+		uint32_t *bits, int try)
 {
 	int wait = 0;
 	struct mars_task_context *task;
@@ -147,18 +146,11 @@ int mars_task_event_flag_wait(uint64_t e
 	task = (struct mars_task_context *)mars_get_workload();
 
 	/* make sure task context has a context save area */
-	if (!task->context_save_area_ea)
+	if (!task->context_save_area_ea && !try)
 		return MARS_ERROR_FORMAT;
 
 	mars_mutex_lock_get(event_flag_ea, (struct mars_mutex *)&event_flag);
 
-	/* check if event flag wait limit reached */
-	if (event_flag.wait_count == MARS_TASK_EVENT_FLAG_WAIT_MAX) {
-		mars_mutex_unlock_put(event_flag_ea,
-			(struct mars_mutex *)&event_flag);
-		return MARS_ERROR_LIMIT;
-	}
-
 	/* check for valid direction */
 	if (event_flag.direction != MARS_TASK_EVENT_FLAG_HOST_TO_MPU &&
 		event_flag.direction != MARS_TASK_EVENT_FLAG_MPU_TO_MPU) {
@@ -180,6 +172,20 @@ int mars_task_event_flag_wait(uint64_t e
 	}
 
 	if (wait) {
+		/* only try so return busy */
+		if (try) {
+			mars_mutex_unlock_put(event_flag_ea,
+				(struct mars_mutex *)&event_flag);
+			return MARS_ERROR_BUSY;
+		}
+
+		/* check if event flag wait limit reached */
+		if (event_flag.wait_count == MARS_TASK_EVENT_FLAG_WAIT_MAX) {
+			mars_mutex_unlock_put(event_flag_ea,
+				(struct mars_mutex *)&event_flag);
+			return MARS_ERROR_LIMIT;
+		}
+
 		/* add id to wait list */
 		event_flag.wait_id[event_flag.wait_count]
 				= mars_get_workload_id();
@@ -212,56 +218,16 @@ int mars_task_event_flag_wait(uint64_t e
 	return MARS_SUCCESS;
 }
 
-int mars_task_event_flag_try_wait(uint64_t event_flag_ea,
+int mars_task_event_flag_wait(uint64_t event_flag_ea,
 				uint32_t mask, uint8_t mask_mode,
 				uint32_t *bits)
 {
-	/* check function params */
-	if (!event_flag_ea)
-		return MARS_ERROR_NULL;
-	if (event_flag_ea & MARS_TASK_EVENT_FLAG_ALIGN_MASK)
-		return MARS_ERROR_ALIGN;
-	if (mask_mode != MARS_TASK_EVENT_FLAG_MASK_OR &&
-		mask_mode != MARS_TASK_EVENT_FLAG_MASK_AND)
-		return MARS_ERROR_PARAMS;
-
-	mars_mutex_lock_get(event_flag_ea, (struct mars_mutex *)&event_flag);
-
-	/* check for valid direction */
-	if (event_flag.direction != MARS_TASK_EVENT_FLAG_HOST_TO_MPU &&
-		event_flag.direction != MARS_TASK_EVENT_FLAG_MPU_TO_MPU) {
-		mars_mutex_unlock_put(event_flag_ea,
-			(struct mars_mutex *)&event_flag);
-		return MARS_ERROR_STATE;
-	}
-
-	/* check condition based on wait mode */
-	switch (mask_mode) {
-	case MARS_TASK_EVENT_FLAG_MASK_OR:
-		if ((event_flag.bits & mask) == 0) {
-			mars_mutex_unlock_put(event_flag_ea,
-				(struct mars_mutex *)&event_flag);
-			return MARS_ERROR_BUSY;
-		}
-		break;
-	case MARS_TASK_EVENT_FLAG_MASK_AND:
-		if ((event_flag.bits & mask) != mask) {
-			mars_mutex_unlock_put(event_flag_ea,
-				(struct mars_mutex *)&event_flag);
-			return MARS_ERROR_BUSY;
-		}
-		break;
-	}
-
-	/* return bits if requested */
-	if (bits)
-		*bits = event_flag.bits;
-
-	/* clear event if clear mode is auto */
-	if (event_flag.clear_mode == MARS_TASK_EVENT_FLAG_CLEAR_AUTO)
-		event_flag.bits &= ~mask;
-
-	mars_mutex_unlock_put(event_flag_ea, (struct mars_mutex *)&event_flag);
+	return wait(event_flag_ea, mask, mask_mode, bits, 0);
+}
 
-	return MARS_SUCCESS;
+int mars_task_event_flag_try_wait(uint64_t event_flag_ea,
+				uint32_t mask, uint8_t mask_mode,
+				uint32_t *bits)
+{
+	return wait(event_flag_ea, mask, mask_mode, bits, 1);
 }





More information about the cbe-oss-dev mailing list