Changeset d9c44c3


Ignore:
Timestamp:
Dec 6, 2016, 4:16:47 PM (8 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:
fa66f4e
Parents:
e4745d7a
Message:

Implemented coroutine for i386 and added coroutines to tests

Location:
src
Files:
1 added
5 edited
1 moved

Legend:

Unmodified
Added
Removed
  • src/libcfa/assert

    re4745d7a rd9c44c3  
    1717#define __ASSERT_H__
    1818
     19#ifdef __CFORALL__
    1920extern "C" {
     21#endif //__CFORALL__
     22
    2023        #include <assert.h>
    2124
     
    2932                void __assert_fail_f( const char *assertion, const char *file, unsigned int line, const char *function, const char *fmt, ... ) __attribute__((noreturn));
    3033        #endif
    31 }
     34       
     35#ifdef __CFORALL__
     36} // extern "C"
     37#endif //__CFORALL__
    3238
    3339#endif // __ASSERT_H__
  • src/libcfa/concurrency/invoke.c

    re4745d7a rd9c44c3  
    44#include <stdio.h>
    55
     6#include "libhdr.h"
    67#include "invoke.h"
    78
     
    1819void __invokeCoroutine__F_P9scoVtablePv__1(struct coVtable *vtable, void* vthis)
    1920{
    20       printf("Invoke : Received %p (v %p)\n", vthis, vtable);
     21      LIB_DEBUG_PRINTF("Invoke : Received %p (v %p)\n", vthis, vtable);
    2122
    2223      struct coroutine* cor = vtable->this_coroutine(vthis);
     
    3435      void (*invoke)(struct coVtable *, void *)
    3536) {
    36 
    37       #if ! defined( __x86_64__ ) && ! defined( __i386__ )
    38             #error Only __x86_64__ and __i386__ is supported for threads in cfa
    39       #endif
    40 
    41       #if defined( __U_SWAPCONTEXT__ )
    42             #error __U_SWAPCONTEXT__ should not be defined for __x86_64__
    43       #endif
     37      LIB_DEBUG_PRINTF("StartCoroutine : Passing in %p (v %p) to %p\n", vthis, vtable, invoke);
    4438
    4539      struct coVtable * vtable = get_vtable( vthis );
     
    4741      struct coStack_t* stack = &this->stack;
    4842
     43#if defined( __i386__ )
     44
     45        struct FakeStack {
     46            void *fixedRegisters[3];                    // fixed registers ebx, edi, esi (popped on 1st uSwitch, values unimportant)
     47            void *rturn;                                      // where to go on return from uSwitch
     48            void *dummyReturn;                          // fake return compiler would have pushed on call to uInvoke
     49            void *argument[2];                          // for 16-byte ABI, 16-byte alignment starts here
     50            void *padding[2];                           // padding to force 16-byte alignment, as "base" is 16-byte aligned
     51        };
     52
     53        ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack );
     54        ((struct machine_context_t *)stack->context)->FP = NULL;                // terminate stack with NULL fp
     55
     56        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->dummyReturn = NULL;
     57        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->argument[0] = vtable; // argument to invoke
     58      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->argument[1] = vthis;  // argument to invoke
     59        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = invoke;
     60
     61#elif defined( __x86_64__ )
     62
    4963      struct FakeStack {
    5064            void *fixedRegisters[5];                    // fixed registers rbx, r12, r13, r14, r15
    5165            void *rturn;                                        // where to go on return from uSwitch
    5266            void *dummyReturn;                          // NULL return address to provide proper alignment
    53       }; // FakeStack
     67      };
    5468
    5569      ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack );
    5670      ((struct machine_context_t *)stack->context)->FP = NULL;          // terminate stack with NULL fp
    57 
    58       fprintf(stderr, "StartCoroutine : Passing in %p (v %p) to %p\n", vthis, vtable, invoke);
    5971
    6072      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->dummyReturn = NULL;
     
    6375      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[1] = vthis;
    6476      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[2] = invoke;
     77#else
     78      #error Only __i386__ and __x86_64__ is supported for threads in cfa
     79#endif
    6580}
  • src/libcfa/concurrency/threads.c

    re4745d7a rd9c44c3  
    166166        coroutine* dst = this_coroutine(cor);
    167167
    168         fprintf(stderr, "Resuming %p from %p\n", dst, src);
    169168        if ( src != dst ) {                             // not resuming self ?
    170169                assertf( dst->notHalted ,
     
    172171                        "Possible cause is terminated coroutine's main routine has already returned.",
    173172                        src->name, src, dst->name, dst );
    174                 fprintf(stderr, "Assigning last pointer\n");
    175173                dst->last = src;                                        // set last resumer
    176174        } // if
  • src/libcfa/libhdr/libdebug.h

    re4745d7a rd9c44c3  
    2525#endif
    2626
     27#ifdef __CFA_DEBUG_PRINT__
     28      #define LIB_DEBUG_PRINTF(...)   printf (__VA_ARGS__)
     29      #define LIB_DEBUG_FPRINTF(...) fprintf (stderr, __VA_ARGS__)
     30#else
     31      #define LIB_DEBUG_PRINTF(...)  ((void)0)
     32      #define LIB_DEBUG_FPRINTF(...) ((void)0)
     33#endif
     34
    2735#endif //__LIB_DEBUG_H__
    2836
  • src/libcfa/stdhdr/assert.h

    re4745d7a rd9c44c3  
    1414//
    1515
     16#ifdef __CFORALL__
    1617extern "C" {
    17 #include_next <assert.h>                                                                // has internal check for multiple expansion
     18#endif //__CFORALL__
     19
     20// has internal check for multiple expansion
     21#include_next <assert.h>
     22
     23#ifdef __CFORALL__
    1824} // extern "C"
     25#endif //__CFORALL__
    1926
    2027// Local Variables: //
  • src/tests/coroutine.c

    re4745d7a rd9c44c3  
    2828
    2929void co_main(Fibonacci* this) {
     30#ifdef MORE_DEBUG
    3031      sout | "Starting main of coroutine " | this | endl;
    3132      sout | "Started from " | this_coroutine(this)->last | endl;
     33#endif
    3234      int fn1, fn2;             // retained between resumes
    3335      this->fn = 0;
     
    6264
    6365int main() {
    64       Fibonacci f1;
    65       sout | "User coroutine : " | &f1 | endl;
     66      Fibonacci f1, f2;
     67#ifdef MORE_DEBUG     
     68      sout | "User coroutines : " | &f1 | ' ' | &f1 | endl;
     69#endif
    6670      for ( int i = 1; i <= 10; i += 1 ) {
    67             sout | next(&f1) | endl;
     71            sout | next(&f1) | ' ' | next(&f2) | endl;
    6872      }
    6973
Note: See TracChangeset for help on using the changeset viewer.