[Cbe-oss-dev] [PATCH 2/7]MARS/core: Task barrier remove duplicate code
Yuji Mano
yuji.mano at am.sony.com
Wed Oct 29 06:05:28 EST 2008
This reuses the duplicate code used in both the wait and try wait routines of
task barrier implementation by moving out into a separate static function.
This also fixes a bug in the notify routine where it would return error if the
task does not have a context save area even for the non task switching try wait
call.
This also fixes a bug of not checking to see if the notify limit has been
reached.
Signed-off-by: Yuji Mano <yuji.mano at am.sony.com>
---
core/src/mpu/lib/task_barrier.c | 120 ++++++++++++++--------------------------
1 file changed, 44 insertions(+), 76 deletions(-)
--- a/core/src/mpu/lib/task_barrier.c
+++ b/core/src/mpu/lib/task_barrier.c
@@ -44,7 +44,7 @@
static struct mars_task_barrier barrier;
-int mars_task_barrier_notify(uint64_t barrier_ea)
+static int notify(uint64_t barrier_ea, int try)
{
int i;
struct mars_task_context *task;
@@ -59,13 +59,27 @@ int mars_task_barrier_notify(uint64_t ba
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(barrier_ea, (struct mars_mutex *)&barrier);
/* previous barrier wait not complete so wait */
if (barrier.notified_count == barrier.total) {
+ /* only try so return busy */
+ if (try) {
+ mars_mutex_unlock_put(barrier_ea,
+ (struct mars_mutex *)&barrier);
+ return MARS_ERROR_BUSY;
+ }
+
+ /* check if barrier notify wait limit reached */
+ if (barrier.notify_wait_count == barrier.total) {
+ mars_mutex_unlock_put(barrier_ea,
+ (struct mars_mutex *)&barrier);
+ return MARS_ERROR_LIMIT;
+ }
+
/* add id to wait list */
barrier.notify_wait_id[barrier.notify_wait_count] =
mars_get_workload_id();
@@ -96,42 +110,17 @@ int mars_task_barrier_notify(uint64_t ba
return MARS_SUCCESS;
}
-int mars_task_barrier_try_notify(uint64_t barrier_ea)
+int mars_task_barrier_notify(uint64_t barrier_ea)
{
- int i;
-
- /* check function params */
- if (!barrier_ea)
- return MARS_ERROR_NULL;
- if (barrier_ea & MARS_TASK_BARRIER_ALIGN_MASK)
- return MARS_ERROR_ALIGN;
-
- mars_mutex_lock_get(barrier_ea, (struct mars_mutex *)&barrier);
-
- /* previous barrier wait not complete so return busy */
- if (barrier.notified_count == barrier.total) {
- mars_mutex_unlock_put(barrier_ea,
- (struct mars_mutex *)&barrier);
- return MARS_ERROR_BUSY;
- }
-
- /* increment notified count */
- barrier.notified_count++;
-
- /* notified count reached total so release barrier */
- if (barrier.notified_count == barrier.total) {
- /* signal all task ids in wait list */
- for (i = 0; i < barrier.wait_count; i++)
- mars_signal_send(barrier.wait_id[i]);
- barrier.wait_count = 0;
- }
-
- mars_mutex_unlock_put(barrier_ea, (struct mars_mutex *)&barrier);
+ return notify(barrier_ea, 0);
+}
- return MARS_SUCCESS;
+int mars_task_barrier_try_notify(uint64_t barrier_ea)
+{
+ return notify(barrier_ea, 1);
}
-int mars_task_barrier_wait(uint64_t barrier_ea)
+static int wait(uint64_t barrier_ea, int try)
{
int i;
struct mars_task_context *task;
@@ -151,15 +140,22 @@ int mars_task_barrier_wait(uint64_t barr
mars_mutex_lock_get(barrier_ea, (struct mars_mutex *)&barrier);
- /* check if barrier wait limit reached */
- if (barrier.wait_count == barrier.total) {
- mars_mutex_unlock_put(barrier_ea,
- (struct mars_mutex *)&barrier);
- return MARS_ERROR_LIMIT;
- }
-
/* not all tasks notified barrier so need to wait */
if (barrier.notified_count != barrier.total) {
+ /* only try so return busy */
+ if (try) {
+ mars_mutex_unlock_put(barrier_ea,
+ (struct mars_mutex *)&barrier);
+ return MARS_ERROR_BUSY;
+ }
+
+ /* check if barrier wait limit reached */
+ if (barrier.wait_count == barrier.total) {
+ mars_mutex_unlock_put(barrier_ea,
+ (struct mars_mutex *)&barrier);
+ return MARS_ERROR_LIMIT;
+ }
+
/* add id to wait list */
barrier.wait_id[barrier.wait_count] = mars_get_workload_id();
barrier.wait_count++;
@@ -192,40 +188,12 @@ int mars_task_barrier_wait(uint64_t barr
return MARS_SUCCESS;
}
-int mars_task_barrier_try_wait(uint64_t barrier_ea)
+int mars_task_barrier_wait(uint64_t barrier_ea)
{
- int i;
-
- /* check function params */
- if (!barrier_ea)
- return MARS_ERROR_NULL;
- if (barrier_ea & MARS_TASK_BARRIER_ALIGN_MASK)
- return MARS_ERROR_ALIGN;
-
- mars_mutex_lock_get(barrier_ea, (struct mars_mutex *)&barrier);
-
- /* not all tasks notified barrier so return busy */
- if (barrier.notified_count != barrier.total) {
- mars_mutex_unlock_put(barrier_ea,
- (struct mars_mutex *)&barrier);
- return MARS_ERROR_BUSY;
- }
-
- /* increment waited count */
- barrier.waited_count++;
-
- /* all tasks have called wait so reset barrier */
- if (barrier.waited_count == barrier.total) {
- barrier.notified_count = 0;
- barrier.waited_count = 0;
-
- /* signal all task ids in notify wait list */
- for (i = 0; i < barrier.notify_wait_count; i++)
- mars_signal_send(barrier.notify_wait_id[i]);
- barrier.notify_wait_count = 0;
- }
-
- mars_mutex_unlock_put(barrier_ea, (struct mars_mutex *)&barrier);
+ return wait(barrier_ea, 0);
+}
- return MARS_SUCCESS;
+int mars_task_barrier_try_wait(uint64_t barrier_ea)
+{
+ return wait(barrier_ea, 1);
}
More information about the cbe-oss-dev
mailing list