embb_mtapi_job_t.c 6.18 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
/*
 * 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 <assert.h>

#include <embb/base/c/internal/unused.h>

#include <embb_mtapi_job_t.h>
#include <embb_mtapi_log.h>
#include <embb_mtapi_alloc.h>
#include <mtapi_status_t.h>
#include <embb_mtapi_action_t.h>
#include <embb_mtapi_node_t.h>


/* ---- POOL STORAGE FUNCTIONS --------------------------------------------- */

mtapi_boolean_t embb_mtapi_job_initialize_list(embb_mtapi_node_t * node) {
  node->job_list = (embb_mtapi_job_t*)embb_mtapi_alloc_allocate(
    sizeof(embb_mtapi_job_t)*node->attributes.max_jobs);
  mtapi_uint_t ii;
  for (ii = 0; ii < node->attributes.max_jobs; ii++) {
    embb_mtapi_job_initialize(
      &node->job_list[ii], node->attributes.max_actions_per_job);
    node->job_list[ii].handle.id = ii;
    node->job_list[ii].handle.tag = 0;
  }
  return MTAPI_TRUE;
}

void embb_mtapi_job_finalize_list(embb_mtapi_node_t * node) {
  mtapi_uint_t ii;
  for (ii = 0; ii < node->attributes.max_jobs; ii++) {
    embb_mtapi_job_finalize(&node->job_list[ii]);
    node->job_list[ii].handle.id = 0;
  }
  embb_mtapi_alloc_deallocate(node->job_list);
  node->job_list = MTAPI_NULL;
}


/* ---- CLASS MEMBERS ------------------------------------------------------ */

mtapi_boolean_t embb_mtapi_job_is_handle_valid(
  embb_mtapi_node_t * node,
  mtapi_job_hndl_t handle) {
  assert(MTAPI_NULL != node);
  return ((0 < handle.id) &&
    (handle.id < node->attributes.max_jobs) &&
    (node->job_list[handle.id].handle.tag == handle.tag)) ?
      MTAPI_TRUE : MTAPI_FALSE;
}

embb_mtapi_job_t * embb_mtapi_job_get_storage_for_handle(
  embb_mtapi_node_t * node,
  mtapi_job_hndl_t handle) {
  assert(MTAPI_NULL != node);
  assert(MTAPI_NULL != node->job_list);
  assert(embb_mtapi_job_is_handle_valid(node, handle));
  return &node->job_list[handle.id];
}

mtapi_boolean_t embb_mtapi_job_is_id_valid(
  embb_mtapi_node_t * node,
  mtapi_job_id_t id) {
  assert(MTAPI_NULL != node);
  return ((0 < id) && (id < node->attributes.max_jobs)) ?
    MTAPI_TRUE : MTAPI_FALSE;
}

embb_mtapi_job_t * embb_mtapi_job_get_storage_for_id(
  embb_mtapi_node_t * node,
  mtapi_job_id_t id) {
  assert(MTAPI_NULL != node);
  assert(MTAPI_NULL != node->job_list);
  assert(embb_mtapi_job_is_id_valid(node, id));
  return &node->job_list[id];
}

void embb_mtapi_job_initialize(
  embb_mtapi_job_t * that,
  mtapi_uint_t max_actions) {
  mtapi_uint_t ii;

  assert(MTAPI_NULL != that);

  that->handle.tag = 0;

  that->domain_id = 0;
  that->node_id = 0;
  that->num_actions = 0;
  that->max_actions = max_actions;
  that->actions = (mtapi_action_hndl_t*)
    embb_mtapi_alloc_allocate(sizeof(mtapi_action_hndl_t)*max_actions);
  for (ii = 0; ii < max_actions; ii++) {
    that->actions[ii].id = EMBB_MTAPI_IDPOOL_INVALID_ID;
  }
}

void embb_mtapi_job_finalize(embb_mtapi_job_t * that) {
  assert(MTAPI_NULL != that);

  that->handle.tag++;

  that->domain_id = 0;
  that->node_id = 0;
  that->num_actions = 0;
  that->max_actions = 0;
  embb_mtapi_alloc_deallocate(that->actions);
  that->actions = NULL;
}

mtapi_boolean_t embb_mtapi_job_add_action(
  embb_mtapi_job_t * that,
  embb_mtapi_action_t * action) {
  mtapi_boolean_t result = MTAPI_TRUE;

  assert(MTAPI_NULL != that);
  assert(MTAPI_NULL != action);

  /* check if job has enough room for another action */
  if (that->max_actions > that->num_actions) {
    that->domain_id = action->domain_id;
    that->node_id = action->node_id;
    that->actions[that->num_actions] = action->handle;
    that->num_actions++;
  } else {
    result = MTAPI_FALSE;
  }

  return result;
}

void embb_mtapi_job_remove_action(
  embb_mtapi_job_t * that,
  embb_mtapi_action_t * action) {
  assert(MTAPI_NULL != that);
  assert(MTAPI_NULL != action);
  mtapi_uint_t ii = 0;

  for (ii = 0; ii + 1 < that->num_actions; ii++) {
    if (that->actions[ii].id == action->handle.id &&
      that->actions[ii].tag == action->handle.tag) {
      that->actions[ii] = that->actions[that->num_actions - 1];
    }
  }

  that->num_actions--;
}


/* ---- INTERFACE FUNCTIONS ------------------------------------------------ */

mtapi_job_hndl_t mtapi_job_get(
  MTAPI_IN mtapi_job_id_t job_id,
  MTAPI_IN mtapi_domain_t domain_id,
  MTAPI_OUT mtapi_status_t* status) {
  mtapi_status_t local_status = MTAPI_ERR_UNKNOWN;
  embb_mtapi_node_t * node = embb_mtapi_node_get_instance();
  embb_mtapi_job_t* job = MTAPI_NULL;
  mtapi_job_hndl_t job_hndl = { 0, EMBB_MTAPI_IDPOOL_INVALID_ID };

  EMBB_UNUSED(domain_id);

  embb_mtapi_log_trace("mtapi_job_get() called\n");

  if (embb_mtapi_node_is_initialized()) {
    if (embb_mtapi_job_is_id_valid(node, job_id)) {
      job = embb_mtapi_job_get_storage_for_id(node, job_id);
      job_hndl = job->handle;
      local_status = MTAPI_SUCCESS;
    } 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 job_hndl;
}