source: src/libcfa/concurrency/invoke.h @ 5c81105

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 5c81105 was 5c81105, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

cleaned-up coroutines code to no longer need a manual start

  • Property mode set to 100644
File size: 2.4 KB
Line 
1#include <stdbool.h>
2#include <stdint.h>
3
4#ifdef __CFORALL__
5extern "C" {
6#endif
7
8#if ! defined(__CFA_INVOKE_PRIVATE__)
9#ifndef _INVOKE_H_
10#define _INVOKE_H_
11
12      #define OFFSET_OF(type, field) ((intptr_t)( &((type*)0)->field))
13      #define VPTR_OFFSET(type, vptr, field) (OFFSET_OF(type, field) - OFFSET_OF(type, vptr))
14
15      struct coVtable_t { 
16            void (*main)(void*);
17            intptr_t offset_coroutine;
18            intptr_t offset_object;
19      };
20
21      typedef struct coVtable_t* covptr_t;
22
23      static inline struct coroutine* get_coroutine(covptr_t* vthis) {
24            return (struct coroutine*) ( ((intptr_t)vthis) + ((intptr_t)(*vthis)->offset_coroutine) ); 
25      }
26
27      static inline void* get_object(covptr_t* vthis) {
28            return (void*) ( ((intptr_t)vthis) + ((intptr_t)(*vthis)->offset_object) ); 
29      }
30
31      struct coStack_t {
32            unsigned int size;          // size of stack
33            void *storage;                      // pointer to stack
34            void *limit;                        // stack grows towards stack limit
35            void *base;                         // base of stack
36            void *context;                      // address of cfa_context_t
37            void *top;                          // address of top of storage
38            bool userStack;     
39      };
40
41      enum coroutine_state { Start, Inactive, Active, Halt };
42
43      struct coroutine {
44            struct coStack_t stack;
45            const char *name;                   // textual name for coroutine/task, initialized by uC++ generated code
46            int errno_;                         // copy of global UNIX variable errno
47            enum coroutine_state state; // current execution status for coroutine
48            bool notHalted;                     // indicate if execuation state is not halted
49
50            struct coroutine *starter;  // first coroutine to resume this one
51            struct coroutine *last;             // last coroutine to resume this one
52      };
53
54      void invokeCoroutine(covptr_t* this);
55      void startCoroutine(covptr_t* this, void (*invoke)(covptr_t*));
56#endif //_INVOKE_H_
57#else //! defined(__CFA_INVOKE_PRIVATE__)
58#ifndef _INVOKE_PRIVATE_H_
59#define _INVOKE_PRIVATE_H_
60     
61     struct machine_context_t {
62            void *SP;
63            void *FP;
64            void *PC;
65      };
66
67      // assembler routines that performs the context switch
68      extern void coInvokeStub( void );
69      void CtxSwitch( void *from, void *to ) asm ("CtxSwitch");
70
71#endif //_INVOKE_PRIVATE_H_
72#endif //! defined(__CFA_INVOKE_PRIVATE__)
73#ifdef __CFORALL__
74}
75#endif
Note: See TracBrowser for help on using the repository browser.