ARGOBOTS  66b1c39742507d8df30e8d28c54839b961a14814
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
task.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 ABTU_ret_err static int task_create(ABTI_local *p_local, ABTI_pool *p_pool,
9  void (*task_func)(void *), void *arg,
10  ABTI_sched *p_sched, int refcount,
11  ABTI_thread **pp_newtask);
12 
40 int ABT_task_create(ABT_pool pool, void (*task_func)(void *), void *arg,
41  ABT_task *newtask)
42 {
43  ABTI_local *p_local = ABTI_local_get_local();
44  ABTI_thread *p_newtask;
45  ABTI_pool *p_pool = ABTI_pool_get_ptr(pool);
46  ABTI_CHECK_NULL_POOL_PTR(p_pool);
47 
48  int refcount = (newtask != NULL) ? 1 : 0;
49  int abt_errno = task_create(p_local, p_pool, task_func, arg, NULL, refcount,
50  &p_newtask);
51  ABTI_CHECK_ERROR(abt_errno);
52 
53  /* Return value */
54  if (newtask)
55  *newtask = ABTI_thread_get_handle(p_newtask);
56  return ABT_SUCCESS;
57 }
58 
90 int ABT_task_create_on_xstream(ABT_xstream xstream, void (*task_func)(void *),
91  void *arg, ABT_task *newtask)
92 {
93  ABTI_local *p_local = ABTI_local_get_local();
94  ABTI_thread *p_newtask;
95 
96  ABTI_xstream *p_xstream = ABTI_xstream_get_ptr(xstream);
97  ABTI_CHECK_NULL_XSTREAM_PTR(p_xstream);
98 
99  /* TODO: need to consider the access type of target pool */
100  ABTI_pool *p_pool = ABTI_xstream_get_main_pool(p_xstream);
101  int refcount = (newtask != NULL) ? 1 : 0;
102  int abt_errno = task_create(p_local, p_pool, task_func, arg, NULL, refcount,
103  &p_newtask);
104  ABTI_CHECK_ERROR(abt_errno);
105 
106  /* Return value */
107  if (newtask)
108  *newtask = ABTI_thread_get_handle(p_newtask);
109  return ABT_SUCCESS;
110 }
111 
112 #ifdef ABT_CONFIG_USE_DOXYGEN
113 
131 int ABT_task_revive(ABT_pool pool, void (*task_func)(void *), void *arg,
132  ABT_task *task);
133 #endif
134 
135 #ifdef ABT_CONFIG_USE_DOXYGEN
136 
149 int ABT_task_free(ABT_task *task);
150 #endif
151 
152 #ifdef ABT_CONFIG_USE_DOXYGEN
153 
165 int ABT_task_join(ABT_task task);
166 #endif
167 
168 #ifdef ABT_CONFIG_USE_DOXYGEN
169 
177 int ABT_task_cancel(ABT_task task);
178 #endif
179 
198 {
199  *task = ABT_TASK_NULL;
200 
201  ABTI_xstream *p_local_xstream;
202  ABTI_SETUP_LOCAL_XSTREAM_WITH_INIT_CHECK(&p_local_xstream);
203 
204  ABTI_thread *p_thread = p_local_xstream->p_thread;
205  if (p_thread->type & ABTI_THREAD_TYPE_YIELDABLE) {
206  return ABT_ERR_INV_THREAD;
207  } else {
208  *task = ABTI_thread_get_handle(p_thread);
209  }
210  return ABT_SUCCESS;
211 }
212 
227 {
228  ABTI_xstream *p_local_xstream;
229  ABTI_SETUP_LOCAL_XSTREAM_WITH_INIT_CHECK(&p_local_xstream);
230 
231  ABTI_thread *p_thread = p_local_xstream->p_thread;
232  ABTI_CHECK_TRUE(!(p_thread->type & ABTI_THREAD_TYPE_YIELDABLE),
234  *id = ABTI_thread_get_id(p_thread);
235  return ABT_SUCCESS;
236 }
237 
238 #ifdef ABT_CONFIG_USE_DOXYGEN
239 
252 int ABT_task_get_xstream(ABT_task task, ABT_xstream *xstream);
253 #endif
254 
255 #ifdef ABT_CONFIG_USE_DOXYGEN
256 
265 int ABT_task_get_state(ABT_task task, ABT_task_state *state);
266 #endif
267 
268 #ifdef ABT_CONFIG_USE_DOXYGEN
269 
281 int ABT_task_get_last_pool(ABT_task task, ABT_pool *pool);
282 #endif
283 
284 #ifdef ABT_CONFIG_USE_DOXYGEN
285 
299 int ABT_task_get_last_pool_id(ABT_task task, int *id);
300 #endif
301 
302 #ifdef ABT_CONFIG_USE_DOXYGEN
303 
320 #endif
321 
322 #ifdef ABT_CONFIG_USE_DOXYGEN
323 
337 int ABT_task_is_migratable(ABT_task task, ABT_bool *flag);
338 #endif
339 
340 #ifdef ABT_CONFIG_USE_DOXYGEN
341 
355 int ABT_task_is_unnamed(ABT_task task, ABT_bool *flag);
356 #endif
357 
358 #ifdef ABT_CONFIG_USE_DOXYGEN
359 
374 int ABT_task_equal(ABT_task task1, ABT_task task2, ABT_bool *result);
375 #endif
376 
377 #ifdef ABT_CONFIG_USE_DOXYGEN
378 
389 int ABT_task_get_id(ABT_task task, ABT_unit_id *task_id);
390 #endif
391 
392 #ifdef ABT_CONFIG_USE_DOXYGEN
393 
405 int ABT_task_get_arg(ABT_task task, void **arg);
406 #endif
407 
408 #ifdef ABT_CONFIG_USE_DOXYGEN
409 
422 int ABT_task_set_specific(ABT_task task, ABT_key key, void *value);
423 #endif
424 
425 #ifdef ABT_CONFIG_USE_DOXYGEN
426 
441 int ABT_task_get_specific(ABT_task task, ABT_key key, void **value);
442 #endif
443 
444 /*****************************************************************************/
445 /* Internal static functions */
446 /*****************************************************************************/
447 
448 ABTU_ret_err static int task_create(ABTI_local *p_local, ABTI_pool *p_pool,
449  void (*task_func)(void *), void *arg,
450  ABTI_sched *p_sched, int refcount,
451  ABTI_thread **pp_newtask)
452 {
453  ABTI_thread *p_newtask;
454  ABT_task h_newtask;
455 
456  /* Allocate a task object */
457  int abt_errno = ABTI_mem_alloc_nythread(p_local, &p_newtask);
458  ABTI_CHECK_ERROR(abt_errno);
459 
460  p_newtask->p_last_xstream = NULL;
461  p_newtask->p_parent = NULL;
462  ABTD_atomic_relaxed_store_int(&p_newtask->state, ABT_THREAD_STATE_READY);
463  ABTD_atomic_relaxed_store_uint32(&p_newtask->request, 0);
464  p_newtask->f_thread = task_func;
465  p_newtask->p_arg = arg;
466  p_newtask->p_pool = p_pool;
467  ABTD_atomic_relaxed_store_ptr(&p_newtask->p_keytable, NULL);
468  p_newtask->id = ABTI_TASK_INIT_ID;
469 
470  /* Create a wrapper work unit */
471  h_newtask = ABTI_thread_get_handle(p_newtask);
472  ABTI_thread_type thread_type =
473  refcount ? (ABTI_THREAD_TYPE_THREAD | ABTI_THREAD_TYPE_NAMED)
474  : ABTI_THREAD_TYPE_THREAD;
475 #ifndef ABT_CONFIG_DISABLE_MIGRATION
476  thread_type |= ABTI_THREAD_TYPE_MIGRATABLE;
477 #endif
478  p_newtask->type |= thread_type;
479  p_newtask->unit = p_pool->u_create_from_task(h_newtask);
480 
481  ABTI_tool_event_thread_create(p_local, p_newtask,
482  ABTI_local_get_xstream_or_null(p_local)
483  ? ABTI_local_get_xstream(p_local)
484  ->p_thread
485  : NULL,
486  p_pool);
487  LOG_DEBUG("[T%" PRIu64 "] created\n", ABTI_thread_get_id(p_newtask));
488 
489  /* Add this task to the scheduler's pool */
490  ABTI_pool_push(p_pool, p_newtask->unit);
491 
492  /* Return value */
493  *pp_newtask = p_newtask;
494 
495  return ABT_SUCCESS;
496 }
uint64_t ABT_unit_id
Definition: abt.h:341
struct ABT_xstream_opaque * ABT_xstream
Definition: abt.h:313
int ABT_task_set_specific(ABT_task task, ABT_key key, void *value)
Set the tasklet-specific value associated with the key.
static ABTU_ret_err int task_create(ABTI_local *p_local, ABTI_pool *p_pool, void(*task_func)(void *), void *arg, ABTI_sched *p_sched, int refcount, ABTI_thread **pp_newtask)
Definition: task.c:448
int ABT_task_cancel(ABT_task task)
Request the cancellation of the target task.
struct ABT_thread_opaque * ABT_task
Definition: abt.h:353
#define ABT_ERR_INV_THREAD
Definition: abt.h:80
int ABT_task_create(ABT_pool pool, void(*task_func)(void *), void *arg, ABT_task *newtask) ABT_API_PUBLIC
Create a new task and return its handle through newtask.
Definition: task.c:40
int ABT_bool
Definition: abt.h:373
int ABT_task_join(ABT_task task)
Wait for the tasklet to terminate.
int ABT_task_get_specific(ABT_task task, ABT_key key, void **value)
Get the tasklet-specific value associated with the key.
int ABT_task_self(ABT_task *task) ABT_API_PUBLIC
Return the handle of the calling tasklet.
Definition: task.c:197
struct ABT_pool_opaque * ABT_pool
Definition: abt.h:329
struct ABT_key_opaque * ABT_key
Definition: abt.h:355
int ABT_task_create_on_xstream(ABT_xstream xstream, void(*task_func)(void *), void *arg, ABT_task *newtask) ABT_API_PUBLIC
Create a new tasklet associated with the target ES (xstream).
Definition: task.c:90
enum ABT_thread_state ABT_task_state
Definition: abt.h:349
int ABT_task_equal(ABT_task task1, ABT_task task2, ABT_bool *result)
Compare two tasklet handles for equality.
#define ABT_SUCCESS
Definition: abt.h:64
int ABT_task_get_last_pool(ABT_task task, ABT_pool *pool)
Return the last pool of task.
int ABT_task_set_migratable(ABT_task task, ABT_bool flag)
Set the tasklet's migratability.
int ABT_task_self_id(ABT_unit_id *id) ABT_API_PUBLIC
Return the ID of the calling tasklet.
Definition: task.c:226
int ABT_task_get_arg(ABT_task task, void **arg)
Retrieve the argument for the tasklet function.
int ABT_task_get_id(ABT_task task, ABT_unit_id *task_id)
Get the tasklet's id.
int ABT_task_get_state(ABT_task task, ABT_task_state *state)
Return the state of task.
#define LOG_DEBUG(fmt,...)
Definition: abti_log.h:26
int ABT_task_get_last_pool_id(ABT_task task, int *id)
Get the last pool's ID of the tasklet.
int ABT_task_free(ABT_task *task)
Release the task object associated with task handle.
int ABT_task_is_unnamed(ABT_task task, ABT_bool *flag)
Check if the target task is unnamed.
#define ABT_TASK_NULL
Definition: abt.h:429
int ABT_task_is_migratable(ABT_task task, ABT_bool *flag)
Get the tasklet's migratability.
int ABT_task_get_xstream(ABT_task task, ABT_xstream *xstream)
Get the ES associated with the target tasklet.
#define ABTU_ret_err
Definition: abtu.h:49
int ABT_task_revive(ABT_pool pool, void(*task_func)(void *), void *arg, ABT_task *task)
Revive the tasklet.