Commit 189a0272 by Marcus Winter Committed by unknown

mtapi_c: action plugin api implementation

parent 5ecbe951
/*
* Copyright (c) 2014, Siemens AG. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <embb/mtapi/c/mtapi_ext.h>
#include <embb_mtapi_action_t.h>
#include <embb_mtapi_node_t.h>
#include <embb_mtapi_job_t.h>
#include <embb_mtapi_log.h>
#include <mtapi_status_t.h>
mtapi_action_hndl_t mtapi_ext_plugin_action_create(
MTAPI_IN mtapi_job_id_t job_id,
MTAPI_IN mtapi_ext_plugin_action_start_function_t start_function,
MTAPI_IN mtapi_ext_plugin_action_cancel_function_t cancel_function,
MTAPI_IN void* node_local_data,
MTAPI_IN mtapi_size_t node_local_data_size,
MTAPI_IN mtapi_action_attributes_t* attributes,
MTAPI_OUT mtapi_status_t* status) {
mtapi_status_t local_status = MTAPI_ERR_UNKNOWN;
mtapi_action_hndl_t action_handle = { 0, EMBB_MTAPI_IDPOOL_INVALID_ID };
embb_mtapi_log_trace("mtapi_ext_plugin_action_create() called\n");
if (embb_mtapi_node_is_initialized()) {
embb_mtapi_node_t* node = embb_mtapi_node_get_instance();
/* check if job is valid */
if (embb_mtapi_job_is_id_valid(node, job_id)) {
embb_mtapi_job_t* job = embb_mtapi_job_get_storage_for_id(node, job_id);
embb_mtapi_action_t* new_action =
embb_mtapi_action_pool_allocate(node->action_pool);
if (MTAPI_NULL != new_action) {
new_action->domain_id = node->domain_id;
new_action->node_id = node->node_id;
new_action->job_id = job_id;
new_action->node_local_data = node_local_data;
new_action->node_local_data_size = node_local_data_size;
new_action->enabled = MTAPI_TRUE;
new_action->is_plugin_action = MTAPI_TRUE;
embb_atomic_store_int(&new_action->num_tasks, 0);
new_action->plugin_start_function = start_function;
new_action->plugin_cancel_function = cancel_function;
/* set defaults if no attributes were given */
if (MTAPI_NULL != attributes) {
new_action->attributes = *attributes;
local_status = MTAPI_SUCCESS;
}
else {
/* use the default */
mtapi_actionattr_init(&new_action->attributes, &local_status);
}
/* check if affinity is sane */
if (0 == new_action->attributes.affinity) {
local_status = MTAPI_ERR_PARAMETER;
}
if (MTAPI_SUCCESS == local_status) {
action_handle = new_action->handle;
embb_mtapi_job_add_action(job, new_action);
}
else {
embb_mtapi_action_pool_deallocate(node->action_pool, new_action);
}
}
else {
/* no more space left in action pool */
local_status = MTAPI_ERR_ACTION_LIMIT;
}
}
else {
local_status = MTAPI_ERR_JOB_INVALID;
}
}
else {
embb_mtapi_log_error("mtapi not initialized\n");
local_status = MTAPI_ERR_NODE_NOTINIT;
}
mtapi_status_set(status, local_status);
return action_handle;
}
......@@ -131,12 +131,14 @@ mtapi_action_hndl_t mtapi_action_create(
new_action->domain_id = node->domain_id;
new_action->node_id = node->node_id;
new_action->job_id = job_id;
new_action->action_function = action_function;
new_action->node_local_data = node_local_data;
new_action->node_local_data_size = node_local_data_size;
new_action->enabled = MTAPI_TRUE;
new_action->is_plugin_action = MTAPI_FALSE;
embb_atomic_store_int(&new_action->num_tasks, 0);
new_action->action_function = action_function;
/* set defaults if no attributes were given */
if (MTAPI_NULL != attributes) {
new_action->attributes = *attributes;
......
......@@ -27,7 +27,7 @@
#ifndef MTAPI_C_SRC_EMBB_MTAPI_ACTION_T_H_
#define MTAPI_C_SRC_EMBB_MTAPI_ACTION_T_H_
#include <embb/mtapi/c/mtapi.h>
#include <embb/mtapi/c/mtapi_ext.h>
#include <embb/base/c/atomic.h>
#include <embb_mtapi_pool_template.h>
......@@ -57,6 +57,10 @@ struct embb_mtapi_action_struct {
mtapi_action_attributes_t attributes;
mtapi_boolean_t enabled;
mtapi_boolean_t is_plugin_action;
mtapi_ext_plugin_action_start_function_t plugin_start_function;
mtapi_ext_plugin_action_cancel_function_t plugin_cancel_function;
embb_atomic_int num_tasks;
};
......
......@@ -230,11 +230,22 @@ static mtapi_task_hndl_t embb_mtapi_task_start(
if (MTAPI_SUCCESS == local_status) {
embb_mtapi_scheduler_t * scheduler = node->scheduler;
mtapi_boolean_t was_scheduled;
embb_mtapi_action_t * local_action =
embb_mtapi_action_pool_get_storage_for_handle(
node->action_pool, task->action);
embb_mtapi_task_set_state(task, MTAPI_TASK_SCHEDULED);
if (local_action->is_plugin_action) {
/* schedule plugin task */
mtapi_status_t plugin_status = MTAPI_ERR_UNKNOWN;
local_action->plugin_start_function(task_hndl, &plugin_status);
was_scheduled = (MTAPI_SUCCESS == plugin_status) ? MTAPI_TRUE : MTAPI_FALSE;
} else {
/* schedule local task */
was_scheduled =
embb_mtapi_scheduler_schedule_task(scheduler, task);
}
if (was_scheduled) {
/* if task is detached, do not return a handle, it will be deleted
......@@ -452,7 +463,17 @@ void mtapi_task_cancel(
embb_mtapi_task_t* local_task =
embb_mtapi_task_pool_get_storage_for_handle(node->task_pool, task);
embb_mtapi_task_set_state(local_task, MTAPI_TASK_CANCELLED);
/* call plugin action cancel function */
if (embb_mtapi_action_pool_is_handle_valid(node->action_pool, local_task->action)) {
embb_mtapi_action_t* local_action =
embb_mtapi_action_pool_get_storage_for_handle(node->action_pool, local_task->action);
if (local_action->is_plugin_action) {
local_action->plugin_cancel_function(task, &local_status);
}
} else {
local_status = MTAPI_SUCCESS;
}
} else {
local_status = MTAPI_ERR_TASK_INVALID;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment