ARGOBOTS  8bef54a19d7b3e50f977f85716578b627bf37d9c
abtd_ythread.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 ABTD_YTHREAD_H_INCLUDED
7 #define ABTD_YTHREAD_H_INCLUDED
8 
9 #if defined(ABT_C_HAVE_VISIBILITY)
10 #define ABT_API_PRIVATE __attribute__((visibility("hidden")))
11 #else
12 #define ABT_API_PRIVATE
13 #endif
14 
15 void ABTD_ythread_func_wrapper(void *p_arg);
16 #if ABT_CONFIG_THREAD_TYPE == ABT_THREAD_TYPE_DYNAMIC_PROMOTION
17 void ABTD_ythread_terminate_no_arg(void);
18 #endif
19 
20 static inline void ABTD_ythread_context_create(ABTD_ythread_context *p_link,
21  size_t stacksize, void *p_stack,
22  ABTD_ythread_context *p_newctx)
23 {
24  /* ABTD_ythread_context_make uses the top address of stack.
25  Note that the parameter, p_stack, points to the bottom of stack. */
26  void *p_stacktop = (void *)(((char *)p_stack) + stacksize);
27 
28  ABTD_ythread_context_make(p_newctx, p_stacktop, stacksize,
29  ABTD_ythread_func_wrapper);
30  ABTD_atomic_relaxed_store_ythread_context_ptr(&p_newctx->p_link, p_link);
31 }
32 
33 static inline void
34 ABTD_ythread_context_invalidate(ABTD_ythread_context *p_newctx)
35 {
36 #if ABT_CONFIG_THREAD_TYPE == ABT_THREAD_TYPE_DYNAMIC_PROMOTION
37  /* p_ctx is used to check whether the context requires dynamic promotion is
38  * necessary or not, so this value must not be NULL. */
39  p_newctx->p_ctx = (void *)((intptr_t)0x1);
40 #else
41  p_newctx->p_ctx = NULL;
42 #endif
43  ABTD_atomic_relaxed_store_ythread_context_ptr(&p_newctx->p_link, NULL);
44 }
45 
46 #if ABT_CONFIG_THREAD_TYPE == ABT_THREAD_TYPE_DYNAMIC_PROMOTION
47 static inline void ABTD_ythread_context_init(ABTD_ythread_context *p_link,
48  ABTD_ythread_context *p_newctx)
49 {
50  p_newctx->p_ctx = NULL;
51  ABTD_atomic_relaxed_store_ythread_context_ptr(&p_newctx->p_link, p_link);
52 }
53 
54 static inline void
55 ABTD_ythread_context_arm_ythread(size_t stacksize, void *p_stack,
56  ABTD_ythread_context *p_newctx)
57 {
58  /*
59  * This function *arms* the dynamic promotion thread (initialized by
60  * ABTD_ythread_context_init) as if it were created by
61  * ABTD_ythread_context_create; this function fully creates the context
62  * so that the thread can be run by ABTD_ythread_context_jump.
63  *
64  * ABTD_ythread_context_make uses the top address of stack.
65  * Note that the parameter, p_stack, points to the bottom of stack.
66  */
67  void *p_stacktop = (void *)(((char *)p_stack) + stacksize);
68  ABTD_ythread_context_make(p_newctx, p_stacktop, stacksize,
69  ABTD_ythread_func_wrapper);
70 }
71 #endif
72 
73 static inline void ABTD_ythread_context_switch(ABTD_ythread_context *p_old,
74  ABTD_ythread_context *p_new)
75 {
76  ABTD_ythread_context_jump(p_old, p_new, p_new);
77 }
78 
79 ABTU_noreturn static inline void
80 ABTD_ythread_finish_context(ABTD_ythread_context *p_old,
81  ABTD_ythread_context *p_new)
82 {
83  ABTD_ythread_context_take(p_old, p_new, p_new);
84 }
85 
86 #if ABT_CONFIG_THREAD_TYPE == ABT_THREAD_TYPE_DYNAMIC_PROMOTION
87 static inline void
88 ABTD_ythread_context_make_and_call(ABTD_ythread_context *p_old,
89  void (*f_thread)(void *), void *p_arg,
90  void *p_stacktop)
91 {
92  ABTD_ythread_context_init_and_call(p_old, p_stacktop, f_thread, p_arg);
93 }
94 
95 static inline ABT_bool
96 ABTD_ythread_context_is_dynamic_promoted(ABTD_ythread_context *p_ctx)
97 {
98  /* Check if the ULT has been dynamically promoted; internally, it checks if
99  * the context is NULL. */
100  return p_ctx->p_ctx ? ABT_TRUE : ABT_FALSE;
101 }
102 
103 static inline void ABTDI_ythread_context_dynamic_promote(void *p_stacktop,
104  void *jump_f)
105 {
106  /* Perform dynamic promotion */
107  void **p_return_address = (void **)(((char *)p_stacktop) - 0x10);
108  void ***p_stack_pointer = (void ***)(((char *)p_stacktop) - 0x08);
109  *p_stack_pointer = p_return_address;
110  *p_return_address = jump_f;
111 }
112 
113 static inline void
114 ABTD_ythread_context_dynamic_promote_ythread(void *p_stacktop)
115 {
116  union fp_conv {
117  void (*f)(void);
118  void *ptr;
119  } conv;
120  conv.f = ABTD_ythread_terminate_no_arg;
121  void *jump_f = conv.ptr;
122  ABTDI_ythread_context_dynamic_promote(p_stacktop, jump_f);
123 }
124 #endif
125 
126 #endif /* ABTD_YTHREAD_H_INCLUDED */
ABT_bool
int ABT_bool
Boolean type.
Definition: abt.h:1001
ABTU_noreturn
#define ABTU_noreturn
Definition: abtu.h:127
ABTDI_ythread_context_dynamic_promote
static void ABTDI_ythread_context_dynamic_promote(void *p_stacktop, void *jump_f)
Definition: abtd_ythread.h:103
ABT_TRUE
#define ABT_TRUE
True constant for ABT_bool.
Definition: abt.h:748
ABT_FALSE
#define ABT_FALSE
False constant for ABT_bool.
Definition: abt.h:750