Changeset 6a3d2e7 for src


Ignore:
Timestamp:
Jan 17, 2017, 1:33:11 PM (7 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
2fc88be
Parents:
aed3f54
Message:

Moved coroutines code in libcfa to coroutines header

Location:
src/libcfa/concurrency
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/concurrency/coroutines

    raed3f54 r6a3d2e7  
     1//                              -*- Mode: CFA -*-
     2//
     3// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
     4//
     5// The contents of this file are covered under the licence agreement in the
     6// file "LICENCE" distributed with Cforall.
     7//
     8// coroutines --
     9//
     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
     15//
     16
     17#ifndef COROUTINES_H
     18#define COROUTINES_H
     19
     20#include "assert"       //
     21#include "invoke.h"
     22
     23//-----------------------------------------------------------------------------
     24// Coroutine trait
     25// Anything that implements this trait can be resumed.
     26// Anything that is resumed is a coroutine.
     27trait is_coroutine(dtype T) {
     28      void co_main(T* this);
     29      coroutine* get_coroutine(T* this);
     30};
     31
     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();
     42
     43forall(dtype T | is_coroutine(T))
     44static inline void resume(T* cor);
     45
     46forall(dtype T | is_coroutine(T))
     47void prime(T* cor);
     48
     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
     113#endif //COROUTINES_H
     114
     115// Local Variables: //
     116// mode: c //
     117// tab-width: 4 //
     118// End: //
  • src/libcfa/concurrency/coroutines.c

    raed3f54 r6a3d2e7  
     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// coroutines.c --
     8//
     9// Author           : Thierry Delisle
     10// Created On       : Mon Nov 28 12:27:26 2016
     11// Last Modified By : Thierry Delisle
     12// Last Modified On : Mon Nov 28 12:27:26 2016
     13// Update Count     : 0
     14//
     15
     16extern "C" {
     17#include <stddef.h>
     18#include <malloc.h>
     19#include <errno.h>
     20#include <string.h>
     21#include <unistd.h>
     22#include <sys/mman.h>
     23}
     24
     25#include "coroutines"
     26#include "libhdr.h"
     27
     28#define __CFA_INVOKE_PRIVATE__
     29#include "invoke.h"
     30
     31//-----------------------------------------------------------------------------
     32// Global state variables
     33
     34// minimum feasible stack size in bytes
     35#define MinStackSize 1000
     36static size_t pageSize = 0;                             // architecture pagesize HACK, should go in proper runtime singleton
     37
     38//Extra private desctructor for the main
     39//FIXME the main should not actually allocate a stack
     40//Since the main is never resumed the extra stack does not cause
     41//any problem but it is wasted memory
     42void ?{}(coStack_t* this, size_t size);
     43void ?{}(coroutine* this, size_t size);
     44
     45//Main coroutine
     46//FIXME do not construct a stack for the main
     47coroutine main_coroutine = { 1000 };
     48
     49//Current coroutine
     50//Will need to be in TLS when multi-threading is added
     51coroutine* current_coroutine = &main_coroutine;
     52
     53//-----------------------------------------------------------------------------
     54// Coroutine ctors and dtors
     55void ?{}(coStack_t* this) {
     56        this->size              = 10240;        // size of stack
     57        this->storage   = NULL; // pointer to stack
     58        this->limit             = NULL; // stack grows towards stack limit
     59        this->base              = NULL; // base of stack
     60        this->context   = NULL; // address of cfa_context_t
     61        this->top               = NULL; // address of top of storage
     62        this->userStack = false;       
     63}
     64
     65void ?{}(coStack_t* this, size_t size) {
     66        this{};
     67        this->size = size;
     68
     69        create_stack(this, this->size);
     70}
     71
     72void ?{}(coroutine* this) {
     73        this->name = "Anonymous Coroutine";
     74        this->errno_ = 0;
     75        this->state = Start;
     76      this->notHalted = true;
     77        this->starter = NULL;
     78        this->last = NULL;
     79}
     80
     81void ?{}(coroutine* this, size_t size) {
     82        this{};
     83        (&this->stack){size};
     84}
     85
     86void ^?{}(coStack_t* this) {
     87        if ( ! this->userStack ) {
     88                LIB_DEBUG_DO(
     89                        if ( mprotect( this->storage, pageSize, PROT_READ | PROT_WRITE ) == -1 ) {
     90                                abortf( "(coStack_t *)%p.^?{}() : internal error, mprotect failure, error(%d) %s.", this, errno, strerror( errno ) );
     91                        }
     92                );
     93                free( this->storage );
     94        }
     95}
     96
     97void ^?{}(coroutine* this) {}
     98
     99// Part of the Public API
     100// Not inline since only ever called once per coroutine
     101forall(dtype T | is_coroutine(T))
     102void prime(T* cor) {
     103        coroutine* this = get_coroutine(cor);
     104        assert(this->state == Start);
     105
     106        this->state = Primed;
     107        resume(cor);
     108}
     109
     110// We need to call suspend from invoke.c, so we expose this wrapper that
     111// is not inline (We can't inline Cforall in C)
     112void suspend_no_inline(void) {
     113        suspend();
     114}
     115
     116void corCxtSw(coroutine* src, coroutine* dst) {
     117        // THREAD_GETMEM( This )->disableInterrupts();
     118
     119        // set state of current coroutine to inactive
     120        src->state = Inactive;
     121
     122        // set new coroutine that task is executing
     123        current_coroutine = dst;                       
     124
     125        // context switch to specified coroutine
     126        CtxSwitch( src->stack.context, dst->stack.context );
     127        // when CtxSwitch returns we are back in the src coroutine             
     128
     129        // set state of new coroutine to active
     130        src->state = Active;
     131
     132        // THREAD_GETMEM( This )->enableInterrupts();
     133} //ctxSwitchDirect
     134
     135void create_stack( coStack_t* this, unsigned int storageSize ) {
     136        //TEMP HACK do this on proper kernel startup
     137        if(pageSize == 0ul) pageSize = sysconf( _SC_PAGESIZE );
     138
     139        size_t cxtSize = libCeiling( sizeof(machine_context_t), 8 ); // minimum alignment
     140
     141        if ( (intptr_t)this->storage == 0 ) {
     142                this->userStack = false;
     143                this->size = libCeiling( storageSize, 16 );
     144                // use malloc/memalign because "new" raises an exception for out-of-memory
     145               
     146                // assume malloc has 8 byte alignment so add 8 to allow rounding up to 16 byte alignment
     147                LIB_DEBUG_DO( this->storage = memalign( pageSize, cxtSize + this->size + pageSize ) );
     148                LIB_NO_DEBUG_DO( this->storage = malloc( cxtSize + this->size + 8 ) );
     149
     150                LIB_DEBUG_DO(
     151                        if ( mprotect( this->storage, pageSize, PROT_NONE ) == -1 ) {
     152                                abortf( "(uMachContext &)%p.createContext() : internal error, mprotect failure, error(%d) %s.", this, (int)errno, strerror( (int)errno ) );
     153                        } // if
     154                );
     155
     156                if ( (intptr_t)this->storage == 0 ) {
     157                        abortf( "Attempt to allocate %d bytes of storage for coroutine or task execution-state but insufficient memory available.", this->size );
     158                } // if
     159
     160                LIB_DEBUG_DO( this->limit = (char *)this->storage + pageSize );
     161                LIB_NO_DEBUG_DO( this->limit = (char *)libCeiling( (unsigned long)this->storage, 16 ) ); // minimum alignment
     162
     163        } else {
     164                assertf( ((size_t)this->storage & (libAlign() - 1)) != 0ul, "Stack storage %p for task/coroutine must be aligned on %d byte boundary.", this->storage, (int)libAlign() );
     165                this->userStack = true;
     166                this->size = storageSize - cxtSize;
     167
     168                if ( this->size % 16 != 0u ) this->size -= 8;
     169
     170                this->limit = (char *)libCeiling( (unsigned long)this->storage, 16 ); // minimum alignment
     171        } // if
     172        assertf( this->size >= MinStackSize, "Stack size %d provides less than minimum of %d bytes for a stack.", this->size, MinStackSize );
     173
     174        this->base = (char *)this->limit + this->size;
     175        this->context = this->base;
     176        this->top = (char *)this->context + cxtSize;
     177}
     178
     179// Local Variables: //
     180// mode: c //
     181// tab-width: 4 //
     182// End: //
  • src/libcfa/concurrency/threads

    raed3f54 r6a3d2e7  
    99//
    1010// Author           : Thierry Delisle
    11 // Created On       : Mon Nov 28 12:27:26 2016
     11// Created On       : Tue Jan 17 12:27:26 2016
    1212// Last Modified By : Thierry Delisle
    13 // Last Modified On : Mon Nov 28 12:27:26 2016
     13// Last Modified On : --
    1414// Update Count     : 0
    1515//
     
    1818#define THREADS_H
    1919
    20 #include "assert"       //
    21 #include "invoke.h"
    2220
    23 //-----------------------------------------------------------------------------
    24 // Coroutine trait
    25 // Anything that implements this trait can be resumed.
    26 // Anything that is resumed is a coroutine.
    27 trait is_coroutine(dtype T) {
    28       void co_main(T* this);
    29       coroutine* get_coroutine(T* this);
    30 };
    31 
    32 //-----------------------------------------------------------------------------
    33 // Ctors and dtors
    34 void ?{}(coStack_t* this);
    35 void ?{}(coroutine* this);
    36 void ^?{}(coStack_t* this);
    37 void ^?{}(coroutine* this);
    38 
    39 //-----------------------------------------------------------------------------
    40 // Public coroutine API
    41 static inline void suspend();
    42 
    43 forall(dtype T | is_coroutine(T))
    44 static inline void resume(T* cor);
    45 
    46 forall(dtype T | is_coroutine(T))
    47 void prime(T* cor);
    48 
    49 //-----------------------------------------------------------------------------
    50 // PRIVATE exposed because of inline
    51 
    52 // Start coroutine routines
    53 extern "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
    62 extern coroutine* current_coroutine; //PRIVATE, never use directly
    63 static inline coroutine* this_coroutine(void) {
    64         return current_coroutine;
    65 }
    66 
    67 // Private wrappers for context switch and stack creation
    68 extern void corCxtSw(coroutine* src, coroutine* dst);
    69 extern void create_stack( coStack_t* this, unsigned int storageSize );
    70 
    71 // Suspend implementation inlined for performance
    72 static 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
    88 forall(dtype T | is_coroutine(T))
    89 static 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 }
    11221
    11322#endif //THREADS_H
  • src/libcfa/concurrency/threads.c

    raed3f54 r6a3d2e7  
     1//                              -*- Mode: CFA -*-
    12//
    23// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
     
    56// file "LICENCE" distributed with Cforall.
    67//
    7 // threads.c --
     8// threads --
    89//
    910// Author           : Thierry Delisle
    10 // Created On       : Mon Nov 28 12:27:26 2016
     11// Created On       : Tue Jan 17 12:27:26 2016
    1112// Last Modified By : Thierry Delisle
    12 // Last Modified On : Mon Nov 28 12:27:26 2016
     13// Last Modified On : --
    1314// Update Count     : 0
    1415//
    1516
    16 extern "C" {
    17 #include <stddef.h>
    18 #include <malloc.h>
    19 #include <errno.h>
    20 #include <string.h>
    21 #include <unistd.h>
    22 #include <sys/mman.h>
    23 }
    2417
    25 #include "threads"
    26 #include "libhdr.h"
    27 
    28 #define __CFA_INVOKE_PRIVATE__
    29 #include "invoke.h"
    30 
    31 //-----------------------------------------------------------------------------
    32 // Global state variables
    33 
    34 // minimum feasible stack size in bytes
    35 #define MinStackSize 1000
    36 static size_t pageSize = 0;                             // architecture pagesize HACK, should go in proper runtime singleton
    37 
    38 //Extra private desctructor for the main
    39 //FIXME the main should not actually allocate a stack
    40 //Since the main is never resumed the extra stack does not cause
    41 //any problem but it is wasted memory
    42 void ?{}(coStack_t* this, size_t size);
    43 void ?{}(coroutine* this, size_t size);
    44 
    45 //Main coroutine
    46 //FIXME do not construct a stack for the main
    47 coroutine main_coroutine = { 1000 };
    48 
    49 //Current coroutine
    50 //Will need to be in TLS when multi-threading is added
    51 coroutine* current_coroutine = &main_coroutine;
    52 
    53 //-----------------------------------------------------------------------------
    54 // Coroutine ctors and dtors
    55 void ?{}(coStack_t* this) {
    56         this->size              = 10240;        // size of stack
    57         this->storage   = NULL; // pointer to stack
    58         this->limit             = NULL; // stack grows towards stack limit
    59         this->base              = NULL; // base of stack
    60         this->context   = NULL; // address of cfa_context_t
    61         this->top               = NULL; // address of top of storage
    62         this->userStack = false;       
    63 }
    64 
    65 void ?{}(coStack_t* this, size_t size) {
    66         this{};
    67         this->size = size;
    68 
    69         create_stack(this, this->size);
    70 }
    71 
    72 void ?{}(coroutine* this) {
    73         this->name = "Anonymous Coroutine";
    74         this->errno_ = 0;
    75         this->state = Start;
    76       this->notHalted = true;
    77         this->starter = NULL;
    78         this->last = NULL;
    79 }
    80 
    81 void ?{}(coroutine* this, size_t size) {
    82         this{};
    83         (&this->stack){size};
    84 }
    85 
    86 void ^?{}(coStack_t* this) {
    87         if ( ! this->userStack ) {
    88                 LIB_DEBUG_DO(
    89                         if ( mprotect( this->storage, pageSize, PROT_READ | PROT_WRITE ) == -1 ) {
    90                                 abortf( "(coStack_t *)%p.^?{}() : internal error, mprotect failure, error(%d) %s.", this, errno, strerror( errno ) );
    91                         }
    92                 );
    93                 free( this->storage );
    94         }
    95 }
    96 
    97 void ^?{}(coroutine* this) {}
    98 
    99 // Part of the Public API
    100 // Not inline since only ever called once per coroutine
    101 forall(dtype T | is_coroutine(T))
    102 void prime(T* cor) {
    103         coroutine* this = get_coroutine(cor);
    104         assert(this->state == Start);
    105 
    106         this->state = Primed;
    107         resume(cor);
    108 }
    109 
    110 // We need to call suspend from invoke.c, so we expose this wrapper that
    111 // is not inline (We can't inline Cforall in C)
    112 void suspend_no_inline(void) {
    113         suspend();
    114 }
    115 
    116 void corCxtSw(coroutine* src, coroutine* dst) {
    117         // THREAD_GETMEM( This )->disableInterrupts();
    118 
    119         // set state of current coroutine to inactive
    120         src->state = Inactive;
    121 
    122         // set new coroutine that task is executing
    123         current_coroutine = dst;                       
    124 
    125         // context switch to specified coroutine
    126         CtxSwitch( src->stack.context, dst->stack.context );
    127         // when CtxSwitch returns we are back in the src coroutine             
    128 
    129         // set state of new coroutine to active
    130         src->state = Active;
    131 
    132         // THREAD_GETMEM( This )->enableInterrupts();
    133 } //ctxSwitchDirect
    134 
    135 void create_stack( coStack_t* this, unsigned int storageSize ) {
    136         //TEMP HACK do this on proper kernel startup
    137         if(pageSize == 0ul) pageSize = sysconf( _SC_PAGESIZE );
    138 
    139         size_t cxtSize = libCeiling( sizeof(machine_context_t), 8 ); // minimum alignment
    140 
    141         if ( (intptr_t)this->storage == 0 ) {
    142                 this->userStack = false;
    143                 this->size = libCeiling( storageSize, 16 );
    144                 // use malloc/memalign because "new" raises an exception for out-of-memory
    145                
    146                 // assume malloc has 8 byte alignment so add 8 to allow rounding up to 16 byte alignment
    147                 LIB_DEBUG_DO( this->storage = memalign( pageSize, cxtSize + this->size + pageSize ) );
    148                 LIB_NO_DEBUG_DO( this->storage = malloc( cxtSize + this->size + 8 ) );
    149 
    150                 LIB_DEBUG_DO(
    151                         if ( mprotect( this->storage, pageSize, PROT_NONE ) == -1 ) {
    152                                 abortf( "(uMachContext &)%p.createContext() : internal error, mprotect failure, error(%d) %s.", this, (int)errno, strerror( (int)errno ) );
    153                         } // if
    154                 );
    155 
    156                 if ( (intptr_t)this->storage == 0 ) {
    157                         abortf( "Attempt to allocate %d bytes of storage for coroutine or task execution-state but insufficient memory available.", this->size );
    158                 } // if
    159 
    160                 LIB_DEBUG_DO( this->limit = (char *)this->storage + pageSize );
    161                 LIB_NO_DEBUG_DO( this->limit = (char *)libCeiling( (unsigned long)this->storage, 16 ) ); // minimum alignment
    162 
    163         } else {
    164                 assertf( ((size_t)this->storage & (libAlign() - 1)) != 0ul, "Stack storage %p for task/coroutine must be aligned on %d byte boundary.", this->storage, (int)libAlign() );
    165                 this->userStack = true;
    166                 this->size = storageSize - cxtSize;
    167 
    168                 if ( this->size % 16 != 0u ) this->size -= 8;
    169 
    170                 this->limit = (char *)libCeiling( (unsigned long)this->storage, 16 ); // minimum alignment
    171         } // if
    172         assertf( this->size >= MinStackSize, "Stack size %d provides less than minimum of %d bytes for a stack.", this->size, MinStackSize );
    173 
    174         this->base = (char *)this->limit + this->size;
    175         this->context = this->base;
    176         this->top = (char *)this->context + cxtSize;
    177 }
    17818
    17919// Local Variables: //
Note: See TracChangeset for help on using the changeset viewer.