source: src/libcfa/concurrency/threads @ dd0c97b

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

Some optimization and clean-up of coroutines

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