ARGOBOTS  dce6e727ffc4ca5b3ffc04cb9517c6689be51ec5
abti_thread.h
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 #ifndef ABTI_THREAD_H_INCLUDED
7 #define ABTI_THREAD_H_INCLUDED
8 
10 {
11 #ifndef ABT_CONFIG_DISABLE_ERROR_CHECK
12  ABTI_thread *p_thread;
13  if (thread == ABT_THREAD_NULL || thread == ABT_TASK_NULL) {
14  p_thread = NULL;
15  } else {
16  p_thread = (ABTI_thread *)thread;
17  }
18  return p_thread;
19 #else
20  return (ABTI_thread *)thread;
21 #endif
22 }
23 
25 {
26 #ifndef ABT_CONFIG_DISABLE_ERROR_CHECK
27  ABT_thread h_thread;
28  if (p_thread == NULL) {
29  h_thread = ABT_THREAD_NULL;
30  } else {
31  h_thread = (ABT_thread)p_thread;
32  }
33  return h_thread;
34 #else
35  return (ABT_thread)p_thread;
36 #endif
37 }
38 
39 /* Inlined functions for User-level Thread (ULT) */
40 
42 {
43  if (type & ABTI_THREAD_TYPE_YIELDABLE) {
44  return ABT_UNIT_TYPE_THREAD;
45  } else if (type == ABTI_THREAD_TYPE_EXT) {
46  return ABT_UNIT_TYPE_EXT;
47  } else {
48  return ABT_UNIT_TYPE_TASK;
49  }
50 }
51 
53 {
54  ABTI_STATIC_ASSERT(offsetof(ABTI_ythread, thread) == 0);
55  return (ABTI_ythread *)p_thread;
56 }
57 
58 static inline ABTI_ythread *
60 {
61  if (p_thread->type & ABTI_THREAD_TYPE_YIELDABLE) {
62  return ABTI_thread_get_ythread(p_thread);
63  } else {
64  return NULL;
65  }
66 }
67 
68 static inline void ABTI_thread_set_request(ABTI_thread *p_thread, uint32_t req)
69 {
70  ABTD_atomic_fetch_or_uint32(&p_thread->request, req);
71 }
72 
73 static inline void ABTI_thread_unset_request(ABTI_thread *p_thread,
74  uint32_t req)
75 {
76  ABTD_atomic_fetch_and_uint32(&p_thread->request, ~req);
77 }
78 
79 #define ABTI_THREAD_HANDLE_REQUEST_NONE ((int)0x0)
80 #define ABTI_THREAD_HANDLE_REQUEST_CANCELLED ((int)0x1)
81 #define ABTI_THREAD_HANDLE_REQUEST_MIGRATED ((int)0x2)
82 
83 static inline int ABTI_thread_handle_request(ABTI_thread *p_thread,
84  ABT_bool allow_termination)
85 {
86 #if defined(ABT_CONFIG_DISABLE_CANCELLATION) && \
87  defined(ABT_CONFIG_DISABLE_MIGRATION)
89 #else
90  /* At least either cancellation or migration is enabled. */
91  const uint32_t request =
93 
94  /* Check cancellation request. */
95 #ifndef ABT_CONFIG_DISABLE_CANCELLATION
96  if (allow_termination && ABTU_unlikely(request & ABTI_THREAD_REQ_CANCEL)) {
98  p_thread->p_last_xstream, p_thread);
100  }
101 #endif /* !ABT_CONFIG_DISABLE_CANCELLATION */
102 
103  /* Check migration request. */
104 #ifndef ABT_CONFIG_DISABLE_MIGRATION
105  if (ABTU_unlikely(request & ABTI_THREAD_REQ_MIGRATE)) {
106  /* This is the case when the ULT requests migration of itself. */
107  int abt_errno =
110  p_thread->p_last_xstream),
111  p_thread);
112  if (abt_errno == ABT_SUCCESS) {
114  }
115  /* Migration failed. */
116  }
117 #endif /* !ABT_CONFIG_DISABLE_MIGRATION */
118 
120 #endif
121 }
122 
123 ABTU_ret_err static inline int
125  ABTI_ythread *p_ythread);
126 static inline void
128  ABTI_ythread *p_ythread);
129 
130 static inline void ABTI_thread_terminate(ABTI_global *p_global,
131  ABTI_xstream *p_local_xstream,
132  ABTI_thread *p_thread)
133 {
134  const ABTI_thread_type thread_type = p_thread->type;
137  ABTI_ythread *p_ythread = ABTI_thread_get_ythread(p_thread);
138  if (ABTD_ythread_context_has_stack(&p_ythread->ctx)) {
139  ABTI_mem_free_ythread_mempool_stack(p_local_xstream, p_ythread);
140  }
141  }
142  if (!(thread_type & ABTI_THREAD_TYPE_NAMED)) {
145  ABTI_thread_free(p_global, ABTI_xstream_get_local(p_local_xstream),
146  p_thread);
147  } else {
148  /* NOTE: We set the ULT's state as TERMINATED after checking refcount
149  * because the ULT can be freed on a different ES. In other words, we
150  * must not access any field of p_thead after changing the state to
151  * TERMINATED. */
154  }
155 }
156 
157 #endif /* ABTI_THREAD_H_INCLUDED */
ABT_THREAD_STATE_TERMINATED
@ ABT_THREAD_STATE_TERMINATED
Definition: abt.h:433
ABTI_THREAD_TYPE_MEM_MALLOC_DESC_MEMPOOL_LAZY_STACK
#define ABTI_THREAD_TYPE_MEM_MALLOC_DESC_MEMPOOL_LAZY_STACK
Definition: abti.h:112
ABT_bool
int ABT_bool
Boolean type.
Definition: abt.h:1043
ABTD_atomic_acquire_load_uint32
static uint32_t ABTD_atomic_acquire_load_uint32(const ABTD_atomic_uint32 *ptr)
Definition: abtd_atomic.h:928
ABT_thread
struct ABT_thread_opaque * ABT_thread
Work unit handle type.
Definition: abt.h:932
ABTI_thread::type
ABTI_thread_type type
Definition: abti.h:426
ABTI_global_get_global
static ABTI_global * ABTI_global_get_global(void)
Definition: abti_global.h:9
ABTI_THREAD_TYPE_YIELDABLE
#define ABTI_THREAD_TYPE_YIELDABLE
Definition: abti.h:87
ABTI_thread::request
ABTD_atomic_uint32 request
Definition: abti.h:433
ABTI_thread_get_ythread
static ABTI_ythread * ABTI_thread_get_ythread(ABTI_thread *p_thread)
Definition: abti_thread.h:52
ABTI_thread_free
void ABTI_thread_free(ABTI_global *p_global, ABTI_local *p_local, ABTI_thread *p_thread)
Definition: thread.c:2616
ABT_THREAD_NULL
#define ABT_THREAD_NULL
Definition: abt.h:1105
ABTI_thread_get_handle
static ABT_thread ABTI_thread_get_handle(ABTI_thread *p_thread)
Definition: abti_thread.h:24
ABTI_thread
Definition: abti.h:422
ABTI_xstream
Definition: abti.h:294
ABTI_mem_alloc_ythread_mempool_stack
static ABTU_ret_err int ABTI_mem_alloc_ythread_mempool_stack(ABTI_xstream *p_local_xstream, ABTI_ythread *p_ythread)
ABTI_THREAD_TYPE_EXT
#define ABTI_THREAD_TYPE_EXT
Definition: abti.h:82
ABTI_thread_type_get_type
static ABT_unit_type ABTI_thread_type_get_type(ABTI_thread_type type)
Definition: abti_thread.h:41
ABTI_THREAD_REQ_MIGRATE
#define ABTI_THREAD_REQ_MIGRATE
Definition: abti.h:50
ABTI_mem_free_ythread_mempool_stack
static void ABTI_mem_free_ythread_mempool_stack(ABTI_xstream *p_local_xstream, ABTI_ythread *p_ythread)
ABTI_thread_get_ythread_or_null
static ABTI_ythread * ABTI_thread_get_ythread_or_null(ABTI_thread *p_thread)
Definition: abti_thread.h:59
ABTI_thread::state
ABTD_atomic_int state
Definition: abti.h:432
ABT_unit_type
ABT_unit_type
Type of a work unit for scheduling.
Definition: abt.h:582
ABTI_THREAD_HANDLE_REQUEST_CANCELLED
#define ABTI_THREAD_HANDLE_REQUEST_CANCELLED
Definition: abti_thread.h:80
ABTI_thread_handle_request_migrate
ABTU_ret_err int ABTI_thread_handle_request_migrate(ABTI_global *p_global, ABTI_local *p_local, ABTI_thread *p_thread)
Definition: thread.c:2674
ABTI_ythread::ctx
ABTD_ythread_context ctx
Definition: abti.h:458
ABTI_STATIC_ASSERT
#define ABTI_STATIC_ASSERT(cond)
Definition: abti_error.h:35
ABT_SUCCESS
#define ABT_SUCCESS
Error code: the routine returns successfully.
Definition: abt.h:92
ABT_TASK_NULL
#define ABT_TASK_NULL
Definition: abt.h:1107
ABTU_ret_err
#define ABTU_ret_err
Definition: abtu.h:155
ABTI_THREAD_HANDLE_REQUEST_MIGRATED
#define ABTI_THREAD_HANDLE_REQUEST_MIGRATED
Definition: abti_thread.h:81
ABTI_THREAD_REQ_CANCEL
#define ABTI_THREAD_REQ_CANCEL
Definition: abti.h:49
ABTI_xstream_get_local
static ABTI_local * ABTI_xstream_get_local(ABTI_xstream *p_xstream)
Definition: abti_stream.h:48
ABTI_thread_type
uint32_t ABTI_thread_type
Definition: abti.h:152
ABTU_unlikely
#define ABTU_unlikely(cond)
Definition: abtu.h:120
ABTI_ythread
Definition: abti.h:456
ABTD_atomic_fetch_and_uint32
static uint32_t ABTD_atomic_fetch_and_uint32(ABTD_atomic_uint32 *ptr, uint32_t v)
Definition: abtd_atomic.h:573
ABT_UNIT_TYPE_THREAD
@ ABT_UNIT_TYPE_THREAD
Definition: abt.h:584
ABTI_thread_handle_request_cancel
void ABTI_thread_handle_request_cancel(ABTI_global *p_global, ABTI_xstream *p_local_xstream, ABTI_thread *p_thread)
Definition: thread.c:2660
ABTD_atomic_fetch_or_uint32
static uint32_t ABTD_atomic_fetch_or_uint32(ABTD_atomic_uint32 *ptr, uint32_t v)
Definition: abtd_atomic.h:631
ABT_UNIT_TYPE_TASK
@ ABT_UNIT_TYPE_TASK
Definition: abt.h:590
ABTI_THREAD_TYPE_NAMED
#define ABTI_THREAD_TYPE_NAMED
Definition: abti.h:88
ABTI_thread::p_last_xstream
ABTI_xstream * p_last_xstream
Definition: abti.h:428
ABTI_thread_unset_request
static void ABTI_thread_unset_request(ABTI_thread *p_thread, uint32_t req)
Definition: abti_thread.h:73
ABTI_thread_handle_request
static int ABTI_thread_handle_request(ABTI_thread *p_thread, ABT_bool allow_termination)
Definition: abti_thread.h:83
ABTI_global
Definition: abti.h:223
ABTI_THREAD_TYPE_MEM_MEMPOOL_DESC_MEMPOOL_LAZY_STACK
#define ABTI_THREAD_TYPE_MEM_MEMPOOL_DESC_MEMPOOL_LAZY_STACK
Definition: abti.h:107
ABTI_THREAD_HANDLE_REQUEST_NONE
#define ABTI_THREAD_HANDLE_REQUEST_NONE
Definition: abti_thread.h:79
ABTD_ythread_context_has_stack
static ABT_bool ABTD_ythread_context_has_stack(const ABTD_ythread_context *p_ctx)
Definition: abtd_fcontext.h:124
ABTD_atomic_release_store_int
static void ABTD_atomic_release_store_int(ABTD_atomic_int *ptr, int val)
Definition: abtd_atomic.h:1065
ABTI_thread_get_ptr
static ABTI_thread * ABTI_thread_get_ptr(ABT_thread thread)
Definition: abti_thread.h:9
ABT_UNIT_TYPE_EXT
@ ABT_UNIT_TYPE_EXT
Definition: abt.h:594
ABTI_thread_terminate
static void ABTI_thread_terminate(ABTI_global *p_global, ABTI_xstream *p_local_xstream, ABTI_thread *p_thread)
Definition: abti_thread.h:130
ABTI_thread_set_request
static void ABTI_thread_set_request(ABTI_thread *p_thread, uint32_t req)
Definition: abti_thread.h:68