ARGOBOTS  1.1b1
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 {
52 #ifndef ABT_CONFIG_ENABLE_VER_20_API
53  /* Argobots 1.x sets newattr to NULL on error. */
54  *newattr = ABT_THREAD_ATTR_NULL;
55 #endif
56  ABTI_global *p_global;
57  ABTI_SETUP_GLOBAL(&p_global);
58 
59  ABTI_thread_attr *p_newattr;
60  int abt_errno = ABTU_malloc(sizeof(ABTI_thread_attr), (void **)&p_newattr);
61  ABTI_CHECK_ERROR(abt_errno);
62 
63  /* Default values */
64  ABTI_thread_attr_init(p_newattr, NULL, p_global->thread_stacksize,
66  *newattr = ABTI_thread_attr_get_handle(p_newattr);
67  return ABT_SUCCESS;
68 }
69 
93 {
94  ABT_thread_attr h_attr = *attr;
97 
98  /* Free the memory */
99  ABTU_free(p_attr);
100  *attr = ABT_THREAD_ATTR_NULL;
101  return ABT_SUCCESS;
102 }
103 
152 int ABT_thread_attr_set_stack(ABT_thread_attr attr, void *stackaddr,
153  size_t stacksize)
154 {
155  ABTI_global *p_global;
156  ABTI_SETUP_GLOBAL(&p_global);
157 
160  /* If stackaddr is not NULL, it must be aligned by 8 bytes. */
161  ABTI_CHECK_TRUE(stackaddr == NULL || ((uintptr_t)stackaddr & 0x7) == 0,
163  thread_attr_set_stack(p_global, p_attr, stackaddr, stacksize);
164  return ABT_SUCCESS;
165 }
166 
192 int ABT_thread_attr_get_stack(ABT_thread_attr attr, void **stackaddr,
193  size_t *stacksize)
194 {
197 
198  *stackaddr = p_attr->p_stack;
199  *stacksize = p_attr->stacksize;
200  return ABT_SUCCESS;
201 }
202 
227 int ABT_thread_attr_set_stacksize(ABT_thread_attr attr, size_t stacksize)
228 {
229  ABTI_global *p_global;
230  ABTI_SETUP_GLOBAL(&p_global);
231 
234 
235  thread_attr_set_stack(p_global, p_attr, p_attr->p_stack, stacksize);
236  return ABT_SUCCESS;
237 }
238 
261 int ABT_thread_attr_get_stacksize(ABT_thread_attr attr, size_t *stacksize)
262 {
265 
266  *stacksize = p_attr->stacksize;
267  return ABT_SUCCESS;
268 }
269 
305  void (*cb_func)(ABT_thread thread,
306  void *cb_arg),
307  void *cb_arg)
308 {
309 #ifndef ABT_CONFIG_DISABLE_MIGRATION
312 
313  /* Set the value */
314  p_attr->f_cb = cb_func;
315  p_attr->p_cb_arg = cb_arg;
316  return ABT_SUCCESS;
317 #else
319 #endif
320 }
321 
350 {
351 #ifndef ABT_CONFIG_DISABLE_MIGRATION
354 
355  /* Set the value */
356  p_attr->migratable = is_migratable;
357  return ABT_SUCCESS;
358 #else
360 #endif
361 }
362 
363 /*****************************************************************************/
364 /* Private APIs */
365 /*****************************************************************************/
366 
367 void ABTI_thread_attr_print(ABTI_thread_attr *p_attr, FILE *p_os, int indent)
368 {
369  if (p_attr == NULL) {
370  fprintf(p_os, "%*sULT attr: [NULL ATTR]\n", indent, "");
371  } else {
372  const char *stacktype;
374  stacktype = "MEMPOOL_DESC";
375  } else if (p_attr->thread_type & ABTI_THREAD_TYPE_MEM_MALLOC_DESC) {
376  stacktype = "MALLOC_DESC";
377  } else if (p_attr->thread_type &
379  stacktype = "MEMPOOL_DESC_STACK";
380  } else if (p_attr->thread_type &
382  stacktype = "MALLOC_DESC_STACK";
383  } else {
384  stacktype = "UNKNOWN";
385  }
386 #ifndef ABT_CONFIG_DISABLE_MIGRATION
387  fprintf(p_os,
388  "%*sULT attr: ["
389  "stack:%p "
390  "stacksize:%zu "
391  "stacktype:%s "
392  "migratable:%s "
393  "cb_arg:%p"
394  "]\n",
395  indent, "", p_attr->p_stack, p_attr->stacksize, stacktype,
396  (p_attr->migratable == ABT_TRUE ? "TRUE" : "FALSE"),
397  p_attr->p_cb_arg);
398 #else
399  fprintf(p_os,
400  "%*sULT attr: ["
401  "stack:%p "
402  "stacksize:%zu "
403  "stacktype:%s "
404  "]\n",
405  indent, "", p_attr->p_stack, p_attr->stacksize, stacktype);
406 #endif
407  }
408  fflush(p_os);
409 }
410 
412  ABTI_thread_attr **pp_dup_attr)
413 {
414  ABTI_thread_attr *p_dup_attr;
415  int abt_errno = ABTU_malloc(sizeof(ABTI_thread_attr), (void **)&p_dup_attr);
416  ABTI_CHECK_ERROR(abt_errno);
417 
418  memcpy(p_dup_attr, p_attr, sizeof(ABTI_thread_attr));
419  *pp_dup_attr = p_dup_attr;
420  return ABT_SUCCESS;
421 }
422 
423 /*****************************************************************************/
424 /* Internal static functions */
425 /*****************************************************************************/
426 
427 static void thread_attr_set_stack(ABTI_global *p_global,
428  ABTI_thread_attr *p_attr, void *stackaddr,
429  size_t stacksize)
430 {
431  /* Get the best thread type. */
432  ABTI_thread_type new_thread_type;
433  if (stackaddr != NULL) {
434  /* This check must be done by the caller. */
435  ABTI_ASSERT(((uintptr_t)stackaddr & 0x7) == 0);
436  /* Only a descriptor will be allocated from a memory pool. A stack
437  * is given by the user. */
438  new_thread_type = ABTI_THREAD_TYPE_MEM_MEMPOOL_DESC;
439  } else {
440  if (stacksize == p_global->thread_stacksize) {
441  /* Both a stack and a descriptor will be allocated from a memory
442  * pool. */
443  new_thread_type = ABTI_THREAD_TYPE_MEM_MEMPOOL_DESC_STACK;
444  } else {
445  /* The stack must be allocated by malloc(). Let's allocate both
446  * a stack and a descriptor together by a single malloc(). */
447  new_thread_type = ABTI_THREAD_TYPE_MEM_MALLOC_DESC_STACK;
448  }
449  }
450 
451  /* Unset the stack type and set new_thread_type. */
453  p_attr->thread_type |= new_thread_type;
454 
455  p_attr->p_stack = stackaddr;
456  p_attr->stacksize = stacksize;
457 }
ABT_bool
int ABT_bool
Boolean type.
Definition: abt.h:999
ABT_thread_attr
struct ABT_thread_attr_opaque * ABT_thread_attr
ULT attribute handle type.
Definition: abt.h:895
ABT_thread
struct ABT_thread_opaque * ABT_thread
Work unit handle type.
Definition: abt.h:888
ABTI_SETUP_GLOBAL
#define ABTI_SETUP_GLOBAL(pp_global)
Definition: abti_error.h:59
ABTI_THREAD_TYPE_MEM_MEMPOOL_DESC
#define ABTI_THREAD_TYPE_MEM_MEMPOOL_DESC
Definition: abti.h:87
ABTI_thread_attr::migratable
ABT_bool migratable
Definition: abti.h:371
ABTI_CHECK_ERROR
#define ABTI_CHECK_ERROR(abt_errno)
Definition: abti_error.h:120
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
ABTI_THREAD_TYPE_MEM_MEMPOOL_DESC_STACK
#define ABTI_THREAD_TYPE_MEM_MEMPOOL_DESC_STACK
Definition: abti.h:89
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:154
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:92
ABTI_CHECK_NULL_THREAD_ATTR_PTR
#define ABTI_CHECK_NULL_THREAD_ATTR_PTR(p)
Definition: abti_error.h:213
ABTI_thread_attr
Definition: abti.h:366
abti.h
ABTI_global::thread_stacksize
size_t thread_stacksize
Definition: abti.h:195
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:413
ABTI_HANDLE_ERROR
#define ABTI_HANDLE_ERROR(n)
Definition: abti_error.h:114
ABTU_malloc
static ABTU_ret_err int ABTU_malloc(size_t size, void **p_ptr)
Definition: abtu.h:218
ABTI_thread_attr_init
static void ABTI_thread_attr_init(ABTI_thread_attr *p_attr, void *p_stack, size_t stacksize, ABTI_thread_type thread_type, ABT_bool migratable)
Definition: abti_thread_attr.h:42
ABTI_thread_attr::p_cb_arg
void * p_cb_arg
Definition: abti.h:373
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:351
ABTI_ASSERT
#define ABTI_ASSERT(cond)
Definition: abti_error.h:12
ABT_THREAD_ATTR_NULL
#define ABT_THREAD_ATTR_NULL
Definition: abt.h:1061
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:306
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:138
ABTI_thread_attr::stacksize
size_t stacksize
Definition: abti.h:368
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:263
ABTI_THREAD_TYPES_MEM
#define ABTI_THREAD_TYPES_MEM
Definition: abti.h:92
ABTI_thread_type
uint32_t ABTI_thread_type
Definition: abti.h:123
ABT_TRUE
#define ABT_TRUE
True constant for ABT_bool.
Definition: abt.h:746
ABT_ERR_INV_ARG
#define ABT_ERR_INV_ARG
Error code: invalid user argument.
Definition: abt.h:250
ABTU_free
static void ABTU_free(void *ptr)
Definition: abtu.h:211
ABTI_thread_attr::p_stack
void * p_stack
Definition: abti.h:367
ABTI_thread_attr::thread_type
ABTI_thread_type thread_type
Definition: abti.h:369
ABTI_thread_attr::f_cb
void(* f_cb)(ABT_thread, void *)
Definition: abti.h:372
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:194
ABTI_CHECK_TRUE
#define ABTI_CHECK_TRUE(cond, abt_errno)
Definition: abti_error.h:130
ABTI_global
Definition: abti.h:182
ABT_ERR_FEATURE_NA
#define ABT_ERR_FEATURE_NA
Error code: unsupported feature.
Definition: abt.h:381
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:229
ABTI_thread_attr_print
void ABTI_thread_attr_print(ABTI_thread_attr *p_attr, FILE *p_os, int indent)
Definition: thread_attr.c:369
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:429
ABTI_THREAD_TYPE_MEM_MALLOC_DESC_STACK
#define ABTI_THREAD_TYPE_MEM_MALLOC_DESC_STACK
Definition: abti.h:90
ABTI_THREAD_TYPE_MEM_MALLOC_DESC
#define ABTI_THREAD_TYPE_MEM_MALLOC_DESC
Definition: abti.h:88