source: src/libcfa/concurrency/threads @ 17e5e2b

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

Added proper include guards to cfa headers so they can be added to the c++ include path without issues

  • Property mode set to 100644
File size: 3.5 KB
RevLine 
[78b3f52]1//                              -*- Mode: CFA -*-
[0e76cf4f]2//
[78b3f52]3// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
[0e76cf4f]4//
5// The contents of this file are covered under the licence agreement in the
6// file "LICENCE" distributed with Cforall.
7//
[78b3f52]8// threads --
[0e76cf4f]9//
[78b3f52]10// Author           : Thierry Delisle
11// Created On       : Mon Nov 28 12:27:26 2016
12// Last Modified By : Thierry Delisle
13// Last Modified On : Mon Nov 28 12:27:26 2016
14// Update Count     : 0
[0e76cf4f]15//
16
[17e5e2b]17#ifdef __CFORALL__
18
19#ifndef THREADS_H
20#define THREADS_H
[0e76cf4f]21
[596f987b]22#include "assert"       //
[5c81105]23#include "invoke.h"
[78b3f52]24
[596f987b]25//-----------------------------------------------------------------------------
26// Coroutine trait
27// Anything that implements this trait can be resumed.
28// Anything that is resumed is a coroutine.
[80d9e49]29trait is_coroutine(dtype T) {
[78b3f52]30      void co_main(T* this);
[80d9e49]31      coroutine* get_coroutine(T* this);
[0e76cf4f]32};
33
[596f987b]34//-----------------------------------------------------------------------------
35// Ctors and dtors
36void ?{}(coStack_t* this);
37void ?{}(coroutine* this);
38void ^?{}(coStack_t* this);
39void ^?{}(coroutine* this);
40
41//-----------------------------------------------------------------------------
42// Public coroutine API
43static inline void suspend();
[0e76cf4f]44
[80d9e49]45forall(dtype T | is_coroutine(T))
[596f987b]46static inline void resume(T* cor);
[0e76cf4f]47
[80d9e49]48forall(dtype T | is_coroutine(T))
49void prime(T* cor);
50
[596f987b]51//-----------------------------------------------------------------------------
52// PRIVATE exposed because of inline
53
54// Start coroutine routines
55extern "C" {
56      forall(dtype T | is_coroutine(T))
57      void CtxInvokeCoroutine(T* this);
58
59      forall(dtype T | is_coroutine(T))
60      void CtxStart(T* this, void (*invoke)(T*));
61}
62
63// Get current coroutine
64extern coroutine* current_coroutine; //PRIVATE, never use directly
65static inline coroutine* this_coroutine(void) {
66        return current_coroutine;
67}
68
69// Private wrappers for context switch and stack creation
70extern void corCxtSw(coroutine* src, coroutine* dst);
71extern void create_stack( coStack_t* this, unsigned int storageSize );
72
73// Suspend implementation inlined for performance
74static inline void suspend() {
75      coroutine* src = this_coroutine();                // optimization
76
77        assertf( src->last != 0,
78                "Attempt to suspend coroutine %.256s (%p) that has never been resumed.\n"
79                "Possible cause is a suspend executed in a member called by a coroutine user rather than by the coroutine main.",
80                src->name, src );
81        assertf( src->last->notHalted,
82                "Attempt by coroutine %.256s (%p) to suspend back to terminated coroutine %.256s (%p).\n"
83                "Possible cause is terminated coroutine's main routine has already returned.",
84                src->name, src, src->last->name, src->last );
85
86        corCxtSw( src, src->last );
87}
88
89// Resume implementation inlined for performance
90forall(dtype T | is_coroutine(T))
91static inline void resume(T* cor) {
92        coroutine* src = this_coroutine();              // optimization
93        coroutine* dst = get_coroutine(cor);
94
95      if( unlikely(!dst->stack.base) ) {
96                create_stack(&dst->stack, dst->stack.size);
97                CtxStart(cor, CtxInvokeCoroutine);
98        }
99
100      // not resuming self ?
101        if ( src != dst ) {
102                assertf( dst->notHalted ,
103                        "Attempt by coroutine %.256s (%p) to resume terminated coroutine %.256s (%p).\n"
104                        "Possible cause is terminated coroutine's main routine has already returned.",
105                        src->name, src, dst->name, dst );
106
107            // set last resumer
108                dst->last = src;
109        } // if
110
111      // always done for performance testing
112        corCxtSw( src, dst );
113}
114
[17e5e2b]115#endif //THREADS_H
116
117#else
118#include_next <thread>
119#endif //__CFORALL__
[78b3f52]120
121// Local Variables: //
122// mode: c //
123// tab-width: 4 //
124// End: //
Note: See TracBrowser for help on using the repository browser.