Changeset 8118303


Ignore:
Timestamp:
Jan 17, 2017, 5:13:47 PM (5 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
aaron-thesis, arm-eh, cleanup-dtors, deferred_resn, demangler, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, resolv-new, with_gc
Children:
c49bf54
Parents:
7350ff9
Message:

First prototype of cfa threads running (1 thread on 1 processor)

Location:
src/libcfa/concurrency
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/concurrency/coroutines

    r7350ff9 r8118303  
    1 //                              -*- Mode: CFA -*-
     1//                              - *- Mode: CFA - *-
    22//
    33// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
     
    1818#define COROUTINES_H
    1919
    20 #include "assert"       //
     20#include "assert"
    2121#include "invoke.h"
    2222
     
    2626// Anything that is resumed is a coroutine.
    2727trait is_coroutine(dtype T) {
    28       void co_main(T* this);
    29       coroutine* get_coroutine(T* this);
     28      void co_main(T * this);
     29      coroutine * get_coroutine(T * this);
    3030};
    3131
    3232//-----------------------------------------------------------------------------
    3333// Ctors and dtors
    34 void ?{}(coStack_t* this);
    35 void ?{}(coroutine* this);
    36 void ^?{}(coStack_t* this);
    37 void ^?{}(coroutine* this);
     34void ?{}(coStack_t * this);
     35void ?{}(coroutine * this);
     36void ^?{}(coStack_t * this);
     37void ^?{}(coroutine * this);
    3838
    3939//-----------------------------------------------------------------------------
     
    4242
    4343forall(dtype T | is_coroutine(T))
    44 static inline void resume(T* cor);
     44static inline void resume(T * cor);
    4545
    4646forall(dtype T | is_coroutine(T))
    47 void prime(T* cor);
     47void prime(T * cor);
    4848
    4949//-----------------------------------------------------------------------------
     
    5353extern "C" {
    5454      forall(dtype T | is_coroutine(T))
    55       void CtxInvokeCoroutine(T* this);
     55      void CtxInvokeCoroutine(T * this);
    5656
    5757      forall(dtype T | is_coroutine(T))
    58       void CtxStart(T* this, void (*invoke)(T*));
     58      void CtxStart(T * this, void ( *invoke)(T *));
    5959}
    6060
    6161// Get current coroutine
    62 extern coroutine* current_coroutine; //PRIVATE, never use directly
    63 static inline coroutine* this_coroutine(void) {
     62extern coroutine * current_coroutine; //PRIVATE, never use directly
     63static inline coroutine * this_coroutine(void) {
    6464        return current_coroutine;
    6565}
    6666
    6767// 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 );
     68extern void corCxtSw(coroutine * src, coroutine * dst);
     69extern void create_stack( coStack_t * this, unsigned int storageSize );
    7070
    7171// Suspend implementation inlined for performance
    7272static inline void suspend() {
    73       coroutine* src = this_coroutine();                // optimization
     73      coroutine * src = this_coroutine();               // optimization
    7474
    7575        assertf( src->last != 0,
     
    8787// Resume implementation inlined for performance
    8888forall(dtype T | is_coroutine(T))
    89 static inline void resume(T* cor) {
    90         coroutine* src = this_coroutine();              // optimization
    91         coroutine* dst = get_coroutine(cor);
     89static inline void resume(T * cor) {
     90        coroutine * src = this_coroutine();             // optimization
     91        coroutine * dst = get_coroutine(cor);
    9292
    9393      if( unlikely(!dst->stack.base) ) {
  • src/libcfa/concurrency/coroutines.c

    r7350ff9 r8118303  
    111111// is not inline (We can't inline Cforall in C)
    112112void suspend_no_inline(void) {
     113        LIB_DEBUG_PRINTF("Suspending back : to %p from %p\n", this_coroutine(), this_coroutine() ? this_coroutine()->last : (void*)-1);
     114
    113115        suspend();
    114116}
  • src/libcfa/concurrency/invoke.c

    r7350ff9 r8118303  
    2020      void *this
    2121) {
    22       LIB_DEBUG_PRINTF("Invoke : Received %p (main %p, get_c %p)\n", this, main, get_coroutine);
     22      LIB_DEBUG_PRINTF("Invoke Coroutine : Received %p (main %p, get_c %p)\n", this, main, get_coroutine);
    2323
    2424      struct coroutine* cor = get_coroutine( this );
     
    3131
    3232      main( this );
     33
     34      //Final suspend, should never return
     35      __suspend_no_inline__F___1();
     36      assertf(false, "Resumed dead coroutine");
     37}
     38
     39void CtxInvokeThread(
     40      void (*main)(void *),
     41      struct thread_h *(*get_thread)(void *),
     42      void *this
     43) {
     44      LIB_DEBUG_PRINTF("Invoke Thread : Received %p (main %p, get_t %p)\n", this, main, get_thread);
     45
     46      __suspend_no_inline__F___1();
     47
     48      struct coroutine* cor = &get_thread( this )->c;
     49      cor->state = Active;
     50
     51      LIB_DEBUG_PRINTF("Invoke Thread : invoking main %p (args %p)\n", main, this);
     52      main( this );
     53
     54      //Final suspend, should never return
     55      __suspend_no_inline__F___1();
     56      assertf(false, "Resumed dead thread");
    3357}
    3458
     
    4064      void (*invoke)(void *)
    4165) {
    42       LIB_DEBUG_PRINTF("StartCoroutine : Passing in %p (main %p, get_c %p) to %p\n", this, main, get_coroutine, invoke);
     66      LIB_DEBUG_PRINTF("StartCoroutine : Passing in %p (main %p) to invoke (%p) from start (%p)\n", this, main, invoke, CtxStart);
    4367
    4468      struct coStack_t* stack = &get_coroutine( this )->stack;
  • src/libcfa/concurrency/invoke.h

    r7350ff9 r8118303  
    3535      };
    3636
     37      struct thread_h {
     38            struct coroutine c;
     39      };
     40
    3741#endif //_INVOKE_H_
    3842#else //! defined(__CFA_INVOKE_PRIVATE__)
  • src/libcfa/concurrency/kernel

    r7350ff9 r8118303  
     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// threads --
     9//
     10// Author           : Thierry Delisle
     11// Created On       : Tue Jan 17 12:27:26 2016
     12// Last Modified By : Thierry Delisle
     13// Last Modified On : --
     14// Update Count     : 0
     15//
     16
     17#ifndef KERNEL_H
     18#define KERNEL_H
     19
     20extern struct thread_h * the_thread;
     21
     22void kernel_run( void );
     23
     24#endif //KERNEL_H
     25
     26// Local Variables: //
     27// mode: c //
     28// tab-width: 4 //
     29// End: //
  • src/libcfa/concurrency/kernel.c

    r7350ff9 r8118303  
     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// kernel.c --
     9//
     10// Author           : Thierry Delisle
     11// Created On       : Tue Jan 17 12:27:26 2016
     12// Last Modified By : Thierry Delisle
     13// Last Modified On : --
     14// Update Count     : 0
     15//
     16
     17//Header
     18#include "kernel"
     19
     20//C Includes
     21#include <stdbool.h>
     22
     23//CFA Includes
     24#include "libhdr.h"
     25#include "threads"
     26
     27//Private includes
     28#define __CFA_INVOKE_PRIVATE__
     29#include "invoke.h"
     30
     31thread_h * the_thread = 0;
     32
     33void kernel_run( void ) {
     34       
     35        bool done = true;
     36        coroutine* processor_cor = this_coroutine();
     37        LIB_DEBUG_PRINTF("Kernel : processor cor is %p\n", processor_cor);
     38
     39        do {
     40                thread_h * dst = the_thread;
     41
     42                LIB_DEBUG_PRINTF("Kernel : picked thread %p\n", dst);
     43
     44                // set new coroutine that task is executing
     45                current_coroutine = &dst->c;
     46
     47                // context switch to specified coroutine
     48                LIB_DEBUG_PRINTF("Kernel : switching to ctx %p (from %p)\n", current_coroutine, processor_cor);
     49                CtxSwitch( processor_cor->stack.context, current_coroutine->stack.context );
     50                // when CtxSwitch returns we are back in the processor coroutine
     51        } while( ! done );
     52}
     53
     54// Local Variables: //
     55// mode: c //
     56// tab-width: 4 //
     57// End: //
  • src/libcfa/concurrency/threads

    r7350ff9 r8118303  
    1818#define THREADS_H
    1919
     20#include "assert"
     21#include "invoke.h"
    2022
     23#include "coroutines"
     24
     25//-----------------------------------------------------------------------------
     26// Coroutine trait
     27// Anything that implements this trait can be resumed.
     28// Anything that is resumed is a coroutine.
     29trait is_thread(dtype T /*| sized(T)*/) {
     30      void co_main(T* this);
     31      thread_h* get_thread(T* this);
     32        /*void ?{}(T*);
     33        void ^?{}(T*);*/
     34};
     35
     36forall(otype T | is_thread(T) )
     37static inline coroutine* get_coroutine(T* this) {
     38        return &get_thread(this)->c;
     39}
     40
     41//-----------------------------------------------------------------------------
     42// Ctors and dtors
     43void ?{}(thread_h* this);
     44void ^?{}(thread_h* this);
     45
     46//-----------------------------------------------------------------------------
     47// thread runner
     48// Structure that actually start and stop threads
     49forall(otype T | is_thread(T) )
     50struct thread {
     51        T handle;
     52};
     53
     54forall(otype T | is_thread(T) )
     55void ?{}( thread(T)* this );
     56
     57forall(otype T, ttype P | is_thread(T) | { void ?{}(T*, P); } )
     58void ?{}( thread(T)* this, P params );
     59
     60forall(otype T | is_thread(T) )
     61void ^?{}( thread(T)* this );
     62
     63//-----------------------------------------------------------------------------
     64// PRIVATE exposed because of inline
    2165
    2266#endif //THREADS_H
  • src/libcfa/concurrency/threads.c

    r7350ff9 r8118303  
    66// file "LICENCE" distributed with Cforall.
    77//
    8 // threads --
     8// threads.c --
    99//
    1010// Author           : Thierry Delisle
     
    1515//
    1616
     17#include "threads"
    1718
     19#include "kernel"
     20#include "libhdr.h"
     21
     22#define __CFA_INVOKE_PRIVATE__
     23#include "invoke.h"
     24
     25#include <stdlib>
     26
     27//-----------------------------------------------------------------------------
     28// Forward declarations
     29forall(otype T | is_thread(T) )
     30void start( thread(T)* this );
     31
     32forall(otype T | is_thread(T) )
     33void stop( thread(T)* this );
     34
     35//-----------------------------------------------------------------------------
     36// Thread ctors and dtors
     37
     38void ?{}(thread_h* this) {
     39        (&this->c){};
     40}
     41
     42void ^?{}(thread_h* this) {
     43        ^(&this->c){};
     44}
     45
     46forall(otype T | is_thread(T) )
     47void ?{}( thread(T)* this ) {
     48        printf("thread() ctor\n");
     49        (&this->handle){};
     50        start(this);
     51}
     52
     53forall(otype T, ttype P | is_thread(T) | { void ?{}(T*, P); } )
     54void ?{}( thread(T)* this, P params ) {
     55        (&this->handle){ params };
     56        start(this);
     57}
     58
     59forall(otype T | is_thread(T) )
     60void ^?{}( thread(T)* this ) {
     61        stop(this);
     62        ^(&this->handle){};
     63}
     64
     65//-----------------------------------------------------------------------------
     66// Starting and stopping threads
     67extern "C" {
     68      forall(dtype T | is_thread(T))
     69      void CtxInvokeThread(T * this);
     70}
     71
     72forall(otype T | is_thread(T))
     73void 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        the_thread = thrd_h;
     87}
     88
     89forall(otype T | is_thread(T) )
     90void stop( thread(T)* this ) {
     91
     92}
    1893
    1994// Local Variables: //
Note: See TracChangeset for help on using the changeset viewer.