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 {
52  ABTI_UB_ASSERT(ABTI_initialized());
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 {
97  ABTI_UB_ASSERT(ABTI_initialized());
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);
102  ABTI_CHECK_NULL_THREAD_ATTR_PTR(p_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 {
161  ABTI_UB_ASSERT(ABTI_initialized());
162 
163  ABTI_global *p_global;
164  ABTI_SETUP_GLOBAL(&p_global);
165 
166  ABTI_thread_attr *p_attr = ABTI_thread_attr_get_ptr(attr);
167  ABTI_CHECK_NULL_THREAD_ATTR_PTR(p_attr);
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 {
203  ABTI_UB_ASSERT(ABTI_initialized());
204  ABTI_UB_ASSERT(stackaddr);
205  ABTI_UB_ASSERT(stacksize);
206 
207  ABTI_thread_attr *p_attr = ABTI_thread_attr_get_ptr(attr);
208  ABTI_CHECK_NULL_THREAD_ATTR_PTR(p_attr);
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 {
241  ABTI_UB_ASSERT(ABTI_initialized());
242 
243  ABTI_global *p_global;
244  ABTI_SETUP_GLOBAL(&p_global);
245 
246  ABTI_thread_attr *p_attr = ABTI_thread_attr_get_ptr(attr);
247  ABTI_CHECK_NULL_THREAD_ATTR_PTR(p_attr);
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 {
277  ABTI_UB_ASSERT(ABTI_initialized());
278  ABTI_UB_ASSERT(stacksize);
279 
280  ABTI_thread_attr *p_attr = ABTI_thread_attr_get_ptr(attr);
281  ABTI_CHECK_NULL_THREAD_ATTR_PTR(p_attr);
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 {
326  ABTI_UB_ASSERT(ABTI_initialized());
327 
328 #ifndef ABT_CONFIG_DISABLE_MIGRATION
329  ABTI_thread_attr *p_attr = ABTI_thread_attr_get_ptr(attr);
330  ABTI_CHECK_NULL_THREAD_ATTR_PTR(p_attr);
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
337  ABTI_HANDLE_ERROR(ABT_ERR_FEATURE_NA);
338 #endif
339 }
340 
369 {
370  ABTI_UB_ASSERT(ABTI_initialized());
371  ABTI_UB_ASSERT_BOOL(is_migratable);
372 
373 #ifndef ABT_CONFIG_DISABLE_MIGRATION
374  ABTI_thread_attr *p_attr = ABTI_thread_attr_get_ptr(attr);
375  ABTI_CHECK_NULL_THREAD_ATTR_PTR(p_attr);
376 
377  /* Set the value */
378  p_attr->migratable = is_migratable;
379  return ABT_SUCCESS;
380 #else
381  ABTI_HANDLE_ERROR(ABT_ERR_FEATURE_NA);
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 
417 ABTU_ret_err int ABTI_thread_attr_dup(const ABTI_thread_attr *p_attr,
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
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
ABT_thread_attr_free
int ABT_thread_attr_free(ABT_thread_attr *attr)
Free a ULT attribute.
Definition: thread_attr.c:95
abti.h
ABTU_malloc
static ABTU_ret_err int ABTU_malloc(size_t size, void **p_ptr)
Definition: abtu.h:235
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
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
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
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
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
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
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