source: src/libcfa/concurrency/invoke.h @ 97e3296

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 97e3296 was 97e3296, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

First working implementation of external scheduling... Still lots of testing to do

  • Property mode set to 100644
File size: 5.3 KB
Line 
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
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Fri Jul 21 22:28:56 2017
13// Update Count     : 1
14//
15
16#include <stdbool.h>
17#include <stdint.h>
18
19#ifdef __CFORALL__
20extern "C" {
21#endif
22
23#if ! defined(__CFA_INVOKE_PRIVATE__)
24#ifndef _INVOKE_H_
25#define _INVOKE_H_
26
27      #define unlikely(x)    __builtin_expect(!!(x), 0)
28      #define thread_local _Thread_local
29
30      struct spinlock {
31            volatile int lock;
32            #ifdef __CFA_DEBUG__
33                  const char * prev_name;
34                  void* prev_thrd;
35            #endif
36      };
37
38      struct __thread_queue_t {
39            struct thread_desc * head;
40            struct thread_desc ** tail;
41      };
42
43      struct __condition_stack_t {
44            struct __condition_criterion_t * top;
45      };
46
47      #ifdef __CFORALL__
48      extern "Cforall" {
49            void ?{}( struct __thread_queue_t * );
50            void append( struct __thread_queue_t *, struct thread_desc * );
51            struct thread_desc * pop_head( struct __thread_queue_t * );
52
53            void ?{}( struct __condition_stack_t * );
54            void push( struct __condition_stack_t *, struct __condition_criterion_t * );
55            struct __condition_criterion_t * pop( struct __condition_stack_t * );
56
57            void ?{}(spinlock * this);
58            void ^?{}(spinlock * this);
59      }
60      #endif
61
62      struct coStack_t {
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
70      };
71
72      enum coroutine_state { Halted, Start, Inactive, Active, Primed };
73
74      struct coroutine_desc {
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
81      };
82
83      struct monitor_desc {
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
87            struct __condition_stack_t signal_stack;  // stack of conditions to run next once we exit the monitor
88            unsigned int recursion;                   // monitor routines can be called recursively, we need to keep track of that
89
90            struct __acceptable_t * acceptables;      // list of acceptable functions, null if any
91            unsigned short acceptable_count;          // number of acceptable functions
92            short accepted_index;                     // the index of the accepted function, -1 if none
93            void (*pre_accept)(void);                 // function to run before an accept
94       };
95
96      struct thread_desc {
97            struct coroutine_desc cor;                // coroutine body used to store context
98            struct monitor_desc mon;                  // monitor body used for mutual exclusion
99            struct thread_desc * next;                // instrusive link field for threads
100            struct monitor_desc ** current_monitors;  // currently held monitors
101            unsigned short current_monitor_count;     // number of currently held monitors
102     };
103
104#endif //_INVOKE_H_
105#else //! defined(__CFA_INVOKE_PRIVATE__)
106#ifndef _INVOKE_PRIVATE_H_
107#define _INVOKE_PRIVATE_H_
108
109      struct machine_context_t {
110            void *SP;
111            void *FP;
112            void *PC;
113      };
114
115      // assembler routines that performs the context switch
116      extern void CtxInvokeStub( void );
117      void CtxSwitch( void * from, void * to ) asm ("CtxSwitch");
118
119      #if   defined( __x86_64__ )
120      #define CtxGet( ctx ) __asm__ ( \
121                  "movq %%rsp,%0\n"   \
122                  "movq %%rbp,%1\n"   \
123            : "=rm" (ctx.SP), "=rm" (ctx.FP) )
124      #elif defined( __i386__ )
125      #define CtxGet( ctx ) __asm__ ( \
126                  "movl %%esp,%0\n"   \
127                  "movl %%ebp,%1\n"   \
128            : "=rm" (ctx.SP), "=rm" (ctx.FP) )
129      #endif
130
131#endif //_INVOKE_PRIVATE_H_
132#endif //! defined(__CFA_INVOKE_PRIVATE__)
133#ifdef __CFORALL__
134}
135#endif
136
137// Local Variables: //
138// mode: c //
139// tab-width: 4 //
140// End: //
Note: See TracBrowser for help on using the repository browser.