- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/libcfa/concurrency/threads.c
rc84e80a re15df4c 23 23 #include "invoke.h" 24 24 25 #include <stdlib> 25 extern "C" { 26 #include <stddef.h> 27 } 28 29 extern processor * get_this_processor(); 26 30 27 31 //----------------------------------------------------------------------------- 28 32 // Forward declarations 29 forall( otype T| is_thread(T) )30 void start( thread(T)* this );33 forall( dtype T | sized(T) | is_thread(T) ) 34 void start( T* this ); 31 35 32 forall( otype T| is_thread(T) )33 void stop( thread(T)* this );36 forall( dtype T | sized(T) | is_thread(T) ) 37 void stop( T* this ); 34 38 35 39 //----------------------------------------------------------------------------- 36 40 // Thread ctors and dtors 37 41 38 void ?{}(thread _h* this) {42 void ?{}(thread* this) { 39 43 (&this->c){}; 44 this->c.name = "Anonymous Coroutine"; 45 (&this->lock){}; 46 this->next = NULL; 40 47 } 41 48 42 void ^?{}(thread _h* this) {49 void ^?{}(thread* this) { 43 50 ^(&this->c){}; 44 51 } 45 52 46 forall(otype T | is_thread(T) ) 47 void ?{}( thread(T)* this ) { 48 printf("thread() ctor\n"); 53 forall( dtype T | sized(T) | is_thread(T) | { void ?{}(T*); } ) 54 void ?{}( scoped(T)* this ) { 49 55 (&this->handle){}; 50 start( this);56 start(&this->handle); 51 57 } 52 58 53 forall( otype T, ttype P| is_thread(T) | { void ?{}(T*, P); } )54 void ?{}( thread(T)* this, P params ) {59 forall( dtype T, ttype P | sized(T) | is_thread(T) | { void ?{}(T*, P); } ) 60 void ?{}( scoped(T)* this, P params ) { 55 61 (&this->handle){ params }; 56 start( this);62 start(&this->handle); 57 63 } 58 64 59 forall( otype T | is_thread(T))60 void ^?{}( thread(T)* this ) {61 stop( this);65 forall( dtype T | sized(T) | is_thread(T) | { void ^?{}(T*); } ) 66 void ^?{}( scoped(T)* this ) { 67 stop(&this->handle); 62 68 ^(&this->handle){}; 63 69 } … … 70 76 } 71 77 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); 78 extern void thread_schedule( thread * ); 79 80 forall( dtype T | sized(T) | is_thread(T) ) 81 void start( T* this ) { 82 coroutine* thrd_c = get_coroutine(this); 83 thread* thrd_h = get_thread (this); 77 84 thrd_c->last = this_coroutine(); 78 current_coroutine = thrd_c;85 get_this_processor()->current_coroutine = thrd_c; 79 86 80 87 // LIB_DEBUG_PRINTF("Thread start : %p (t %p, c %p)\n", handle, thrd_c, thrd_h); 81 88 82 89 create_stack(&thrd_c->stack, thrd_c->stack.size); 83 CtxStart( handle, CtxInvokeThread);90 CtxStart(this, CtxInvokeThread); 84 91 CtxSwitch( thrd_c->last->stack.context, thrd_c->stack.context ); 85 92 86 scheduler_add(thrd_h);93 thread_schedule(thrd_h); 87 94 } 88 95 89 forall(otype T | is_thread(T) ) 90 void stop( thread(T)* this ) { 96 forall( dtype T | sized(T) | is_thread(T) ) 97 void stop( T* this ) { 98 thread* thrd = get_thread(this); 99 if( thrd->c.notHalted ) { 100 lock( &thrd->lock ); 101 } 102 } 91 103 104 void signal_termination( thread * this ) { 105 this->c.state = Halt; 106 this->c.notHalted = false; 107 unlock( &this->lock ); 108 } 109 110 void yield( void ) { 111 thread_schedule( this_thread() ); 112 suspend(); 92 113 } 93 114
Note:
See TracChangeset
for help on using the changeset viewer.