ARGOBOTS
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 
27 {
28  int abt_errno = ABT_SUCCESS;
29  ABTI_thread_attr *p_newattr;
30 
31  p_newattr = (ABTI_thread_attr *)ABTU_malloc(sizeof(ABTI_thread_attr));
32 
33  /* Default values */
34  ABTI_thread_attr_init(p_newattr, NULL, ABTI_global_get_thread_stacksize(),
35  ABTI_STACK_TYPE_MEMPOOL, ABT_TRUE);
36 
37  /* Return value */
38  *newattr = ABTI_thread_attr_get_handle(p_newattr);
39 
40  return abt_errno;
41 }
42 
56 {
57  int abt_errno = ABT_SUCCESS;
58  ABT_thread_attr h_attr = *attr;
59  ABTI_thread_attr *p_attr = ABTI_thread_attr_get_ptr(h_attr);
60  ABTI_CHECK_NULL_THREAD_ATTR_PTR(p_attr);
61 
62  /* Free the memory */
63  ABTU_free(p_attr);
64 
65  /* Return value */
66  *attr = ABT_THREAD_ATTR_NULL;
67 
68 fn_exit:
69  return abt_errno;
70 
71 fn_fail:
72  HANDLE_ERROR_FUNC_WITH_CODE(abt_errno);
73  goto fn_exit;
74 }
75 
97 int ABT_thread_attr_set_stack(ABT_thread_attr attr, void *stackaddr,
98  size_t stacksize)
99 {
100  int abt_errno = ABT_SUCCESS;
101  ABTI_thread_attr *p_attr = ABTI_thread_attr_get_ptr(attr);
102  ABTI_CHECK_NULL_THREAD_ATTR_PTR(p_attr);
103 
104  if (stackaddr != NULL) {
105  if (((uintptr_t)stackaddr & 0x7) != 0) {
106  abt_errno = ABT_ERR_OTHER;
107  goto fn_fail;
108  }
109  p_attr->p_stack = stackaddr;
110  p_attr->stacktype = ABTI_STACK_TYPE_USER;
111  } else {
112  p_attr->stacktype = ABTI_STACK_TYPE_MALLOC;
113  }
114  p_attr->stacksize = stacksize;
115 
116 fn_exit:
117  return abt_errno;
118 
119 fn_fail:
120  HANDLE_ERROR_FUNC_WITH_CODE(abt_errno);
121  goto fn_exit;
122 }
123 
140 int ABT_thread_attr_get_stack(ABT_thread_attr attr, void **stackaddr,
141  size_t *stacksize)
142 {
143  int abt_errno = ABT_SUCCESS;
144  ABTI_thread_attr *p_attr = ABTI_thread_attr_get_ptr(attr);
145  ABTI_CHECK_NULL_THREAD_ATTR_PTR(p_attr);
146 
147  *stackaddr = p_attr->p_stack;
148  *stacksize = p_attr->stacksize;
149 
150 fn_exit:
151  return abt_errno;
152 
153 fn_fail:
154  HANDLE_ERROR_FUNC_WITH_CODE(abt_errno);
155  goto fn_exit;
156 }
157 
171 {
172  int abt_errno = ABT_SUCCESS;
173  ABTI_thread_attr *p_attr = ABTI_thread_attr_get_ptr(attr);
174  ABTI_CHECK_NULL_THREAD_ATTR_PTR(p_attr);
175 
176  /* Set the value */
177  p_attr->stacksize = stacksize;
178 
179 fn_exit:
180  return abt_errno;
181 
182 fn_fail:
183  HANDLE_ERROR_FUNC_WITH_CODE(abt_errno);
184  goto fn_exit;
185 }
186 
199 int ABT_thread_attr_get_stacksize(ABT_thread_attr attr, size_t *stacksize)
200 {
201  int abt_errno = ABT_SUCCESS;
202  ABTI_thread_attr *p_attr = ABTI_thread_attr_get_ptr(attr);
203  ABTI_CHECK_NULL_THREAD_ATTR_PTR(p_attr);
204 
205  *stacksize = p_attr->stacksize;
206 
207 fn_exit:
208  return abt_errno;
209 
210 fn_fail:
211  HANDLE_ERROR_FUNC_WITH_CODE(abt_errno);
212  goto fn_exit;
213 }
214 
229  void (*cb_func)(ABT_thread thread,
230  void *cb_arg),
231  void *cb_arg)
232 {
233 #ifndef ABT_CONFIG_DISABLE_MIGRATION
234  int abt_errno = ABT_SUCCESS;
235  ABTI_thread_attr *p_attr = ABTI_thread_attr_get_ptr(attr);
236  ABTI_CHECK_NULL_THREAD_ATTR_PTR(p_attr);
237 
238  /* Set the value */
239  p_attr->f_cb = cb_func;
240  p_attr->p_cb_arg = cb_arg;
241 
242 fn_exit:
243  return abt_errno;
244 
245 fn_fail:
246  HANDLE_ERROR_FUNC_WITH_CODE(abt_errno);
247  goto fn_exit;
248 #else
249  return ABT_ERR_FEATURE_NA;
250 #endif
251 }
252 
270 {
271 #ifndef ABT_CONFIG_DISABLE_MIGRATION
272  int abt_errno = ABT_SUCCESS;
273  ABTI_thread_attr *p_attr = ABTI_thread_attr_get_ptr(attr);
274  ABTI_CHECK_NULL_THREAD_ATTR_PTR(p_attr);
275 
276  /* Set the value */
277  p_attr->migratable = flag;
278 
279 fn_exit:
280  return abt_errno;
281 
282 fn_fail:
283  HANDLE_ERROR_FUNC_WITH_CODE(abt_errno);
284  goto fn_exit;
285 #else
286  return ABT_ERR_FEATURE_NA;
287 #endif
288 }
289 
290 /*****************************************************************************/
291 /* Private APIs */
292 /*****************************************************************************/
293 
294 /*
295  *
296  */
297 
311 void ABTI_thread_attr_print(ABTI_thread_attr *p_attr, FILE *p_os, int indent)
312 {
313  char *prefix = ABTU_get_indent_str(indent);
314  char attr[100];
315 
316  ABTI_thread_attr_get_str(p_attr, attr);
317  fprintf(p_os, "%sULT attr: %s\n", prefix, attr);
318  fflush(p_os);
319  ABTU_free(prefix);
320 }
321 
322 void ABTI_thread_attr_get_str(ABTI_thread_attr *p_attr, char *p_buf)
323 {
324  if (p_attr == NULL) {
325  sprintf(p_buf, "[NULL ATTR]");
326  return;
327  }
328 
329  char *stacktype;
330  switch (p_attr->stacktype) {
331  case ABTI_STACK_TYPE_MEMPOOL:
332  stacktype = "MEMPOOL";
333  break;
334  case ABTI_STACK_TYPE_MALLOC:
335  stacktype = "MALLOC";
336  break;
337  case ABTI_STACK_TYPE_USER:
338  stacktype = "USER";
339  break;
340  case ABTI_STACK_TYPE_MAIN:
341  stacktype = "MAIN";
342  break;
343  default:
344  stacktype = "UNKNOWN";
345  break;
346  }
347 
348 #ifndef ABT_CONFIG_DISABLE_MIGRATION
349  sprintf(p_buf,
350  "["
351  "stack:%p "
352  "stacksize:%zu "
353  "stacktype:%s "
354  "migratable:%s "
355  "cb_arg:%p"
356  "]",
357  p_attr->p_stack, p_attr->stacksize, stacktype,
358  (p_attr->migratable == ABT_TRUE ? "TRUE" : "FALSE"),
359  p_attr->p_cb_arg);
360 #else
361  sprintf(p_buf,
362  "["
363  "stack:%p "
364  "stacksize:%zu "
365  "stacktype:%s "
366  "]",
367  p_attr->p_stack, p_attr->stacksize, stacktype);
368 #endif
369 }
370 
371 ABTI_thread_attr *ABTI_thread_attr_dup(ABTI_thread_attr *p_attr)
372 {
373  ABTI_thread_attr *p_dupattr;
374 
375  p_dupattr = (ABTI_thread_attr *)ABTU_malloc(sizeof(ABTI_thread_attr));
376  ABTI_thread_attr_copy(p_dupattr, p_attr);
377 
378  return p_dupattr;
379 }
struct ABT_thread_attr_opaque * ABT_thread_attr
Definition: abt.h:281
char * ABTU_get_indent_str(int indent)
Definition: util.c:12
int ABT_thread_attr_set_stacksize(ABT_thread_attr attr, size_t stacksize)
Set the stack size in the attribute object.
Definition: thread_attr.c:170
int ABT_thread_attr_set_stack(ABT_thread_attr attr, void *stackaddr, size_t stacksize)
Set stack attributes.
Definition: thread_attr.c:97
int ABT_thread_attr_set_migratable(ABT_thread_attr attr, ABT_bool flag)
Set the ULT's migratability in the attribute object.
Definition: thread_attr.c:269
int ABT_thread_attr_get_stacksize(ABT_thread_attr attr, size_t *stacksize)
Get the stack size from the attribute object.
Definition: thread_attr.c:199
int ABT_thread_attr_set_callback(ABT_thread_attr attr, void(*cb_func)(ABT_thread thread, void *cb_arg), void *cb_arg)
Set callback function and its argument in the attribute object.
Definition: thread_attr.c:228
static void * ABTU_malloc(size_t size)
Definition: abtu.h:39
int ABT_bool
Definition: abt.h:309
#define ABT_ERR_OTHER
Definition: abt.h:67
int ABT_thread_attr_create(ABT_thread_attr *newattr)
Create a new ULT attribute object.
Definition: thread_attr.c:26
struct ABT_thread_opaque * ABT_thread
Definition: abt.h:279
#define HANDLE_ERROR_FUNC_WITH_CODE(n)
Definition: abti_error.h:241
#define ABT_SUCCESS
Definition: abt.h:64
#define ABT_TRUE
Definition: abt.h:223
#define ABT_THREAD_ATTR_NULL
Definition: abt.h:345
int ABT_thread_attr_get_stack(ABT_thread_attr attr, void **stackaddr, size_t *stacksize)
Get stack attributes.
Definition: thread_attr.c:140
int ABT_thread_attr_free(ABT_thread_attr *attr)
Free the ULT attribute object.
Definition: thread_attr.c:55
#define ABT_ERR_FEATURE_NA
Definition: abt.h:115
static void ABTU_free(void *ptr)
Definition: abtu.h:32