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