ARGOBOTS  dce6e727ffc4ca5b3ffc04cb9517c6689be51ec5
thread_attr.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 static void thread_attr_set_stack(ABTI_global *p_global,
9  ABTI_thread_attr *p_attr, void *stackaddr,
10  size_t stacksize);
11 
51 {
53  ABTI_UB_ASSERT(newattr);
54 
55 #ifndef ABT_CONFIG_ENABLE_VER_20_API
56  /* Argobots 1.x sets newattr to NULL on error. */
57  *newattr = ABT_THREAD_ATTR_NULL;
58 #endif
59  ABTI_global *p_global;
60  ABTI_SETUP_GLOBAL(&p_global);
61 
62  ABTI_thread_attr *p_newattr;
63  int abt_errno = ABTU_malloc(sizeof(ABTI_thread_attr), (void **)&p_newattr);
64  ABTI_CHECK_ERROR(abt_errno);
65 
66  /* Default values */
67  ABTI_thread_attr_init(p_newattr, NULL, p_global->thread_stacksize,
68  ABT_TRUE);
69  *newattr = ABTI_thread_attr_get_handle(p_newattr);
70  return ABT_SUCCESS;
71 }
72 
96 {
98  ABTI_UB_ASSERT(attr);
99 
100  ABT_thread_attr h_attr = *attr;
101  ABTI_thread_attr *p_attr = ABTI_thread_attr_get_ptr(h_attr);
103 
104  /* Free the memory */
105  ABTU_free(p_attr);
106  *attr = ABT_THREAD_ATTR_NULL;
107  return ABT_SUCCESS;
108 }
109 
158 int ABT_thread_attr_set_stack(ABT_thread_attr attr, void *stackaddr,
159  size_t stacksize)
160 {
162 
163  ABTI_global *p_global;
164  ABTI_SETUP_GLOBAL(&p_global);
165 
168  /* If stackaddr is not NULL, it must be aligned by 8 bytes. */
169  ABTI_CHECK_TRUE(stackaddr == NULL || ((uintptr_t)stackaddr & 0x7) == 0,
171  thread_attr_set_stack(p_global, p_attr, stackaddr, stacksize);
172  return ABT_SUCCESS;
173 }
174 
200 int ABT_thread_attr_get_stack(ABT_thread_attr attr, void **stackaddr,
201  size_t *stacksize)
202 {
204  ABTI_UB_ASSERT(stackaddr);
205  ABTI_UB_ASSERT(stacksize);
206 
209 
210  *stackaddr = p_attr->p_stack;
211  *stacksize = p_attr->stacksize;
212  return ABT_SUCCESS;
213 }
214 
239 int ABT_thread_attr_set_stacksize(ABT_thread_attr attr, size_t stacksize)
240 {
242 
243  ABTI_global *p_global;
244  ABTI_SETUP_GLOBAL(&p_global);
245 
248 
249  thread_attr_set_stack(p_global, p_attr, p_attr->p_stack, stacksize);
250  return ABT_SUCCESS;
251 }
252 
275 int ABT_thread_attr_get_stacksize(ABT_thread_attr attr, size_t *stacksize)
276 {
278  ABTI_UB_ASSERT(stacksize);
279 
282 
283  *stacksize = p_attr->stacksize;
284  return ABT_SUCCESS;
285 }
286 
322  void (*cb_func)(ABT_thread thread,
323  void *cb_arg),
324  void *cb_arg)
325 {
327 
328 #ifndef ABT_CONFIG_DISABLE_MIGRATION
331 
332  /* Set the value */
333  p_attr->f_cb = cb_func;
334  p_attr->p_cb_arg = cb_arg;
335  return ABT_SUCCESS;
336 #else
338 #endif
339 }
340 
369 {
371  ABTI_UB_ASSERT_BOOL(is_migratable);
372 
373 #ifndef ABT_CONFIG_DISABLE_MIGRATION
376 
377  /* Set the value */
378  p_attr->migratable = is_migratable;
379  return ABT_SUCCESS;
380 #else
382 #endif
383 }
384 
385 /*****************************************************************************/
386 /* Private APIs */
387 /*****************************************************************************/
388 
389 void ABTI_thread_attr_print(ABTI_thread_attr *p_attr, FILE *p_os, int indent)
390 {
391  if (p_attr == NULL) {
392  fprintf(p_os, "%*sULT attr: [NULL ATTR]\n", indent, "");
393  } else {
394 #ifndef ABT_CONFIG_DISABLE_MIGRATION
395  fprintf(p_os,
396  "%*sULT attr: ["
397  "stack:%p "
398  "stacksize:%zu "
399  "migratable:%s "
400  "cb_arg:%p"
401  "]\n",
402  indent, "", p_attr->p_stack, p_attr->stacksize,
403  (p_attr->migratable == ABT_TRUE ? "TRUE" : "FALSE"),
404  p_attr->p_cb_arg);
405 #else
406  fprintf(p_os,
407  "%*sULT attr: ["
408  "stack:%p "
409  "stacksize:%zu "
410  "]\n",
411  indent, "", p_attr->p_stack, p_attr->stacksize);
412 #endif
413  }
414  fflush(p_os);
415 }
416 
418  ABTI_thread_attr **pp_dup_attr)
419 {
420  ABTI_thread_attr *p_dup_attr;
421  int abt_errno = ABTU_malloc(sizeof(ABTI_thread_attr), (void **)&p_dup_attr);
422  ABTI_CHECK_ERROR(abt_errno);
423 
424  memcpy(p_dup_attr, p_attr, sizeof(ABTI_thread_attr));
425  *pp_dup_attr = p_dup_attr;
426  return ABT_SUCCESS;
427 }
428 
429 /*****************************************************************************/
430 /* Internal static functions */
431 /*****************************************************************************/
432 
433 static void thread_attr_set_stack(ABTI_global *p_global,
434  ABTI_thread_attr *p_attr, void *stackaddr,
435  size_t stacksize)
436 {
437  if (stackaddr != NULL) {
438  /* This check must be done by the caller. */
439  ABTI_ASSERT(((uintptr_t)stackaddr & 0x7) == 0);
440  /* Only a descriptor will be allocated from a memory pool. A stack
441  * is given by the user. */
442  }
443  p_attr->p_stack = stackaddr;
444  p_attr->stacksize = stacksize;
445 }
ABT_bool
int ABT_bool
Boolean type.
Definition: abt.h:1043
ABT_thread_attr
struct ABT_thread_attr_opaque * ABT_thread_attr
ULT attribute handle type.
Definition: abt.h:939
ABT_thread
struct ABT_thread_opaque * ABT_thread
Work unit handle type.
Definition: abt.h:932
ABTI_SETUP_GLOBAL
#define ABTI_SETUP_GLOBAL(pp_global)
Definition: abti_error.h:75
ABTI_thread_attr::migratable
ABT_bool migratable
Definition: abti.h:443
ABTI_CHECK_ERROR
#define ABTI_CHECK_ERROR(abt_errno)
Definition: abti_error.h:136
ABTI_thread_attr_get_handle
static ABT_thread_attr ABTI_thread_attr_get_handle(ABTI_thread_attr *p_attr)
Definition: abti_thread_attr.h:27
ABT_thread_attr_create
int ABT_thread_attr_create(ABT_thread_attr *newattr)
Create a new ULT attribute.
Definition: thread_attr.c:50
ABT_thread_attr_set_stack
int ABT_thread_attr_set_stack(ABT_thread_attr attr, void *stackaddr, size_t stacksize)
Set stack attributes in a ULT attribute.
Definition: thread_attr.c:160
ABTI_thread_attr_get_ptr
static ABTI_thread_attr * ABTI_thread_attr_get_ptr(ABT_thread_attr attr)
Definition: abti_thread_attr.h:11
ABT_thread_attr_free
int ABT_thread_attr_free(ABT_thread_attr *attr)
Free a ULT attribute.
Definition: thread_attr.c:95
ABTI_CHECK_NULL_THREAD_ATTR_PTR
#define ABTI_CHECK_NULL_THREAD_ATTR_PTR(p)
Definition: abti_error.h:247
ABTI_thread_attr
Definition: abti.h:439
ABTI_thread_attr_init
static void ABTI_thread_attr_init(ABTI_thread_attr *p_attr, void *p_stack, size_t stacksize, ABT_bool migratable)
Definition: abti_thread_attr.h:42
abti.h
ABTI_global::thread_stacksize
size_t thread_stacksize
Definition: abti.h:237
ABTI_thread_attr_dup
ABTU_ret_err int ABTI_thread_attr_dup(const ABTI_thread_attr *p_attr, ABTI_thread_attr **pp_dup_attr)
Definition: thread_attr.c:419
ABTI_HANDLE_ERROR
#define ABTI_HANDLE_ERROR(n)
Definition: abti_error.h:130
ABTU_malloc
static ABTU_ret_err int ABTU_malloc(size_t size, void **p_ptr)
Definition: abtu.h:235
ABTI_thread_attr::p_cb_arg
void * p_cb_arg
Definition: abti.h:445
ABT_thread_attr_set_migratable
int ABT_thread_attr_set_migratable(ABT_thread_attr attr, ABT_bool is_migratable)
Set the ULT's migratability in a ULT attribute.
Definition: thread_attr.c:370
ABTI_ASSERT
#define ABTI_ASSERT(cond)
Definition: abti_error.h:12
ABTI_initialized
ABT_bool ABTI_initialized(void)
Definition: global.c:187
ABT_THREAD_ATTR_NULL
#define ABT_THREAD_ATTR_NULL
Definition: abt.h:1106
ABT_thread_attr_set_callback
int ABT_thread_attr_set_callback(ABT_thread_attr attr, void(*cb_func)(ABT_thread thread, void *cb_arg), void *cb_arg)
Set a callback function and its argument in a ULT attribute.
Definition: thread_attr.c:323
ABT_SUCCESS
#define ABT_SUCCESS
Error code: the routine returns successfully.
Definition: abt.h:92
ABTU_ret_err
#define ABTU_ret_err
Definition: abtu.h:155
ABTI_thread_attr::stacksize
size_t stacksize
Definition: abti.h:441
ABT_thread_attr_get_stacksize
int ABT_thread_attr_get_stacksize(ABT_thread_attr attr, size_t *stacksize)
Get the stack size from a ULT attribute.
Definition: thread_attr.c:277
ABT_TRUE
#define ABT_TRUE
True constant for ABT_bool.
Definition: abt.h:784
ABTI_UB_ASSERT
#define ABTI_UB_ASSERT(cond)
Definition: abti_error.h:19
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
ABTI_thread_attr::p_stack
void * p_stack
Definition: abti.h:440
ABTI_thread_attr::f_cb
void(* f_cb)(ABT_thread, void *)
Definition: abti.h:444
ABT_thread_attr_get_stack
int ABT_thread_attr_get_stack(ABT_thread_attr attr, void **stackaddr, size_t *stacksize)
Get stack attributes from a ULT attribute.
Definition: thread_attr.c:202
ABTI_CHECK_TRUE
#define ABTI_CHECK_TRUE(cond, abt_errno)
Definition: abti_error.h:146
ABTI_global
Definition: abti.h:223
ABT_ERR_FEATURE_NA
#define ABT_ERR_FEATURE_NA
Error code: unsupported feature.
Definition: abt.h:391
ABT_thread_attr_set_stacksize
int ABT_thread_attr_set_stacksize(ABT_thread_attr attr, size_t stacksize)
Set stack size in a ULT attribute.
Definition: thread_attr.c:241
ABTI_thread_attr_print
void ABTI_thread_attr_print(ABTI_thread_attr *p_attr, FILE *p_os, int indent)
Definition: thread_attr.c:391
thread_attr_set_stack
static void thread_attr_set_stack(ABTI_global *p_global, ABTI_thread_attr *p_attr, void *stackaddr, size_t stacksize)
Definition: thread_attr.c:435
ABTI_UB_ASSERT_BOOL
#define ABTI_UB_ASSERT_BOOL(bool_val)
Definition: abti_error.h:26