[0157ca7] | 1 | // -*- Mode: C -*- |
---|
[8def349] | 2 | // |
---|
| 3 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo |
---|
| 4 | // |
---|
| 5 | // The contents of this file are covered under the licence agreement in the |
---|
| 6 | // file "LICENCE" distributed with Cforall. |
---|
| 7 | // |
---|
| 8 | // invoke.h -- |
---|
| 9 | // |
---|
| 10 | // Author : Thierry Delisle |
---|
| 11 | // Created On : Tue Jan 17 12:27:26 2016 |
---|
| 12 | // Last Modified By : Thierry Delisle |
---|
| 13 | // Last Modified On : -- |
---|
| 14 | // Update Count : 0 |
---|
| 15 | // |
---|
| 16 | |
---|
[5c81105] | 17 | #include <stdbool.h> |
---|
| 18 | #include <stdint.h> |
---|
[78b3f52] | 19 | |
---|
[5c81105] | 20 | #ifdef __CFORALL__ |
---|
| 21 | extern "C" { |
---|
| 22 | #endif |
---|
| 23 | |
---|
| 24 | #if ! defined(__CFA_INVOKE_PRIVATE__) |
---|
[78b3f52] | 25 | #ifndef _INVOKE_H_ |
---|
| 26 | #define _INVOKE_H_ |
---|
| 27 | |
---|
[596f987b] | 28 | #define unlikely(x) __builtin_expect(!!(x), 0) |
---|
[8def349] | 29 | #define thread_local _Thread_local |
---|
[bd98b58] | 30 | |
---|
[db6f06a] | 31 | struct spinlock { |
---|
| 32 | volatile int lock; |
---|
[1c273d0] | 33 | #ifdef __CFA_DEBUG__ |
---|
[b227f68] | 34 | const char * prev_name; |
---|
| 35 | void* prev_thrd; |
---|
[1c273d0] | 36 | #endif |
---|
[db6f06a] | 37 | }; |
---|
| 38 | |
---|
[5ea06d6] | 39 | struct __thread_queue_t { |
---|
[348006f] | 40 | struct thread_desc * head; |
---|
| 41 | struct thread_desc ** tail; |
---|
[bd98b58] | 42 | }; |
---|
| 43 | |
---|
[0c78741] | 44 | struct __condition_stack_t { |
---|
| 45 | struct __condition_criterion_t * top; |
---|
[690f13c] | 46 | }; |
---|
| 47 | |
---|
[bd98b58] | 48 | #ifdef __CFORALL__ |
---|
| 49 | extern "Cforall" { |
---|
[242a902] | 50 | void ?{}( struct __thread_queue_t & ); |
---|
[5ea06d6] | 51 | void append( struct __thread_queue_t *, struct thread_desc * ); |
---|
| 52 | struct thread_desc * pop_head( struct __thread_queue_t * ); |
---|
[db6f06a] | 53 | |
---|
[242a902] | 54 | void ?{}( struct __condition_stack_t & ); |
---|
[0c78741] | 55 | void push( struct __condition_stack_t *, struct __condition_criterion_t * ); |
---|
| 56 | struct __condition_criterion_t * pop( struct __condition_stack_t * ); |
---|
[690f13c] | 57 | |
---|
[242a902] | 58 | void ?{}(spinlock & this); |
---|
| 59 | void ^?{}(spinlock & this); |
---|
[bd98b58] | 60 | } |
---|
| 61 | #endif |
---|
[596f987b] | 62 | |
---|
| 63 | struct coStack_t { |
---|
[5ea06d6] | 64 | unsigned int size; // size of stack |
---|
| 65 | void *storage; // pointer to stack |
---|
| 66 | void *limit; // stack grows towards stack limit |
---|
| 67 | void *base; // base of stack |
---|
| 68 | void *context; // address of cfa_context_t |
---|
| 69 | void *top; // address of top of storage |
---|
| 70 | bool userStack; // whether or not the user allocated the stack |
---|
[5c81105] | 71 | }; |
---|
[78b3f52] | 72 | |
---|
[ee897e4b] | 73 | enum coroutine_state { Halted, Start, Inactive, Active, Primed }; |
---|
[78b3f52] | 74 | |
---|
[c3acb841] | 75 | struct coroutine_desc { |
---|
[5ea06d6] | 76 | struct coStack_t stack; // stack information of the coroutine |
---|
| 77 | const char *name; // textual name for coroutine/task, initialized by uC++ generated code |
---|
| 78 | int errno_; // copy of global UNIX variable errno |
---|
| 79 | enum coroutine_state state; // current execution status for coroutine |
---|
| 80 | struct coroutine_desc *starter; // first coroutine to resume this one |
---|
| 81 | struct coroutine_desc *last; // last coroutine to resume this one |
---|
[5c81105] | 82 | }; |
---|
[78b3f52] | 83 | |
---|
[cb0e6de] | 84 | struct monitor_desc { |
---|
[5ea06d6] | 85 | struct spinlock lock; // spinlock to protect internal data |
---|
| 86 | struct thread_desc * owner; // current owner of the monitor |
---|
| 87 | struct __thread_queue_t entry_queue; // queue of threads that are blocked waiting for the monitor |
---|
[0c78741] | 88 | struct __condition_stack_t signal_stack; // stack of conditions to run next once we exit the monitor |
---|
[5ea06d6] | 89 | unsigned int recursion; // monitor routines can be called recursively, we need to keep track of that |
---|
[cb0e6de] | 90 | }; |
---|
| 91 | |
---|
[348006f] | 92 | struct thread_desc { |
---|
[5ea06d6] | 93 | struct coroutine_desc cor; // coroutine body used to store context |
---|
| 94 | struct monitor_desc mon; // monitor body used for mutual exclusion |
---|
| 95 | struct thread_desc * next; // instrusive link field for threads |
---|
| 96 | struct monitor_desc ** current_monitors; // currently held monitors |
---|
| 97 | unsigned short current_monitor_count; // number of currently held monitors |
---|
[8118303] | 98 | }; |
---|
| 99 | |
---|
[5c81105] | 100 | #endif //_INVOKE_H_ |
---|
| 101 | #else //! defined(__CFA_INVOKE_PRIVATE__) |
---|
| 102 | #ifndef _INVOKE_PRIVATE_H_ |
---|
| 103 | #define _INVOKE_PRIVATE_H_ |
---|
[b227f68] | 104 | |
---|
[596f987b] | 105 | struct machine_context_t { |
---|
[5c81105] | 106 | void *SP; |
---|
| 107 | void *FP; |
---|
| 108 | void *PC; |
---|
| 109 | }; |
---|
[78b3f52] | 110 | |
---|
[5c81105] | 111 | // assembler routines that performs the context switch |
---|
[b58a5772] | 112 | extern void CtxInvokeStub( void ); |
---|
[eb2e723] | 113 | void CtxSwitch( void * from, void * to ) asm ("CtxSwitch"); |
---|
[f32e53e] | 114 | |
---|
| 115 | #if defined( __x86_64__ ) |
---|
| 116 | #define CtxGet( ctx ) __asm__ ( \ |
---|
| 117 | "movq %%rsp,%0\n" \ |
---|
| 118 | "movq %%rbp,%1\n" \ |
---|
| 119 | : "=rm" (ctx.SP), "=rm" (ctx.FP) ) |
---|
| 120 | #elif defined( __i386__ ) |
---|
| 121 | #define CtxGet( ctx ) __asm__ ( \ |
---|
| 122 | "movl %%esp,%0\n" \ |
---|
| 123 | "movl %%ebp,%1\n" \ |
---|
| 124 | : "=rm" (ctx.SP), "=rm" (ctx.FP) ) |
---|
| 125 | #endif |
---|
[78b3f52] | 126 | |
---|
[5c81105] | 127 | #endif //_INVOKE_PRIVATE_H_ |
---|
| 128 | #endif //! defined(__CFA_INVOKE_PRIVATE__) |
---|
| 129 | #ifdef __CFORALL__ |
---|
| 130 | } |
---|
| 131 | #endif |
---|