Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/concurrency/threads.c

    rc84e80a r596f987b  
    1 //                              -*- Mode: CFA -*-
    21//
    32// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
     
    98//
    109// Author           : Thierry Delisle
    11 // Created On       : Tue Jan 17 12:27:26 2016
     10// Created On       : Mon Nov 28 12:27:26 2016
    1211// Last Modified By : Thierry Delisle
    13 // Last Modified On : --
     12// Last Modified On : Mon Nov 28 12:27:26 2016
    1413// Update Count     : 0
    1514//
    1615
     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
    1725#include "threads"
    18 
    19 #include "kernel"
    2026#include "libhdr.h"
    2127
     
    2329#include "invoke.h"
    2430
    25 #include <stdlib>
     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;
    2652
    2753//-----------------------------------------------------------------------------
    28 // Forward declarations
    29 forall(otype T | is_thread(T) )
    30 void start( thread(T)* this );
    31 
    32 forall(otype T | is_thread(T) )
    33 void stop( thread(T)* this );
    34 
    35 //-----------------------------------------------------------------------------
    36 // Thread ctors and dtors
    37 
    38 void ?{}(thread_h* this) {
    39         (&this->c){};
     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;       
    4063}
    4164
    42 void ^?{}(thread_h* this) {
    43         ^(&this->c){};
     65void ?{}(coStack_t* this, size_t size) {
     66        this{};
     67        this->size = size;
     68
     69        create_stack(this, this->size);
    4470}
    4571
    46 forall(otype T | is_thread(T) )
    47 void ?{}( thread(T)* this ) {
    48         printf("thread() ctor\n");
    49         (&this->handle){};
    50         start(this);
     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;
    5179}
    5280
    53 forall(otype T, ttype P | is_thread(T) | { void ?{}(T*, P); } )
    54 void ?{}( thread(T)* this, P params ) {
    55         (&this->handle){ params };
    56         start(this);
     81void ?{}(coroutine* this, size_t size) {
     82        this{};
     83        (&this->stack){size};
    5784}
    5885
    59 forall(otype T | is_thread(T) )
    60 void ^?{}( thread(T)* this ) {
    61         stop(this);
    62         ^(&this->handle){};
     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        }
    6395}
    6496
    65 //-----------------------------------------------------------------------------
    66 // Starting and stopping threads
    67 extern "C" {
    68       forall(dtype T | is_thread(T))
    69       void CtxInvokeThread(T * this);
     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);
    70108}
    71109
    72 forall(otype T | is_thread(T))
    73 void start( thread(T)* this ) {
    74         T* handle  = &this->handle;
    75         coroutine* thrd_c = get_coroutine(handle);
    76         thread_h*  thrd_h = get_thread   (handle);
    77         thrd_c->last = this_coroutine();
    78         current_coroutine = thrd_c;
    79 
    80         // LIB_DEBUG_PRINTF("Thread start : %p (t %p, c %p)\n", handle, thrd_c, thrd_h);
    81 
    82         create_stack(&thrd_c->stack, thrd_c->stack.size);
    83         CtxStart(handle, CtxInvokeThread);
    84         CtxSwitch( thrd_c->last->stack.context, thrd_c->stack.context );
    85 
    86         scheduler_add(thrd_h);
     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();
    87114}
    88115
    89 forall(otype T | is_thread(T) )
    90 void stop( thread(T)* this ) {
     116void corCxtSw(coroutine* src, coroutine* dst) {
     117        // THREAD_GETMEM( This )->disableInterrupts();
    91118
     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;
    92177}
    93178
Note: See TracChangeset for help on using the changeset viewer.