embb_mtapi_scheduler_t.c 22.8 KB
Newer Older
1
/*
Marcus Winter committed
2
 * Copyright (c) 2014-2016, Siemens AG. All rights reserved.
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
 *
 * 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/base.h>

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

#include <embb_mtapi_scheduler_t.h>
#include <embb_mtapi_log.h>
#include <embb_mtapi_node_t.h>
#include <embb_mtapi_task_queue_t.h>
#include <embb_mtapi_thread_context_t.h>
#include <embb_mtapi_task_context_t.h>
#include <embb_mtapi_task_t.h>
Tobias Langer committed
40
#ifdef EMBB_HARD_REALTIME
41
#include <embb_mtapi_priority_queue_t.h>
Tobias Langer committed
42
#endif /*EMBB_HARD_REALTIME*/
43 44 45
#include <embb_mtapi_action_t.h>
#include <embb_mtapi_alloc.h>
#include <embb_mtapi_queue_t.h>
46
#include <embb_mtapi_group_t.h>
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


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

embb_mtapi_task_t * embb_mtapi_scheduler_get_private_task_from_context(
  embb_mtapi_scheduler_t * that,
  embb_mtapi_thread_context_t * thread_context,
  mtapi_uint_t priority) {
  EMBB_UNUSED(that);

  assert(MTAPI_NULL != that);
  assert(NULL != thread_context);

  embb_mtapi_task_t * task =
    embb_mtapi_task_queue_pop(thread_context->private_queue[priority]);
  return task;
}

embb_mtapi_task_t * embb_mtapi_scheduler_get_public_task_from_context(
  embb_mtapi_scheduler_t * that,
  embb_mtapi_thread_context_t * thread_context,
  mtapi_uint_t priority) {
  EMBB_UNUSED(that);

  embb_mtapi_task_t * task;

  assert(MTAPI_NULL != that);
  assert(NULL != thread_context);

  task = embb_mtapi_task_queue_pop(thread_context->queue[priority]);
  return task;
}

embb_mtapi_task_t * embb_mtapi_scheduler_get_next_task_vhpf(
  embb_mtapi_scheduler_t * that,
  embb_mtapi_node_t * node,
  embb_mtapi_thread_context_t * thread_context) {
  embb_mtapi_task_t * task = MTAPI_NULL;
85
  mtapi_uint_t ii;
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

  assert(MTAPI_NULL != that);
  assert(MTAPI_NULL != node);
  assert(NULL != thread_context);

  for (ii = 0;
    ii < node->attributes.max_priorities && MTAPI_NULL == task;
    ii++) {
    /* try local queues, first private. */
    task = embb_mtapi_scheduler_get_private_task_from_context(
      that, thread_context, ii);
    if (MTAPI_NULL == task) {
      /* found nothing, so local public next. */
      task = embb_mtapi_scheduler_get_public_task_from_context(
        that, thread_context, ii);
      if (MTAPI_NULL == task) {
        /* still nothing, steal from public queues of other workers.
           the process starts at the worker "after" the current worker,
           it might be better to start at a random worker
        */
        mtapi_uint_t context_index =
          (thread_context->worker_index + 1) % that->worker_count;
108
        mtapi_uint_t kk;
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
        for (kk = 0;
          kk < that->worker_count - 1 && MTAPI_NULL == task;
          kk++) {
          task = embb_mtapi_task_queue_pop(
            that->worker_contexts[context_index].queue[ii]);
          context_index =
            (context_index + 1) % that->worker_count;
        }
      }
    }
  }
  return task;
}

embb_mtapi_task_t * embb_mtapi_scheduler_get_next_task_lf(
  embb_mtapi_scheduler_t * that,
  embb_mtapi_node_t * node,
  embb_mtapi_thread_context_t * thread_context) {
  embb_mtapi_task_t * task = MTAPI_NULL;
128
  mtapi_uint_t prio;
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

  assert(MTAPI_NULL != that);
  assert(MTAPI_NULL != node);
  assert(NULL != thread_context);

  /* Try local queues on all priorities, first private. */
  for (prio = 0;
    MTAPI_NULL == task && prio < node->attributes.max_priorities;
    prio++) {
    task = embb_mtapi_scheduler_get_private_task_from_context(
      that, thread_context, prio);
  }

  /* found nothing, so local public next. */
  for (prio = 0;
    MTAPI_NULL == task && prio < node->attributes.max_priorities;
    prio++) {
    task = embb_mtapi_scheduler_get_public_task_from_context(
      that, thread_context, prio);
  }

  /* still nothing, steal from public queues of other workers.
     the process starts at the worker "after" the current worker,
     it might be better to start at a random worker
  */
  for (prio = 0;
    MTAPI_NULL == task && prio < node->attributes.max_priorities;
    prio++) {
    mtapi_uint_t context_index =
      (thread_context->worker_index + 1) % that->worker_count;
159
    mtapi_uint_t kk;
160 161 162 163 164 165 166 167 168 169 170 171
    for (kk = 0;
      kk < that->worker_count - 1 && MTAPI_NULL == task;
      kk++) {
      task = embb_mtapi_task_queue_pop(
        that->worker_contexts[context_index].queue[prio]);
      context_index =
        (context_index + 1) % that->worker_count;
    }
  }
  return task;
}

172 173 174 175 176 177 178 179 180 181 182 183
#ifdef EMBB_HARD_REALTIME
embb_mtapi_task_t * embb_mtapi_scheduler_get_next_task_gedf(
  embb_mtapi_scheduler_t * that,
  embb_mtapi_node_t * node,
  embb_mtapi_thread_context_t * thread_context) {
  embb_mtapi_task_t * task = MTAPI_NULL;

  EMBB_UNUSED(thread_context);

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

184
  task = embb_mtapi_priority_queue_pop(&node->global_task_queue);
185 186 187 188 189

  return task;
}
#endif /*EMBB_HARD_REALTIME*/

190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
embb_mtapi_task_t * embb_mtapi_scheduler_get_next_task(
  embb_mtapi_scheduler_t * that,
  embb_mtapi_node_t * node,
  embb_mtapi_thread_context_t * thread_context) {
  embb_mtapi_task_t * task = MTAPI_NULL;

  assert(MTAPI_NULL != that);
  assert(MTAPI_NULL != node);
  assert(NULL != thread_context);

  switch (that->mode) {
  case WORK_STEAL_LF:
    task = embb_mtapi_scheduler_get_next_task_lf(
      that, node, thread_context);
    break;
  case WORK_STEAL_VHPF:
    task = embb_mtapi_scheduler_get_next_task_vhpf(
      that, node, thread_context);
    break;
209 210 211 212 213 214
#ifdef EMBB_HARD_REALTIME
  case GLOBAL_EDF:
    task = embb_mtapi_scheduler_get_next_task_gedf(
      that, node, thread_context);
    break;
#endif /*EMBB_HARD_REALTIME*/
215 216 217 218 219 220 221 222 223 224 225
  case NUM_SCHEDULER_MODES:
  default:
    embb_mtapi_log_error(
      "embb_mtapi_Scheduler_getNextTask() unknown scheduler mode: %d\n",
      that->mode);
  }
  return task;
}

embb_mtapi_thread_context_t * embb_mtapi_scheduler_get_current_thread_context(
  embb_mtapi_scheduler_t * that) {
226
  mtapi_uint_t ii;
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
  embb_mtapi_thread_context_t * context = NULL;

  assert(MTAPI_NULL != that);

  /* find out on which thread we are */
  for (ii = 0; ii < that->worker_count; ii++) {
    context = (embb_mtapi_thread_context_t*)embb_tss_get(
      &(that->worker_contexts[ii].tss_id));
    if (NULL != context) {
      break;
    }
  }

  return context;
}

243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
void embb_mtapi_scheduler_finalize_task(
  embb_mtapi_task_t * task,
  embb_mtapi_node_t * node,
  embb_mtapi_queue_t * queue,
  embb_mtapi_group_t * group) {
  /* tell queue that a task is done */
  if (MTAPI_NULL != queue) {
    embb_mtapi_queue_task_finished(queue);
  }
  /* move task to group queue */
  if (MTAPI_NULL != group) {
    embb_mtapi_task_queue_push(&group->queue, task);
  }
  /* issue task complete callback if set */
  if (MTAPI_NULL != task->attributes.complete_func) {
    task->attributes.complete_func(task->handle, MTAPI_NULL);
  }
260 261
  /* delete task if detached and not in a group */
  if (MTAPI_NULL == group && MTAPI_TRUE == task->attributes.is_detached) {
262 263 264 265
    embb_mtapi_task_delete(task, node->task_pool);
  }
}

266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
mtapi_boolean_t embb_mtapi_scheduler_execute_task(
  embb_mtapi_task_t * task,
  embb_mtapi_node_t * node,
  embb_mtapi_thread_context_t * thread_context) {
  embb_mtapi_task_context_t task_context;
  mtapi_boolean_t result = MTAPI_FALSE;
  embb_mtapi_queue_t * local_queue = MTAPI_NULL;
  embb_mtapi_group_t * local_group = MTAPI_NULL;
  embb_mtapi_action_t * local_action = MTAPI_NULL;

  /* is task associated with a queue? */
  if (embb_mtapi_queue_pool_is_handle_valid(
    node->queue_pool, task->queue)) {
    local_queue =
      embb_mtapi_queue_pool_get_storage_for_handle(
        node->queue_pool, task->queue);
  }

  /* is task associated with a group? */
  if (embb_mtapi_group_pool_is_handle_valid(
    node->group_pool, task->group)) {
    local_group =
      embb_mtapi_group_pool_get_storage_for_handle(
        node->group_pool, task->group);
  }

  if (embb_mtapi_action_pool_is_handle_valid(
    node->action_pool, task->action)) {
    local_action =
      embb_mtapi_action_pool_get_storage_for_handle(
        node->action_pool, task->action);
  }

  switch (embb_atomic_load_int(&task->state)) {
  case MTAPI_TASK_SCHEDULED:
    /* multi-instance task, another instance might be running */
  case MTAPI_TASK_RUNNING:
    /* there was work, execute it */
    embb_mtapi_task_context_initialize_with_thread_context_and_task(
      &task_context, thread_context, task);
    if (embb_mtapi_task_execute(task, &task_context)) {
307
      embb_mtapi_scheduler_finalize_task(task, node, local_queue, local_group);
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
    }
    result = MTAPI_TRUE;
    break;

  case MTAPI_TASK_RETAINED:
    /* put task into queue again for later execution */
    embb_mtapi_scheduler_schedule_task(
      node->scheduler, task, 0);
    /* yield, as there may be only retained tasks in the queue */
    embb_thread_yield();
    /* task is not done, so do not notify queue */
    break;

  case MTAPI_TASK_CANCELLED:
    /* set return value to cancelled */
    task->error_code = MTAPI_ERR_ACTION_CANCELLED;
    if (embb_atomic_fetch_and_add_unsigned_int(
      &task->instances_todo, (unsigned int)-1) == 0) {
326
      embb_mtapi_scheduler_finalize_task(task, node, local_queue, local_group);
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
    }
    if (MTAPI_NULL != local_action) {
      embb_atomic_fetch_and_add_int(&local_action->num_tasks, -1);
    }
    break;

  case MTAPI_TASK_COMPLETED:
  case MTAPI_TASK_DELETED:
  case MTAPI_TASK_WAITING:
  case MTAPI_TASK_CREATED:
  case MTAPI_TASK_PRENATAL:
  case MTAPI_TASK_ERROR:
  case MTAPI_TASK_INTENTIONALLY_UNUSED:
  default:
    /* do nothing, although this is an error */
    break;
  }

  return result;
}

348 349 350 351 352 353 354 355 356 357 358 359
void embb_mtapi_scheduler_execute_task_or_yield(
  embb_mtapi_scheduler_t * that,
  embb_mtapi_node_t * node,
  embb_mtapi_thread_context_t * thread_context) {
  assert(MTAPI_NULL != that);
  assert(MTAPI_NULL != node);

  if (NULL != thread_context) {
    embb_mtapi_task_t* new_task = embb_mtapi_scheduler_get_next_task(
      that, node, thread_context);
    /* if there was work, execute it */
    if (MTAPI_NULL != new_task) {
360
      embb_mtapi_scheduler_execute_task(new_task, node, thread_context);
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
    } else {
      embb_thread_yield();
    }
  } else {
    embb_thread_yield();
  }
}

embb_mtapi_scheduler_worker_func_t *
embb_mtapi_scheduler_worker_func(embb_mtapi_scheduler_t * that) {
  EMBB_UNUSED(that);

  assert(MTAPI_NULL != that);

  /* Currently just returns embb_mtapi_scheduler_worker,
     but could return any custom worker function, e.g. depending
     on scheduler->mode.
  */
  return &embb_mtapi_scheduler_worker;
}

int embb_mtapi_scheduler_worker(void * arg) {
  embb_mtapi_thread_context_t * thread_context =
    (embb_mtapi_thread_context_t*)arg;
  embb_mtapi_node_t * node;
  embb_duration_t sleep_duration;
  int err;
  int counter = 0;

  embb_mtapi_log_trace(
    "embb_mtapi_scheduler_worker() called for thread %d on core %d\n",
    thread_context->worker_index, thread_context->core_num);

  assert(MTAPI_NULL != thread_context);

  err = embb_tss_create(&thread_context->tss_id);
  if (EMBB_SUCCESS != err) {
    /* report error to scheduler */
    embb_atomic_store_int(&thread_context->run, -1);
    return MTAPI_FALSE;
  }

  /* node is initialized here, otherwise the worker would not run */
  node = thread_context->node;

  embb_tss_set(&(thread_context->tss_id), thread_context);

  embb_duration_set_milliseconds(&sleep_duration, 10);

  /* signal that we're up & running */
  embb_atomic_store_int(&thread_context->run, 1);
  /* potentially wait for node to come up completely */
  while (MTAPI_FALSE == embb_atomic_load_int(&node->is_scheduler_running)) {
    embb_thread_yield();
  }

  /* do work while not requested to stop */
  while (embb_atomic_load_int(&thread_context->run)) {
    /* try to get work */
    embb_mtapi_task_t * task = embb_mtapi_scheduler_get_next_task(
      node->scheduler, node, thread_context);
    /* check if there was work */
    if (MTAPI_NULL != task) {
424
      if (embb_mtapi_scheduler_execute_task(task, node, thread_context)) {
425
        counter = 0;
426
      }
427 428 429 430 431 432
    } else if (counter < 1024) {
      /* spin and yield for a while before going to sleep */
      embb_thread_yield();
      counter++;
    } else {
      /* no work, go to sleep */
433
      embb_atomic_store_int(&thread_context->is_sleeping, 1);
434 435 436 437 438 439
      embb_mutex_lock(&thread_context->work_available_mutex);
      embb_condition_wait_for(
        &thread_context->work_available,
        &thread_context->work_available_mutex,
        &sleep_duration);
      embb_mutex_unlock(&thread_context->work_available_mutex);
440
      embb_atomic_store_int(&thread_context->is_sleeping, 0);
441 442 443 444 445 446 447 448 449 450 451 452 453 454
    }
  }

  embb_tss_delete(&(thread_context->tss_id));

  return MTAPI_TRUE;
}

mtapi_boolean_t embb_mtapi_scheduler_wait_for_task(
  embb_mtapi_task_t * task,
  mtapi_timeout_t timeout) {
  embb_mtapi_node_t* node = embb_mtapi_node_get_instance();
  embb_mtapi_thread_context_t * context = NULL;
  embb_duration_t wait_duration;
455
  embb_time_t start_time;
456 457 458 459 460 461 462
  embb_time_t end_time;

  assert(MTAPI_NULL != node);
  assert(MTAPI_NULL != task);

  if (MTAPI_INFINITE < timeout) {
    embb_duration_set_milliseconds(&wait_duration, (unsigned long long)timeout);
463
    embb_time_now(&start_time);
464 465 466 467 468 469 470 471
    embb_time_in(&end_time, &wait_duration);
  }

  /* find out on which thread we are */
  context = embb_mtapi_scheduler_get_current_thread_context(
    node->scheduler);

  /* now wait and schedule new tasks if we are on a worker */
472 473
  mtapi_task_state_t task_state =
    (mtapi_task_state_t)embb_atomic_load_int(&task->state);
474
  while (
475 476 477
    (MTAPI_TASK_SCHEDULED == task_state) ||
    (MTAPI_TASK_RUNNING == task_state) ||
    (MTAPI_TASK_RETAINED == task_state) ) {
478 479 480
    if (MTAPI_INFINITE < timeout) {
      embb_time_t current_time;
      embb_time_now(&current_time);
481 482 483 484 485 486
      if (embb_time_compare(&current_time, &start_time) < 0) {
        /* time has moved backwards, maybe a wraparound or jitter
           move end_time backward to avoid endeless loop */
        start_time = current_time;
        embb_time_in(&end_time, &wait_duration);
      }
487 488 489 490 491 492 493 494 495 496 497
      if (embb_time_compare(&current_time, &end_time) > 0) {
        /* timeout! */
        return MTAPI_FALSE;
      }
    }

    /* do other work if applicable */
    embb_mtapi_scheduler_execute_task_or_yield(
      node->scheduler,
      node,
      context);
498

Marcus Winter committed
499
    task_state = (mtapi_task_state_t)embb_atomic_load_int(&task->state);
500 501 502 503 504 505 506 507 508 509 510 511 512 513
  }

  return MTAPI_TRUE;
}

mtapi_boolean_t embb_mtapi_scheduler_initialize(
  embb_mtapi_scheduler_t * that) {
  return embb_mtapi_scheduler_initialize_with_mode(that, WORK_STEAL_VHPF);
}

mtapi_boolean_t embb_mtapi_scheduler_initialize_with_mode(
  embb_mtapi_scheduler_t * that,
  embb_mtapi_scheduler_mode_t mode) {
  embb_mtapi_node_t* node = embb_mtapi_node_get_instance();
514
  mtapi_uint_t ii;
515 516 517 518 519 520

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

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

521
  embb_atomic_init_int(&that->affine_task_counter, 0);
522 523

  /* Paranoia sanitizing of scheduler mode */
524
  if (mode >= NUM_SCHEDULER_MODES) {
525 526 527 528 529 530 531 532 533 534 535
    mode = WORK_STEAL_VHPF;
  }
  that->mode = mode;

  assert(node->attributes.num_cores ==
    embb_core_set_count(&node->attributes.core_affinity));
  that->worker_count = node->attributes.num_cores;

  that->worker_contexts = (embb_mtapi_thread_context_t*)
    embb_mtapi_alloc_allocate(
      sizeof(embb_mtapi_thread_context_t)*that->worker_count);
536 537 538 539
  if (NULL == that->worker_contexts) {
    return MTAPI_FALSE;
  }
  mtapi_boolean_t isinit = MTAPI_TRUE;
540 541 542 543 544 545 546 547 548 549 550
  for (ii = 0; ii < that->worker_count; ii++) {
    unsigned int core_num = 0;
    mtapi_uint_t ll = 0;
    mtapi_boolean_t run = MTAPI_TRUE;
    while (run) {
      if (embb_core_set_contains(&node->attributes.core_affinity, core_num)) {
        if (ll == ii) break;
        ll++;
      }
      core_num++;
    }
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
    embb_thread_priority_t priority = EMBB_THREAD_PRIORITY_NORMAL;
    if (NULL != node->attributes.worker_priorities) {
      mtapi_worker_priority_entry_t * entry =
        node->attributes.worker_priorities;
      mtapi_worker_priority_type_t type = entry->type;
      while (type != MTAPI_WORKER_PRIORITY_END) {
        if (type == MTAPI_WORKER_PRIORITY_DEFAULT) {
          priority = entry->priority;
        } else if (type ==
          (mtapi_worker_priority_type_t)(MTAPI_WORKER_PRIORITY_WORKER + ii)) {
          priority = entry->priority;
          break;
        }
        entry++;
        type = entry->type;
      }
    }
    isinit &= embb_mtapi_thread_context_initialize(
      &that->worker_contexts[ii], node, ii, core_num, priority);
570
  }
571 572 573
  if (!isinit) {
    return MTAPI_FALSE;
  }
574 575 576 577 578 579 580 581 582 583 584
  for (ii = 0; ii < that->worker_count; ii++) {
    if (MTAPI_FALSE == embb_mtapi_thread_context_start(
      &that->worker_contexts[ii], that)) {
      /* on error return false, finalize will shut everything down */
      return MTAPI_FALSE;
    }
  }
  return MTAPI_TRUE;
}

void embb_mtapi_scheduler_finalize(embb_mtapi_scheduler_t * that) {
585
  mtapi_uint_t ii;
586 587 588 589
  embb_mtapi_log_trace("embb_mtapi_scheduler_finalize() called\n");

  assert(MTAPI_NULL != that);

590 591 592 593 594 595 596 597
  if (MTAPI_NULL != that->worker_contexts) {
    /* finalize all workers */
    for (ii = 0; ii < that->worker_count; ii++) {
      embb_mtapi_thread_context_stop(&that->worker_contexts[ii]);
    }
    for (ii = 0; ii < that->worker_count; ii++) {
      embb_mtapi_thread_context_finalize(&that->worker_contexts[ii]);
    }
598

599 600 601 602
    that->worker_count = 0;
    embb_mtapi_alloc_deallocate(that->worker_contexts);
    that->worker_contexts = MTAPI_NULL;
  }
603 604

  embb_atomic_destroy_int(&that->affine_task_counter);
605 606 607 608 609 610 611 612 613
}

embb_mtapi_scheduler_t * embb_mtapi_scheduler_new() {
  embb_mtapi_scheduler_t * that =
    (embb_mtapi_scheduler_t*)embb_mtapi_alloc_allocate(
      sizeof(embb_mtapi_scheduler_t));
  if (MTAPI_NULL != that) {
    if (MTAPI_FALSE == embb_mtapi_scheduler_initialize(that)) {
      /* on error delete and return MTAPI_NULL */
614
      embb_mtapi_scheduler_finalize(that);
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
      embb_mtapi_scheduler_delete(that);
      return MTAPI_NULL;
    }
  }
  return that;
}

void embb_mtapi_scheduler_delete(embb_mtapi_scheduler_t * that) {
  assert(MTAPI_NULL != that);

  embb_mtapi_scheduler_finalize(that);
  embb_mtapi_alloc_deallocate(that);
}

mtapi_boolean_t embb_mtapi_scheduler_process_tasks(
  embb_mtapi_scheduler_t* that,
  embb_mtapi_task_visitor_function_t process,
  void * user_data) {
  mtapi_uint_t ii;
  mtapi_boolean_t result = MTAPI_TRUE;

  assert(MTAPI_NULL != that);

  for (ii = 0; ii < that->worker_count; ii++) {
    result = embb_mtapi_thread_context_process_tasks(
      &that->worker_contexts[ii], process, user_data);
    if (MTAPI_FALSE == result) {
      break;
    }
  }

  return result;
}

mtapi_boolean_t embb_mtapi_scheduler_schedule_task(
  embb_mtapi_scheduler_t * that,
651 652
  embb_mtapi_task_t * task,
  mtapi_uint_t instance) {
653 654
  embb_mtapi_scheduler_t * scheduler = that;
  /* distribute round robin */
655
  mtapi_uint_t ii = (task->handle.id + instance) % scheduler->worker_count;
656 657 658 659 660
  mtapi_boolean_t pushed = MTAPI_FALSE;
  embb_mtapi_node_t* node = embb_mtapi_node_get_instance();

  assert(MTAPI_NULL != node);

Tobias Langer committed
661 662
  if (embb_mtapi_action_pool_is_handle_valid(
    node->action_pool, task->action)) {
Tobias Langer committed
663
#ifdef EMBB_HARD_REALTIME
Tobias Langer committed
664
    if (scheduler->mode == GLOBAL_EDF) {
Tobias Langer committed
665 666 667 668
      /* fetch action and schedule */
      embb_mtapi_action_t* local_action =
        embb_mtapi_action_pool_get_storage_for_handle(
        node->action_pool, task->action);
Tobias Langer committed
669 670
      /* one more task in flight for this action */
      embb_atomic_fetch_and_add_int(&local_action->num_tasks, 1);
671

672
      pushed = embb_mtapi_priority_queue_push(&node->global_task_queue, task);
Tobias Langer committed
673 674
      if (!pushed) {
        embb_atomic_fetch_and_add_int(&local_action->num_tasks, -1);
675
      }
Tobias Langer committed
676 677 678 679 680 681 682
    } else {
#endif /*EMBB_HARD_REALTIME*/
    embb_mtapi_queue_t* local_queue = MTAPI_NULL;
    /* fetch action and schedule */
    embb_mtapi_action_t* local_action =
      embb_mtapi_action_pool_get_storage_for_handle(
      node->action_pool, task->action);
683

Tobias Langer committed
684 685 686 687 688 689 690 691 692 693
    mtapi_affinity_t affinity =
      local_action->attributes.affinity & task->attributes.affinity;

    /* check if task is running from an ordered queue */
    if (embb_mtapi_queue_pool_is_handle_valid(node->queue_pool, task->queue)) {
      local_queue = embb_mtapi_queue_pool_get_storage_for_handle(
        node->queue_pool, task->queue);
      if (local_queue->attributes.ordered) {
        /* yes, modify affinity accordingly */
        affinity = local_queue->ordered_affinity;
Tobias Langer committed
694
      }
Tobias Langer committed
695
    }
696

Tobias Langer committed
697 698 699 700
    /* check affinity */
    if (affinity == 0) {
      affinity = node->affinity_all;
    }
Tobias Langer committed
701

Tobias Langer committed
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
    /* one more task in flight for this action */
    embb_atomic_fetch_and_add_int(&local_action->num_tasks, 1);

    if (affinity == node->affinity_all) {
      /* no affinity restrictions, schedule for stealing */
      pushed = embb_mtapi_task_queue_push(
        scheduler->worker_contexts[ii].queue[task->attributes.priority],
        task);
    } else {
      mtapi_status_t affinity_status;

      /* affinity is restricted, check and adapt scheduling target */
      ii = (mtapi_uint_t)embb_atomic_fetch_and_add_int(
        &scheduler->affine_task_counter, 1);
      while (MTAPI_FALSE == mtapi_affinity_get(
        &affinity, ii, &affinity_status)) {
        ii = (ii + 1) % scheduler->worker_count;
719
      }
Tobias Langer committed
720 721 722 723 724
      /* schedule into private queue to disable stealing */
      pushed = embb_mtapi_task_queue_push(
        scheduler->worker_contexts[ii].private_queue[task->attributes.priority],
        task);
    }
725

Tobias Langer committed
726 727 728 729 730
    if (pushed) {
      /* signal the worker thread a task was pushed to */
      if (embb_atomic_load_int(&scheduler->worker_contexts[ii].is_sleeping)) {
        embb_condition_notify_one(
          &scheduler->worker_contexts[ii].work_available);
731
      }
Tobias Langer committed
732 733 734
    } else {
      /* task could not be launched */
      embb_atomic_fetch_and_add_int(&local_action->num_tasks, -1);
735
    }
Tobias Langer committed
736
#ifdef EMBB_HARD_REALTIME
Tobias Langer committed
737
    }
Tobias Langer committed
738
#endif /*EMBB_HARD_REALTIME*/
Tobias Langer committed
739
  }
740 741 742

  return pushed;
}