ARGOBOTS  66b1c39742507d8df30e8d28c54839b961a14814
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
abtd_ucontext.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_UCONTEXT_H_INCLUDED
7 #define ABTD_UCONTEXT_H_INCLUDED
8 
9 static void ABTD_ucontext_wrapper(int arg1, int arg2)
10 {
11  ABTD_ythread_context *p_self;
12 #if SIZEOF_VOID_P == 8
13  p_self = (ABTD_ythread_context *)(((uintptr_t)((uint32_t)arg1) << 32) |
14  ((uintptr_t)((uint32_t)arg2)));
15 #elif SIZEOF_VOID_P == 4
16  p_self = (ABTD_ythread_context *)((uintptr_t)arg1);
17 #else
18 #error "Unknown pointer size."
19 #endif
20  p_self->f_uctx_thread(p_self->p_uctx_arg);
21  /* ABTD_ythread_context_jump or take must be called at the end of
22  * f_uctx_thread, */
23  ABTI_ASSERT(0);
25 }
26 
28  void *sp, size_t size,
29  void (*thread_func)(void *))
30 {
31  int ret = getcontext(&p_ctx->uctx);
32  ABTI_ASSERT(ret == 0); /* getcontext() should not return an error. */
33  p_ctx->p_ctx = &p_ctx->uctx;
34 
35  /* uc_link is not used. */
36  p_ctx->uctx.uc_link = NULL;
37  p_ctx->uctx.uc_stack.ss_sp = (void *)(((char *)sp) - size);
38  p_ctx->uctx.uc_stack.ss_size = size;
39  p_ctx->f_uctx_thread = thread_func;
40 
41 #if SIZEOF_VOID_P == 8
42  int arg_upper = (int)(((uintptr_t)p_ctx) >> 32);
43  int arg_lower = (int)(((uintptr_t)p_ctx) >> 0);
44  makecontext(&p_ctx->uctx, (void (*)())ABTD_ucontext_wrapper, 2, arg_upper,
45  arg_lower);
46 #elif SIZEOF_VOID_P == 4
47  int arg = (int)((uintptr_t)p_ctx);
48  makecontext(&p_ctx->uctx, (void (*)())ABTD_ucontext_wrapper, 1, arg);
49 #else
50 #error "Unknown pointer size."
51 #endif
52 }
53 
55  ABTD_ythread_context *p_new,
56  void *arg)
57 {
58  p_new->p_uctx_arg = arg;
59  int ret = swapcontext(&p_old->uctx, &p_new->uctx);
60  /* Fatal. This out-of-stack error is not recoverable. */
61  ABTI_ASSERT(ret == 0);
62 }
63 
64 ABTU_noreturn static inline void
66  ABTD_ythread_context *p_new, void *arg)
67 {
68  p_new->p_uctx_arg = arg;
69  int ret = setcontext(&p_new->uctx);
70  ABTI_ASSERT(ret == 0); /* setcontext() should not return an error. */
72 }
73 
74 #if ABT_CONFIG_THREAD_TYPE == ABT_THREAD_TYPE_DYNAMIC_PROMOTION
75 #error "ABTD_ythread_context_make_and_call is not implemented."
76 #endif
77 
78 #endif /* ABTD_UCONTEXT_H_INCLUDED */
#define ABTU_unreachable()
Definition: abtu.h:25
static void ABTD_ythread_context_make(ABTD_ythread_context *p_ctx, void *sp, size_t size, void(*thread_func)(void *))
Definition: abtd_ucontext.h:27
static void ABTD_ucontext_wrapper(int arg1, int arg2)
Definition: abtd_ucontext.h:9
static ABTU_noreturn void ABTD_ythread_context_take(ABTD_ythread_context *p_old, ABTD_ythread_context *p_new, void *arg)
Definition: abtd_ucontext.h:65
#define ABTU_noreturn
Definition: abtu.h:31
static void ABTD_ythread_context_jump(ABTD_ythread_context *p_old, ABTD_ythread_context *p_new, void *arg)
Definition: abtd_ucontext.h:54
#define ABTI_ASSERT(cond)
Definition: abti_error.h:12