6 #ifndef ABTI_UNIT_H_INCLUDED
7 #define ABTI_UNIT_H_INCLUDED
10 #define ABTI_UNIT_BUILTIN_POOL_BIT ((uintptr_t)0x1)
14 if (((uintptr_t)unit) & ABTI_UNIT_BUILTIN_POOL_BIT) {
23 static inline ABT_unit ABTI_unit_get_builtin_unit(ABTI_thread *p_thread)
25 ABTI_ASSERT(!(((uintptr_t)p_thread) & ABTI_UNIT_BUILTIN_POOL_BIT));
26 return (
ABT_unit)(((uintptr_t)p_thread) | ABTI_UNIT_BUILTIN_POOL_BIT);
29 static inline void ABTI_unit_init_builtin(ABTI_thread *p_thread)
31 p_thread->p_prev = NULL;
32 p_thread->p_next = NULL;
33 ABTD_atomic_relaxed_store_int(&p_thread->is_in_pool, 0);
34 p_thread->unit = ABTI_unit_get_builtin_unit(p_thread);
37 static inline ABTI_thread *ABTI_unit_get_thread_from_builtin_unit(
ABT_unit unit)
39 ABTI_ASSERT(ABTI_unit_is_builtin(unit));
40 return (ABTI_thread *)(((uintptr_t)unit) & (~ABTI_UNIT_BUILTIN_POOL_BIT));
43 static inline ABTI_thread *ABTI_unit_get_thread(ABTI_global *p_global,
48 return ABTI_unit_get_thread_from_builtin_unit(unit);
50 return ABTI_unit_get_thread_from_user_defined_unit(p_global, unit);
55 ABTI_unit_set_associated_pool(ABTI_global *p_global,
ABT_unit unit,
56 ABTI_pool *p_pool, ABTI_thread **pp_thread)
59 ABTI_thread *p_thread = ABTI_unit_get_thread_from_builtin_unit(unit);
63 p_thread->p_pool = p_pool;
64 *pp_thread = p_thread;
68 ABT_pool pool = ABTI_pool_get_handle(p_pool);
70 p_pool->required_def.p_create_unit(pool, ABTI_thread_get_handle(
74 int ret = ABTI_unit_map_thread(p_global, new_unit, p_thread);
76 p_pool->required_def.p_free_unit(pool, new_unit);
79 p_thread->unit = new_unit;
80 p_thread->p_pool = p_pool;
81 *pp_thread = p_thread;
86 ABTI_thread *p_thread =
87 ABTI_unit_get_thread_from_user_defined_unit(p_global, unit);
88 if (p_pool->is_builtin) {
91 ABTI_unit_unmap_thread(p_global, unit);
92 ABT_pool old_pool = ABTI_pool_get_handle(p_thread->p_pool);
93 p_thread->p_pool->required_def.p_free_unit(old_pool, unit);
94 ABTI_unit_init_builtin(p_thread);
95 p_thread->p_pool = p_pool;
96 *pp_thread = p_thread;
98 }
else if (p_thread->p_pool == p_pool) {
100 *pp_thread = p_thread;
104 ABT_pool pool = ABTI_pool_get_handle(p_pool);
106 p_pool->required_def.p_create_unit(pool, ABTI_thread_get_handle(
110 int ret = ABTI_unit_map_thread(p_global, new_unit, p_thread);
112 p_pool->required_def.p_free_unit(pool, new_unit);
115 ABTI_unit_unmap_thread(p_global, unit);
116 ABT_pool old_pool = ABTI_pool_get_handle(p_thread->p_pool);
117 p_thread->p_pool->required_def.p_free_unit(old_pool, unit);
118 p_thread->unit = new_unit;
119 p_thread->p_pool = p_pool;
120 *pp_thread = p_thread;
128 ABTU_ret_err static inline int ABTI_thread_init_pool(ABTI_global *p_global,
129 ABTI_thread *p_thread,
133 ABTI_unit_init_builtin(p_thread);
134 p_thread->p_pool = p_pool;
137 ABT_pool pool = ABTI_pool_get_handle(p_pool);
139 p_pool->required_def.p_create_unit(pool, ABTI_thread_get_handle(
143 int ret = ABTI_unit_map_thread(p_global, new_unit, p_thread);
145 p_pool->required_def.p_free_unit(pool, new_unit);
148 p_thread->unit = new_unit;
149 p_thread->p_pool = p_pool;
155 ABTI_thread_set_associated_pool(ABTI_global *p_global, ABTI_thread *p_thread,
159 if (
ABTU_likely(ABTI_unit_is_builtin(unit) && p_pool->is_builtin)) {
162 p_thread->p_pool = p_pool;
164 }
else if (ABTI_unit_is_builtin(unit)) {
166 ABT_pool pool = ABTI_pool_get_handle(p_pool);
168 p_pool->required_def.p_create_unit(pool, ABTI_thread_get_handle(
172 int ret = ABTI_unit_map_thread(p_global, new_unit, p_thread);
174 p_pool->required_def.p_free_unit(pool, new_unit);
177 p_thread->unit = new_unit;
178 p_thread->p_pool = p_pool;
180 }
else if (p_pool->is_builtin) {
183 ABTI_unit_unmap_thread(p_global, unit);
184 ABT_pool old_pool = ABTI_pool_get_handle(p_thread->p_pool);
185 p_thread->p_pool->required_def.p_free_unit(old_pool, unit);
186 ABTI_unit_init_builtin(p_thread);
187 p_thread->p_pool = p_pool;
189 }
else if (p_thread->p_pool == p_pool) {
194 ABT_pool pool = ABTI_pool_get_handle(p_pool);
196 p_pool->required_def.p_create_unit(pool, ABTI_thread_get_handle(
200 int ret = ABTI_unit_map_thread(p_global, new_unit, p_thread);
202 p_pool->required_def.p_free_unit(pool, new_unit);
205 ABTI_unit_unmap_thread(p_global, unit);
206 ABT_pool old_pool = ABTI_pool_get_handle(p_thread->p_pool);
207 p_thread->p_pool->required_def.p_free_unit(old_pool, unit);
208 p_thread->unit = new_unit;
209 p_thread->p_pool = p_pool;
214 static inline void ABTI_thread_unset_associated_pool(ABTI_global *p_global,
215 ABTI_thread *p_thread)
219 ABTI_unit_unmap_thread(p_global, unit);
220 ABT_pool old_pool = ABTI_pool_get_handle(p_thread->p_pool);
221 p_thread->p_pool->required_def.p_free_unit(old_pool, unit);
223 #if ABTI_IS_ERROR_CHECK_ENABLED
225 p_thread->p_pool = NULL;