ARGOBOTS  dce6e727ffc4ca5b3ffc04cb9517c6689be51ec5
sched_config.c
Go to the documentation of this file.
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  * See COPYRIGHT in top-level directory.
4  */
5 
6 #include "abti.h"
7 
8 #include <stdlib.h>
9 #include <stdarg.h>
10 #include <string.h>
11 
12 #define SCHED_CONFIG_HTABLE_SIZE 8
13 typedef struct {
14  ABT_sched_config_type type; /* Element type. */
15  union {
16  int v_int;
17  double v_double;
18  void *v_ptr;
19  } val;
21 
23  int val);
25  double val);
27  void *ptr);
28 ABTU_ret_err static int
31  const void *p_val);
32 static void sched_config_read_element(const sched_config_element *p_elem,
33  void *ptr);
34 
39 /* Global configurable parameters */
41  .type =
43 
45  .type = ABT_SCHED_CONFIG_INT };
46 
48  .type =
50 
52  .type = ABT_SCHED_CONFIG_INT };
53 
120 {
121  ABTI_UB_ASSERT(ABTI_initialized());
122 
123  int abt_errno;
124  ABTI_sched_config *p_config;
125 
126  abt_errno = ABTU_calloc(1, sizeof(ABTI_sched_config), (void **)&p_config);
127  ABTI_CHECK_ERROR(abt_errno);
128 
129  abt_errno =
131  sizeof(sched_config_element), &p_config->p_table);
132  if (abt_errno != ABT_SUCCESS) {
133  ABTU_free(p_config);
134  ABTI_HANDLE_ERROR(abt_errno);
135  }
136 
137  va_list varg_list;
138  va_start(varg_list, config);
139 
140  /* We read (var, value) until we find ABT_sched_config_var_end */
141  while (1) {
142  ABT_sched_config_var var = va_arg(varg_list, ABT_sched_config_var);
143  int idx = var.idx;
144  if (idx == ABT_sched_config_var_end.idx)
145  break;
146  /* Add the argument */
148  switch (var.type) {
149  case ABT_SCHED_CONFIG_INT: {
150  sched_config_create_element_int(&data, va_arg(varg_list, int));
151  break;
152  }
155  va_arg(varg_list, double));
156  break;
157  }
158  case ABT_SCHED_CONFIG_PTR: {
160  va_arg(varg_list, void *));
161  break;
162  }
163  default:
164  abt_errno = ABT_ERR_INV_ARG;
165  }
166  if (abt_errno == ABT_SUCCESS) {
167  abt_errno = ABTU_hashtable_set(p_config->p_table, idx, &data, NULL);
168  }
169  if (abt_errno != ABT_SUCCESS) {
170  ABTU_hashtable_free(p_config->p_table);
171  ABTU_free(p_config);
172  va_end(varg_list);
173  ABTI_HANDLE_ERROR(abt_errno);
174  }
175  }
176  va_end(varg_list);
177 
178  *config = ABTI_sched_config_get_handle(p_config);
179  return ABT_SUCCESS;
180 }
181 
221 int ABT_sched_config_read(ABT_sched_config config, int num_vars, ...)
222 {
223  ABTI_UB_ASSERT(ABTI_initialized());
224 
225  int idx;
226  ABTI_sched_config *p_config = ABTI_sched_config_get_ptr(config);
227  ABTI_CHECK_NULL_SCHED_CONFIG_PTR(p_config);
228 
229  va_list varg_list;
230  va_start(varg_list, num_vars);
231  for (idx = 0; idx < num_vars; idx++) {
232  void *ptr = va_arg(varg_list, void *);
233  if (ptr) {
235  int found;
236  ABTU_hashtable_get(p_config->p_table, idx, &data, &found);
237  if (found) {
238  sched_config_read_element(&data, ptr);
239  }
240  }
241  }
242  va_end(varg_list);
243  return ABT_SUCCESS;
244 }
245 
270 {
271  ABTI_UB_ASSERT(ABTI_initialized());
272 
273  ABTI_sched_config *p_config = ABTI_sched_config_get_ptr(*config);
274  ABTI_CHECK_NULL_SCHED_CONFIG_PTR(p_config);
275 
276  ABTU_hashtable_free(p_config->p_table);
277  ABTU_free(p_config);
278 
279  *config = ABT_SCHED_CONFIG_NULL;
280 
281  return ABT_SUCCESS;
282 }
283 
329  ABT_sched_config_type type, const void *val)
330 {
331  ABTI_UB_ASSERT(ABTI_initialized());
332 
333  ABTI_sched_config *p_config = ABTI_sched_config_get_ptr(config);
334  ABTI_CHECK_NULL_SCHED_CONFIG_PTR(p_config);
335  if (val) {
336  /* Add a value. */
338  int abt_errno;
339  abt_errno = sched_config_create_element_typed(&data, type, val);
340  ABTI_CHECK_ERROR(abt_errno);
341  abt_errno = ABTU_hashtable_set(p_config->p_table, idx, &data, NULL);
342  ABTI_CHECK_ERROR(abt_errno);
343  } else {
344  /* Delete a value. */
345  ABTU_hashtable_delete(p_config->p_table, idx, NULL);
346  }
347  return ABT_SUCCESS;
348 }
349 
389  ABT_sched_config_type *type, void *val)
390 {
391  ABTI_UB_ASSERT(ABTI_initialized());
392 
393  ABTI_sched_config *p_config = ABTI_sched_config_get_ptr(config);
394  ABTI_CHECK_NULL_SCHED_CONFIG_PTR(p_config);
396  int found;
397  ABTU_hashtable_get(p_config->p_table, idx, &data, &found);
398  if (found) {
399  if (val) {
400  sched_config_read_element(&data, val);
401  }
402  if (type) {
403  *type = data.type;
404  }
405  } else {
406  ABTI_HANDLE_ERROR(ABT_ERR_INV_ARG);
407  }
408  return ABT_SUCCESS;
409 }
410 
411 /*****************************************************************************/
412 /* Private APIs */
413 /*****************************************************************************/
414 
415 ABTU_ret_err int ABTI_sched_config_read(const ABTI_sched_config *p_config,
416  int idx, void *p_val)
417 {
418  int found;
420  ABTU_hashtable_get(p_config->p_table, idx, &data, &found);
421  if (found) {
422  if (p_val) {
423  sched_config_read_element(&data, p_val);
424  }
425  return ABT_SUCCESS;
426  } else {
427  return ABT_ERR_INV_ARG;
428  }
429 }
430 
431 /*****************************************************************************/
432 /* Internal static functions */
433 /*****************************************************************************/
434 
436  int val)
437 {
438  memset(p_elem, 0, sizeof(sched_config_element));
439  p_elem->type = ABT_SCHED_CONFIG_INT;
440  p_elem->val.v_int = val;
441 }
442 
444  double val)
445 {
446  memset(p_elem, 0, sizeof(sched_config_element));
447  p_elem->type = ABT_SCHED_CONFIG_DOUBLE;
448  p_elem->val.v_double = val;
449 }
450 
452  void *ptr)
453 {
454  memset(p_elem, 0, sizeof(sched_config_element));
455  p_elem->type = ABT_SCHED_CONFIG_PTR;
456  p_elem->val.v_ptr = ptr;
457 }
458 
459 ABTU_ret_err static int
461  ABT_sched_config_type type, const void *p_val)
462 {
463  switch (type) {
464  case ABT_SCHED_CONFIG_INT: {
465  sched_config_create_element_int(p_elem, *(const int *)p_val);
466  break;
467  }
469  sched_config_create_element_double(p_elem, *(const double *)p_val);
470  break;
471  }
472  case ABT_SCHED_CONFIG_PTR: {
473  sched_config_create_element_ptr(p_elem, *(void *const *)p_val);
474  break;
475  }
476  default:
477  return ABT_ERR_INV_ARG;
478  }
479  return ABT_SUCCESS;
480 }
481 
483  void *ptr)
484 {
485  switch (p_elem->type) {
486  case ABT_SCHED_CONFIG_INT: {
487  *((int *)ptr) = p_elem->val.v_int;
488  break;
489  }
491  *((double *)ptr) = p_elem->val.v_double;
492  break;
493  }
494  case ABT_SCHED_CONFIG_PTR: {
495  *((void **)ptr) = p_elem->val.v_ptr;
496  break;
497  }
498  default:
499  ABTI_ASSERT(0);
500  }
501 }
ABT_sched_config_var
A struct that sets and gets a scheduler configuration.
Definition: abt.h:1349
ABTU_hashtable_create
ABTU_ret_err int ABTU_hashtable_create(size_t num_entries, size_t data_size, ABTU_hashtable **pp_hashtable)
Definition: hashtable.c:21
ABT_sched_config_set
int ABT_sched_config_set(ABT_sched_config config, int idx, ABT_sched_config_type type, const void *val)
Register a value to a scheduler configuration.
Definition: sched_config.c:328
sched_config_create_element_int
static void sched_config_create_element_int(sched_config_element *p_elem, int val)
Definition: sched_config.c:435
sched_config_element::v_ptr
void * v_ptr
Definition: sched_config.c:18
sched_config_create_element_double
static void sched_config_create_element_double(sched_config_element *p_elem, double val)
Definition: sched_config.c:443
ABT_sched_config
struct ABT_sched_config_opaque * ABT_sched_config
Scheduler configuration handle type.
Definition: abt.h:852
sched_config_element::v_int
int v_int
Definition: sched_config.c:16
sched_config_read_element
static void sched_config_read_element(const sched_config_element *p_elem, void *ptr)
Definition: sched_config.c:482
ABTU_hashtable_get
void ABTU_hashtable_get(const ABTU_hashtable *p_hashtable, int key, void *data, int *found)
Definition: hashtable.c:56
ABT_sched_config_automatic
ABT_sched_config_var ABT_sched_config_automatic
Predefined ABT_sched_config_var to configure whether the scheduler is freed automatically or not.
Definition: sched_config.c:47
ABT_sched_config_read
int ABT_sched_config_read(ABT_sched_config config, int num_vars,...)
Retrieve values from a scheduler configuration.
Definition: sched_config.c:221
abti.h
ABTU_hashtable_delete
void ABTU_hashtable_delete(ABTU_hashtable *p_hashtable, int key, int *deleted)
Definition: hashtable.c:144
sched_config_element::v_double
double v_double
Definition: sched_config.c:17
ABT_sched_config_access
ABT_sched_config_var ABT_sched_config_access
Unused predefined ABT_sched_config_var.
Definition: sched_config.c:44
sched_config_element::type
ABT_sched_config_type type
Definition: sched_config.c:14
ABT_SCHED_CONFIG_PTR
@ ABT_SCHED_CONFIG_PTR
Definition: abt.h:1342
ABT_sched_config_var::type
ABT_sched_config_type type
Definition: abt.h:1353
ABT_sched_config_free
int ABT_sched_config_free(ABT_sched_config *config)
Free a scheduler configuration.
Definition: sched_config.c:269
ABT_SCHED_CONFIG_DOUBLE
@ ABT_SCHED_CONFIG_DOUBLE
Definition: abt.h:1340
ABTU_calloc
static ABTU_ret_err int ABTU_calloc(size_t num, size_t size, void **p_ptr)
Definition: abtu.h:244
ABT_SUCCESS
#define ABT_SUCCESS
Error code: the routine returns successfully.
Definition: abt.h:92
ABT_SCHED_CONFIG_NULL
#define ABT_SCHED_CONFIG_NULL
Definition: abt.h:1101
ABTU_ret_err
#define ABTU_ret_err
Definition: abtu.h:155
sched_config_element::val
union sched_config_element::@2 val
ABT_ERR_INV_ARG
#define ABT_ERR_INV_ARG
Error code: invalid user argument.
Definition: abt.h:260
ABTU_free
static void ABTU_free(void *ptr)
Definition: abtu.h:228
sched_config_create_element_ptr
static void sched_config_create_element_ptr(sched_config_element *p_elem, void *ptr)
Definition: sched_config.c:451
sched_config_create_element_typed
static ABTU_ret_err int sched_config_create_element_typed(sched_config_element *p_elem, ABT_sched_config_type type, const void *p_val)
Definition: sched_config.c:460
ABTU_hashtable_free
void ABTU_hashtable_free(ABTU_hashtable *p_hashtable)
Definition: hashtable.c:42
ABT_sched_config_var_end
ABT_sched_config_var ABT_sched_config_var_end
Predefined ABT_sched_config_var to mark the last parameter.
Definition: sched_config.c:40
ABT_SCHED_CONFIG_INT
@ ABT_SCHED_CONFIG_INT
Definition: abt.h:1338
ABTU_hashtable_set
ABTU_ret_err int ABTU_hashtable_set(ABTU_hashtable *p_hashtable, int key, const void *data, int *overwritten)
Definition: hashtable.c:91
ABT_sched_basic_freq
ABT_sched_config_var ABT_sched_basic_freq
Predefined ABT_sched_config_var to configure the frequency for checking events of the basic scheduler...
Definition: sched_config.c:51
ABT_sched_config_var::idx
int idx
Definition: abt.h:1351
ABT_sched_config_get
int ABT_sched_config_get(ABT_sched_config config, int idx, ABT_sched_config_type *type, void *val)
Retrieve a value from a scheduler configuration.
Definition: sched_config.c:388
sched_config_element
Definition: sched_config.c:13
SCHED_CONFIG_HTABLE_SIZE
#define SCHED_CONFIG_HTABLE_SIZE
Definition: sched_config.c:12
ABT_sched_config_create
int ABT_sched_config_create(ABT_sched_config *config,...)
Create a new scheduler configuration.
Definition: sched_config.c:119
ABT_sched_config_type
ABT_sched_config_type
A struct that sets and gets a scheduler configuration.
Definition: abt.h:1336