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 
9 static inline ABTI_thread *ABTI_thread_get_ptr(ABT_thread thread)
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 
24 static inline ABT_thread ABTI_thread_get_handle(ABTI_thread *p_thread)
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 
41 static inline ABT_unit_type ABTI_thread_type_get_type(ABTI_thread_type type)
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 
52 static inline ABTI_ythread *ABTI_thread_get_ythread(ABTI_thread *p_thread)
53 {
54  ABTI_STATIC_ASSERT(offsetof(ABTI_ythread, thread) == 0);
55  return (ABTI_ythread *)p_thread;
56 }
57 
58 static inline ABTI_ythread *
59 ABTI_thread_get_ythread_or_null(ABTI_thread *p_thread)
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)
88  return ABTI_THREAD_HANDLE_REQUEST_NONE;
89 #else
90  /* At least either cancellation or migration is enabled. */
91  const uint32_t request =
92  ABTD_atomic_acquire_load_uint32(&p_thread->request);
93 
94  /* Check cancellation request. */
95 #ifndef ABT_CONFIG_DISABLE_CANCELLATION
96  if (allow_termination && ABTU_unlikely(request & ABTI_THREAD_REQ_CANCEL)) {
97  ABTI_thread_handle_request_cancel(ABTI_global_get_global(),
98  p_thread->p_last_xstream, p_thread);
99  return ABTI_THREAD_HANDLE_REQUEST_CANCELLED;
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 =
108  ABTI_thread_handle_request_migrate(ABTI_global_get_global(),
109  ABTI_xstream_get_local(
110  p_thread->p_last_xstream),
111  p_thread);
112  if (abt_errno == ABT_SUCCESS) {
113  return ABTI_THREAD_HANDLE_REQUEST_MIGRATED;
114  }
115  /* Migration failed. */
116  }
117 #endif /* !ABT_CONFIG_DISABLE_MIGRATION */
118 
119  return ABTI_THREAD_HANDLE_REQUEST_NONE;
120 #endif
121 }
122 
123 ABTU_ret_err static inline int
124 ABTI_mem_alloc_ythread_mempool_stack(ABTI_xstream *p_local_xstream,
125  ABTI_ythread *p_ythread);
126 static inline void
127 ABTI_mem_free_ythread_mempool_stack(ABTI_xstream *p_local_xstream,
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;
135  if (thread_type & (ABTI_THREAD_TYPE_MEM_MEMPOOL_DESC_MEMPOOL_LAZY_STACK |
136  ABTI_THREAD_TYPE_MEM_MALLOC_DESC_MEMPOOL_LAZY_STACK)) {
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)) {
143  ABTD_atomic_release_store_int(&p_thread->state,
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. */
152  ABTD_atomic_release_store_int(&p_thread->state,
154  }
155 }
156 
157 #endif /* ABTI_THREAD_H_INCLUDED */
ABT_THREAD_STATE_TERMINATED
@ ABT_THREAD_STATE_TERMINATED
Definition: abt.h:433
ABT_bool
int ABT_bool
Boolean type.
Definition: abt.h:1043
ABT_thread
struct ABT_thread_opaque * ABT_thread
Work unit handle type.
Definition: abt.h:932
ABT_THREAD_NULL
#define ABT_THREAD_NULL
Definition: abt.h:1105
ABT_unit_type
ABT_unit_type
Type of a work unit for scheduling.
Definition: abt.h:582
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
ABTU_unlikely
#define ABTU_unlikely(cond)
Definition: abtu.h:120
ABT_UNIT_TYPE_THREAD
@ ABT_UNIT_TYPE_THREAD
Definition: abt.h:584
ABT_UNIT_TYPE_TASK
@ ABT_UNIT_TYPE_TASK
Definition: abt.h:590
ABT_UNIT_TYPE_EXT
@ ABT_UNIT_TYPE_EXT
Definition: abt.h:594