ARGOBOTS  dce6e727ffc4ca5b3ffc04cb9517c6689be51ec5
abtd_fcontext.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_FCONTEXT_H_INCLUDED
7 #define ABTD_FCONTEXT_H_INCLUDED
8 
9 typedef struct {
10  void *dummy;
11 } fcontext_t;
12 
13 static inline int ABTDI_fcontext_is_created(const fcontext_t *p_ftx)
14 {
15  /* Return 0 if p_ftx->dummy is NULL. */
16  return !!p_ftx->dummy;
17 }
18 
19 static inline void ABTDI_fcontext_init(fcontext_t *p_ftx)
20 {
21  p_ftx->dummy = NULL;
22 }
23 
24 #if defined(ABT_C_HAVE_VISIBILITY)
25 #define ABT_API_PRIVATE __attribute__((visibility("hidden")))
26 #else
27 #define ABT_API_PRIVATE
28 #endif
29 
30 void switch_fcontext(fcontext_t *p_new_ctx,
31  fcontext_t *p_old_ctx) ABT_API_PRIVATE;
33 void init_and_switch_fcontext(fcontext_t *p_new_ctx,
34  void (*f_thread)(fcontext_t *), void *p_stacktop,
35  fcontext_t *p_old_ctx) ABT_API_PRIVATE;
36 void init_and_jump_fcontext(fcontext_t *p_new_ctx,
37  void (*f_thread)(fcontext_t *),
38  void *p_stacktop) ABT_API_PRIVATE;
39 void switch_with_call_fcontext(void *cb_arg, void (*f_cb)(void *),
40  fcontext_t *p_new_ctx,
41  fcontext_t *p_old_ctx) ABT_API_PRIVATE;
42 void jump_with_call_fcontext(void *cb_arg, void (*f_cb)(void *),
43  fcontext_t *p_new_ctx) ABT_API_PRIVATE;
44 void init_and_switch_with_call_fcontext(void *cb_arg, void (*f_cb)(void *),
45  fcontext_t *p_new_ctx,
46  void (*f_thread)(fcontext_t *),
47  void *p_stacktop,
48  fcontext_t *p_old_ctx) ABT_API_PRIVATE;
49 void init_and_jump_with_call_fcontext(void *cb_arg, void (*f_cb)(void *),
50  fcontext_t *p_new_ctx,
51  void (*f_thread)(fcontext_t *),
52  void *p_stacktop) ABT_API_PRIVATE;
53 void peek_fcontext(void *arg, void (*f_peek)(void *),
54  fcontext_t *p_target_ctx) ABT_API_PRIVATE;
55 
57  fcontext_t ctx; /* actual context of fcontext */
58  void *p_stacktop;
59  size_t stacksize;
61  p_link; /* pointer to the waiter's context */
62 };
63 
64 static inline ABTD_ythread_context *
66 {
67  return (ABTD_ythread_context *)(((char *)p_fctx) -
68  offsetof(ABTD_ythread_context, ctx));
69 }
70 
72 {
75  /* ABTD_ythread_func_wrapper() must context-switch to another before it
76  * finishes. */
78 }
79 
81  void *p_stacktop, size_t stacksize)
82 {
83  ABTDI_fcontext_init(&p_ctx->ctx);
84  p_ctx->p_stacktop = p_stacktop;
85  p_ctx->stacksize = stacksize;
87 }
88 
90  size_t stacksize)
91 {
92  ABTDI_fcontext_init(&p_ctx->ctx);
93  p_ctx->p_stacktop = NULL;
94  p_ctx->stacksize = stacksize;
96 }
97 
98 static inline void
100  void *p_stacktop)
101 {
102  p_ctx->p_stacktop = p_stacktop;
103 }
104 
105 static inline void
107 {
108  p_ctx->p_stacktop = NULL;
109 }
110 
112 {
113  ABTDI_fcontext_init(&p_ctx->ctx);
115 }
116 
117 static inline void *
119 {
120  return p_ctx->p_stacktop;
121 }
122 
123 static inline ABT_bool
125 {
126  return p_ctx->p_stacktop ? ABT_TRUE : ABT_FALSE;
127 }
128 
129 static inline size_t
131 {
132  return p_ctx->stacksize;
133 }
134 
135 static inline ABT_bool
137 {
138  return ABTDI_fcontext_is_created(&p_ctx->ctx) ? ABT_TRUE : ABT_FALSE;
139 }
140 
142  ABTD_ythread_context *p_new)
143 {
145  /* The context is already initialized. */
146  switch_fcontext(&p_new->ctx, &p_old->ctx);
147 }
148 
149 static inline void
151  ABTD_ythread_context *p_new)
152 {
154  /* First time. */
156  p_new->p_stacktop, &p_old->ctx);
157 }
158 
159 ABTU_noreturn static inline void
161 {
163  /* The context is already initialized. */
164  jump_fcontext(&p_new->ctx);
166 }
167 
168 ABTU_noreturn static inline void
170 {
172  /* First time. */
174  p_new->p_stacktop);
176 }
177 
178 static inline void
180  ABTD_ythread_context *p_new,
181  void (*f_cb)(void *), void *cb_arg)
182 {
184  /* The context is already initialized. */
185 
186  switch_with_call_fcontext(cb_arg, f_cb, &p_new->ctx, &p_old->ctx);
187 }
188 
191  void (*f_cb)(void *), void *cb_arg)
192 {
194  /* First time. */
195  init_and_switch_with_call_fcontext(cb_arg, f_cb, &p_new->ctx,
197  p_new->p_stacktop, &p_old->ctx);
198 }
199 
200 ABTU_noreturn static inline void
202  void (*f_cb)(void *), void *cb_arg)
203 {
205  /* The context is already initialized. */
206  jump_with_call_fcontext(cb_arg, f_cb, &p_new->ctx);
208 }
209 
211  ABTD_ythread_context *p_new, void (*f_cb)(void *), void *cb_arg)
212 {
214  /* First time. */
215  init_and_jump_with_call_fcontext(cb_arg, f_cb, &p_new->ctx,
217  p_new->p_stacktop);
219 }
220 
221 static inline ABT_bool
223  void (*f_peek)(void *), void *arg)
224 {
225  if (ABTDI_fcontext_is_created(&p_target_ctx->ctx)) {
226  peek_fcontext(arg, f_peek, &p_target_ctx->ctx);
227  return ABT_TRUE;
228  } else {
229  return ABT_FALSE;
230  }
231 }
232 
233 #endif /* ABTD_FCONTEXT_H_INCLUDED */
jump_fcontext
void jump_fcontext(fcontext_t *p_new_ctx) ABT_API_PRIVATE
init_and_switch_with_call_fcontext
void init_and_switch_with_call_fcontext(void *cb_arg, void(*f_cb)(void *), fcontext_t *p_new_ctx, void(*f_thread)(fcontext_t *), void *p_stacktop, fcontext_t *p_old_ctx) ABT_API_PRIVATE
switch_with_call_fcontext
void switch_with_call_fcontext(void *cb_arg, void(*f_cb)(void *), fcontext_t *p_new_ctx, fcontext_t *p_old_ctx) ABT_API_PRIVATE
ABT_bool
int ABT_bool
Boolean type.
Definition: abt.h:1043
ABTD_ythread_context::stacksize
size_t stacksize
Definition: abtd_fcontext.h:59
ABTD_ythread_context_start_and_jump_with_call
static ABTU_noreturn void ABTD_ythread_context_start_and_jump_with_call(ABTD_ythread_context *p_new, void(*f_cb)(void *), void *cb_arg)
Definition: abtd_fcontext.h:210
ABTD_ythread_context_get_stacktop
static void * ABTD_ythread_context_get_stacktop(ABTD_ythread_context *p_ctx)
Definition: abtd_fcontext.h:118
ABTD_ythread_context_start_and_switch
static void ABTD_ythread_context_start_and_switch(ABTD_ythread_context *p_old, ABTD_ythread_context *p_new)
Definition: abtd_fcontext.h:150
init_and_switch_fcontext
void init_and_switch_fcontext(fcontext_t *p_new_ctx, void(*f_thread)(fcontext_t *), void *p_stacktop, fcontext_t *p_old_ctx) ABT_API_PRIVATE
ABTD_ythread_context::ctx
fcontext_t ctx
Definition: abtd_fcontext.h:57
ABTD_ythread_context_jump
static ABTU_noreturn void ABTD_ythread_context_jump(ABTD_ythread_context *p_new)
Definition: abtd_fcontext.h:160
ABTD_ythread_context_start_and_jump
static ABTU_noreturn void ABTD_ythread_context_start_and_jump(ABTD_ythread_context *p_new)
Definition: abtd_fcontext.h:169
ABTD_atomic_relaxed_store_ythread_context_ptr
static void ABTD_atomic_relaxed_store_ythread_context_ptr(ABTD_ythread_context_atomic_ptr *ptr, ABTD_ythread_context *p_ctx)
Definition: abtd_context.h:38
jump_with_call_fcontext
void jump_with_call_fcontext(void *cb_arg, void(*f_cb)(void *), fcontext_t *p_new_ctx) ABT_API_PRIVATE
ABTU_noreturn
#define ABTU_noreturn
Definition: abtu.h:127
ABTD_ythread_context_peek
static ABT_bool ABTD_ythread_context_peek(ABTD_ythread_context *p_target_ctx, void(*f_peek)(void *), void *arg)
Definition: abtd_fcontext.h:222
ABTD_ythread_context::p_link
ABTD_ythread_context_atomic_ptr p_link
Definition: abtd_fcontext.h:61
ABTD_ythread_context
Definition: abtd_fcontext.h:56
ABTD_ythread_context_func_wrapper
static void ABTD_ythread_context_func_wrapper(fcontext_t *p_fctx)
Definition: abtd_fcontext.h:71
ABTD_ythread_context_is_started
static ABT_bool ABTD_ythread_context_is_started(const ABTD_ythread_context *p_ctx)
Definition: abtd_fcontext.h:136
ABTU_unreachable
#define ABTU_unreachable()
Definition: abtu.h:133
ABTD_ythread_context_start_and_switch_with_call
static void ABTD_ythread_context_start_and_switch_with_call(ABTD_ythread_context *p_old, ABTD_ythread_context *p_new, void(*f_cb)(void *), void *cb_arg)
Definition: abtd_fcontext.h:189
ABTD_ythread_context_switch_with_call
static void ABTD_ythread_context_switch_with_call(ABTD_ythread_context *p_old, ABTD_ythread_context *p_new, void(*f_cb)(void *), void *cb_arg)
Definition: abtd_fcontext.h:179
ABTD_ythread_context_lazy_unset_stack
static void ABTD_ythread_context_lazy_unset_stack(ABTD_ythread_context *p_ctx)
Definition: abtd_fcontext.h:106
ABTD_ythread_context_reinit
static void ABTD_ythread_context_reinit(ABTD_ythread_context *p_ctx)
Definition: abtd_fcontext.h:111
switch_fcontext
void switch_fcontext(fcontext_t *p_new_ctx, fcontext_t *p_old_ctx) ABT_API_PRIVATE
init_and_jump_fcontext
void init_and_jump_fcontext(fcontext_t *p_new_ctx, void(*f_thread)(fcontext_t *), void *p_stacktop) ABT_API_PRIVATE
fcontext_t
Definition: abtd_fcontext.h:9
ABTD_ythread_context_lazy_set_stack
static void ABTD_ythread_context_lazy_set_stack(ABTD_ythread_context *p_ctx, void *p_stacktop)
Definition: abtd_fcontext.h:99
ABTD_ythread_context_atomic_ptr
Definition: abtd_context.h:20
ABTDI_fcontext_is_created
static int ABTDI_fcontext_is_created(const fcontext_t *p_ftx)
Definition: abtd_fcontext.h:13
fcontext_t::dummy
void * dummy
Definition: abtd_fcontext.h:10
ABTD_ythread_context::p_stacktop
void * p_stacktop
Definition: abtd_fcontext.h:58
ABT_TRUE
#define ABT_TRUE
True constant for ABT_bool.
Definition: abt.h:784
ABTD_ythread_context_jump_with_call
static ABTU_noreturn void ABTD_ythread_context_jump_with_call(ABTD_ythread_context *p_new, void(*f_cb)(void *), void *cb_arg)
Definition: abtd_fcontext.h:201
ABT_FALSE
#define ABT_FALSE
False constant for ABT_bool.
Definition: abt.h:786
ABTD_ythread_context_init
static void ABTD_ythread_context_init(ABTD_ythread_context *p_ctx, void *p_stacktop, size_t stacksize)
Definition: abtd_fcontext.h:80
ABTI_UB_ASSERT
#define ABTI_UB_ASSERT(cond)
Definition: abti_error.h:19
ABTD_ythread_context_switch
static void ABTD_ythread_context_switch(ABTD_ythread_context *p_old, ABTD_ythread_context *p_new)
Definition: abtd_fcontext.h:141
ABTD_ythread_context_init_lazy
static void ABTD_ythread_context_init_lazy(ABTD_ythread_context *p_ctx, size_t stacksize)
Definition: abtd_fcontext.h:89
peek_fcontext
void peek_fcontext(void *arg, void(*f_peek)(void *), fcontext_t *p_target_ctx) ABT_API_PRIVATE
ABTD_ythread_func_wrapper
void ABTD_ythread_func_wrapper(ABTD_ythread_context *p_arg)
Definition: abtd_ythread.c:8
init_and_jump_with_call_fcontext
void init_and_jump_with_call_fcontext(void *cb_arg, void(*f_cb)(void *), fcontext_t *p_new_ctx, void(*f_thread)(fcontext_t *), void *p_stacktop) ABT_API_PRIVATE
ABTD_ythread_context_get_stacksize
static size_t ABTD_ythread_context_get_stacksize(ABTD_ythread_context *p_ctx)
Definition: abtd_fcontext.h:130
ABTDI_fcontext_init
static void ABTDI_fcontext_init(fcontext_t *p_ftx)
Definition: abtd_fcontext.h:19
ABTD_ythread_context_has_stack
static ABT_bool ABTD_ythread_context_has_stack(const ABTD_ythread_context *p_ctx)
Definition: abtd_fcontext.h:124
ABTDI_ythread_context_get_context
static ABTD_ythread_context * ABTDI_ythread_context_get_context(fcontext_t *p_fctx)
Definition: abtd_fcontext.h:65
ABT_API_PRIVATE
#define ABT_API_PRIVATE
Definition: abtd_fcontext.h:27