| 1 | //                              -*- Mode: C -*- | 
|---|
| 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 |  | 
|---|
| 17 | #include <stdbool.h> | 
|---|
| 18 | #include <stdint.h> | 
|---|
| 19 |  | 
|---|
| 20 | #ifdef __CFORALL__ | 
|---|
| 21 | extern "C" { | 
|---|
| 22 | #endif | 
|---|
| 23 |  | 
|---|
| 24 | #if ! defined(__CFA_INVOKE_PRIVATE__) | 
|---|
| 25 | #ifndef _INVOKE_H_ | 
|---|
| 26 | #define _INVOKE_H_ | 
|---|
| 27 |  | 
|---|
| 28 | #define unlikely(x)    __builtin_expect(!!(x), 0) | 
|---|
| 29 | #define thread_local _Thread_local | 
|---|
| 30 |  | 
|---|
| 31 | struct spinlock { | 
|---|
| 32 | volatile int lock; | 
|---|
| 33 | }; | 
|---|
| 34 |  | 
|---|
| 35 | struct simple_thread_list { | 
|---|
| 36 | struct thread_desc * head; | 
|---|
| 37 | struct thread_desc ** tail; | 
|---|
| 38 | }; | 
|---|
| 39 |  | 
|---|
| 40 | #ifdef __CFORALL__ | 
|---|
| 41 | extern "Cforall" { | 
|---|
| 42 | void ?{}( struct simple_thread_list * ); | 
|---|
| 43 | void append( struct simple_thread_list *, struct thread_desc * ); | 
|---|
| 44 | struct thread_desc * pop_head( struct simple_thread_list * ); | 
|---|
| 45 |  | 
|---|
| 46 | void ?{}(spinlock * this); | 
|---|
| 47 | void ^?{}(spinlock * this); | 
|---|
| 48 | } | 
|---|
| 49 | #endif | 
|---|
| 50 |  | 
|---|
| 51 | struct coStack_t { | 
|---|
| 52 | unsigned int size;                  // size of stack | 
|---|
| 53 | void *storage;                      // pointer to stack | 
|---|
| 54 | void *limit;                        // stack grows towards stack limit | 
|---|
| 55 | void *base;                         // base of stack | 
|---|
| 56 | void *context;                      // address of cfa_context_t | 
|---|
| 57 | void *top;                          // address of top of storage | 
|---|
| 58 | bool userStack;                     // whether or not the user allocated the stack | 
|---|
| 59 | }; | 
|---|
| 60 |  | 
|---|
| 61 | enum coroutine_state { Halted, Start, Inactive, Active, Primed }; | 
|---|
| 62 |  | 
|---|
| 63 | struct coroutine_desc { | 
|---|
| 64 | struct coStack_t stack;             // stack information of the coroutine | 
|---|
| 65 | const char *name;                   // textual name for coroutine/task, initialized by uC++ generated code | 
|---|
| 66 | int errno_;                         // copy of global UNIX variable errno | 
|---|
| 67 | enum coroutine_state state;         // current execution status for coroutine | 
|---|
| 68 | struct coroutine_desc *starter;     // first coroutine to resume this one | 
|---|
| 69 | struct coroutine_desc *last;              // last coroutine to resume this one | 
|---|
| 70 | }; | 
|---|
| 71 |  | 
|---|
| 72 | struct monitor_desc { | 
|---|
| 73 | struct spinlock lock; | 
|---|
| 74 | struct thread_desc * owner; | 
|---|
| 75 | struct simple_thread_list entry_queue; | 
|---|
| 76 | unsigned int recursion; | 
|---|
| 77 | }; | 
|---|
| 78 |  | 
|---|
| 79 | struct thread_desc { | 
|---|
| 80 | struct coroutine_desc cor;          // coroutine body used to store context | 
|---|
| 81 | struct monitor_desc mon;            // monitor body used for mutual exclusion | 
|---|
| 82 | struct thread_desc * next;          // instrusive link field for threads | 
|---|
| 83 | }; | 
|---|
| 84 |  | 
|---|
| 85 | #endif //_INVOKE_H_ | 
|---|
| 86 | #else //! defined(__CFA_INVOKE_PRIVATE__) | 
|---|
| 87 | #ifndef _INVOKE_PRIVATE_H_ | 
|---|
| 88 | #define _INVOKE_PRIVATE_H_ | 
|---|
| 89 |  | 
|---|
| 90 | struct machine_context_t { | 
|---|
| 91 | void *SP; | 
|---|
| 92 | void *FP; | 
|---|
| 93 | void *PC; | 
|---|
| 94 | }; | 
|---|
| 95 |  | 
|---|
| 96 | // assembler routines that performs the context switch | 
|---|
| 97 | extern void CtxInvokeStub( void ); | 
|---|
| 98 | void CtxSwitch( void * from, void * to ) asm ("CtxSwitch"); | 
|---|
| 99 | void CtxGet( void * this ) asm ("CtxGet"); | 
|---|
| 100 |  | 
|---|
| 101 | #endif //_INVOKE_PRIVATE_H_ | 
|---|
| 102 | #endif //! defined(__CFA_INVOKE_PRIVATE__) | 
|---|
| 103 | #ifdef __CFORALL__ | 
|---|
| 104 | } | 
|---|
| 105 | #endif | 
|---|