summaryrefslogtreecommitdiff
path: root/openmp/libompd/src/omp-state.cpp
blob: ca87907aa6b2c7bba415855975a25da90a64cf77 (plain)
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
/*
 * omp-state.cpp -- OMPD states
 */

//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "omp-debug.h"
#include "ompd-private.h"
#include <cstring>

void __ompd_init_states(const ompd_callbacks_t *table) { callbacks = table; }

static const char *get_ompd_state_name(ompd_word_t state) {
  switch (state) {
#define ompd_state_macro(state, code)                                          \
  case code:                                                                   \
    return #state;
    FOREACH_OMPD_STATE(ompd_state_macro)
#undef ompd_state_macro
  default:
    return NULL;
  }
}

ompd_rc_t
ompd_enumerate_states(ompd_address_space_handle_t *address_space_handle,
                      ompd_word_t current_state, ompd_word_t *next_state,
                      const char **next_state_name, ompd_word_t *more_enums) {
  ompd_rc_t ret;
  if (current_state > ompt_state_undefined &&
      current_state >= OMPD_LAST_OMP_STATE) {
    return ompd_rc_bad_input;
  }
  const char *find_next_state_name;
  *next_state = (current_state == ompt_state_undefined ? ompt_state_work_serial
                                                       : current_state + 1);
  while (!(find_next_state_name = get_ompd_state_name(*next_state))) {
    ++(*next_state);
  }

  char *next_state_name_cpy;
  ret = callbacks->alloc_memory(strlen(find_next_state_name) + 1,
                                (void **)&next_state_name_cpy);
  if (ret != ompd_rc_ok) {
    return ret;
  }
  strcpy(next_state_name_cpy, find_next_state_name);

  *next_state_name = next_state_name_cpy;

  if (*next_state == OMPD_LAST_OMP_STATE) {
    *more_enums = 0;
  } else {
    *more_enums = 1;
  }

  return ompd_rc_ok;
}