Ignore:
File:
1 edited

Legend:

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

    rc84e80a re15df4c  
    2323#include "invoke.h"
    2424
    25 #include <stdlib>
     25extern "C" {
     26        #include <stddef.h>
     27}
     28
     29extern processor * get_this_processor();
    2630
    2731//-----------------------------------------------------------------------------
    2832// Forward declarations
    29 forall(otype T | is_thread(T) )
    30 void start( thread(T)* this );
     33forall( dtype T | sized(T) | is_thread(T) )
     34void start( T* this );
    3135
    32 forall(otype T | is_thread(T) )
    33 void stop( thread(T)* this );
     36forall( dtype T | sized(T) | is_thread(T) )
     37void stop( T* this );
    3438
    3539//-----------------------------------------------------------------------------
    3640// Thread ctors and dtors
    3741
    38 void ?{}(thread_h* this) {
     42void ?{}(thread* this) {
    3943        (&this->c){};
     44        this->c.name = "Anonymous Coroutine";
     45        (&this->lock){};
     46        this->next = NULL;
    4047}
    4148
    42 void ^?{}(thread_h* this) {
     49void ^?{}(thread* this) {
    4350        ^(&this->c){};
    4451}
    4552
    46 forall(otype T | is_thread(T) )
    47 void ?{}( thread(T)* this ) {
    48         printf("thread() ctor\n");
     53forall( dtype T | sized(T) | is_thread(T) | { void ?{}(T*); } )
     54void ?{}( scoped(T)* this ) {
    4955        (&this->handle){};
    50         start(this);
     56        start(&this->handle);
    5157}
    5258
    53 forall(otype T, ttype P | is_thread(T) | { void ?{}(T*, P); } )
    54 void ?{}( thread(T)* this, P params ) {
     59forall( dtype T, ttype P | sized(T) | is_thread(T) | { void ?{}(T*, P); } )
     60void ?{}( scoped(T)* this, P params ) {
    5561        (&this->handle){ params };
    56         start(this);
     62        start(&this->handle);
    5763}
    5864
    59 forall(otype T | is_thread(T) )
    60 void ^?{}( thread(T)* this ) {
    61         stop(this);
     65forall( dtype T | sized(T) | is_thread(T) | { void ^?{}(T*); } )
     66void ^?{}( scoped(T)* this ) {
     67        stop(&this->handle);
    6268        ^(&this->handle){};
    6369}
     
    7076}
    7177
    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);
     78extern void thread_schedule( thread * );
     79
     80forall( dtype T | sized(T) | is_thread(T) )
     81void start( T* this ) {
     82        coroutine* thrd_c = get_coroutine(this);
     83        thread*  thrd_h = get_thread   (this);
    7784        thrd_c->last = this_coroutine();
    78         current_coroutine = thrd_c;
     85        get_this_processor()->current_coroutine = thrd_c;
    7986
    8087        // LIB_DEBUG_PRINTF("Thread start : %p (t %p, c %p)\n", handle, thrd_c, thrd_h);
    8188
    8289        create_stack(&thrd_c->stack, thrd_c->stack.size);
    83         CtxStart(handle, CtxInvokeThread);
     90        CtxStart(this, CtxInvokeThread);
    8491        CtxSwitch( thrd_c->last->stack.context, thrd_c->stack.context );
    8592
    86         scheduler_add(thrd_h);
     93        thread_schedule(thrd_h);
    8794}
    8895
    89 forall(otype T | is_thread(T) )
    90 void stop( thread(T)* this ) {
     96forall( dtype T | sized(T) | is_thread(T) )
     97void stop( T* this ) {
     98        thread*  thrd = get_thread(this);
     99        if( thrd->c.notHalted ) {
     100                lock( &thrd->lock );
     101        }
     102}
    91103
     104void signal_termination( thread * this ) {
     105        this->c.state = Halt;
     106      this->c.notHalted = false;
     107        unlock( &this->lock );
     108}
     109
     110void yield( void ) {
     111        thread_schedule( this_thread() );
     112        suspend();
    92113}
    93114
Note: See TracChangeset for help on using the changeset viewer.